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