2022-05-24 02:25:12 +03:00
|
|
|
package shadowsocks_2022
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2022-06-06 09:12:59 +03:00
|
|
|
"strconv"
|
2022-12-03 16:04:14 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-05-24 02:25:12 +03:00
|
|
|
|
2022-05-25 12:25:26 +03:00
|
|
|
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
|
2022-06-06 09:12:59 +03:00
|
|
|
C "github.com/sagernet/sing/common"
|
2022-08-28 05:40:55 +03:00
|
|
|
A "github.com/sagernet/sing/common/auth"
|
2022-05-24 02:25:12 +03:00
|
|
|
B "github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2022-06-01 06:12:43 +03:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-05-24 02:25:12 +03: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"
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2022-05-24 02:25:12 +03:00
|
|
|
"github.com/xtls/xray-core/common/log"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2022-05-25 03:49:52 +03:00
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
2022-05-24 02:25:12 +03:00
|
|
|
"github.com/xtls/xray-core/common/session"
|
2023-04-23 14:31:41 +03:00
|
|
|
"github.com/xtls/xray-core/common/singbridge"
|
2022-05-24 02:25:12 +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((*MultiUserServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewMultiServer(ctx, config.(*MultiUserServerConfig))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiUserInbound struct {
|
2022-12-03 16:04:14 +03:00
|
|
|
sync.Mutex
|
2022-05-24 02:25:12 +03:00
|
|
|
networks []net.Network
|
2022-05-25 03:49:52 +03:00
|
|
|
users []*User
|
|
|
|
service *shadowaead_2022.MultiService[int]
|
2022-05-24 02:25:12 +03: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 12:25:26 +03:00
|
|
|
users: config.Users,
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|
|
|
|
if config.Key == "" {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("missing key")
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|
|
|
|
psk, err := base64.StdEncoding.DecodeString(config.Key)
|
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("parse config").Base(err)
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|
2023-03-20 09:59:13 +03:00
|
|
|
service, err := shadowaead_2022.NewMultiService[int](config.Method, psk, 500, inbound, nil)
|
2022-05-24 02:25:12 +03:00
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("create service").Base(err)
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|
|
|
|
|
2022-05-25 03:49:52 +03:00
|
|
|
for i, user := range config.Users {
|
2022-05-24 02:25:12 +03:00
|
|
|
if user.Email == "" {
|
|
|
|
u := uuid.New()
|
2022-06-06 09:12:59 +03:00
|
|
|
user.Email = "unnamed-user-" + strconv.Itoa(i) + "-" + u.String()
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 09:12:59 +03: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 {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("create service").Base(err)
|
2022-06-06 09:12:59 +03:00
|
|
|
}
|
2022-05-24 02:25:12 +03:00
|
|
|
|
|
|
|
inbound.service = service
|
|
|
|
return inbound, nil
|
|
|
|
}
|
|
|
|
|
2022-12-03 16:04:14 +03:00
|
|
|
// AddUser implements proxy.UserManager.AddUser().
|
|
|
|
func (i *MultiUserInbound) AddUser(ctx context.Context, u *protocol.MemoryUser) error {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
account := u.Account.(*MemoryAccount)
|
|
|
|
if account.Email != "" {
|
|
|
|
for idx := range i.users {
|
|
|
|
if i.users[idx].Email == account.Email {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("User ", account.Email, " already exists.")
|
2022-12-03 16:04:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.users = append(i.users, &User{
|
|
|
|
Key: account.Key,
|
2022-12-15 14:21:52 +03:00
|
|
|
Email: account.Email,
|
2022-12-03 16:04:14 +03:00
|
|
|
Level: account.Level,
|
|
|
|
})
|
|
|
|
|
|
|
|
// sync to multi service
|
|
|
|
// Considering implements shadowsocks2022 in xray-core may have better performance.
|
|
|
|
i.service.UpdateUsersWithPasswords(
|
|
|
|
C.MapIndexed(i.users, func(index int, it *User) int { return index }),
|
|
|
|
C.Map(i.users, func(it *User) string { return it.Key }),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUser implements proxy.UserManager.RemoveUser().
|
|
|
|
func (i *MultiUserInbound) RemoveUser(ctx context.Context, email string) error {
|
|
|
|
if email == "" {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("Email must not be empty.")
|
2022-12-03 16:04:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
|
|
|
|
idx := -1
|
|
|
|
for ii, u := range i.users {
|
|
|
|
if strings.EqualFold(u.Email, email) {
|
|
|
|
idx = ii
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx == -1 {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("User ", email, " not found.")
|
2022-12-03 16:04:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ulen := len(i.users)
|
|
|
|
|
|
|
|
i.users[idx] = i.users[ulen-1]
|
|
|
|
i.users[ulen-1] = nil
|
|
|
|
i.users = i.users[:ulen-1]
|
|
|
|
|
|
|
|
// sync to multi service
|
|
|
|
// Considering implements shadowsocks2022 in xray-core may have better performance.
|
|
|
|
i.service.UpdateUsersWithPasswords(
|
|
|
|
C.MapIndexed(i.users, func(index int, it *User) int { return index }),
|
|
|
|
C.Map(i.users, func(it *User) string { return it.Key }),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:25:12 +03:00
|
|
|
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)
|
2023-04-06 13:21:35 +03:00
|
|
|
inbound.Name = "shadowsocks-2022-multi"
|
2024-05-14 04:52:24 +03:00
|
|
|
inbound.CanSpliceCopy = 3
|
2022-05-24 02:25:12 +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-05-24 02:25:12 +03:00
|
|
|
} else {
|
|
|
|
reader := buf.NewReader(connection)
|
|
|
|
pc := &natPacketConn{connection}
|
|
|
|
for {
|
|
|
|
mb, err := reader.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
2022-06-01 06:12:43 +03:00
|
|
|
buf.ReleaseMulti(mb)
|
2023-04-23 14:31:41 +03:00
|
|
|
return singbridge.ReturnError(err)
|
2022-05-24 02:25:12 +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-05-24 02:25:12 +03:00
|
|
|
if err != nil {
|
2022-06-28 02:50:04 +03:00
|
|
|
packet.Release()
|
2022-06-01 06:12:43 +03:00
|
|
|
buf.ReleaseMulti(mb)
|
2022-05-24 02:25:12 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
2022-05-25 03:49:52 +03:00
|
|
|
inbound := session.InboundFromContext(ctx)
|
2022-09-17 04:54:37 +03:00
|
|
|
userInt, _ := A.UserFromContext[int](ctx)
|
|
|
|
user := i.users[userInt]
|
2022-05-25 03:49:52 +03:00
|
|
|
inbound.User = &protocol.MemoryUser{
|
|
|
|
Email: user.Email,
|
|
|
|
Level: uint32(user.Level),
|
|
|
|
}
|
2022-08-28 05:40:55 +03:00
|
|
|
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
|
2022-05-24 02:25:12 +03:00
|
|
|
From: metadata.Source,
|
|
|
|
To: metadata.Destination,
|
|
|
|
Status: log.AccessAccepted,
|
2022-05-25 03:49:52 +03:00
|
|
|
Email: user.Email,
|
2022-05-24 02:25:12 +03:00
|
|
|
})
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogInfo(ctx, "tunnelling request to tcp:", metadata.Destination)
|
2022-05-24 02:25:12 +03:00
|
|
|
dispatcher := session.DispatcherFromContext(ctx)
|
2023-07-19 01:28:51 +03:00
|
|
|
destination := singbridge.ToDestination(metadata.Destination, net.Network_TCP)
|
|
|
|
if !destination.IsValid() {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("invalid destination")
|
2023-07-19 01:28:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
link, err := dispatcher.Dispatch(ctx, destination)
|
2022-05-24 02:25:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-23 14:31:41 +03:00
|
|
|
return singbridge.CopyConn(ctx, conn, link, conn)
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *MultiUserInbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
|
2022-05-25 03:49:52 +03:00
|
|
|
inbound := session.InboundFromContext(ctx)
|
2022-09-17 04:54:37 +03:00
|
|
|
userInt, _ := A.UserFromContext[int](ctx)
|
|
|
|
user := i.users[userInt]
|
2022-05-25 03:49:52 +03:00
|
|
|
inbound.User = &protocol.MemoryUser{
|
|
|
|
Email: user.Email,
|
|
|
|
Level: uint32(user.Level),
|
|
|
|
}
|
2022-08-28 05:40:55 +03:00
|
|
|
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
|
2022-05-24 02:25:12 +03:00
|
|
|
From: metadata.Source,
|
|
|
|
To: metadata.Destination,
|
|
|
|
Status: log.AccessAccepted,
|
2022-05-25 03:49:52 +03:00
|
|
|
Email: user.Email,
|
2022-05-24 02:25:12 +03:00
|
|
|
})
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogInfo(ctx, "tunnelling request to udp:", metadata.Destination)
|
2022-05-24 02:25:12 +03:00
|
|
|
dispatcher := session.DispatcherFromContext(ctx)
|
2023-04-23 14:31:41 +03:00
|
|
|
destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
|
2022-05-24 02:25:12 +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-05-24 02:25:12 +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 *MultiUserInbound) NewError(ctx context.Context, err error) {
|
2022-06-01 06:12:43 +03:00
|
|
|
if E.IsClosed(err) {
|
|
|
|
return
|
|
|
|
}
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogWarning(ctx, err.Error())
|
2022-05-24 02:25:12 +03:00
|
|
|
}
|