2023-05-15 21:15:29 -04:00
|
|
|
package node
|
2022-07-28 11:00:05 -04:00
|
|
|
|
|
|
|
import (
|
2023-01-10 22:51:11 -05:00
|
|
|
"encoding/base64"
|
2022-07-28 11:00:05 -04:00
|
|
|
"fmt"
|
2022-08-16 01:04:33 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2022-07-28 11:00:05 -04:00
|
|
|
"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"
|
2022-12-18 10:31:06 -05:00
|
|
|
"github.com/xtls/xray-core/proxy/shadowsocks_2022"
|
2022-07-28 11:00:05 -04:00
|
|
|
"github.com/xtls/xray-core/proxy/trojan"
|
|
|
|
"github.com/xtls/xray-core/proxy/vless"
|
2023-05-17 21:11:28 -04:00
|
|
|
"log"
|
2022-12-18 10:31:06 -05:00
|
|
|
"strings"
|
2022-07-28 11:00:05 -04:00
|
|
|
)
|
|
|
|
|
2023-05-21 22:36:10 -04:00
|
|
|
const xtlsFLow = "xtls-rprx-vision"
|
|
|
|
|
2023-05-17 21:11:28 -04:00
|
|
|
func (c *Controller) addNewUser(userInfo []panel.UserInfo, nodeInfo *panel.NodeInfo) (err error) {
|
|
|
|
users := make([]*protocol.User, 0, len(userInfo))
|
|
|
|
switch nodeInfo.NodeType {
|
|
|
|
case "v2ray":
|
|
|
|
if c.EnableVless {
|
|
|
|
users = c.buildVlessUsers(userInfo)
|
|
|
|
} else {
|
|
|
|
users = c.buildVmessUsers(userInfo)
|
|
|
|
}
|
2023-05-18 23:27:08 -04:00
|
|
|
case "trojan":
|
2023-05-17 21:11:28 -04:00
|
|
|
users = c.buildTrojanUsers(userInfo)
|
2023-05-18 23:27:08 -04:00
|
|
|
case "shadowsocks":
|
2023-05-17 21:11:28 -04:00
|
|
|
users = c.buildSSUsers(userInfo, getCipherFromString(nodeInfo.Cipher))
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported node type: %s", nodeInfo.NodeType)
|
|
|
|
}
|
|
|
|
err = c.server.AddUsers(users, c.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("add users error: %s", err)
|
|
|
|
}
|
|
|
|
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userInfo))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildVmessUsers(userInfo []panel.UserInfo) (users []*protocol.User) {
|
2022-08-12 06:02:06 -04:00
|
|
|
users = make([]*protocol.User, len(userInfo))
|
|
|
|
for i, user := range userInfo {
|
2022-10-09 20:59:00 -04:00
|
|
|
users[i] = c.buildVmessUser(&user, 0)
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildVmessUser(userInfo *panel.UserInfo, serverAlterID uint16) (user *protocol.User) {
|
2022-07-28 11:00:05 -04:00
|
|
|
vmessAccount := &conf.VMessAccount{
|
2022-12-18 10:31:06 -05:00
|
|
|
ID: userInfo.Uuid,
|
2022-07-28 11:00:05 -04:00
|
|
|
AlterIds: serverAlterID,
|
|
|
|
Security: "auto",
|
|
|
|
}
|
2022-10-09 20:59:00 -04:00
|
|
|
return &protocol.User{
|
2022-07-28 11:00:05 -04:00
|
|
|
Level: 0,
|
|
|
|
Email: c.buildUserTag(userInfo), // Uid: InboundTag|email|uid
|
|
|
|
Account: serial.ToTypedMessage(vmessAccount.Build()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildVlessUsers(userInfo []panel.UserInfo) (users []*protocol.User) {
|
2022-08-12 06:02:06 -04:00
|
|
|
users = make([]*protocol.User, len(userInfo))
|
|
|
|
for i := range userInfo {
|
|
|
|
users[i] = c.buildVlessUser(&(userInfo)[i])
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildVlessUser(userInfo *panel.UserInfo) (user *protocol.User) {
|
2022-07-28 11:00:05 -04:00
|
|
|
vlessAccount := &vless.Account{
|
2023-05-21 22:36:10 -04:00
|
|
|
Id: userInfo.Uuid,
|
|
|
|
}
|
|
|
|
if c.EnableXtls {
|
|
|
|
vlessAccount.Flow = xtlsFLow
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
2022-10-09 20:59:00 -04:00
|
|
|
return &protocol.User{
|
2022-07-28 11:00:05 -04:00
|
|
|
Level: 0,
|
|
|
|
Email: c.buildUserTag(userInfo),
|
|
|
|
Account: serial.ToTypedMessage(vlessAccount),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildTrojanUsers(userInfo []panel.UserInfo) (users []*protocol.User) {
|
2022-08-12 06:02:06 -04:00
|
|
|
users = make([]*protocol.User, len(userInfo))
|
|
|
|
for i := range userInfo {
|
|
|
|
users[i] = c.buildTrojanUser(&(userInfo)[i])
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildTrojanUser(userInfo *panel.UserInfo) (user *protocol.User) {
|
2022-07-28 11:00:05 -04:00
|
|
|
trojanAccount := &trojan.Account{
|
2022-12-18 10:31:06 -05:00
|
|
|
Password: userInfo.Uuid,
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
2022-10-09 20:59:00 -04:00
|
|
|
return &protocol.User{
|
2022-07-28 11:00:05 -04:00
|
|
|
Level: 0,
|
|
|
|
Email: c.buildUserTag(userInfo),
|
|
|
|
Account: serial.ToTypedMessage(trojanAccount),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildSSUsers(userInfo []panel.UserInfo, cypher shadowsocks.CipherType) (users []*protocol.User) {
|
2022-08-12 06:02:06 -04:00
|
|
|
users = make([]*protocol.User, len(userInfo))
|
|
|
|
for i := range userInfo {
|
2022-10-18 13:16:53 -04:00
|
|
|
users[i] = c.buildSSUser(&(userInfo)[i], cypher)
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildSSUser(userInfo *panel.UserInfo, cypher shadowsocks.CipherType) (user *protocol.User) {
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.nodeInfo.ServerKey == "" {
|
|
|
|
ssAccount := &shadowsocks.Account{
|
|
|
|
Password: userInfo.Uuid,
|
|
|
|
CipherType: cypher,
|
|
|
|
}
|
|
|
|
return &protocol.User{
|
|
|
|
Level: 0,
|
|
|
|
Email: c.buildUserTag(userInfo),
|
|
|
|
Account: serial.ToTypedMessage(ssAccount),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ssAccount := &shadowsocks_2022.User{
|
2023-01-10 22:51:11 -05:00
|
|
|
Key: base64.StdEncoding.EncodeToString([]byte(userInfo.Uuid[:32])),
|
2022-12-18 10:31:06 -05:00
|
|
|
}
|
|
|
|
return &protocol.User{
|
|
|
|
Level: 0,
|
|
|
|
Email: c.buildUserTag(userInfo),
|
|
|
|
Account: serial.ToTypedMessage(ssAccount),
|
|
|
|
}
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:15:29 -04:00
|
|
|
func (c *Controller) buildUserTag(user *panel.UserInfo) string {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Sprintf("%s|%s|%d", c.Tag, user.Uuid, user.Id)
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|