make agent support multiple tunnel
This commit is contained in:
parent
a56afa8af3
commit
7fc2d320fc
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"makefile.configureOnOpen": false
|
||||||
|
}
|
|
@ -1,3 +1,7 @@
|
||||||
# go http tunnel
|
# go http tunnel
|
||||||
|
|
||||||
create tcp tunnel with websocket pipe
|
create tcp tunnel with websocket pipe
|
||||||
|
|
||||||
|
|
||||||
|
## history
|
||||||
|
- 2024-11-27 make agent support multiple tunnel
|
|
@ -17,7 +17,7 @@ import (
|
||||||
type DataEndPoint struct {
|
type DataEndPoint struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
|
*goemitter.EventEmitter
|
||||||
cmdSession *CommandClient
|
cmdSession *CommandClient
|
||||||
dataSession string
|
dataSession string
|
||||||
wsConn *ws.Conn
|
wsConn *ws.Conn
|
||||||
|
@ -26,6 +26,9 @@ type DataEndPoint struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DataEndPoint) Close() {
|
func (d *DataEndPoint) Close() {
|
||||||
|
if d.wsConn == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
d.wsConn.Close()
|
d.wsConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +42,8 @@ func (d *DataEndPoint) Connect() {
|
||||||
"Session": {d.cmdSession.SessionId},
|
"Session": {d.cmdSession.SessionId},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
d.Emit("Error", d.dataSession)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
d.wsConn = conn
|
d.wsConn = conn
|
||||||
for {
|
for {
|
||||||
|
@ -81,12 +85,14 @@ func (d *DataEndPoint) Listen() {
|
||||||
listener, err := net.Listen("tcp", d.Host+":"+strconv.Itoa(d.Port))
|
listener, err := net.Listen("tcp", d.Host+":"+strconv.Itoa(d.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err, d)
|
log.Println(err, d)
|
||||||
panic(err)
|
d.Emit("Error", d.dataSession)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
d.Emit("Error", d.dataSession)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
dconn := NewDataConnection(0, &conn, d)
|
dconn := NewDataConnection(0, &conn, d)
|
||||||
d.conns[dconn.id] = dconn
|
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 {
|
func NewDataEndPoint(cmdSession *CommandClient, dataSession string) *DataEndPoint {
|
||||||
return &DataEndPoint{
|
return &DataEndPoint{
|
||||||
cmdSession: cmdSession,
|
EventEmitter: goemitter.NewEmitter(),
|
||||||
dataSession: dataSession,
|
cmdSession: cmdSession,
|
||||||
conns: make(map[int32]*DataConn),
|
dataSession: dataSession,
|
||||||
|
conns: make(map[int32]*DataConn),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ type TunnelAgent struct {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cmdClient *client.CommandClient
|
cmdClient *client.CommandClient
|
||||||
dataEp *client.DataEndPoint
|
dataEps map[string]*client.DataEndPoint = make(map[string]*client.DataEndPoint)
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -38,10 +38,16 @@ func main() {
|
||||||
cmdClient.On(util.CmdTypeMap[util.ConnectInited], func(args ...interface{}) {
|
cmdClient.On(util.CmdTypeMap[util.ConnectInited], func(args ...interface{}) {
|
||||||
playload, _ := args[0].(map[string]string)
|
playload, _ := args[0].(map[string]string)
|
||||||
sessionId := playload["sessionId"]
|
sessionId := playload["sessionId"]
|
||||||
dataEp = client.NewDataEndPoint(cmdClient, sessionId)
|
dataEp := client.NewDataEndPoint(cmdClient, sessionId)
|
||||||
dataEp.Host = playload["host"]
|
dataEp.Host = playload["host"]
|
||||||
dataEp.Port, _ = strconv.Atoi(playload["port"])
|
dataEp.Port, _ = strconv.Atoi(playload["port"])
|
||||||
log.Println("connect inited:", sessionId, dataEp.Host, dataEp.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()
|
go dataEp.Connect()
|
||||||
})
|
})
|
||||||
cmdClient.On(util.CmdTypeMap[util.ErrorCmd], func(args ...interface{}) {
|
cmdClient.On(util.CmdTypeMap[util.ErrorCmd], func(args ...interface{}) {
|
||||||
|
|
Loading…
Reference in New Issue