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-07-27 21:13:11 -04:00
|
|
|
"github.com/inazumav/sing-box/inbound"
|
|
|
|
"github.com/inazumav/sing-box/option"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Box) AddUsers(p *core.AddUsersParams) (added int, err error) {
|
|
|
|
switch p.NodeInfo.Type {
|
|
|
|
case "v2ray":
|
2023-07-30 07:49:42 -04:00
|
|
|
if p.NodeInfo.ExtraConfig.EnableVless == "true" {
|
|
|
|
us := make([]option.VLESSUser, len(p.UserInfo))
|
|
|
|
for i := range p.UserInfo {
|
|
|
|
us[i] = option.VLESSUser{
|
|
|
|
Name: p.UserInfo[i].Uuid,
|
|
|
|
Flow: p.NodeInfo.ExtraConfig.VlessFlow,
|
|
|
|
UUID: p.UserInfo[i].Uuid,
|
|
|
|
}
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
2023-07-30 07:49:42 -04:00
|
|
|
err = b.inbounds[p.Tag].(*inbound.VLESS).AddUsers(us)
|
|
|
|
} else {
|
|
|
|
us := make([]option.VMessUser, len(p.UserInfo))
|
|
|
|
for i := range p.UserInfo {
|
|
|
|
us[i] = option.VMessUser{
|
|
|
|
Name: p.UserInfo[i].Uuid,
|
|
|
|
UUID: p.UserInfo[i].Uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = b.inbounds[p.Tag].(*inbound.VMess).AddUsers(us)
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
|
|
|
case "shadowsocks":
|
|
|
|
us := make([]option.ShadowsocksUser, len(p.UserInfo))
|
|
|
|
for i := range p.UserInfo {
|
2023-08-06 21:50:48 -04:00
|
|
|
var password = p.UserInfo[i].Uuid
|
|
|
|
switch p.NodeInfo.Cipher {
|
|
|
|
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{
|
|
|
|
Name: p.UserInfo[i].Uuid,
|
2023-08-06 21:50:48 -04:00
|
|
|
Password: password,
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
|
|
|
}
|
2023-08-06 21:32:33 -04:00
|
|
|
err = b.inbounds[p.Tag].(*inbound.ShadowsocksMulti).AddUsers(us)
|
2023-08-07 00:23:34 -04:00
|
|
|
case "trojan":
|
|
|
|
us := make([]option.TrojanUser, len(p.UserInfo))
|
|
|
|
for i := range p.UserInfo {
|
|
|
|
us[i] = option.TrojanUser{
|
|
|
|
Name: p.UserInfo[i].Uuid,
|
|
|
|
Password: p.UserInfo[i].Uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = b.inbounds[p.Tag].(*inbound.Trojan).AddUsers(us)
|
2023-08-08 06:24:10 -04:00
|
|
|
case "hysteria":
|
|
|
|
us := make([]option.HysteriaUser, len(p.UserInfo))
|
|
|
|
for i := range p.UserInfo {
|
|
|
|
us[i] = option.HysteriaUser{
|
|
|
|
Name: p.UserInfo[i].Uuid,
|
|
|
|
AuthString: p.UserInfo[i].Uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = b.inbounds[p.Tag].(*inbound.Hysteria).AddUsers(us)
|
2023-07-27 21:13:11 -04:00
|
|
|
}
|
2023-07-30 07:49:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-07-27 21:13:11 -04:00
|
|
|
return len(p.UserInfo), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Box) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Box) DelUsers(users []panel.UserInfo, tag string) error {
|
|
|
|
var del UserDeleter
|
|
|
|
if i, ok := b.inbounds[tag]; ok {
|
|
|
|
switch i.Type() {
|
|
|
|
case "vmess":
|
|
|
|
del = i.(*inbound.VMess)
|
|
|
|
case "shadowsocks":
|
|
|
|
del = i.(*inbound.ShadowsocksMulti)
|
2023-08-08 06:24:10 -04:00
|
|
|
case "trojan":
|
|
|
|
del = i.(*inbound.Trojan)
|
|
|
|
case "hysteria":
|
|
|
|
del = i.(*inbound.Hysteria)
|
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
|
|
|
|
}
|