mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 12:27:37 +03:00
Add more context
This commit is contained in:
parent
3a9adc8f84
commit
34cba35e3b
10 changed files with 45 additions and 17 deletions
|
@ -347,9 +347,8 @@ func (c *client) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
|
|||
return rw.CopyConn(ctx, serverConn, conn)
|
||||
}
|
||||
|
||||
func (c *client) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error {
|
||||
func (c *client) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
|
||||
logrus.Info("outbound ", metadata.Protocol, " UDP ", metadata.Source, " ==> ", metadata.Destination)
|
||||
ctx := context.Background()
|
||||
udpConn, err := c.dialer.DialContext(ctx, "udp", c.server.String())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -228,13 +228,13 @@ func (s *server) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me
|
|||
return rw.CopyConn(ctx, conn, destConn)
|
||||
}
|
||||
|
||||
func (s *server) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error {
|
||||
func (s *server) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
|
||||
logrus.Info("inbound UDP ", metadata.Source, " ==> ", metadata.Destination)
|
||||
udpConn, err := net.ListenUDP("udp", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return socks.CopyNetPacketConn(context.Background(), udpConn, conn)
|
||||
return socks.CopyNetPacketConn(ctx, udpConn, conn)
|
||||
}
|
||||
|
||||
func (s *server) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
|
|
|
@ -109,7 +109,7 @@ func (c *localClient) NewConnection(ctx context.Context, conn net.Conn, metadata
|
|||
return rw.CopyConn(context.Background(), upstream, conn)
|
||||
}
|
||||
|
||||
func (c *localClient) NewPacketConnection(conn socks.PacketConn, _ M.Metadata) error {
|
||||
func (c *localClient) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
|
||||
upstream, err := net.Dial("tcp", c.upstream)
|
||||
if err != nil {
|
||||
return E.Cause(err, "connect to upstream")
|
||||
|
@ -121,7 +121,7 @@ func (c *localClient) NewPacketConnection(conn socks.PacketConn, _ M.Metadata) e
|
|||
}
|
||||
|
||||
client := uot.NewClientConn(upstream)
|
||||
return socks.CopyPacketConn(context.Background(), client, conn)
|
||||
return socks.CopyPacketConn(ctx, client, conn)
|
||||
}
|
||||
|
||||
func (c *localClient) OnError(err error) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package random
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
|
@ -10,9 +11,37 @@ import (
|
|||
|
||||
var System = rand.Reader
|
||||
|
||||
func Blake3KeyedHash() io.Reader {
|
||||
func Blake3KeyedHash() Source {
|
||||
key := make([]byte, 32)
|
||||
common.Must1(io.ReadFull(System, key))
|
||||
h := blake3.New(1024, key)
|
||||
return h.XOF()
|
||||
return Source{h.XOF()}
|
||||
}
|
||||
|
||||
const (
|
||||
rngMax = 1 << 63
|
||||
rngMask = rngMax - 1
|
||||
)
|
||||
|
||||
type Source struct {
|
||||
io.Reader
|
||||
}
|
||||
|
||||
func (s Source) Int63() int64 {
|
||||
return s.Int64() & rngMask
|
||||
}
|
||||
|
||||
func (s Source) Int64() int64 {
|
||||
var num int64
|
||||
common.Must(binary.Read(s, binary.BigEndian, &num))
|
||||
return num
|
||||
}
|
||||
|
||||
func (s Source) Uint64() uint64 {
|
||||
var num uint64
|
||||
common.Must(binary.Read(s, binary.BigEndian, &num))
|
||||
return num
|
||||
}
|
||||
|
||||
func (s Source) Seed(int64) {
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ func (s *Service[T]) NewPacket(key T, writer func() socks.PacketWriter, buffer *
|
|||
})
|
||||
if !loaded {
|
||||
go func() {
|
||||
err := s.handler.NewPacketConnection(c, metadata)
|
||||
err := s.handler.NewPacketConnection(c.ctx, c, metadata)
|
||||
if err != nil {
|
||||
s.handler.HandleError(err)
|
||||
}
|
||||
|
|
|
@ -203,8 +203,8 @@ func (s *nonePacketWriter) WritePacket(buffer *buf.Buffer, destination *M.AddrPo
|
|||
return s.PacketConn.WritePacket(buffer, s.sourceAddr)
|
||||
}
|
||||
|
||||
func (s *NoneService) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error {
|
||||
return s.handler.NewPacketConnection(conn, metadata)
|
||||
func (s *NoneService) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
|
||||
return s.handler.NewPacketConnection(ctx, conn, metadata)
|
||||
}
|
||||
|
||||
func (s *NoneService) HandleError(err error) {
|
||||
|
|
|
@ -217,8 +217,8 @@ func (w *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination *M.Addr
|
|||
return w.PacketConn.WritePacket(buffer, w.source)
|
||||
}
|
||||
|
||||
func (s *Service) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error {
|
||||
return s.handler.NewPacketConnection(conn, metadata)
|
||||
func (s *Service) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
|
||||
return s.handler.NewPacketConnection(ctx, conn, metadata)
|
||||
}
|
||||
|
||||
func (s *Service) HandleError(err error) {
|
||||
|
|
|
@ -382,8 +382,8 @@ func (m *Service) newUDPSession() *serverUDPSession {
|
|||
return session
|
||||
}
|
||||
|
||||
func (s *Service) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error {
|
||||
return s.handler.NewPacketConnection(conn, metadata)
|
||||
func (s *Service) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error {
|
||||
return s.handler.NewPacketConnection(ctx, conn, metadata)
|
||||
}
|
||||
|
||||
func (s *Service) HandleError(err error) {
|
||||
|
|
|
@ -37,7 +37,7 @@ type UDPHandler interface {
|
|||
}
|
||||
|
||||
type UDPConnectionHandler interface {
|
||||
NewPacketConnection(conn PacketConn, metadata M.Metadata) error
|
||||
NewPacketConnection(ctx context.Context, conn PacketConn, metadata M.Metadata) error
|
||||
}
|
||||
|
||||
type PacketConnStub struct{}
|
||||
|
|
|
@ -134,7 +134,7 @@ func HandleConnection(ctx context.Context, conn net.Conn, authenticator auth.Aut
|
|||
metadata.Protocol = "socks"
|
||||
metadata.Destination = request.Destination
|
||||
go func() {
|
||||
err := handler.NewPacketConnection(NewAssociatePacketConn(conn, udpConn, request.Destination), metadata)
|
||||
err := handler.NewPacketConnection(ctx, NewAssociatePacketConn(conn, udpConn, request.Destination), metadata)
|
||||
if err != nil {
|
||||
handler.HandleError(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue