-
Notifications
You must be signed in to change notification settings - Fork 0
/
newOnConnect.go
36 lines (33 loc) · 977 Bytes
/
newOnConnect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Try and prevent spamming by limiting the number of request to once every 10 seconds
// (unless someeone else connects)
// This function is only set for External connectionings in run.go
package farmer
import (
"github.com/goarchit/archit/log"
"github.com/valyala/gorpc"
"io"
"net"
"time"
)
var LastRemoteAddr string
var LastConnectTime time.Time
func newOnConnectFunc() gorpc.OnConnectFunc {
return func(remoteAddr string, rwc io.ReadWriteCloser) (io.ReadWriteCloser, error) {
now := time.Now()
log.Trace("NewOnConnect from", remoteAddr)
ra, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
log.Critical("newOnConnectFunc: Error Splitting IP address",remoteAddr,":",err)
}
if ra == LastRemoteAddr {
if now.Sub(LastConnectTime) < ( 10 * time.Second) {
log.Trace("Anti-Spammer delay being added")
time.Sleep(1958 * time.Millisecond)
}
} else {
LastRemoteAddr = ra
}
LastConnectTime = time.Now()
return rwc, nil
}
}