-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
executable file
·180 lines (163 loc) · 4.17 KB
/
proxy.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package websocketproxy
import (
"fmt"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"net/http"
"net/url"
"os"
"time"
)
const Timeout = time.Hour * 24 * 7
// WebsocketProxy is the generic interface for a proxy implementation
type WebsocketProxy interface {
Dial() (*websocket.Conn, error)
Handler(w http.ResponseWriter, r *http.Request)
Close()
Wait(<-chan os.Signal)
}
type connection struct {
client *websocket.Conn
server *websocket.Conn
}
type websocketProxy struct {
URL *url.URL
Header http.Header
Upgrader *websocket.Upgrader
auth Auth
keyManager KeyManager
connections []*connection
}
// NewSimpleProxy returns a configured proxy instance from just a url
func NewSimpleProxy(url *url.URL) (WebsocketProxy, error) {
return NewProxy(url, nil, nil, nil, nil)
}
// NewProxy returns a configured WebsocketProxy instance and fetches keys if required
func NewProxy(
url *url.URL,
header http.Header,
origins []string,
auth Auth,
keyManager KeyManager,
) (WebsocketProxy, error) {
wsp := websocketProxy{
URL: url,
Header: header,
Upgrader: &websocket.Upgrader{
CheckOrigin: checkOrigin(origins),
},
auth: auth,
keyManager: keyManager,
}
if wsp.auth != nil && wsp.keyManager != nil {
if err := wsp.keyManager.FetchKeys(); err != nil {
return wsp, fmt.Errorf("error fetching keys: %v", err)
}
}
return wsp, nil
}
// Dial connects to the Websocket backend and returns an error if failing
func (wp websocketProxy) Dial() (*websocket.Conn, error) {
c, _, err := websocket.DefaultDialer.Dial(wp.URL.String(), wp.Header)
if err != nil {
return nil, err
}
err = c.SetReadDeadline(time.Now().Add(Timeout))
if err != nil {
return nil, err
}
return c, c.SetWriteDeadline(time.Now().Add(Timeout))
}
// Handler for an in-built http server. It authenticates the user if required,
// dials the backend and then stores the connection between client & server,
// relaying any messages sent by either client and server
func (wp websocketProxy) Handler(w http.ResponseWriter, r *http.Request) {
if wp.auth != nil {
if ok := wp.auth.Authenticate(r, wp.keyManager); !ok {
http.Error(w, "Invalid key", 401)
return
}
}
cc, err := wp.Upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
sc, err := wp.Dial()
if err != nil {
return
}
conn := &connection{cc, sc}
defer wp.close(conn)
log.WithField("IP", conn.client.RemoteAddr()).Info("New connection")
g := errgroup.Group{}
g.Go(func() error {
return wp.read(w, conn)
})
g.Go(func() error {
return wp.write(w, conn)
})
if err := g.Wait(); err != nil {
log.Errorf("Error during handling websocket connection: %v", err)
}
}
// Close will disconnect all the active connections between client and server
func (wp websocketProxy) Close() {
for _, c := range wp.connections {
wp.close(c)
}
}
// Wait listens to any interrupt signals and then closes all connections if one is received
func (wp websocketProxy) Wait(interrupt <-chan os.Signal) {
<-interrupt
wp.Close()
}
func (wp websocketProxy) write(_ http.ResponseWriter, conn *connection) error {
for {
t, msg, err := conn.client.ReadMessage()
if err != nil {
return err
}
err = conn.server.WriteMessage(t, msg)
if err != nil {
return err
}
log.WithField("IP", conn.client.RemoteAddr()).
WithField("Message", string(msg)).
Debug("Written message to server")
}
}
func (wp websocketProxy) read(_ http.ResponseWriter, conn *connection) error {
for {
t, msg, err := conn.server.ReadMessage()
if err != nil {
return err
}
err = conn.client.WriteMessage(t, msg)
if err != nil {
return err
}
log.WithField("IP", conn.client.RemoteAddr()).
WithField("Message", string(msg)).
Debug("Read message from server")
}
}
func (wp websocketProxy) close(conn *connection) {
_ = conn.client.Close()
_ = conn.server.Close()
log.WithField("IP", conn.client.RemoteAddr()).Info("Closed connection")
}
func checkOrigin(origins []string) func(*http.Request) bool {
return func(r *http.Request) bool {
if len(origins) == 0 {
return true
}
co := r.Header.Get("Origin")
for _, o := range origins {
if o == co {
return true
}
}
return false
}
}