V2bX/core/sing/user.go

146 lines
3.6 KiB
Go
Raw Normal View History

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 {
2023-08-19 08:06:42 -04:00
case "vmess", "vless":
if p.NodeInfo.Type == "vless" {
us := make([]option.VLESSUser, len(p.Users))
for i := range p.Users {
2023-07-30 07:49:42 -04:00
us[i] = option.VLESSUser{
2023-08-19 08:06:42 -04:00
Name: p.Users[i].Uuid,
Flow: p.VAllss.Flow,
UUID: p.Users[i].Uuid,
2023-07-30 07:49:42 -04:00
}
2023-07-27 21:13:11 -04:00
}
2024-12-12 16:22:44 -05:00
err = in.(*vless.Inbound).AddUsers(us)
2023-07-30 07:49:42 -04:00
} else {
2023-08-19 08:06:42 -04:00
us := make([]option.VMessUser, len(p.Users))
for i := range p.Users {
2023-07-30 07:49:42 -04:00
us[i] = option.VMessUser{
2023-08-19 08:06:42 -04:00
Name: p.Users[i].Uuid,
UUID: p.Users[i].Uuid,
2023-07-30 07:49:42 -04:00
}
}
2024-12-12 16:22:44 -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)
case "hysteria":
2023-08-19 08:06:42 -04:00
us := make([]option.HysteriaUser, len(p.Users))
for i := range p.Users {
us[i] = option.HysteriaUser{
Name: p.Users[i].Uuid,
2023-08-19 08:06:42 -04:00
AuthString: p.Users[i].Uuid,
}
}
2024-12-12 16:22:44 -05:00
err = in.(*hysteria.Inbound).AddUsers(us)
case "hysteria2":
us := make([]option.Hysteria2User, len(p.Users))
id := make([]int, len(p.Users))
for i := range p.Users {
us[i] = option.Hysteria2User{
Name: p.Users[i].Uuid,
Password: p.Users[i].Uuid,
}
id[i] = p.Users[i].Id
}
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
}
2023-10-26 01:06:43 -04:00
func (b *Sing) DelUsers(users []panel.UserInfo, tag string) error {
2023-07-27 21:13:11 -04:00
var del UserDeleter
if i, ok := b.inbounds[tag]; ok {
switch i.Type() {
case "vmess":
2024-12-12 16:22:44 -05:00
del = i.(*vmess.Inbound)
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)
case "trojan":
2024-12-12 16:22:44 -05:00
del = i.(*trojan.Inbound)
case "hysteria":
2024-12-12 16:22:44 -05:00
del = i.(*hysteria.Inbound)
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 {
2023-10-13 03:32:06 -04:00
b.hookServer.ClearConn(tag, users[i].Uuid)
2023-07-27 21:13:11 -04:00
uuids[i] = users[i].Uuid
}
err := del.DelUsers(uuids)
if err != nil {
return err
}
return nil
}