Improve udpnat

This commit is contained in:
世界 2022-05-30 12:19:38 +08:00
parent 1a554a61f3
commit b7d63b55c9
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 24 additions and 9 deletions

View file

@ -2,5 +2,5 @@
PROJECTS=$(dirname "$0")/../..
go get -v github.com/sagernet/sing@$(git -C $PROJECTS/sing rev-parse HEAD)
go get -x github.com/sagernet/sing@$(git -C $PROJECTS/sing rev-parse HEAD)
go mod tidy

2
go.mod
View file

@ -3,7 +3,7 @@ module github.com/sagernet/sing-shadowsocks
go 1.18
require (
github.com/sagernet/sing v0.0.0-20220530023925-d634f5f8d1f9
github.com/sagernet/sing v0.0.0-20220530041323-c82c144a8e00
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
lukechampine.com/blake3 v1.1.7
)

4
go.sum
View file

@ -1,8 +1,8 @@
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/sagernet/sing v0.0.0-20220530023925-d634f5f8d1f9 h1:KqmiPU4dmM5bXsWF3i321bhdaY0iw9aSk9lHnKFqcA8=
github.com/sagernet/sing v0.0.0-20220530023925-d634f5f8d1f9/go.mod h1:w2HnJzXKHpD6F5Z/9XlSD4qbcpHY2RSZuQnFzqgELMg=
github.com/sagernet/sing v0.0.0-20220530041323-c82c144a8e00 h1:kQCITv1yBrSSL6RIBPWTe1+goyWLbS4DELWTeRKmoh8=
github.com/sagernet/sing v0.0.0-20220530041323-c82c144a8e00/go.mod h1:w2HnJzXKHpD6F5Z/9XlSD4qbcpHY2RSZuQnFzqgELMg=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=

View file

@ -187,6 +187,9 @@ func (s *NoneService) NewConnection(ctx context.Context, conn net.Conn, metadata
return s.handler.NewConnection(ctx, conn, metadata)
}
func (s *NoneService) WriteIsThreadUnsafe() {
}
func (s *NoneService) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
destination, err := M.SocksaddrSerializer.ReadAddrPort(buffer)
if err != nil {

View file

@ -189,6 +189,9 @@ func (c *serverConn) Upstream() any {
return c.Conn
}
func (s *Service) WriteIsThreadUnsafe() {
}
func (s *Service) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {

View file

@ -175,6 +175,9 @@ func (s *Relay[U]) newConnection(ctx context.Context, conn net.Conn, metadata M.
return s.handler.NewConnection(ctx, conn, metadata)
}
func (s *Relay[U]) WriteIsThreadUnsafe() {
}
func (s *Relay[U]) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {

View file

@ -306,6 +306,9 @@ func (c *serverConn) Upstream() any {
return c.Conn
}
func (s *Service) WriteIsThreadUnsafe() {
}
func (s *Service) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {
@ -489,21 +492,21 @@ func (s *serverUDPSession) nextPacketId() uint64 {
return atomic.AddUint64(&s.packetId, 1)
}
func (m *Service) newUDPSession() *serverUDPSession {
func (s *Service) newUDPSession() *serverUDPSession {
session := &serverUDPSession{}
if m.udpCipher != nil {
if s.udpCipher != nil {
session.rng = Blake3KeyedHash(rand.Reader)
common.Must(binary.Read(session.rng, binary.BigEndian, &session.sessionId))
} else {
common.Must(binary.Read(rand.Reader, binary.BigEndian, &session.sessionId))
}
session.packetId--
if m.udpCipher == nil {
if s.udpCipher == nil {
sessionId := make([]byte, 8)
binary.BigEndian.PutUint64(sessionId, session.sessionId)
key := SessionKey(m.psk, sessionId, m.keySaltLength)
key := SessionKey(s.psk, sessionId, s.keySaltLength)
var err error
session.cipher, err = m.constructor(common.Dup(key))
session.cipher, err = s.constructor(common.Dup(key))
common.Must(err)
common.KeepAlive(key)
}

View file

@ -235,6 +235,9 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta
}, metadata)
}
func (s *MultiService[U]) WriteIsThreadUnsafe() {
}
func (s *MultiService[U]) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {