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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
package kcp
import (
"sync"
"time"
)
var (
// BlacklistDuration sets a duration for which a session is blacklisted
// once it's established. This is simillar to TIME_WAIT state in TCP, whereby
// any connection attempt with the same session parameters is ignored for
// some amount of time.
//
// This is only useful when dial attempts happen from a pre-determined port,
// for example when you are dialing from the same connection you are listening on
// to punch through NAT, and helps with the fact that KCP is state-less.
// This helps better deal with scenarios where a process on one of the side (A)
// get's restarted, and stray packets from other side (B) makes it look like
// as if someone is trying to connect to A. Even if session dies on B,
// new stray reply packets from A resurrect the session on B, causing the
// session to be alive forever.
BlacklistDuration time.Duration
blacklist = blacklistMap{
entries: make(map[sessionKey]int64),
}
)
// a global map for blacklisting conversations
type blacklistMap struct {
entries map[sessionKey]int64
mut sync.RWMutex
}
func (m *blacklistMap) add(address string, conv uint32) {
if BlacklistDuration == 0 {
return
}
timeout := time.Now().Add(BlacklistDuration).UnixNano()
m.mut.Lock()
m.entries[sessionKey{
addr: address,
convID: conv,
}] = timeout
m.reap()
m.mut.Unlock()
}
func (m *blacklistMap) has(address string, conv uint32) bool {
if BlacklistDuration == 0 {
return false
}
m.mut.RLock()
t, ok := m.entries[sessionKey{
addr: address,
convID: conv,
}]
m.mut.RUnlock()
return ok && t > time.Now().UnixNano()
}
func (m *blacklistMap) reap() {
now := time.Now().UnixNano()
for k, t := range m.entries {
if t < now {
delete(m.entries, k)
}
}
}
|