update: Implement the proxy.UserManager of ss2022

This commit is contained in:
Senis John 2022-12-03 21:04:14 +08:00 committed by yuhan6665
parent 79eda46c62
commit 143229b148
5 changed files with 103 additions and 1 deletions

View file

@ -0,0 +1,29 @@
package shadowsocks_2022
import (
"github.com/xtls/xray-core/common/protocol"
)
// MemoryAccount is an account type converted from Account.
type MemoryAccount struct {
Key string
Email string
Level int32
}
// AsAccount implements protocol.AsAccount.
func (u *User) AsAccount() (protocol.Account, error) {
return &MemoryAccount{
Key: u.GetKey(),
Email: u.GetEmail(),
Level: u.GetLevel(),
}, nil
}
// Equals implements protocol.Account.Equals().
func (a *MemoryAccount) Equals(another protocol.Account) bool {
if account, ok := another.(*MemoryAccount); ok {
return a.Key == account.Key
}
return false
}

View file

@ -11,6 +11,7 @@ import (
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/log"

View file

@ -4,6 +4,8 @@ import (
"context"
"encoding/base64"
"strconv"
"strings"
"sync"
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
C "github.com/sagernet/sing/common"
@ -13,6 +15,7 @@ import (
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/log"
@ -31,6 +34,7 @@ func init() {
}
type MultiUserInbound struct {
sync.Mutex
networks []net.Network
users []*User
service *shadowaead_2022.MultiService[int]
@ -78,6 +82,73 @@ func NewMultiServer(ctx context.Context, config *MultiUserServerConfig) (*MultiU
return inbound, nil
}
// 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 {
return newError("User ", account.Email, " already exists.")
}
}
}
i.users = append(i.users, &User{
Key: account.Key,
Email: strings.ToLower(account.Email),
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 == "" {
return newError("Email must not be empty.")
}
i.Lock()
defer i.Unlock()
email = strings.ToLower(email)
idx := -1
for ii, u := range i.users {
if strings.EqualFold(u.Email, email) {
idx = ii
break
}
}
if idx == -1 {
return newError("User ", email, " not found.")
}
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
}
func (i *MultiUserInbound) Network() []net.Network {
return i.networks
}