2022-05-31 07:55:38 +00:00
|
|
|
//go:build go1.18
|
|
|
|
|
2022-05-23 23:25:12 +00:00
|
|
|
package shadowsocks_2022
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2022-06-06 06:12:59 +00:00
|
|
|
"strconv"
|
2022-05-23 23:25:12 +00:00
|
|
|
|
2022-05-25 09:25:26 +00:00
|
|
|
"github.com/sagernet/sing-shadowsocks"
|
|
|
|
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
|
2022-06-06 06:12:59 +00:00
|
|
|
C "github.com/sagernet/sing/common"
|
2022-05-23 23:25:12 +00:00
|
|
|
B "github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2022-06-01 03:12:43 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-05-23 23:25:12 +00:00
|
|
|
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/log"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2022-05-25 00:49:52 +00:00
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
2022-05-23 23:25:12 +00:00
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"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((*MultiUserServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewMultiServer(ctx, config.(*MultiUserServerConfig))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiUserInbound struct {
|
|
|
|
networks []net.Network
|
2022-05-25 00:49:52 +00:00
|
|
|
users []*User
|
|
|
|
service *shadowaead_2022.MultiService[int]
|
2022-05-23 23:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMultiServer(ctx context.Context, config *MultiUserServerConfig) (*MultiUserInbound, error) {
|
|
|
|
networks := config.Network
|
|
|
|
if len(networks) == 0 {
|
|
|
|
networks = []net.Network{
|
|
|
|
net.Network_TCP,
|
|
|
|
net.Network_UDP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inbound := &MultiUserInbound{
|
|
|
|
networks: networks,
|
2022-05-25 09:25:26 +00:00
|
|
|
users: config.Users,
|
2022-05-23 23:25:12 +00:00
|
|
|
}
|
|
|
|
if config.Key == "" {
|
|
|
|
return nil, newError("missing key")
|
|
|
|
}
|
|
|
|
psk, err := base64.StdEncoding.DecodeString(config.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("parse config").Base(err)
|
|
|
|
}
|
2022-05-25 09:25:26 +00:00
|
|
|
service, err := shadowaead_2022.NewMultiService[int](config.Method, psk, 500, inbound)
|
2022-05-23 23:25:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("create service").Base(err)
|
|
|
|
}
|
|
|
|
|
2022-05-25 00:49:52 +00:00
|
|
|
for i, user := range config.Users {
|
2022-05-23 23:25:12 +00:00
|
|
|
if user.Email == "" {
|
|
|
|
u := uuid.New()
|
2022-06-06 06:12:59 +00:00
|
|
|
user.Email = "unnamed-user-" + strconv.Itoa(i) + "-" + u.String()
|
2022-05-23 23:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 06:12:59 +00:00
|
|
|
err = service.UpdateUsersWithPasswords(
|
|
|
|
C.MapIndexed(config.Users, func(index int, it *User) int { return index }),
|
|
|
|
C.Map(config.Users, func(it *User) string { return it.Key }),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("create service").Base(err)
|
|
|
|
}
|
2022-05-23 23:25:12 +00:00
|
|
|
|
|
|
|
inbound.service = service
|
|
|
|
return inbound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) Network() []net.Network {
|
|
|
|
return i.networks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) Process(ctx context.Context, network net.Network, connection stat.Connection, dispatcher routing.Dispatcher) error {
|
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
|
|
|
|
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 {
|
2022-06-01 03:12:43 +00:00
|
|
|
return returnError(i.service.NewConnection(ctx, connection, metadata))
|
2022-05-23 23:25:12 +00:00
|
|
|
} else {
|
|
|
|
reader := buf.NewReader(connection)
|
|
|
|
pc := &natPacketConn{connection}
|
|
|
|
for {
|
|
|
|
mb, err := reader.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
2022-06-01 03:12:43 +00:00
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return returnError(err)
|
2022-05-23 23:25:12 +00:00
|
|
|
}
|
|
|
|
for _, buffer := range mb {
|
2022-06-01 03:12:43 +00:00
|
|
|
err = i.service.NewPacket(ctx, pc, B.As(buffer.Bytes()).ToOwned(), metadata)
|
2022-05-23 23:25:12 +00:00
|
|
|
if err != nil {
|
2022-06-01 03:12:43 +00:00
|
|
|
buf.ReleaseMulti(mb)
|
2022-05-23 23:25:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-01 03:12:43 +00:00
|
|
|
buffer.Release()
|
2022-05-23 23:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
2022-05-25 00:49:52 +00:00
|
|
|
userCtx := ctx.(*shadowsocks.UserContext[int])
|
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
user := i.users[userCtx.User]
|
|
|
|
inbound.User = &protocol.MemoryUser{
|
|
|
|
Email: user.Email,
|
|
|
|
Level: uint32(user.Level),
|
|
|
|
}
|
2022-05-23 23:25:12 +00:00
|
|
|
ctx = log.ContextWithAccessMessage(userCtx.Context, &log.AccessMessage{
|
|
|
|
From: metadata.Source,
|
|
|
|
To: metadata.Destination,
|
|
|
|
Status: log.AccessAccepted,
|
2022-05-25 00:49:52 +00:00
|
|
|
Email: user.Email,
|
2022-05-23 23:25:12 +00:00
|
|
|
})
|
|
|
|
newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
dispatcher := session.DispatcherFromContext(ctx)
|
|
|
|
link, err := dispatcher.Dispatch(ctx, toDestination(metadata.Destination, net.Network_TCP))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outConn := &pipeConnWrapper{
|
|
|
|
&buf.BufferedReader{Reader: link.Reader},
|
|
|
|
link.Writer,
|
|
|
|
conn,
|
|
|
|
}
|
|
|
|
return bufio.CopyConn(ctx, conn, outConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
2022-05-25 00:49:52 +00:00
|
|
|
userCtx := ctx.(*shadowsocks.UserContext[int])
|
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
user := i.users[userCtx.User]
|
|
|
|
inbound.User = &protocol.MemoryUser{
|
|
|
|
Email: user.Email,
|
|
|
|
Level: uint32(user.Level),
|
|
|
|
}
|
2022-05-23 23:25:12 +00:00
|
|
|
ctx = log.ContextWithAccessMessage(userCtx.Context, &log.AccessMessage{
|
|
|
|
From: metadata.Source,
|
|
|
|
To: metadata.Destination,
|
|
|
|
Status: log.AccessAccepted,
|
2022-05-25 00:49:52 +00:00
|
|
|
Email: user.Email,
|
2022-05-23 23:25:12 +00:00
|
|
|
})
|
|
|
|
newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
dispatcher := session.DispatcherFromContext(ctx)
|
|
|
|
destination := toDestination(metadata.Destination, net.Network_UDP)
|
|
|
|
link, err := dispatcher.Dispatch(ctx, destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outConn := &packetConnWrapper{
|
|
|
|
Reader: link.Reader,
|
|
|
|
Writer: link.Writer,
|
|
|
|
Dest: destination,
|
|
|
|
}
|
|
|
|
return bufio.CopyPacketConn(ctx, conn, outConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) HandleError(err error) {
|
2022-06-01 03:12:43 +00:00
|
|
|
if E.IsClosed(err) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-23 23:25:12 +00:00
|
|
|
newError(err).AtWarning().WriteToLog()
|
|
|
|
}
|