2024-11-13 00:00:06 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-11-16 13:18:22 +08:00
|
|
|
"git.pyer.club/kingecg/gotunnelserver/util"
|
2024-11-13 00:00:06 +08:00
|
|
|
"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()
|
2024-11-16 13:18:22 +08:00
|
|
|
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)
|
2024-11-16 13:18:22 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
})
|
2024-11-16 13:18:22 +08:00
|
|
|
clientSession.Once("Close", func(p ...interface{}) {
|
|
|
|
delete(s.clientSession, clientSession.Id)
|
|
|
|
})
|
2024-11-13 00:00:06 +08:00
|
|
|
}
|