-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* relay packet to remote server * remove deprecated go version * package mapper * nat table * port for udp associate from request * echo packet server for test * time to live packet * reset packet payload * test command udp associate * packet metrics and rules * packet write timeout * change closeListenerFn * comments for options
- Loading branch information
Showing
11 changed files
with
527 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package socks5 | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type natEntry struct { | ||
src net.Addr | ||
packet *packet | ||
timestamp time.Time | ||
} | ||
|
||
type natTable struct { | ||
mutex sync.RWMutex | ||
table map[string]*natEntry | ||
} | ||
|
||
func newNatTable() *natTable { | ||
return &natTable{table: make(map[string]*natEntry)} | ||
} | ||
|
||
func (n *natTable) set(src, dst net.Addr, packet *packet) { | ||
n.mutex.Lock() | ||
n.table[dst.String()] = &natEntry{ | ||
src: src, | ||
packet: packet, | ||
timestamp: time.Now(), | ||
} | ||
n.mutex.Unlock() | ||
} | ||
|
||
func (n *natTable) get(dst net.Addr) (net.Addr, *packet, bool) { | ||
n.mutex.RLock() | ||
defer n.mutex.RUnlock() | ||
|
||
val, ok := n.table[dst.String()] | ||
if !ok { | ||
return nil, nil, ok | ||
} | ||
|
||
return val.src, val.packet, ok | ||
} | ||
|
||
func (n *natTable) delete(dst net.Addr) { | ||
n.mutex.Lock() | ||
delete(n.table, dst.String()) | ||
n.mutex.Unlock() | ||
} | ||
|
||
func (n *natTable) cleanup(period, ttl time.Duration) func() { | ||
if period <= 0 || ttl <= 0 { | ||
return func() {} | ||
} | ||
|
||
ticker := time.NewTicker(period) | ||
done := make(chan struct{}) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-done: | ||
ticker.Stop() | ||
return | ||
case <-ticker.C: | ||
n.mutex.Lock() | ||
for key, val := range n.table { | ||
if time.Since(val.timestamp) >= ttl { | ||
delete(n.table, key) | ||
} | ||
} | ||
n.mutex.Unlock() | ||
} | ||
} | ||
}() | ||
|
||
return func() { | ||
close(done) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.