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