Skip to content

Commit 5e5ad04

Browse files
committed
update: multi bug fix
1 parent 324f815 commit 5e5ad04

File tree

20 files changed

+422
-106
lines changed

20 files changed

+422
-106
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ build/
33
.idea/
44
.vscode/
55
test*
6-
server
6+
./server
7+
!server/

cmd/server/define/type.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
package define
22

3+
import "github.com/gorilla/websocket"
4+
35
type RelayChan chan RelayCommandResp
46

57
func NewChannels() RelayChan {
68
return make(chan RelayCommandResp, 1000)
79
}
10+
11+
type WebsocketClient struct {
12+
*websocket.Conn
13+
RelayChan chan RelayCommandResp
14+
}
15+
16+
func NewWebSocketClient(conn *websocket.Conn) *WebsocketClient {
17+
return &WebsocketClient{
18+
Conn: conn,
19+
RelayChan: NewChannels(),
20+
}
21+
}
22+
23+
func (c *WebsocketClient) SendCommand(command *RelayCommand) error {
24+
return c.WriteMessage(websocket.TextMessage, command.Marshal())
25+
}

cmd/server/http_proxy/main.go

+92-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"sync"
1010

1111
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
12-
"github.com/gorilla/websocket"
1312
log "github.com/sirupsen/logrus"
1413
)
1514

@@ -21,7 +20,8 @@ func logRequest(next http.HandlerFunc) http.HandlerFunc {
2120
}
2221
}
2322

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{}) {
2525
var (
2626
caCertFile = "cert/cert.pem"
2727
caKeyFile = "cert/key.pem"
@@ -34,16 +34,16 @@ func CreateHttpProxyServer(TargetConn *websocket.Conn, Port string, rch chan def
3434
os.Exit(1)
3535
}
3636
37-
wsproxy := createHTTPProxy(TargetConn, rch)
38-
forwardHandler := wsproxy.ServeHTTP
37+
// wsproxy := createHTTPProxy(TargetConn, rch)
38+
// forwardHandler := wsproxy.ServeHTTP
3939
40-
connectHandler := newInterceptHandler(certGen.Get, logRequest(forwardHandler), httpServerExitDone, stop)
40+
// connectHandler := newInterceptHandler(certGen.Get, logRequest(forwardHandler), httpServerExitDone, stop)
4141
4242
handler := logRequest(func(w http.ResponseWriter, r *http.Request) {
4343
if r.Method == "CONNECT" {
44-
connectHandler.ServeHTTP(w, r)
44+
// connectHandler.ServeHTTP(w, r)
4545
} else {
46-
forwardHandler(w, r)
46+
// forwardHandler(w, r)
4747
// wsproxy.ServeHTTP(w, r)
4848
}
4949
})
@@ -69,3 +69,88 @@ func CreateHttpProxyServer(TargetConn *websocket.Conn, Port string, rch chan def
6969
httpServerExitDone.Wait()
7070
log.Info("HTTP server stopped")
7171
}
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+
}

cmd/server/http_proxy/proxy.go

+10-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ package http_proxy
22

33
import (
44
"fmt"
5+
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
6+
"github.com/google/uuid"
7+
log "github.com/sirupsen/logrus"
58
"io"
69
"io/ioutil"
710
"net/http"
811
"strings"
9-
10-
"github.com/google/uuid"
11-
12-
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
13-
"github.com/gorilla/websocket"
14-
log "github.com/sirupsen/logrus"
1512
)
1613

1714
type proxy struct {
18-
Conn *websocket.Conn
19-
ResponseChannel chan define.RelayCommandResp
15+
Conn *define.WebsocketClient
2016
}
2117

2218
func (p *proxy) corsMiddleware(wr http.ResponseWriter, req *http.Request) bool {
@@ -140,7 +136,7 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
140136
// generate TaskId
141137
var taskId string = uuid.New().String()
142138

143-
FetchCommand := define.RelayCommand{
139+
FetchCommand := &define.RelayCommand{
144140
CommandId: taskId,
145141
CommandDetail: define.Fetch{
146142
Url: p.prepareURL(req),
@@ -154,13 +150,13 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
154150

155151
FetchCommand.CommandDetail.Option.Mode = p.preJudgeCORS(req)
156152

157-
if FetchCommand.SendTo(p.Conn) != nil {
153+
if p.Conn.SendCommand(FetchCommand) != nil {
158154
log.Error("Error while sending FetchCommand")
159155
http.Error(wr, "Server Error", http.StatusInternalServerError)
160156
return
161157
}
162158

163-
for resp := range p.ResponseChannel {
159+
for resp := range p.Conn.RelayChan {
164160
if resp.CommandId == taskId {
165161
r := resp.CommandResult
166162
if r.Error != "" {
@@ -216,15 +212,14 @@ func (p *proxy) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
216212
io.Copy(wr, res)
217213
break
218214
} else {
219-
p.ResponseChannel <- resp
215+
p.Conn.RelayChan <- resp
220216
}
221217
}
222218
}
223219

224-
func createHTTPProxy(TargetConn *websocket.Conn, rch chan define.RelayCommandResp) *proxy {
220+
func createHTTPProxy(TargetConn *define.WebsocketClient) *proxy {
225221
handler := &proxy{
226-
Conn: TargetConn,
227-
ResponseChannel: rch,
222+
Conn: TargetConn,
228223
}
229224
return handler
230225
}

cmd/server/main.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"os"
66

7-
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
87
"github.com/esonhugh/proxyinbrowser/cmd/server/sessionmanager"
98
"github.com/esonhugh/proxyinbrowser/cmd/server/terminal"
109
log "github.com/sirupsen/logrus"
@@ -19,14 +18,12 @@ func main() {
1918
})
2019
buf := &bytes.Buffer{}
2120
log.SetOutput(buf)
22-
rch := define.NewChannels()
23-
go sessionmanager.RunServer(rch, buf)
21+
go sessionmanager.RunServer(buf)
2422
// app.RunApp(rch)
2523

2624
ch := make(chan os.Signal, 1)
2725
app := terminal.CreateApplication(terminal.ApplicationSpec{
2826
ConsoleLogBuffer: buf,
29-
Rch: rch,
3027
CloseCh: ch,
3128
})
3229
go app.Run()

cmd/server/sessionmanager/bundle.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/server/sessionmanager/server.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"io"
77
"net/http"
8+
"net/http/httputil"
89
"strings"
910
"sync"
1011
"time"
@@ -21,7 +22,7 @@ var WebsocketConnMap = SafeWebsocketConnMap{mapper: sync.Map{}}
2122
//go:embed bundle.js
2223
var fileContent string
2324

24-
func RunServer(rch define.RelayChan, buffer io.Writer) {
25+
func RunServer(buffer io.Writer) {
2526
gin.DefaultWriter = buffer
2627
gin.DisableConsoleColor()
2728

@@ -42,7 +43,19 @@ func RunServer(rch define.RelayChan, buffer io.Writer) {
4243

4344
router.GET("/:id/init", func(c *gin.Context) {
4445
c.Header("Content-Type", "application/javascript")
45-
c.String(http.StatusOK, strings.ReplaceAll(fileContent, "__ENDPOINT__", c.Param("id")))
46+
content := strings.ReplaceAll(
47+
strings.ReplaceAll(
48+
fileContent, "localhost:9999", c.Request.Host),
49+
"__ENDPOINT__", c.Param("id"))
50+
c.String(http.StatusOK, content)
51+
})
52+
53+
router.Any("/:id/infos", func(c *gin.Context) {
54+
req, err := httputil.DumpRequest(c.Request, true)
55+
if err != nil {
56+
log.Error("Try dump request error", err)
57+
}
58+
log.Info(string(req))
4659
})
4760

4861
// Handle WebSocket connections
@@ -56,8 +69,10 @@ func RunServer(rch define.RelayChan, buffer io.Writer) {
5669
}
5770

5871
connId := uuid.New().String()
59-
WebsocketConnMap.Set(connId, conn)
72+
client := define.NewWebSocketClient(conn)
73+
WebsocketConnMap.Set(connId, client)
6074
defer WebsocketConnMap.Delete(connId)
75+
6176
log.Infof("receive new connection! alloc new session id: %v", connId)
6277
l := log.WithField("session", connId)
6378

@@ -87,7 +102,7 @@ func RunServer(rch define.RelayChan, buffer io.Writer) {
87102

88103
var test define.RelayCommandResp
89104
if json.Unmarshal(p, &test) == nil {
90-
rch <- test
105+
client.RelayChan <- test
91106
continue
92107
}
93108
time.Sleep(1000 * time.Millisecond)

cmd/server/sessionmanager/type.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package sessionmanager
22

33
import (
4+
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
45
"sync"
5-
6-
"github.com/gorilla/websocket"
76
)
87

98
type SafeWebsocketConnMap struct {
109
mapper sync.Map
1110
}
1211

13-
func (s *SafeWebsocketConnMap) Get(key string) *websocket.Conn {
12+
func (s *SafeWebsocketConnMap) Get(key string) *define.WebsocketClient {
1413
if v, e := s.mapper.Load(key); e {
15-
if rv, ok := v.(*websocket.Conn); ok {
14+
if rv, ok := v.(*define.WebsocketClient); ok {
1615
return rv
1716
}
1817
}
1918
return nil
2019
}
2120

22-
func (s *SafeWebsocketConnMap) Set(key string, conn *websocket.Conn) {
21+
func (s *SafeWebsocketConnMap) Set(key string, conn *define.WebsocketClient) {
2322
s.mapper.Store(key, conn)
2423
}
2524

cmd/server/terminal/app.go

-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import (
55
"os"
66
"time"
77

8-
"github.com/esonhugh/proxyinbrowser/cmd/server/define"
98
"github.com/gdamore/tcell/v2"
109
"github.com/rivo/tview"
1110
)
1211

1312
type ApplicationSpec struct {
1413
ConsoleLogBuffer *bytes.Buffer
15-
Rch define.RelayChan
1614
CloseCh chan os.Signal
1715
}
1816

0 commit comments

Comments
 (0)