-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwsproxy.go
63 lines (49 loc) · 1.3 KB
/
wsproxy.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
package main
import (
"log"
"net/http"
"github.com/akamensky/argparse"
"os"
"fmt"
)
var helpDescr = `
wsproxy - Websocket to TCP/UDP socket proxy.
Websocket URL format:
ws://<addr>:<port>/ws/...
with the following parameters:
- proto (tcp, udp)
- addr
- port
- format (text, bin)
Static fileserver URL format (optional):
http://<addr>:<port>/www/...
`
func main(){
parser := argparse.NewParser("wsproxy", helpDescr)
listenaddr := parser.String("l", "listen", &argparse.Options{
Required: true,
Help: "<Address>:<port> to accept Websocket connections from",
})
wwwdir := parser.String("w", "www", &argparse.Options {
Required: false,
Help: "www directory to serve HTML files from",
})
workers := parser.Int("n", "max-workers", &argparse.Options{
Required: false,
Help: "number of maximum simultaneous proxy connections",
Default: 5,
})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
os.Exit(1)
}
SetMaxWorkers(*workers)
if wwwdir != nil {
http.Handle("/www/", http.StripPrefix("/www/", http.FileServer(http.Dir(*wwwdir))))
log.Printf("starting www server on %v at /www", *listenaddr)
}
http.HandleFunc("/ws", HandleWebsocket)
log.Printf("starting proxy on %v at /ws", *listenaddr)
http.ListenAndServe(*listenaddr, nil)
}