diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..082b194 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "makefile.configureOnOpen": false +} \ No newline at end of file diff --git a/Readme.md b/Readme.md index a0ddb39..7195c14 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,7 @@ # go http tunnel -create tcp tunnel with websocket pipe \ No newline at end of file +create tcp tunnel with websocket pipe + + +## history +- 2024-11-27 make agent support multiple tunnel \ No newline at end of file diff --git a/client/dataconn.go b/client/dataconn.go index ea32add..e041e2a 100644 --- a/client/dataconn.go +++ b/client/dataconn.go @@ -17,7 +17,7 @@ import ( type DataEndPoint struct { Host string Port int - + *goemitter.EventEmitter cmdSession *CommandClient dataSession string wsConn *ws.Conn @@ -26,6 +26,9 @@ type DataEndPoint struct { } func (d *DataEndPoint) Close() { + if d.wsConn == nil { + return + } d.wsConn.Close() } @@ -39,7 +42,8 @@ func (d *DataEndPoint) Connect() { "Session": {d.cmdSession.SessionId}, }) if err != nil { - panic(err) + d.Emit("Error", d.dataSession) + return } d.wsConn = conn for { @@ -81,12 +85,14 @@ func (d *DataEndPoint) Listen() { listener, err := net.Listen("tcp", d.Host+":"+strconv.Itoa(d.Port)) if err != nil { log.Println(err, d) - panic(err) + d.Emit("Error", d.dataSession) + return } for { conn, err := listener.Accept() if err != nil { - panic(err) + d.Emit("Error", d.dataSession) + return } dconn := NewDataConnection(0, &conn, d) d.conns[dconn.id] = dconn @@ -107,9 +113,10 @@ func (d *DataEndPoint) Write(p []byte) (n int, err error) { func NewDataEndPoint(cmdSession *CommandClient, dataSession string) *DataEndPoint { return &DataEndPoint{ - cmdSession: cmdSession, - dataSession: dataSession, - conns: make(map[int32]*DataConn), + EventEmitter: goemitter.NewEmitter(), + cmdSession: cmdSession, + dataSession: dataSession, + conns: make(map[int32]*DataConn), } } diff --git a/tunnelagent/main.go b/tunnelagent/main.go index 8072591..7bc755d 100644 --- a/tunnelagent/main.go +++ b/tunnelagent/main.go @@ -18,7 +18,7 @@ type TunnelAgent struct { var ( cmdClient *client.CommandClient - dataEp *client.DataEndPoint + dataEps map[string]*client.DataEndPoint = make(map[string]*client.DataEndPoint) ) func main() { @@ -38,10 +38,16 @@ func main() { cmdClient.On(util.CmdTypeMap[util.ConnectInited], func(args ...interface{}) { playload, _ := args[0].(map[string]string) sessionId := playload["sessionId"] - dataEp = client.NewDataEndPoint(cmdClient, sessionId) + dataEp := client.NewDataEndPoint(cmdClient, sessionId) dataEp.Host = playload["host"] dataEp.Port, _ = strconv.Atoi(playload["port"]) log.Println("connect inited:", sessionId, dataEp.Host, dataEp.Port) + dataEps[sessionId] = dataEp + dataEp.Once("Error", func(args ...interface{}) { + log.Println("data endpoint error:", sessionId) + delete(dataEps, sessionId) + dataEp.Close() + }) go dataEp.Connect() }) cmdClient.On(util.CmdTypeMap[util.ErrorCmd], func(args ...interface{}) {