mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 20:07:38 +03:00
Append conn info in shadowsocks server error
This commit is contained in:
parent
6ebf3e60d7
commit
736369f171
8 changed files with 107 additions and 6 deletions
|
@ -2,6 +2,8 @@ package shadowsocks
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
|
@ -29,3 +31,31 @@ type UserContext[U comparable] struct {
|
|||
context.Context
|
||||
User U
|
||||
}
|
||||
|
||||
type ServerConnError struct {
|
||||
net.Conn
|
||||
Source *M.AddrPort
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e *ServerConnError) Unwrap() error {
|
||||
return e.Cause
|
||||
}
|
||||
|
||||
func (e *ServerConnError) Error() string {
|
||||
return fmt.Sprint("shadowsocks: serve TCP from ", e.Source, ": ", e.Cause)
|
||||
}
|
||||
|
||||
type ServerPacketError struct {
|
||||
socks.PacketConn
|
||||
Source *M.AddrPort
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e *ServerPacketError) Unwrap() error {
|
||||
return e.Cause
|
||||
}
|
||||
|
||||
func (e *ServerPacketError) Error() string {
|
||||
return fmt.Sprint("shadowsocks: serve UDP from ", e.Source, ": ", e.Cause)
|
||||
}
|
||||
|
|
|
@ -79,6 +79,14 @@ func NewService(method string, key []byte, password []byte, secureRNG io.Reader,
|
|||
}
|
||||
|
||||
func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
err := s.newConnection(ctx, conn, metadata)
|
||||
if err != nil {
|
||||
err = &shadowsocks.ServerConnError{Conn: conn, Source: metadata.Source, Cause: err}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
_salt := buf.Make(s.keySaltLength)
|
||||
salt := common.Dup(_salt)
|
||||
|
||||
|
@ -191,6 +199,14 @@ func (c *serverConn) WriterReplaceable() bool {
|
|||
}
|
||||
|
||||
func (s *Service) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
err := s.newPacket(conn, buffer, metadata)
|
||||
if err != nil {
|
||||
err = &shadowsocks.ServerPacketError{PacketConn: conn, Source: metadata.Source, Cause: err}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) newPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
if buffer.Len() < s.keySaltLength {
|
||||
return E.New("bad packet")
|
||||
}
|
||||
|
|
|
@ -73,6 +73,14 @@ func NewService(method string, psk [KeySaltSize]byte, secureRNG io.Reader, udpTi
|
|||
}
|
||||
|
||||
func (s *Service) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
err := s.newConnection(ctx, conn, metadata)
|
||||
if err != nil {
|
||||
err = &shadowsocks.ServerConnError{Conn: conn, Source: metadata.Source, Cause: err}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
requestSalt := make([]byte, KeySaltSize)
|
||||
_, err := io.ReadFull(conn, requestSalt)
|
||||
if err != nil {
|
||||
|
@ -230,6 +238,14 @@ func (c *serverConn) WriterReplaceable() bool {
|
|||
}
|
||||
|
||||
func (s *Service) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
err := s.newPacket(conn, buffer, metadata)
|
||||
if err != nil {
|
||||
err = &shadowsocks.ServerPacketError{PacketConn: conn, Source: metadata.Source, Cause: err}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) newPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
var packetHeader []byte
|
||||
if s.udpCipher != nil {
|
||||
_, err := s.udpCipher.Open(buffer.Index(PacketNonceSize), buffer.To(PacketNonceSize), buffer.From(PacketNonceSize), nil)
|
||||
|
|
|
@ -73,6 +73,14 @@ func NewMultiService[U comparable](method string, iPSK [KeySaltSize]byte, secure
|
|||
}
|
||||
|
||||
func (s *MultiService[U]) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
err := s.newConnection(ctx, conn, metadata)
|
||||
if err != nil {
|
||||
err = &shadowsocks.ServerConnError{Conn: conn, Source: metadata.Source, Cause: err}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
||||
requestSalt := make([]byte, KeySaltSize)
|
||||
_, err := io.ReadFull(conn, requestSalt)
|
||||
if err != nil {
|
||||
|
@ -166,6 +174,14 @@ func (s *MultiService[U]) NewConnection(ctx context.Context, conn net.Conn, meta
|
|||
}
|
||||
|
||||
func (s *MultiService[U]) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
err := s.newPacket(conn, buffer, metadata)
|
||||
if err != nil {
|
||||
err = &shadowsocks.ServerPacketError{PacketConn: conn, Source: metadata.Source, Cause: err}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *MultiService[U]) newPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
|
||||
packetHeader := buffer.To(aes.BlockSize)
|
||||
s.udpBlockCipher.Decrypt(packetHeader, packetHeader)
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ type PacketConn interface {
|
|||
|
||||
Close() error
|
||||
LocalAddr() net.Addr
|
||||
RemoteAddr() net.Addr
|
||||
SetDeadline(t time.Time) error
|
||||
SetReadDeadline(t time.Time) error
|
||||
SetWriteDeadline(t time.Time) error
|
||||
|
@ -42,6 +43,10 @@ type UDPConnectionHandler interface {
|
|||
|
||||
type PacketConnStub struct{}
|
||||
|
||||
func (s *PacketConnStub) RemoteAddr() net.Addr {
|
||||
return &common.DummyAddr{}
|
||||
}
|
||||
|
||||
func (s *PacketConnStub) SetDeadline(t time.Time) error {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
|
@ -113,3 +118,7 @@ func (p *PacketConnWrapper) ReadPacket(buffer *buf.Buffer) (*M.AddrPort, error)
|
|||
func (p *PacketConnWrapper) WritePacket(buffer *buf.Buffer, destination *M.AddrPort) error {
|
||||
return common.Error(p.WriteTo(buffer.Bytes(), destination.UDPAddr()))
|
||||
}
|
||||
|
||||
func (p *PacketConnWrapper) RemoteAddr() net.Addr {
|
||||
return &common.DummyAddr{}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue