Skip to content

Commit

Permalink
Fix nil reference & improve client timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jhead committed May 23, 2020
1 parent def70b9 commit 94ac050
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 5 additions & 3 deletions internal/clientmap/clientmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ func (cm *ClientMap) Delete(clientAddr net.Addr) {

cm.mutex.Lock()

cm.clients[key].conn.Close()
delete(cm.clients, key)

if client, exists := cm.clients[key]; exists {
client.conn.Close()
delete(cm.clients, key)
}

cm.mutex.Unlock()
}

Expand Down
11 changes: 7 additions & 4 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type ProxyServer struct {
clientMap *clientmap.ClientMap
prefs ProxyPrefs
dead *abool.AtomicBool
serverOffline bool
serverOffline bool
}

type ProxyPrefs struct {
Expand Down Expand Up @@ -195,6 +195,9 @@ func (proxy *ProxyServer) processDataFromClients(listener net.PacketConn, packet
return err
}

// Wait 5 seconds for the server to respond to whatever we sent, or else timeout
_ = serverConn.SetReadDeadline(time.Now().Add(time.Second * 5))

if packetID := data[0]; packetID == proto.UnconnectedPingID {
log.Info().Msgf("Received LAN ping from client: %s", client.String())

Expand All @@ -220,12 +223,12 @@ func (proxy *ProxyServer) processDataFromServer(remoteConn *net.UDPConn, client
buffer := make([]byte, maxMTU)

for !proxy.dead.IsSet() {
// Set a read timeout of 5 seconds
_ = remoteConn.SetReadDeadline(time.Now().Add(time.Second * 5))

// Read the next packet from the server
read, _, err := remoteConn.ReadFrom(buffer)

// Remove read timeout, server responded
_ = remoteConn.SetReadDeadline(time.Time{})

// Read error
if err != nil {
log.Warn().Msgf("%v", err)
Expand Down

0 comments on commit 94ac050

Please sign in to comment.