mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 21:30:16 +02:00
cleanup dangling channels
This commit is contained in:
parent
e3ebed186d
commit
b2dc1f32f8
3 changed files with 32 additions and 4 deletions
|
@ -3,6 +3,7 @@ package croc
|
||||||
import (
|
import (
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -36,6 +37,8 @@ type channelData struct {
|
||||||
curve elliptic.Curve
|
curve elliptic.Curve
|
||||||
// connection information is stored when the clients do connect over TCP
|
// connection information is stored when the clients do connect over TCP
|
||||||
connection [2]net.Conn
|
connection [2]net.Conn
|
||||||
|
// startTime is the time that the channel was opened
|
||||||
|
startTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type response struct {
|
type response struct {
|
||||||
|
|
|
@ -27,6 +27,9 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer(tcpPorts []string, port string) (err error) {
|
func startServer(tcpPorts []string, port string) (err error) {
|
||||||
|
// start cleanup on dangling channels
|
||||||
|
go channelCleanup()
|
||||||
|
|
||||||
// start server
|
// start server
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
@ -134,8 +137,11 @@ func startServer(tcpPorts []string, port string) (err error) {
|
||||||
r.UUID = rs.channel[r.Channel].uuids[p.Role]
|
r.UUID = rs.channel[r.Channel].uuids[p.Role]
|
||||||
log.Debugf("(%s) %s has joined as role %d", r.Channel, r.UUID, 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 {
|
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 {
|
switch curve := p.Curve; curve {
|
||||||
case "p224":
|
case "p224":
|
||||||
rs.channel[r.Channel].curve = elliptic.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)
|
log.Debugf("(%s) using curve '%s'", r.Channel, p.Curve)
|
||||||
rs.channel[r.Channel].State["curve"] = []byte(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)
|
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))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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**
|
**Encryption**
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue