2023-07-27 21:13:11 -04:00
|
|
|
package sing
|
|
|
|
|
|
|
|
import (
|
2023-08-06 21:50:48 -04:00
|
|
|
"encoding/base64"
|
2023-07-27 21:13:11 -04:00
|
|
|
"errors"
|
|
|
|
|
2023-07-29 07:27:15 -04:00
|
|
|
"github.com/InazumaV/V2bX/api/panel"
|
|
|
|
"github.com/InazumaV/V2bX/common/counter"
|
|
|
|
"github.com/InazumaV/V2bX/core"
|
2023-10-26 01:06:43 -04:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-12-12 16:22:44 -05:00
|
|
|
"github.com/sagernet/sing-box/protocol/hysteria"
|
|
|
|
"github.com/sagernet/sing-box/protocol/hysteria2"
|
|
|
|
"github.com/sagernet/sing-box/protocol/shadowsocks"
|
|
|
|
"github.com/sagernet/sing-box/protocol/trojan"
|
|
|
|
"github.com/sagernet/sing-box/protocol/vless"
|
|
|
|
"github.com/sagernet/sing-box/protocol/vmess"
|
2023-07-27 21:13:11 -04:00
|
|
|
)
|
|
|
|
|
2023-10-26 01:06:43 -04:00
|
|
|
func (b *Sing) AddUsers(p *core.AddUsersParams) (added int, err error) {
|
2024-12-12 16:22:44 -05:00
|
|
|
in, found := b.box.Inbound().Get(p.Tag)
|
|
|
|
if !found {
|
|
|
|
return 0, errors.New("the inbound not found")
|
|
|
|
}
|
2023-07-27 21:13:11 -04:00
|
|
|
switch p.NodeInfo.Type {
|
2025-01-10 02:33:05 -05:00
|
|
|
case "vless":
|
|
|
|
us := make([]option.VLESSUser, len(p.Users))
|
|
|
|
for i := range p.Users {
|
|
|
|
us[i] = option.VLESSUser{
|
|
|
|
Name: p.Users[i].Uuid,
|
|
|
|
Flow: p.VAllss.Flow,
|
|
|
|
UUID: p.Users[i].Uuid,
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
2025-01-10 02:33:05 -05:00
|
|
|
}
|
|
|
|
err = in.(*vless.Inbound).AddUsers(us)
|
|
|
|
case "vmess":
|
|
|
|
us := make([]option.VMessUser, len(p.Users))
|
|
|
|
for i := range p.Users {
|
|
|
|
us[i] = option.VMessUser{
|
|
|
|
Name: p.Users[i].Uuid,
|
|
|
|
UUID: p.Users[i].Uuid,
|
2023-07-30 07:49:42 -04:00
|
|
|
}
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
2025-01-10 02:33:05 -05:00
|
|
|
err = in.(*vmess.Inbound).AddUsers(us)
|
2023-07-27 21:13:11 -04:00
|
|
|
case "shadowsocks":
|
2023-08-19 08:06:42 -04:00
|
|
|
us := make([]option.ShadowsocksUser, len(p.Users))
|
|
|
|
for i := range p.Users {
|
|
|
|
var password = p.Users[i].Uuid
|
|
|
|
switch p.Shadowsocks.Cipher {
|
2023-08-06 21:50:48 -04:00
|
|
|
case "2022-blake3-aes-128-gcm":
|
|
|
|
password = base64.StdEncoding.EncodeToString([]byte(password[:16]))
|
|
|
|
case "2022-blake3-aes-256-gcm":
|
|
|
|
password = base64.StdEncoding.EncodeToString([]byte(password[:32]))
|
|
|
|
}
|
2023-07-27 21:13:11 -04:00
|
|
|
us[i] = option.ShadowsocksUser{
|
2023-08-19 08:06:42 -04:00
|
|
|
Name: p.Users[i].Uuid,
|
2023-08-06 21:50:48 -04:00
|
|
|
Password: password,
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
|
|
|
}
|
2024-12-12 16:22:44 -05:00
|
|
|
err = in.(*shadowsocks.MultiInbound).AddUsers(us)
|
2023-08-07 00:23:34 -04:00
|
|
|
case "trojan":
|
2023-08-19 08:06:42 -04:00
|
|
|
us := make([]option.TrojanUser, len(p.Users))
|
|
|
|
for i := range p.Users {
|
2023-08-07 00:23:34 -04:00
|
|
|
us[i] = option.TrojanUser{
|
2023-08-19 08:06:42 -04:00
|
|
|
Name: p.Users[i].Uuid,
|
|
|
|
Password: p.Users[i].Uuid,
|
2023-08-07 00:23:34 -04:00
|
|
|
}
|
|
|
|
}
|
2024-12-12 16:22:44 -05:00
|
|
|
err = in.(*trojan.Inbound).AddUsers(us)
|
2023-08-08 06:24:10 -04:00
|
|
|
case "hysteria":
|
2023-08-19 08:06:42 -04:00
|
|
|
us := make([]option.HysteriaUser, len(p.Users))
|
|
|
|
for i := range p.Users {
|
2023-08-08 06:24:10 -04:00
|
|
|
us[i] = option.HysteriaUser{
|
2024-02-20 23:02:13 -05:00
|
|
|
Name: p.Users[i].Uuid,
|
2023-08-19 08:06:42 -04:00
|
|
|
AuthString: p.Users[i].Uuid,
|
2023-08-08 06:24:10 -04:00
|
|
|
}
|
|
|
|
}
|
2024-12-12 16:22:44 -05:00
|
|
|
err = in.(*hysteria.Inbound).AddUsers(us)
|
2023-11-17 17:05:28 -05:00
|
|
|
case "hysteria2":
|
|
|
|
us := make([]option.Hysteria2User, len(p.Users))
|
2024-02-20 23:02:13 -05:00
|
|
|
id := make([]int, len(p.Users))
|
2023-11-17 17:05:28 -05:00
|
|
|
for i := range p.Users {
|
|
|
|
us[i] = option.Hysteria2User{
|
2024-02-20 23:02:13 -05:00
|
|
|
Name: p.Users[i].Uuid,
|
2023-11-17 17:05:28 -05:00
|
|
|
Password: p.Users[i].Uuid,
|
|
|
|
}
|
2024-02-20 23:02:13 -05:00
|
|
|
id[i] = p.Users[i].Id
|
2023-11-17 17:05:28 -05:00
|
|
|
}
|
2024-12-12 16:22:44 -05:00
|
|
|
err = in.(*hysteria2.Inbound).AddUsers(us, id)
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
2023-07-30 07:49:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-08-19 08:06:42 -04:00
|
|
|
return len(p.Users), err
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
|
|
|
|
2023-10-26 01:06:43 -04:00
|
|
|
func (b *Sing) GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64) {
|
2023-08-06 03:34:12 -04:00
|
|
|
if v, ok := b.hookServer.counter.Load(tag); ok {
|
2023-07-29 06:47:47 -04:00
|
|
|
c := v.(*counter.TrafficCounter)
|
2023-07-27 21:13:11 -04:00
|
|
|
up = c.GetUpCount(uuid)
|
|
|
|
down = c.GetDownCount(uuid)
|
|
|
|
if reset {
|
|
|
|
c.Reset(uuid)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserDeleter interface {
|
|
|
|
DelUsers(uuid []string) error
|
|
|
|
}
|
|
|
|
|
2025-01-10 02:33:05 -05:00
|
|
|
func (b *Sing) DelUsers(users []panel.UserInfo, tag string, info *panel.NodeInfo) error {
|
2023-07-27 21:13:11 -04:00
|
|
|
var del UserDeleter
|
2025-01-05 06:57:18 -05:00
|
|
|
if i, found := b.box.Inbound().Get(tag); found {
|
2025-01-10 02:33:05 -05:00
|
|
|
switch info.Type {
|
2023-07-27 21:13:11 -04:00
|
|
|
case "vmess":
|
2024-12-12 16:22:44 -05:00
|
|
|
del = i.(*vmess.Inbound)
|
2023-08-25 00:42:47 -04:00
|
|
|
case "vless":
|
2024-12-12 16:22:44 -05:00
|
|
|
del = i.(*vless.Inbound)
|
2023-07-27 21:13:11 -04:00
|
|
|
case "shadowsocks":
|
2024-12-12 16:22:44 -05:00
|
|
|
del = i.(*shadowsocks.MultiInbound)
|
2023-08-08 06:24:10 -04:00
|
|
|
case "trojan":
|
2024-12-12 16:22:44 -05:00
|
|
|
del = i.(*trojan.Inbound)
|
2023-08-08 06:24:10 -04:00
|
|
|
case "hysteria":
|
2024-12-12 16:22:44 -05:00
|
|
|
del = i.(*hysteria.Inbound)
|
2023-11-17 17:05:28 -05:00
|
|
|
case "hysteria2":
|
2024-12-12 16:22:44 -05:00
|
|
|
del = i.(*hysteria2.Inbound)
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("the inbound not found")
|
|
|
|
}
|
|
|
|
uuids := make([]string, len(users))
|
|
|
|
for i := range users {
|
|
|
|
uuids[i] = users[i].Uuid
|
|
|
|
}
|
|
|
|
err := del.DelUsers(uuids)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|