mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-02 17:58:46 +00:00
noise finalmask: Better reset (#6188)
https://github.com/XTLS/Xray-core/pull/6170 Explanation: https://github.com/XTLS/Xray-core/pull/5657#issue-3901983829
This commit is contained in:
@@ -12,85 +12,41 @@ type noiseConn struct {
|
|||||||
net.PacketConn
|
net.PacketConn
|
||||||
config *Config
|
config *Config
|
||||||
m map[string]time.Time
|
m map[string]time.Time
|
||||||
stop chan struct{}
|
mu sync.Mutex
|
||||||
once sync.Once
|
|
||||||
mutex sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnClient(c *Config, raw net.PacketConn) (net.PacketConn, error) {
|
func NewConnClient(c *Config, raw net.PacketConn) (net.PacketConn, error) {
|
||||||
conn := &noiseConn{
|
return &noiseConn{
|
||||||
PacketConn: raw,
|
PacketConn: raw,
|
||||||
config: c,
|
config: c,
|
||||||
m: make(map[string]time.Time),
|
m: make(map[string]time.Time),
|
||||||
stop: make(chan struct{}),
|
}, nil
|
||||||
}
|
|
||||||
|
|
||||||
if conn.config.ResetMax > 0 {
|
|
||||||
go conn.reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
return conn, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnServer(c *Config, raw net.PacketConn) (net.PacketConn, error) {
|
func NewConnServer(c *Config, raw net.PacketConn) (net.PacketConn, error) {
|
||||||
return NewConnClient(c, raw)
|
return NewConnClient(c, raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *noiseConn) reset() {
|
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
c.mutex.RLock()
|
|
||||||
now := time.Now()
|
|
||||||
timeOut := make([]string, 0, len(c.m))
|
|
||||||
for key, last := range c.m {
|
|
||||||
if now.After(last) {
|
|
||||||
timeOut = append(timeOut, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.mutex.RUnlock()
|
|
||||||
|
|
||||||
for _, key := range timeOut {
|
|
||||||
c.mutex.Lock()
|
|
||||||
delete(c.m, key)
|
|
||||||
c.mutex.Unlock()
|
|
||||||
}
|
|
||||||
case <-c.stop:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *noiseConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
func (c *noiseConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||||
c.mutex.RLock()
|
c.mu.Lock()
|
||||||
_, ready := c.m[addr.String()]
|
defer c.mu.Unlock()
|
||||||
c.mutex.RUnlock()
|
|
||||||
|
|
||||||
if !ready {
|
t := c.m[addr.String()]
|
||||||
c.mutex.Lock()
|
|
||||||
_, ready = c.m[addr.String()]
|
if t.IsZero() || (c.config.ResetMax > 0 && time.Now().After(t)) {
|
||||||
if !ready {
|
for _, item := range c.config.Items {
|
||||||
for _, item := range c.config.Items {
|
if item.RandMax > 0 {
|
||||||
if item.RandMax > 0 {
|
buf := make([]byte, crypto.RandBetween(item.RandMin, item.RandMax))
|
||||||
item.Packet = make([]byte, crypto.RandBetween(item.RandMin, item.RandMax))
|
crypto.RandBytesBetween(buf, byte(item.RandRangeMin), byte(item.RandRangeMax))
|
||||||
crypto.RandBytesBetween(item.Packet, byte(item.RandRangeMin), byte(item.RandRangeMax))
|
c.PacketConn.WriteTo(buf, addr)
|
||||||
}
|
} else {
|
||||||
c.PacketConn.WriteTo(item.Packet, addr)
|
c.PacketConn.WriteTo(item.Packet, addr)
|
||||||
time.Sleep(time.Duration(crypto.RandBetween(item.DelayMin, item.DelayMax)) * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
c.m[addr.String()] = time.Now().Add(time.Duration(crypto.RandBetween(c.config.ResetMin, c.config.ResetMax)) * time.Second)
|
time.Sleep(time.Duration(crypto.RandBetween(item.DelayMin, item.DelayMax)) * time.Millisecond)
|
||||||
}
|
}
|
||||||
c.mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.m[addr.String()] = time.Now().Add(time.Duration(crypto.RandBetween(c.config.ResetMin, c.config.ResetMax)) * time.Second)
|
||||||
|
|
||||||
return c.PacketConn.WriteTo(p, addr)
|
return c.PacketConn.WriteTo(p, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *noiseConn) Close() error {
|
|
||||||
c.once.Do(func() {
|
|
||||||
close(c.stop)
|
|
||||||
})
|
|
||||||
return c.PacketConn.Close()
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user