9
9
"sync"
10
10
11
11
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
12
- "github.com/gorilla/websocket"
13
12
log "github.com/sirupsen/logrus"
14
13
)
15
14
@@ -21,7 +20,8 @@ func logRequest(next http.HandlerFunc) http.HandlerFunc {
21
20
}
22
21
}
23
22
24
- func CreateHttpProxyServer (TargetConn * websocket.Conn , Port string , rch chan define.RelayCommandResp , stop chan struct {}) {
23
+ /*
24
+ func OldCreateHttpProxyServer(TargetConn *websocket.Conn, Port string, rch chan define.RelayCommandResp, stop chan struct{}) {
25
25
var (
26
26
caCertFile = "cert/cert.pem"
27
27
caKeyFile = "cert/key.pem"
@@ -34,16 +34,16 @@ func CreateHttpProxyServer(TargetConn *websocket.Conn, Port string, rch chan def
34
34
os.Exit(1)
35
35
}
36
36
37
- wsproxy := createHTTPProxy (TargetConn , rch )
38
- forwardHandler := wsproxy .ServeHTTP
37
+ // wsproxy := createHTTPProxy(TargetConn, rch)
38
+ // forwardHandler := wsproxy.ServeHTTP
39
39
40
- connectHandler := newInterceptHandler (certGen .Get , logRequest (forwardHandler ), httpServerExitDone , stop )
40
+ // connectHandler := newInterceptHandler(certGen.Get, logRequest(forwardHandler), httpServerExitDone, stop)
41
41
42
42
handler := logRequest(func(w http.ResponseWriter, r *http.Request) {
43
43
if r.Method == "CONNECT" {
44
- connectHandler .ServeHTTP (w , r )
44
+ // connectHandler.ServeHTTP(w, r)
45
45
} else {
46
- forwardHandler (w , r )
46
+ // forwardHandler(w, r)
47
47
// wsproxy.ServeHTTP(w, r)
48
48
}
49
49
})
@@ -69,3 +69,88 @@ func CreateHttpProxyServer(TargetConn *websocket.Conn, Port string, rch chan def
69
69
httpServerExitDone.Wait()
70
70
log.Info("HTTP server stopped")
71
71
}
72
+ */
73
+
74
+ var p * WebsocketHTTPProxy
75
+
76
+ func Serve (TargetConn * define.WebsocketClient , Port string ) {
77
+ p = NewWebSocketHTTPProxy (TargetConn )
78
+ p .Serve (Port )
79
+ }
80
+
81
+ func Stop () {
82
+ p .Stop ()
83
+ }
84
+
85
+ type WebsocketHTTPProxy struct {
86
+ conn * define.WebsocketClient
87
+ tlsConfig struct {
88
+ CaKeyFile string
89
+ CaCertFile string
90
+ }
91
+
92
+ stop chan struct {}
93
+ ExitDoneWg * sync.WaitGroup
94
+ }
95
+
96
+ func NewWebSocketHTTPProxy (conn * define.WebsocketClient ) * WebsocketHTTPProxy {
97
+ return & WebsocketHTTPProxy {
98
+ conn : conn ,
99
+ tlsConfig : struct {
100
+ CaKeyFile string
101
+ CaCertFile string
102
+ }{
103
+ "cert/key.pem" ,
104
+ "cert/cert.pem" ,
105
+ },
106
+ stop : make (chan struct {}),
107
+ ExitDoneWg : new (sync.WaitGroup ),
108
+ }
109
+ }
110
+
111
+ func (c * WebsocketHTTPProxy ) Serve (port string ) {
112
+ certGen , err := newCertGenerator (c .tlsConfig .CaCertFile , c .tlsConfig .CaKeyFile )
113
+ if err != nil {
114
+ log .Print (err )
115
+ os .Exit (1 )
116
+ }
117
+
118
+ wsproxy := createHTTPProxy (c .conn )
119
+ forwardHandler := wsproxy .ServeHTTP
120
+
121
+ connectHandler := newInterceptHandler (certGen .Get , logRequest (forwardHandler ), c .ExitDoneWg , c .stop )
122
+
123
+ handler := logRequest (func (w http.ResponseWriter , r * http.Request ) {
124
+ if r .Method == "CONNECT" {
125
+ connectHandler .ServeHTTP (w , r )
126
+ } else {
127
+ forwardHandler (w , r )
128
+ // wsproxy.ServeHTTP(w, r)
129
+ }
130
+ })
131
+
132
+ srv := & http.Server {
133
+ Addr : ":" + port ,
134
+ Handler : http .HandlerFunc (handler ),
135
+ }
136
+
137
+ go func () {
138
+ c .ExitDoneWg .Add (1 )
139
+ defer c .ExitDoneWg .Done ()
140
+ err := srv .ListenAndServe ()
141
+ if ! errors .Is (err , http .ErrServerClosed ) && err != nil {
142
+ log .Error (err )
143
+ }
144
+ }()
145
+ log .Infof ("HTTP Proxy server is started on port: %v" , port )
146
+ <- c .stop // block if receive stop command
147
+ if err := srv .Shutdown (context .TODO ()); err != nil {
148
+ log .Fatalf ("HTTP server Shutdown: %v" , err )
149
+ }
150
+ c .ExitDoneWg .Wait ()
151
+ log .Info ("HTTP server stopped" )
152
+ }
153
+
154
+ func (c * WebsocketHTTPProxy ) Stop () {
155
+ c .stop <- struct {}{}
156
+ }
0 commit comments