make agent support multiple tunnel

This commit is contained in:
程广 2024-11-27 10:39:21 +08:00
parent a56afa8af3
commit 7fc2d320fc
4 changed files with 30 additions and 10 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"makefile.configureOnOpen": false
}

View File

@ -1,3 +1,7 @@
# go http tunnel
create tcp tunnel with websocket pipe
## history
- 2024-11-27 make agent support multiple tunnel

View File

@ -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),
}
}

View File

@ -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{}) {