-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b07fb48
commit cd17884
Showing
12 changed files
with
307 additions
and
40 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,90 @@ | ||
package udpnat | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"os" | ||
"time" | ||
|
||
"github.com/sagernet/sing/common/buf" | ||
M "github.com/sagernet/sing/common/metadata" | ||
N "github.com/sagernet/sing/common/network" | ||
"github.com/sagernet/sing/common/pipe" | ||
) | ||
|
||
type natConn struct { | ||
writer N.PacketWriter | ||
localAddr M.Socksaddr | ||
packetChan chan *Packet | ||
doneChan chan struct{} | ||
readDeadline pipe.Deadline | ||
readWaitOptions N.ReadWaitOptions | ||
} | ||
|
||
func (c *natConn) ReadPacket(buffer *buf.Buffer) (addr M.Socksaddr, err error) { | ||
select { | ||
case p := <-c.packetChan: | ||
_, err = buffer.ReadOnceFrom(p.Buffer) | ||
destination := p.Destination | ||
p.Buffer.Release() | ||
PutPacket(p) | ||
return destination, err | ||
case <-c.doneChan: | ||
return M.Socksaddr{}, io.ErrClosedPipe | ||
case <-c.readDeadline.Wait(): | ||
return M.Socksaddr{}, os.ErrDeadlineExceeded | ||
} | ||
} | ||
|
||
func (c *natConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { | ||
return c.writer.WritePacket(buffer, destination) | ||
} | ||
|
||
func (c *natConn) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) { | ||
c.readWaitOptions = options | ||
return false | ||
} | ||
|
||
func (c *natConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) { | ||
select { | ||
case packet := <-c.packetChan: | ||
buffer = c.readWaitOptions.Copy(packet.Buffer) | ||
destination = packet.Destination | ||
PutPacket(packet) | ||
return | ||
case <-c.doneChan: | ||
return nil, M.Socksaddr{}, io.ErrClosedPipe | ||
case <-c.readDeadline.Wait(): | ||
return nil, M.Socksaddr{}, os.ErrDeadlineExceeded | ||
} | ||
} | ||
|
||
func (c *natConn) Close() error { | ||
select { | ||
case <-c.doneChan: | ||
default: | ||
close(c.doneChan) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *natConn) LocalAddr() net.Addr { | ||
return c.localAddr | ||
} | ||
|
||
func (c *natConn) RemoteAddr() net.Addr { | ||
return M.Socksaddr{} | ||
} | ||
|
||
func (c *natConn) SetDeadline(t time.Time) error { | ||
return os.ErrInvalid | ||
} | ||
|
||
func (c *natConn) SetReadDeadline(t time.Time) error { | ||
c.readDeadline.Set(t) | ||
return nil | ||
} | ||
|
||
func (c *natConn) SetWriteDeadline(t time.Time) error { | ||
return os.ErrInvalid | ||
} |
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,28 @@ | ||
package udpnat | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/sagernet/sing/common/buf" | ||
M "github.com/sagernet/sing/common/metadata" | ||
) | ||
|
||
var packetPool = sync.Pool{ | ||
New: func() any { | ||
return new(Packet) | ||
}, | ||
} | ||
|
||
type Packet struct { | ||
Buffer *buf.Buffer | ||
Destination M.Socksaddr | ||
} | ||
|
||
func NewPacket() *Packet { | ||
return packetPool.Get().(*Packet) | ||
} | ||
|
||
func PutPacket(packet *Packet) { | ||
*packet = Packet{} | ||
packetPool.Put(packet) | ||
} |
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,93 @@ | ||
package udpnat | ||
|
||
import ( | ||
"context" | ||
"net/netip" | ||
"time" | ||
|
||
"github.com/sagernet/sing/common" | ||
M "github.com/sagernet/sing/common/metadata" | ||
N "github.com/sagernet/sing/common/network" | ||
"github.com/sagernet/sing/common/pipe" | ||
"github.com/sagernet/sing/contrab/freelru" | ||
"github.com/sagernet/sing/contrab/maphash" | ||
) | ||
|
||
type Service struct { | ||
nat *freelru.LRU[netip.AddrPort, *natConn] | ||
handler N.UDPConnectionHandlerEx | ||
prepare PrepareFunc | ||
metrics Metrics | ||
} | ||
|
||
type PrepareFunc func(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) | ||
|
||
type Metrics struct { | ||
Creates uint64 | ||
Rejects uint64 | ||
Inputs uint64 | ||
Drops uint64 | ||
} | ||
|
||
func New(handler N.UDPConnectionHandlerEx, prepare PrepareFunc, timeout time.Duration) *Service { | ||
nat := common.Must1(freelru.New[netip.AddrPort, *natConn](1024, maphash.NewHasher[netip.AddrPort]().Hash32)) | ||
nat.SetLifetime(timeout) | ||
nat.SetHealthCheck(func(port netip.AddrPort, conn *natConn) bool { | ||
select { | ||
case <-conn.doneChan: | ||
return false | ||
default: | ||
return true | ||
} | ||
}) | ||
nat.SetOnEvict(func(_ netip.AddrPort, conn *natConn) { | ||
conn.Close() | ||
}) | ||
return &Service{ | ||
nat: nat, | ||
handler: handler, | ||
prepare: prepare, | ||
} | ||
} | ||
|
||
func (s *Service) NewPacket(bufferSlices [][]byte, source M.Socksaddr, destination M.Socksaddr, userData any) { | ||
conn, loaded := s.nat.Get(source.AddrPort()) | ||
if !loaded { | ||
ok, ctx, writer, onClose := s.prepare(source, destination, userData) | ||
if !ok { | ||
s.metrics.Rejects++ | ||
return | ||
} | ||
conn = &natConn{ | ||
writer: writer, | ||
localAddr: source, | ||
packetChan: make(chan *Packet, 64), | ||
doneChan: make(chan struct{}), | ||
readDeadline: pipe.MakeDeadline(), | ||
} | ||
s.nat.Add(source.AddrPort(), conn) | ||
s.handler.NewPacketConnectionEx(ctx, conn, source, destination, onClose) | ||
s.metrics.Creates++ | ||
} | ||
packet := NewPacket() | ||
buffer := conn.readWaitOptions.NewPacketBuffer() | ||
for _, bufferSlice := range bufferSlices { | ||
buffer.Write(bufferSlice) | ||
} | ||
*packet = Packet{ | ||
Buffer: buffer, | ||
Destination: destination, | ||
} | ||
select { | ||
case conn.packetChan <- packet: | ||
s.metrics.Inputs++ | ||
default: | ||
packet.Buffer.Release() | ||
PutPacket(packet) | ||
s.metrics.Drops++ | ||
} | ||
} | ||
|
||
func (s *Service) Metrics() Metrics { | ||
return s.metrics | ||
} |
Oops, something went wrong.