Skip to content

Commit 1424c6a

Browse files
authored
Fix sending on closed channels (#1)
1 parent 49d2f6a commit 1424c6a

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

pkg/proxy/proxy.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func (p *Proxy) onAgentConnected(newClient *ws.Client) {
9090
go func() {
9191
for msg := range newAgent.client.Recv {
9292
for r := range newAgent.remotes {
93-
r.client.Send <- msg
93+
if !r.client.Closed() {
94+
r.client.Send <- msg
95+
}
9496
}
9597
}
9698

@@ -132,7 +134,9 @@ func (p *Proxy) onRemoteConnected(newClient *ws.Client) {
132134
// Pipe received messages to the agent.
133135
go func() {
134136
for msg := range newRemote.client.Recv {
135-
targetAgent.client.Send <- msg
137+
if !targetAgent.client.Closed() {
138+
targetAgent.client.Send <- msg
139+
}
136140
}
137141

138142
log.Println("Closed remote:", target.Id)

pkg/ws/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (c *Client) Close() {
3333
}
3434
}
3535

36+
func (c *Client) Closed() bool {
37+
return c.closed
38+
}
39+
3640
func (c *Client) readPump() {
3741
defer c.Close()
3842

@@ -52,6 +56,10 @@ func (c *Client) readPump() {
5256
break
5357
}
5458

59+
if c.closed {
60+
break
61+
}
62+
5563
c.Recv <- Message{
5664
MessageType: t,
5765
Data: msg,

pkg/ws/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Server struct {
1313
}
1414

1515
var upgrader = websocket.Upgrader{
16+
// Allow all origins.
1617
CheckOrigin: func(_ *http.Request) bool {
17-
// TODO: Actually check origin
1818
return true
1919
},
2020
}

0 commit comments

Comments
 (0)