refactor: separate core components and improve session & server handling

This commit is contained in:
2025-02-06 22:14:13 +07:00
parent dc219e2f7f
commit 8a1604fde8
9 changed files with 556 additions and 287 deletions

69
session/session.go Normal file
View File

@ -0,0 +1,69 @@
package session
import (
"fmt"
"golang.org/x/crypto/ssh"
"net"
)
type Session struct {
ConnChannels []ssh.Channel
Connection *ssh.ServerConn
GlobalRequest <-chan *ssh.Request
Listener net.Listener
TunnelType TunnelType
Done chan bool
}
type TunnelType string
const (
HTTP TunnelType = "http"
TCP TunnelType = "tcp"
UDP TunnelType = "udp"
UNKNOWN TunnelType = "unknown"
)
var Clients map[string]*Session
func init() {
Clients = make(map[string]*Session)
}
func New(conn *ssh.ServerConn, sshChannel <-chan ssh.NewChannel, req <-chan *ssh.Request) *Session {
session := &Session{
ConnChannels: []ssh.Channel{},
Connection: conn,
GlobalRequest: req,
TunnelType: UNKNOWN,
Done: make(chan bool),
}
go session.handleGlobalRequest()
go func() {
for newChannel := range sshChannel {
go session.HandleSessionChannel(newChannel)
}
}()
return session
}
func (session *Session) Close() {
session.Done <- true
session.Listener.Close()
for _, ch := range session.ConnChannels {
if err := ch.Close(); err != nil {
fmt.Println("Error closing channel : ", err.Error())
continue
}
}
if err := session.Connection.Close(); err != nil {
fmt.Println("Error closing connection : ", err.Error())
}
}