github.com/moqsien/xraycore@v1.8.5/proxy/shadowsocks_2022/inbound.go (about)

     1  package shadowsocks_2022
     2  
     3  import (
     4  	"context"
     5  
     6  	shadowsocks "github.com/sagernet/sing-shadowsocks"
     7  	"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
     8  	C "github.com/sagernet/sing/common"
     9  	B "github.com/sagernet/sing/common/buf"
    10  	"github.com/sagernet/sing/common/bufio"
    11  	E "github.com/sagernet/sing/common/exceptions"
    12  	M "github.com/sagernet/sing/common/metadata"
    13  	N "github.com/sagernet/sing/common/network"
    14  	"github.com/moqsien/xraycore/common"
    15  	"github.com/moqsien/xraycore/common/buf"
    16  	"github.com/moqsien/xraycore/common/log"
    17  	"github.com/moqsien/xraycore/common/net"
    18  	"github.com/moqsien/xraycore/common/protocol"
    19  	"github.com/moqsien/xraycore/common/session"
    20  	"github.com/moqsien/xraycore/common/singbridge"
    21  	"github.com/moqsien/xraycore/features/routing"
    22  	"github.com/moqsien/xraycore/transport/internet/stat"
    23  )
    24  
    25  func init() {
    26  	common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
    27  		return NewServer(ctx, config.(*ServerConfig))
    28  	}))
    29  }
    30  
    31  type Inbound struct {
    32  	networks []net.Network
    33  	service  shadowsocks.Service
    34  	email    string
    35  	level    int
    36  }
    37  
    38  func NewServer(ctx context.Context, config *ServerConfig) (*Inbound, error) {
    39  	networks := config.Network
    40  	if len(networks) == 0 {
    41  		networks = []net.Network{
    42  			net.Network_TCP,
    43  			net.Network_UDP,
    44  		}
    45  	}
    46  	inbound := &Inbound{
    47  		networks: networks,
    48  		email:    config.Email,
    49  		level:    int(config.Level),
    50  	}
    51  	if !C.Contains(shadowaead_2022.List, config.Method) {
    52  		return nil, newError("unsupported method ", config.Method)
    53  	}
    54  	service, err := shadowaead_2022.NewServiceWithPassword(config.Method, config.Key, 500, inbound, nil)
    55  	if err != nil {
    56  		return nil, newError("create service").Base(err)
    57  	}
    58  	inbound.service = service
    59  	return inbound, nil
    60  }
    61  
    62  func (i *Inbound) Network() []net.Network {
    63  	return i.networks
    64  }
    65  
    66  func (i *Inbound) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
    67  	inbound := session.InboundFromContext(ctx)
    68  	inbound.Name = "shadowsocks-2022"
    69  
    70  	var metadata M.Metadata
    71  	if inbound.Source.IsValid() {
    72  		metadata.Source = M.ParseSocksaddr(inbound.Source.NetAddr())
    73  	}
    74  
    75  	ctx = session.ContextWithDispatcher(ctx, dispatcher)
    76  
    77  	if network == net.Network_TCP {
    78  		return singbridge.ReturnError(i.service.NewConnection(ctx, connection, metadata))
    79  	} else {
    80  		reader := buf.NewReader(connection)
    81  		pc := &natPacketConn{connection}
    82  		for {
    83  			mb, err := reader.ReadMultiBuffer()
    84  			if err != nil {
    85  				buf.ReleaseMulti(mb)
    86  				return singbridge.ReturnError(err)
    87  			}
    88  			for _, buffer := range mb {
    89  				packet := B.As(buffer.Bytes()).ToOwned()
    90  				err = i.service.NewPacket(ctx, pc, packet, metadata)
    91  				if err != nil {
    92  					packet.Release()
    93  					buf.ReleaseMulti(mb)
    94  					return err
    95  				}
    96  				buffer.Release()
    97  			}
    98  		}
    99  	}
   100  }
   101  
   102  func (i *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
   103  	inbound := session.InboundFromContext(ctx)
   104  	inbound.User = &protocol.MemoryUser{
   105  		Email: i.email,
   106  		Level: uint32(i.level),
   107  	}
   108  	ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
   109  		From:   metadata.Source,
   110  		To:     metadata.Destination,
   111  		Status: log.AccessAccepted,
   112  		Email:  i.email,
   113  	})
   114  	newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
   115  	dispatcher := session.DispatcherFromContext(ctx)
   116  	link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
   117  	if err != nil {
   118  		return err
   119  	}
   120  	return singbridge.CopyConn(ctx, nil, link, conn)
   121  }
   122  
   123  func (i *Inbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
   124  	inbound := session.InboundFromContext(ctx)
   125  	inbound.User = &protocol.MemoryUser{
   126  		Email: i.email,
   127  		Level: uint32(i.level),
   128  	}
   129  	ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
   130  		From:   metadata.Source,
   131  		To:     metadata.Destination,
   132  		Status: log.AccessAccepted,
   133  		Email:  i.email,
   134  	})
   135  	newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
   136  	dispatcher := session.DispatcherFromContext(ctx)
   137  	destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
   138  	link, err := dispatcher.Dispatch(ctx, destination)
   139  	if err != nil {
   140  		return err
   141  	}
   142  	outConn := &singbridge.PacketConnWrapper{
   143  		Reader: link.Reader,
   144  		Writer: link.Writer,
   145  		Dest:   destination,
   146  	}
   147  	return bufio.CopyPacketConn(ctx, conn, outConn)
   148  }
   149  
   150  func (i *Inbound) NewError(ctx context.Context, err error) {
   151  	if E.IsClosed(err) {
   152  		return
   153  	}
   154  	newError(err).AtWarning().WriteToLog()
   155  }
   156  
   157  type natPacketConn struct {
   158  	net.Conn
   159  }
   160  
   161  func (c *natPacketConn) ReadPacket(buffer *B.Buffer) (addr M.Socksaddr, err error) {
   162  	_, err = buffer.ReadFrom(c)
   163  	return
   164  }
   165  
   166  func (c *natPacketConn) WritePacket(buffer *B.Buffer, addr M.Socksaddr) error {
   167  	_, err := buffer.WriteTo(c)
   168  	return err
   169  }