diff --git a/src/models.go b/src/models.go index a3073ea..0a14c22 100644 --- a/src/models.go +++ b/src/models.go @@ -3,6 +3,7 @@ package croc import ( "crypto/elliptic" "net" + "time" ) const ( @@ -36,6 +37,8 @@ type channelData struct { curve elliptic.Curve // connection information is stored when the clients do connect over TCP connection [2]net.Conn + // startTime is the time that the channel was opened + startTime time.Time } type response struct { diff --git a/src/server.go b/src/server.go index 46f3351..362f9eb 100644 --- a/src/server.go +++ b/src/server.go @@ -27,6 +27,9 @@ func init() { } func startServer(tcpPorts []string, port string) (err error) { + // start cleanup on dangling channels + go channelCleanup() + // start server gin.SetMode(gin.ReleaseMode) r := gin.New() @@ -134,8 +137,11 @@ func startServer(tcpPorts []string, port string) (err error) { r.UUID = rs.channel[r.Channel].uuids[p.Role] log.Debugf("(%s) %s has joined as role %d", r.Channel, r.UUID, p.Role) - // if channel is not open, set curve + // if channel is not open, set initial parameters if !rs.channel[r.Channel].isopen { + rs.channel[r.Channel].isopen = true + rs.channel[r.Channel].Ports = tcpPorts + rs.channel[r.Channel].startTime = time.Now() switch curve := p.Curve; curve { case "p224": rs.channel[r.Channel].curve = elliptic.P224() @@ -153,8 +159,6 @@ func startServer(tcpPorts []string, port string) (err error) { } log.Debugf("(%s) using curve '%s'", r.Channel, p.Curve) rs.channel[r.Channel].State["curve"] = []byte(p.Curve) - rs.channel[r.Channel].Ports = tcpPorts - rs.channel[r.Channel].isopen = true } r.Message = fmt.Sprintf("assigned role %d in channel '%s'", p.Role, r.Channel) @@ -182,3 +186,24 @@ func middleWareHandler() gin.HandlerFunc { log.Infof("%v %v %v %s", c.Request.RemoteAddr, c.Request.Method, c.Request.URL, time.Since(t)) } } + +func channelCleanup() { + maximumWait := 10 * time.Minute + for { + rs.Lock() + keys := make([]string, len(rs.channel)) + i := 0 + for key := range rs.channel { + keys[i] = key + i++ + } + for _, key := range keys { + if time.Since(rs.channel[key].startTime) > maximumWait { + log.Debugf("channel %s has exceeded time, deleting", key) + delete(rs.channel, key) + } + } + rs.Unlock() + time.Sleep(1 * time.Minute) + } +} diff --git a/src/testing_data/README.md.2 b/src/testing_data/README.md.2 index 6a51eb5..91edd16 100644 --- a/src/testing_data/README.md.2 +++ b/src/testing_data/README.md.2 @@ -1,4 +1,4 @@ -tion is wiped from the relay server. The encrypted file data never is stored on the relay. + wiped from the relay server. The encrypted file data never is stored on the relay. **Encryption**