Xray-core/proxy/shadowsocks_2022/inbound_relay.go

183 lines
5.5 KiB
Go
Raw Permalink Normal View History

2022-06-19 17:17:23 +03:00
package shadowsocks_2022
import (
"context"
"strconv"
"strings"
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
C "github.com/sagernet/sing/common"
A "github.com/sagernet/sing/common/auth"
2022-06-19 17:17:23 +03:00
B "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
2022-06-19 17:17:23 +03:00
"github.com/xtls/xray-core/common/log"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/session"
2023-04-23 14:31:41 +03:00
"github.com/xtls/xray-core/common/singbridge"
2022-06-19 17:17:23 +03:00
"github.com/xtls/xray-core/common/uuid"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport/internet/stat"
)
func init() {
common.Must(common.RegisterConfig((*RelayServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRelayServer(ctx, config.(*RelayServerConfig))
}))
}
type RelayInbound struct {
networks []net.Network
destinations []*RelayDestination
service *shadowaead_2022.RelayService[int]
}
func NewRelayServer(ctx context.Context, config *RelayServerConfig) (*RelayInbound, error) {
networks := config.Network
if len(networks) == 0 {
networks = []net.Network{
net.Network_TCP,
net.Network_UDP,
}
}
inbound := &RelayInbound{
networks: networks,
destinations: config.Destinations,
}
if !C.Contains(shadowaead_2022.List, config.Method) || !strings.Contains(config.Method, "aes") {
return nil, errors.New("unsupported method ", config.Method)
2022-06-19 17:17:23 +03:00
}
service, err := shadowaead_2022.NewRelayServiceWithPassword[int](config.Method, config.Key, 500, inbound)
if err != nil {
return nil, errors.New("create service").Base(err)
2022-06-19 17:17:23 +03:00
}
for i, destination := range config.Destinations {
if destination.Email == "" {
u := uuid.New()
destination.Email = "unnamed-destination-" + strconv.Itoa(i) + "-" + u.String()
}
}
err = service.UpdateUsersWithPasswords(
C.MapIndexed(config.Destinations, func(index int, it *RelayDestination) int { return index }),
C.Map(config.Destinations, func(it *RelayDestination) string { return it.Key }),
C.Map(config.Destinations, func(it *RelayDestination) M.Socksaddr {
2023-04-23 14:31:41 +03:00
return singbridge.ToSocksaddr(net.Destination{
2022-06-19 17:17:23 +03:00
Address: it.Address.AsAddress(),
Port: net.Port(it.Port),
})
}),
)
if err != nil {
return nil, errors.New("create service").Base(err)
2022-06-19 17:17:23 +03:00
}
inbound.service = service
return inbound, nil
}
func (i *RelayInbound) Network() []net.Network {
return i.networks
}
func (i *RelayInbound) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
inbound := session.InboundFromContext(ctx)
inbound.Name = "shadowsocks-2022-relay"
inbound.CanSpliceCopy = 3
2022-06-19 17:17:23 +03:00
var metadata M.Metadata
if inbound.Source.IsValid() {
metadata.Source = M.ParseSocksaddr(inbound.Source.NetAddr())
}
ctx = session.ContextWithDispatcher(ctx, dispatcher)
if network == net.Network_TCP {
2023-04-23 14:31:41 +03:00
return singbridge.ReturnError(i.service.NewConnection(ctx, connection, metadata))
2022-06-19 17:17:23 +03:00
} else {
reader := buf.NewReader(connection)
pc := &natPacketConn{connection}
for {
mb, err := reader.ReadMultiBuffer()
if err != nil {
buf.ReleaseMulti(mb)
2023-04-23 14:31:41 +03:00
return singbridge.ReturnError(err)
2022-06-19 17:17:23 +03:00
}
for _, buffer := range mb {
2022-06-28 02:50:04 +03:00
packet := B.As(buffer.Bytes()).ToOwned()
2023-12-15 18:45:41 +03:00
buffer.Release()
2022-06-28 02:50:04 +03:00
err = i.service.NewPacket(ctx, pc, packet, metadata)
2022-06-19 17:17:23 +03:00
if err != nil {
2022-06-28 02:50:04 +03:00
packet.Release()
2022-06-19 17:17:23 +03:00
buf.ReleaseMulti(mb)
return err
}
}
}
}
}
func (i *RelayInbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
inbound := session.InboundFromContext(ctx)
2022-09-17 04:54:37 +03:00
userInt, _ := A.UserFromContext[int](ctx)
user := i.destinations[userInt]
2022-06-19 17:17:23 +03:00
inbound.User = &protocol.MemoryUser{
Email: user.Email,
Level: uint32(user.Level),
}
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
2022-06-19 17:17:23 +03:00
From: metadata.Source,
To: metadata.Destination,
Status: log.AccessAccepted,
Email: user.Email,
})
errors.LogInfo(ctx, "tunnelling request to tcp:", metadata.Destination)
2022-06-19 17:17:23 +03:00
dispatcher := session.DispatcherFromContext(ctx)
2023-04-23 14:31:41 +03:00
link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
2022-06-19 17:17:23 +03:00
if err != nil {
return err
}
2023-04-23 14:31:41 +03:00
return singbridge.CopyConn(ctx, nil, link, conn)
2022-06-19 17:17:23 +03:00
}
func (i *RelayInbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
inbound := session.InboundFromContext(ctx)
2022-09-17 04:54:37 +03:00
userInt, _ := A.UserFromContext[int](ctx)
user := i.destinations[userInt]
2022-06-19 17:17:23 +03:00
inbound.User = &protocol.MemoryUser{
Email: user.Email,
Level: uint32(user.Level),
}
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
2022-06-19 17:17:23 +03:00
From: metadata.Source,
To: metadata.Destination,
Status: log.AccessAccepted,
Email: user.Email,
})
errors.LogInfo(ctx, "tunnelling request to udp:", metadata.Destination)
2022-06-19 17:17:23 +03:00
dispatcher := session.DispatcherFromContext(ctx)
2023-04-23 14:31:41 +03:00
destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
2022-06-19 17:17:23 +03:00
link, err := dispatcher.Dispatch(ctx, destination)
if err != nil {
return err
}
2023-04-23 14:31:41 +03:00
outConn := &singbridge.PacketConnWrapper{
2022-06-19 17:17:23 +03:00
Reader: link.Reader,
Writer: link.Writer,
Dest: destination,
}
return bufio.CopyPacketConn(ctx, conn, outConn)
}
2022-07-16 04:32:54 +03:00
func (i *RelayInbound) NewError(ctx context.Context, err error) {
2022-06-19 17:17:23 +03:00
if E.IsClosed(err) {
return
}
errors.LogWarning(ctx, err.Error())
2022-06-19 17:17:23 +03:00
}