fix: handle closed connection access to prevent runtime error

This commit is contained in:
2025-02-08 18:33:43 +07:00
parent bb6e4e2568
commit af6c6ba8a6
2 changed files with 8 additions and 0 deletions

View File

@ -56,6 +56,7 @@ func (s *Session) handleGlobalRequest() {
break break
} }
Clients[slug] = s Clients[slug] = s
s.Slug = slug
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(portToBind)) binary.Write(buf, binary.BigEndian, uint32(portToBind))
log.Printf("Forwarding approved on port: %d", portToBind) log.Printf("Forwarding approved on port: %d", portToBind)

View File

@ -2,11 +2,14 @@ package session
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"net" "net"
) )
type Session struct { type Session struct {
ID uuid.UUID
Slug string
ConnChannels []ssh.Channel ConnChannels []ssh.Channel
Connection *ssh.ServerConn Connection *ssh.ServerConn
GlobalRequest <-chan *ssh.Request GlobalRequest <-chan *ssh.Request
@ -33,6 +36,8 @@ func init() {
func New(conn *ssh.ServerConn, sshChannel <-chan ssh.NewChannel, req <-chan *ssh.Request) *Session { func New(conn *ssh.ServerConn, sshChannel <-chan ssh.NewChannel, req <-chan *ssh.Request) *Session {
session := &Session{ session := &Session{
ID: uuid.New(),
Slug: "",
ConnChannels: []ssh.Channel{}, ConnChannels: []ssh.Channel{},
Connection: conn, Connection: conn,
GlobalRequest: req, GlobalRequest: req,
@ -53,6 +58,8 @@ func (session *Session) Close() {
session.Done <- true session.Done <- true
if session.TunnelType != HTTP { if session.TunnelType != HTTP {
session.Listener.Close() session.Listener.Close()
} else {
delete(Clients, session.Slug)
} }
for _, ch := range session.ConnChannels { for _, ch := range session.ConnChannels {