-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (66 loc) · 1.79 KB
/
main.go
File metadata and controls
80 lines (66 loc) · 1.79 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
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"time"
"websocket-proxy/config"
"websocket-proxy/hub"
"websocket-proxy/server"
log "github.com/sirupsen/logrus"
)
/**
websocket-proxy --port 4041 --path ws-proxy --dst-host 127.0.0.1 --dst-port 4040 --dst-path wsp-test
*/
var (
flagLogFile = flag.String("log-file", "/tmp/websocket-proxy.log", "Path to log file")
flagHost = flag.String("host", "", "Run on host")
flagPort = flag.String("port", "", "Run on port")
flagPath = flag.String("path", "", "URI path")
flagDstHost = flag.String("dst-host", "127.0.0.1", "Destination host")
flagDstPort = flag.String("dst-port", "", "Destination port")
flagDstPath = flag.String("dst-path", "", "Destination path")
flagDstInsecure = flag.Bool("insecure", false, "Use ws instead of wss for destination connect")
)
func main() {
flag.Parse()
if *flagPort == "" {
log.Fatal("You have to specify port")
}
initLogs(*flagLogFile)
serverConfig := &config.ServerConfig{
Host: *flagHost,
Port: *flagPort,
Path: *flagPath,
}
dstConfig := &config.DstConfig{
ServerConfig: config.ServerConfig{
Host: *flagDstHost,
Port: *flagDstPort,
Path: *flagDstPath,
},
Insecure: *flagDstInsecure,
}
serverConfig.Validate()
dstConfig.Validate()
h := hub.New()
// handle os signals here
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT)
go func() {
<-signalCh
go h.CloseAllConnections()
<-time.After(500 * time.Millisecond)
os.Exit(0)
}()
server.New(h, serverConfig, dstConfig).Start()
}
func initLogs(file string) {
logFile, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660)
if err != nil {
log.Fatalf("Failed to init logs: %v", err)
}
log.SetOutput(logFile)
log.SetFormatter(&log.JSONFormatter{})
}