V2bX/common/builder/user.go
2023-06-25 08:22:05 +08:00

132 lines
3.6 KiB
Go

package builder
import (
"encoding/base64"
"fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/infra/conf"
"github.com/xtls/xray-core/proxy/shadowsocks"
"github.com/xtls/xray-core/proxy/shadowsocks_2022"
"github.com/xtls/xray-core/proxy/trojan"
"github.com/xtls/xray-core/proxy/vless"
"strings"
)
func BuildVmessUsers(tag string, userInfo []panel.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i, user := range userInfo {
users[i] = BuildVmessUser(tag, &user)
}
return users
}
func BuildVmessUser(tag string, userInfo *panel.UserInfo) (user *protocol.User) {
vmessAccount := &conf.VMessAccount{
ID: userInfo.Uuid,
AlterIds: 0,
Security: "auto",
}
return &protocol.User{
Level: 0,
Email: BuildUserTag(tag, userInfo.Uuid), // Uid: InboundTag|email
Account: serial.ToTypedMessage(vmessAccount.Build()),
}
}
func BuildVlessUsers(tag string, userInfo []panel.UserInfo, flow string) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i := range userInfo {
users[i] = BuildVlessUser(tag, &(userInfo)[i], flow)
}
return users
}
func BuildVlessUser(tag string, userInfo *panel.UserInfo, flow string) (user *protocol.User) {
vlessAccount := &vless.Account{
Id: userInfo.Uuid,
}
vlessAccount.Flow = flow
return &protocol.User{
Level: 0,
Email: BuildUserTag(tag, userInfo.Uuid),
Account: serial.ToTypedMessage(vlessAccount),
}
}
func BuildTrojanUsers(tag string, userInfo []panel.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i := range userInfo {
users[i] = BuildTrojanUser(tag, &(userInfo)[i])
}
return users
}
func BuildTrojanUser(tag string, userInfo *panel.UserInfo) (user *protocol.User) {
trojanAccount := &trojan.Account{
Password: userInfo.Uuid,
}
return &protocol.User{
Level: 0,
Email: BuildUserTag(tag, userInfo.Uuid),
Account: serial.ToTypedMessage(trojanAccount),
}
}
func BuildSSUsers(tag string, userInfo []panel.UserInfo, cypher string, serverKey string) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i := range userInfo {
users[i] = BuildSSUser(tag, &userInfo[i], cypher, serverKey)
}
return users
}
func BuildSSUser(tag string, userInfo *panel.UserInfo, cypher string, serverKey string) (user *protocol.User) {
if serverKey == "" {
ssAccount := &shadowsocks.Account{
Password: userInfo.Uuid,
CipherType: getCipherFromString(cypher),
}
return &protocol.User{
Level: 0,
Email: BuildUserTag(tag, userInfo.Uuid),
Account: serial.ToTypedMessage(ssAccount),
}
} else {
var keyLength int
switch cypher {
case "2022-blake3-aes-128-gcm":
keyLength = 16
case "2022-blake3-aes-256-gcm":
keyLength = 32
}
ssAccount := &shadowsocks_2022.User{
Key: base64.StdEncoding.EncodeToString([]byte(userInfo.Uuid[:keyLength])),
}
return &protocol.User{
Level: 0,
Email: tag,
Account: serial.ToTypedMessage(ssAccount),
}
}
}
func BuildUserTag(tag string, uuid string) string {
return fmt.Sprintf("%s|%s", tag, uuid)
}
func getCipherFromString(c string) shadowsocks.CipherType {
switch strings.ToLower(c) {
case "aes-128-gcm", "aead_aes_128_gcm":
return shadowsocks.CipherType_AES_128_GCM
case "aes-256-gcm", "aead_aes_256_gcm":
return shadowsocks.CipherType_AES_256_GCM
case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305":
return shadowsocks.CipherType_CHACHA20_POLY1305
case "none", "plain":
return shadowsocks.CipherType_NONE
default:
return shadowsocks.CipherType_UNKNOWN
}
}