gotunnelserver/server/handleClient.go

53 lines
1.4 KiB
Go
Raw Permalink Normal View History

package server
import (
"net/http"
"git.pyer.club/kingecg/gotunnelserver/util"
"github.com/gorilla/websocket"
)
func (s *Server) HandleClient(conn *websocket.Conn, r *http.Request) {
2024-11-13 22:26:29 +08:00
clientSession := NewSession(conn)
clientSession.Start()
authEntity := r.Context().Value("authEntity")
if authEntity == nil {
clientSession.Close()
return
}
clientSession.Name = authEntity.(*util.AuthEntity).Username
2024-11-13 22:26:29 +08:00
s.clientSession[clientSession.Id] = clientSession
command := NcmdSession(clientSession.Id)
clientSession.Send(command)
clientSession.On("NewConnection", func(args ...interface{}) {
orgPlayload := args[0].(map[string]string) // orgPayload should include target, host and port field
targetSessionId, ok := orgPlayload["target"]
if !ok {
clientSession.Send(NewErrorResponse("target field is required", command))
return
}
targetSession := s.findSession(targetSessionId)
if targetSession == nil {
clientSession.Send(NewErrorResponse("target session not found", command))
return
}
2024-11-15 16:49:01 +08:00
spipe := NewPipe(clientSession.Id, targetSessionId)
spipe.Once("Close", func(p ...interface{}) {
delete(s.pipes, spipe.Id)
})
2024-11-15 16:49:01 +08:00
s.pipes[spipe.Id] = spipe
command := NcmdConnectionInited(spipe.Id)
2024-11-13 22:26:29 +08:00
for k, v := range orgPlayload {
command.Payload[k] = v
}
clientSession.Send(command)
targetSession.Send(command)
})
clientSession.Once("Close", func(p ...interface{}) {
delete(s.clientSession, clientSession.Id)
})
}