Chore: http

This commit is contained in:
yaling888
2022-04-27 05:14:03 +08:00
committed by Meta Gowork
parent cca3a1a934
commit 564a6fdf35
4 changed files with 56 additions and 50 deletions

View File

@ -2,7 +2,6 @@ package nat
import (
"net/netip"
"sync"
"github.com/Dreamacro/clash/common/generics/list"
)
@ -25,7 +24,6 @@ type binding struct {
}
type table struct {
mu sync.Mutex
tuples map[tuple]*list.Element[*binding]
ports [portLength]*list.Element[*binding]
available *list.List[*binding]
@ -39,13 +37,13 @@ func (t *table) tupleOf(port uint16) tuple {
elm := t.ports[offset]
t.available.MoveToFront(elm)
return elm.Value.tuple
}
func (t *table) portOf(tuple tuple) uint16 {
t.mu.Lock()
elm := t.tuples[tuple]
t.mu.Unlock()
if elm == nil {
return 0
}
@ -59,11 +57,8 @@ func (t *table) newConn(tuple tuple) uint16 {
elm := t.available.Back()
b := elm.Value
t.mu.Lock()
delete(t.tuples, b.tuple)
t.tuples[tuple] = elm
t.mu.Unlock()
b.tuple = tuple
t.available.MoveToFront(elm)
@ -71,19 +66,6 @@ func (t *table) newConn(tuple tuple) uint16 {
return portBegin + b.offset
}
func (t *table) delete(tup tuple) {
t.mu.Lock()
elm := t.tuples[tup]
if elm == nil {
t.mu.Unlock()
return
}
delete(t.tuples, tup)
t.mu.Unlock()
t.available.MoveToBack(elm)
}
func newTable() *table {
result := &table{
tuples: make(map[tuple]*list.Element[*binding], portLength),

View File

@ -16,8 +16,6 @@ type conn struct {
net.Conn
tuple tuple
close func(tuple tuple)
}
func (t *TCP) Accept() (net.Conn, error) {
@ -39,9 +37,6 @@ func (t *TCP) Accept() (net.Conn, error) {
return &conn{
Conn: c,
tuple: tup,
close: func(tuple tuple) {
t.table.delete(tuple)
},
}, nil
}
@ -57,11 +52,6 @@ func (t *TCP) SetDeadline(time time.Time) error {
return t.listener.SetDeadline(time)
}
func (c *conn) Close() error {
c.close(c.tuple)
return c.Conn.Close()
}
func (c *conn) LocalAddr() net.Addr {
return &net.TCPAddr{
IP: c.tuple.SourceAddr.Addr().AsSlice(),