This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwss-proxy.go
81 lines (63 loc) · 1.87 KB
/
wss-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
package main
import (
"flag"
"log"
"net"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
} // NOT a good practice
var (
toURL = flag.String("to", "http://127.0.0.1:80", "the address and port for which to proxy requests to")
fromURL = flag.String("from", "127.0.0.1:4430", "the tcp address and port this proxy should listen for requests on")
certFile = flag.String("cert", "", "path to a tls certificate file")
keyFile = flag.String("key", "", "path to a private key file")
)
func handler(w http.ResponseWriter, r *http.Request) {
println("Connection")
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
_, p, err := ws.ReadMessage()
if err != nil {
log.Println(err)
return
}
println("Printing",*toURL)
tcpAddr, err := net.ResolveTCPAddr("tcp", *toURL)
if err != nil {
log.Println("ResolveTCPAddr failed:", err.Error())
return
}
connTCP, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
log.Println("Dial failed:", err.Error())
return
}
_, err = connTCP.Write([]byte(p))
if err != nil {
log.Println("Write to server failed:", err.Error())
return
}
connTCP.Close()
println("Print done!",*toURL)
}
func main() {
flag.Parse()
validCertFile := *certFile != ""
validKeyFile := *keyFile != ""
if (!validCertFile || !validKeyFile){
log.Fatal("No existing cert or key specified")
}
log.Printf("Proxying calls from https://%s (SSL/TLS) to %s", *fromURL, *toURL)
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServeTLS(*fromURL, *certFile, *keyFile, nil))
}