1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-28 01:16:10 +03:00

cleanup dangling channels

This commit is contained in:
Zack Scholl 2018-06-28 09:13:36 -07:00
parent e3ebed186d
commit b2dc1f32f8
3 changed files with 32 additions and 4 deletions

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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**