-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
86 lines (74 loc) · 1.44 KB
/
Copy pathutils.go
File metadata and controls
86 lines (74 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package http2tcp
import (
"context"
"errors"
"github.com/gorilla/websocket"
"log"
"net/http"
"sync"
)
type ServerConfig struct {
Host string
Port string
Path string
Auth string
}
type ClientConfig struct {
WebsocketServer string
Auth string
Targets []*Target
}
type Target struct {
LocalHost string
LocalPort string
RemoteHost string `json:"remote_host"`
RemotePort string `json:"remote_port"`
Auth string `json:"auth"`
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func writeError(writer http.ResponseWriter, err error) {
_, err = writer.Write([]byte(err.Error()))
if err != nil {
log.Println("write error err:", err)
}
}
type CancelAllContext struct {
context.Context
doneCh []chan struct{}
lock sync.RWMutex
isDone bool
}
func WithCancelAll(ctx context.Context) *CancelAllContext {
return &CancelAllContext{
Context: ctx,
doneCh: []chan struct{}{},
lock: sync.RWMutex{},
isDone: false,
}
}
func (c *CancelAllContext) GetDoneCh() (chan struct{}, error) {
c.lock.Lock()
defer c.lock.Unlock()
if c.isDone {
return nil, errors.New("all context has been canceled")
}
ch := make(chan struct{}, 1)
c.doneCh = append(c.doneCh, ch)
return ch, nil
}
func (c *CancelAllContext) CancelAll() {
c.lock.Lock()
defer c.lock.Unlock()
if c.isDone {
return
}
for _, ch := range c.doneCh {
ch <- struct{}{}
close(ch)
}
c.isDone = true
}