Skip to content

Commit 96ef656

Browse files
committed
fix: Crash issues on waitgroups
1 parent 81b167e commit 96ef656

File tree

6 files changed

+176
-179
lines changed

6 files changed

+176
-179
lines changed

app/handlers/command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func OutsideCommandRunner(c *fiber.Ctx) error {
5454
}
5555
}
5656

57-
session, err := bridge.Connections.GetRaw(
57+
session, err := bridge.Sessions.GetRaw(
5858
c.Locals("user_id").(string),
5959
c.FormValue("remote_host"),
6060
c.FormValue("username"),
@@ -70,7 +70,7 @@ func OutsideCommandRunner(c *fiber.Ctx) error {
7070
)
7171

7272
if isCreated {
73-
bridge.Connections.SetRaw(
73+
bridge.Sessions.SetRaw(
7474
c.Locals("user_id").(string),
7575
c.FormValue("remote_host"),
7676
c.FormValue("username"),
@@ -88,7 +88,7 @@ func OutsideCommandRunner(c *fiber.Ctx) error {
8888

8989
if c.FormValue("disconnect") == "1" {
9090
session.CloseAllConnections()
91-
bridge.Connections.Delete(c.Locals("user_id").(string) + c.FormValue("remote_host") + c.FormValue("username"))
91+
bridge.Sessions.Delete(c.Locals("user_id").(string) + c.FormValue("remote_host") + c.FormValue("username"))
9292
}
9393

9494
return c.SendString(output)

internal/bridge/clean.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// Clean clears long standing sessions from memory
1212
func Clean() {
1313
now := time.Now()
14-
for key, session := range Connections {
14+
for key, session := range Sessions.Connections {
1515
if now.Sub(session.LastConnection).Seconds() > 266 {
1616
closeSession(session, key)
1717
continue
@@ -65,9 +65,9 @@ func Clean() {
6565
}
6666
}
6767

68-
for key, tunnel := range Tunnels {
68+
for _, tunnel := range Tunnels.Connections {
6969
if now.Sub(tunnel.LastConnection).Seconds() > 266 {
70-
closeTunnel(tunnel, key)
70+
closeTunnel(tunnel)
7171
continue
7272
}
7373

@@ -81,7 +81,7 @@ func Clean() {
8181
10*time.Second,
8282
)
8383
if err != nil {
84-
closeTunnel(tunnel, key)
84+
closeTunnel(tunnel)
8585
continue
8686
}
8787

@@ -94,7 +94,7 @@ func Clean() {
9494
default:
9595
_, _, err := tunnel.SshClient.SendRequest("[email protected]", true, nil)
9696
if err != nil {
97-
closeTunnel(tunnel, key)
97+
closeTunnel(tunnel)
9898
logger.Sugar().Warnw("error when sending request")
9999
}
100100

@@ -106,7 +106,7 @@ func Clean() {
106106
case <-ch:
107107
continue
108108
case <-time.After(10 * time.Second):
109-
closeTunnel(tunnel, key)
109+
closeTunnel(tunnel)
110110
continue
111111
}
112112
}
@@ -120,13 +120,13 @@ func closeSession(s *Session, key string) {
120120
s.CloseAllConnections()
121121
s.Mutex.Unlock()
122122

123-
Connections.Delete(key)
123+
Sessions.Delete(key)
124124
}
125125

126-
func closeTunnel(t *Tunnel, key string) {
126+
func closeTunnel(t *Tunnel) {
127127
t.Mutex.Lock()
128128
t.Stop()
129129
t.Mutex.Unlock()
130130

131-
Tunnels.Delete(key)
131+
t.errHandler()
132132
}

internal/bridge/connection.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import (
77
"time"
88
)
99

10-
type Pool map[string]*Session
11-
type TunnelPool map[string]*Tunnel
10+
type Pool struct {
11+
Connections map[string]*Session
12+
sync.Mutex
13+
}
14+
type TunnelPool struct {
15+
Connections map[string]*Tunnel
16+
sync.Mutex
17+
}
1218

1319
var (
14-
Connections Pool = make(Pool)
15-
Tunnels TunnelPool = make(TunnelPool)
16-
mutex sync.Mutex
20+
Sessions Pool = Pool{Connections: make(map[string]*Session)}
21+
Tunnels TunnelPool = TunnelPool{Connections: make(map[string]*Tunnel)}
1722
)
1823

1924
// Get connection from pool
2025
func (p *Pool) Get(userID, serverID string) (*Session, error) {
21-
if conn, ok := Connections[userID+serverID]; ok {
26+
if conn, ok := p.Connections[userID+serverID]; ok {
2227
return conn, nil
2328
} else {
2429
return nil, errors.New("connection does not exist")
@@ -27,12 +32,14 @@ func (p *Pool) Get(userID, serverID string) (*Session, error) {
2732

2833
// Set connection on pool
2934
func (p *Pool) Set(userID, serverID string, session *Session) {
30-
Connections[userID+serverID] = session
35+
p.Lock()
36+
defer p.Unlock()
37+
p.Connections[userID+serverID] = session
3138
}
3239

3340
// GetRaw connection object from pool
3441
func (p *Pool) GetRaw(userID, remoteHost, username string) (*Session, error) {
35-
if conn, ok := Connections[userID+remoteHost+username]; ok {
42+
if conn, ok := p.Connections[userID+remoteHost+username]; ok {
3643
conn.LastConnection = time.Now()
3744
return conn, nil
3845
} else {
@@ -42,19 +49,21 @@ func (p *Pool) GetRaw(userID, remoteHost, username string) (*Session, error) {
4249

4350
// SetRaw connection object on pool
4451
func (p *Pool) SetRaw(userID, remoteHost, username string, session *Session) {
45-
Connections[userID+remoteHost+username] = session
52+
p.Lock()
53+
defer p.Unlock()
54+
p.Connections[userID+remoteHost+username] = session
4655
}
4756

4857
// Delete connection object from pool
4958
func (p *Pool) Delete(key string) {
50-
mutex.Lock()
51-
defer mutex.Unlock()
52-
delete(Connections, key)
59+
p.Lock()
60+
defer p.Unlock()
61+
delete(p.Connections, key)
5362
}
5463

5564
// Get Tunnel connection from pool
5665
func (t *TunnelPool) Get(remoteHost, remotePort, username string) (*Tunnel, error) {
57-
if tunnel, ok := Tunnels[remoteHost+":"+remotePort+":"+username]; ok {
66+
if tunnel, ok := Tunnels.Connections[remoteHost+":"+remotePort+":"+username]; ok {
5867
tunnel.LastConnection = time.Now()
5968
return tunnel, nil
6069
} else {
@@ -64,16 +73,16 @@ func (t *TunnelPool) Get(remoteHost, remotePort, username string) (*Tunnel, erro
6473

6574
// Set Tunnel connection to pool
6675
func (t *TunnelPool) Set(remoteHost, remotePort, username string, tunnel *Tunnel) {
67-
mutex.Lock()
68-
defer mutex.Unlock()
69-
Tunnels[remoteHost+":"+remotePort+":"+username] = tunnel
76+
t.Lock()
77+
defer t.Unlock()
78+
Tunnels.Connections[remoteHost+":"+remotePort+":"+username] = tunnel
7079
}
7180

7281
// Delete Tunnel connection from pool
7382
func (t *TunnelPool) Delete(key string) {
74-
mutex.Lock()
75-
defer mutex.Unlock()
76-
delete(Tunnels, key)
83+
t.Lock()
84+
defer t.Unlock()
85+
delete(Tunnels.Connections, key)
7786
}
7887

7988
// VerifyAuth verifies key when connecting

internal/bridge/initialize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212

1313
// GetSession gets session from memory and if does not exists creates a new one
1414
func GetSession(userID, serverID, host string) (*Session, error) {
15-
session, err := Connections.Get(userID, serverID)
15+
session, err := Sessions.Get(userID, serverID)
1616
if err != nil {
1717
session = &Session{}
1818
isConnected := session.CreateShell(userID, serverID, host)
1919
if !isConnected {
2020
return nil, logger.FiberError(fiber.StatusForbidden, "cannot connect to server")
2121
}
22-
Connections.Set(userID, serverID, session)
22+
Sessions.Set(userID, serverID, session)
2323
}
2424

2525
session.LastConnection = time.Now()

internal/bridge/session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ type Session struct {
2525
SFTP *sftp.Client
2626
SMB *smb2.Session
2727
WinRM *winrm.Client
28-
Mutex sync.Mutex
2928
LastConnection time.Time
3029
WindowsLetter string
3130
WindowsPath string
3231
Username string
3332
IpAddr string
3433
Port string
3534
password string
35+
36+
sync.Mutex
3637
}
3738

3839
// CloseAllConnections closes all connections on session

0 commit comments

Comments
 (0)