V2bX/core/xray/user.go

108 lines
2.8 KiB
Go
Raw Normal View History

package xray
import (
"context"
"fmt"
2023-07-15 07:43:37 -04:00
2023-06-18 22:31:42 -04:00
"github.com/Yuzuki616/V2bX/api/panel"
2023-07-12 13:44:15 -04:00
"github.com/Yuzuki616/V2bX/common/format"
vCore "github.com/Yuzuki616/V2bX/core"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/proxy"
)
2023-05-22 09:01:31 -04:00
func (c *Core) GetUserManager(tag string) (proxy.UserManager, error) {
handler, err := c.ihm.GetHandler(context.Background(), tag)
if err != nil {
return nil, fmt.Errorf("no such inbound tag: %s", err)
}
inboundInstance, ok := handler.(proxy.GetInbound)
if !ok {
return nil, fmt.Errorf("handler %s is not implement proxy.GetInbound", tag)
}
userManager, ok := inboundInstance.GetInbound().(proxy.UserManager)
if !ok {
return nil, fmt.Errorf("handler %s is not implement proxy.UserManager", tag)
}
return userManager, nil
}
2023-06-18 22:31:42 -04:00
func (c *Core) DelUsers(users []panel.UserInfo, tag string) error {
2023-05-22 09:01:31 -04:00
userManager, err := c.GetUserManager(tag)
if err != nil {
return fmt.Errorf("get user manager error: %s", err)
}
2023-06-18 22:48:11 -04:00
var up, down, user string
2023-06-18 22:31:42 -04:00
for i := range users {
2023-07-12 13:44:15 -04:00
user = format.UserTag(tag, users[i].Uuid)
2023-06-18 22:48:11 -04:00
err = userManager.RemoveUser(context.Background(), user)
if err != nil {
return err
}
2023-06-18 22:48:11 -04:00
up = "user>>>" + user + ">>>traffic>>>uplink"
down = "user>>>" + user + ">>>traffic>>>downlink"
c.shm.UnregisterCounter(up)
c.shm.UnregisterCounter(down)
}
return nil
}
func (c *Core) GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64) {
2023-07-12 13:44:15 -04:00
upName := "user>>>" + format.UserTag(tag, uuid) + ">>>traffic>>>uplink"
downName := "user>>>" + format.UserTag(tag, uuid) + ">>>traffic>>>downlink"
2023-05-22 09:01:31 -04:00
upCounter := c.shm.GetCounter(upName)
downCounter := c.shm.GetCounter(downName)
2022-09-07 05:26:12 -04:00
if reset {
if upCounter != nil {
up = upCounter.Set(0)
}
if downCounter != nil {
down = downCounter.Set(0)
}
} else {
if upCounter != nil {
up = upCounter.Value()
}
if downCounter != nil {
down = downCounter.Value()
}
}
return up, down
}
2023-05-22 09:01:31 -04:00
func (c *Core) AddUsers(p *vCore.AddUsersParams) (added int, err error) {
2023-05-22 09:01:31 -04:00
users := make([]*protocol.User, 0, len(p.UserInfo))
switch p.NodeInfo.Type {
2023-05-22 09:01:31 -04:00
case "v2ray":
2023-07-15 07:43:37 -04:00
if p.NodeInfo.ExtraConfig.EnableVless {
users = buildVlessUsers(p.Tag, p.UserInfo, p.NodeInfo.ExtraConfig.VlessFlow)
2023-05-22 09:01:31 -04:00
} else {
2023-07-12 13:44:15 -04:00
users = buildVmessUsers(p.Tag, p.UserInfo)
2023-05-22 09:01:31 -04:00
}
case "trojan":
2023-07-12 13:44:15 -04:00
users = buildTrojanUsers(p.Tag, p.UserInfo)
2023-05-22 09:01:31 -04:00
case "shadowsocks":
2023-07-12 13:44:15 -04:00
users = buildSSUsers(p.Tag,
2023-05-22 09:01:31 -04:00
p.UserInfo,
2023-05-30 04:08:21 -04:00
p.NodeInfo.Cipher,
2023-05-22 09:01:31 -04:00
p.NodeInfo.ServerKey)
default:
return 0, fmt.Errorf("unsupported node type: %s", p.NodeInfo.Type)
2023-05-22 09:01:31 -04:00
}
man, err := c.GetUserManager(p.Tag)
if err != nil {
return 0, fmt.Errorf("get user manager error: %s", err)
}
for _, u := range users {
mUser, err := u.ToMemoryUser()
if err != nil {
return 0, err
}
err = man.AddUser(context.Background(), mUser)
if err != nil {
return 0, err
}
}
return len(users), nil
}