forked from arahmanhamdy/wsproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
55 lines (46 loc) · 1.08 KB
/
setup.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
package wsproxy
import (
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func init() {
caddy.RegisterPlugin("wsproxy", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup configures a new WebSocket middleware instance.
func setup(c *caddy.Controller) error {
websocks, err := webSocketParse(c)
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return WebSocket{Next: next, Sockets: websocks}
})
return nil
}
func webSocketParse(c *caddy.Controller) ([]Config, error) {
var websocks []Config
for c.Next() {
var val, path, tcpSocketAddr string
// Path or socket address; not sure which yet
if !c.NextArg() {
return nil, c.ArgErr()
}
val = c.Val()
// The next argument on this line will be the TCP socket ip:port
if c.NextArg() {
path = val
tcpSocketAddr = c.Val()
} else {
path = "/"
tcpSocketAddr = val
}
websocks = append(websocks, Config{
Path: path,
TCPSocketAddr: tcpSocketAddr,
})
}
return websocks, nil
}