change project structure, performance optimization

This commit is contained in:
yuzuki999 2022-08-12 18:02:06 +08:00
parent 4bd389aa05
commit ba1e088afb
51 changed files with 181 additions and 190 deletions

5
.gitignore vendored
View File

@ -6,11 +6,10 @@ example/mytest
example/access.logo
example/error.log
api/chooseparser.go.bak
common/Inboundbuilder/.lego/
common/legocmd/.lego/
app/Inboundbuilder/.lego/
app/legocmd/.lego/
.vscode/launch.json
example/.lego
example/cert
example/config.yml
./vscode
.idea/*

View File

@ -2,9 +2,9 @@ package api
type API interface {
GetNodeInfo() (nodeInfo *NodeInfo, err error)
GetUserList() (userList *[]UserInfo, err error)
ReportUserTraffic(userTraffic *[]UserTraffic) (err error)
GetUserList() (userList []UserInfo, err error)
ReportUserTraffic(userTraffic []UserTraffic) (err error)
Describe() ClientInfo
GetNodeRule() (ruleList *[]DetectRule, protocolList *[]string, err error)
GetNodeRule() (ruleList []DetectRule, protocolList []string, err error)
Debug()
}

View File

@ -160,10 +160,10 @@ func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
return nodeInfo, nil
}
func (c *Client) GetNodeRule() (*[]DetectRule, *[]string, error) {
func (c *Client) GetNodeRule() ([]DetectRule, []string, error) {
ruleList := c.LocalRuleList
if c.NodeType != "V2ray" || c.RemoteRuleCache == nil {
return &ruleList, nil, nil
return ruleList, nil, nil
}
// V2board only support the rule for v2ray
// fix: reuse config response
@ -185,7 +185,7 @@ func (c *Client) GetNodeRule() (*[]DetectRule, *[]string, error) {
}
}
c.RemoteRuleCache = nil
return &ruleList, &protocolList, nil
return ruleList, protocolList, nil
}
// ParseTrojanNodeResponse parse the response for the given nodeinfor format
@ -211,9 +211,9 @@ func (c *Client) ParseSSNodeResponse() (*NodeInfo, error) {
if err != nil {
return nil, err
}
if len(*userInfo) > 0 {
port = (*userInfo)[0].Port
method = (*userInfo)[0].Cipher
if len(userInfo) > 0 {
port = userInfo[0].Port
method = userInfo[0].Cipher
} else {
return nil, fmt.Errorf("shadowsocks node need a active user")
}

View File

@ -45,7 +45,7 @@ type UserListBody struct {
}
// GetUserList will pull user form sspanel
func (c *Client) GetUserList() (UserList *[]UserInfo, err error) {
func (c *Client) GetUserList() (UserList []UserInfo, err error) {
var path string
switch c.NodeType {
case "V2ray":
@ -69,7 +69,7 @@ func (c *Client) GetUserList() (UserList *[]UserInfo, err error) {
if err != nil {
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
return &userList.Data, nil
return userList.Data, nil
}
type UserTraffic struct {
@ -79,7 +79,7 @@ type UserTraffic struct {
}
// ReportUserTraffic reports the user traffic
func (c *Client) ReportUserTraffic(userTraffic *[]UserTraffic) error {
func (c *Client) ReportUserTraffic(userTraffic []UserTraffic) error {
var path string
switch c.NodeType {
case "V2ray":
@ -90,8 +90,8 @@ func (c *Client) ReportUserTraffic(userTraffic *[]UserTraffic) error {
path = "/api/v1/server/ShadowsocksTidalab/submit"
}
data := make([]UserTraffic, len(*userTraffic))
for i, traffic := range *userTraffic {
data := make([]UserTraffic, len(userTraffic))
for i, traffic := range userTraffic {
data[i] = UserTraffic{
UID: traffic.UID,
Upload: traffic.Upload,

View File

@ -1,2 +1,2 @@
// Package app contains the third-party app used to replace the default app in xray-core
// Package app contains feature implementations of XrayR.
package app

View File

@ -13,7 +13,7 @@ import (
"path/filepath"
"strings"
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration"

View File

@ -11,7 +11,7 @@ import (
"strings"
"time"
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
"github.com/urfave/cli"

View File

@ -1,7 +1,7 @@
package cmd
import (
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/urfave/cli"
)

View File

@ -5,7 +5,7 @@ import (
"crypto/x509"
"time"
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/lego"

View File

@ -1,7 +1,7 @@
package cmd
import (
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/urfave/cli"
)

View File

@ -6,7 +6,7 @@ import (
"os"
"strings"
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration"

View File

@ -9,7 +9,7 @@ import (
"strings"
"time"
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration"

View File

@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/Yuzuki616/V2bX/common/legoCmd/log"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/challenge/http01"

View File

@ -11,7 +11,7 @@ import (
"runtime"
"strings"
"github.com/Yuzuki616/V2bX/common/legoCmd/cmd"
"github.com/Yuzuki616/V2bX/app/legoCmd/cmd"
"github.com/urfave/cli"
)

View File

@ -3,7 +3,7 @@ package legoCmd_test
import (
"testing"
"github.com/Yuzuki616/V2bX/common/legoCmd"
"github.com/Yuzuki616/V2bX/app/legoCmd"
)
func TestLegoClient(t *testing.T) {

View File

@ -34,7 +34,7 @@ func New() *Limiter {
}
}
func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList *[]api.UserInfo) error {
func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList []api.UserInfo) error {
inboundInfo := &InboundInfo{
Tag: tag,
NodeSpeedLimit: nodeInfo.SpeedLimit,
@ -42,37 +42,38 @@ func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList
UserOnlineIP: new(sync.Map),
}
userMap := new(sync.Map)
for i := range *userList {
for i := range userList {
/*if (*userList)[i].SpeedLimit == 0 {
(*userList)[i].SpeedLimit = nodeInfo.SpeedLimit
}
if (*userList)[i].DeviceLimit == 0 {
(*userList)[i].DeviceLimit = nodeInfo.DeviceLimit
}*/
userMap.Store(fmt.Sprintf("%s|%s|%d", tag, (*userList)[i].V2rayUser.Email, (*userList)[i].UID), UserInfo{
UID: (*userList)[i].UID,
SpeedLimit: nodeInfo.SpeedLimit,
DeviceLimit: nodeInfo.DeviceLimit,
})
userMap.Store(fmt.Sprintf("%s|%s|%d", tag, (userList)[i].V2rayUser.Email, (userList)[i].UID),
UserInfo{
UID: (userList)[i].UID,
SpeedLimit: nodeInfo.SpeedLimit,
DeviceLimit: nodeInfo.DeviceLimit,
})
}
inboundInfo.UserInfo = userMap
l.InboundInfo.Store(tag, inboundInfo) // Replace the old inbound info
return nil
}
func (l *Limiter) UpdateInboundLimiter(tag string, nodeInfo *api.NodeInfo, updatedUserList *[]api.UserInfo) error {
func (l *Limiter) UpdateInboundLimiter(tag string, nodeInfo *api.NodeInfo, updatedUserList []api.UserInfo) error {
if value, ok := l.InboundInfo.Load(tag); ok {
inboundInfo := value.(*InboundInfo)
// Update User info
for i := range *updatedUserList {
for i := range updatedUserList {
inboundInfo.UserInfo.Store(fmt.Sprintf("%s|%s|%d", tag,
(*updatedUserList)[i].V2rayUser.Email, (*updatedUserList)[i].UID), UserInfo{
UID: (*updatedUserList)[i].UID,
(updatedUserList)[i].V2rayUser.Email, (updatedUserList)[i].UID), UserInfo{
UID: (updatedUserList)[i].UID,
SpeedLimit: nodeInfo.SpeedLimit,
DeviceLimit: nodeInfo.DeviceLimit,
})
inboundInfo.BucketHub.Delete(fmt.Sprintf("%s|%s|%d", tag,
(*updatedUserList)[i].V2rayUser.Email, (*updatedUserList)[i].UID)) // Delete old limiter bucket
(updatedUserList)[i].V2rayUser.Email, (updatedUserList)[i].UID)) // Delete old limiter bucket
}
} else {
return fmt.Errorf("no such inbound in limiter: %s", tag)
@ -90,7 +91,7 @@ type UserIp struct {
IPs []string `json:"Ips"`
}
func (l *Limiter) GetOnlineUserIp(tag string) (*[]UserIp, error) {
func (l *Limiter) GetOnlineUserIp(tag string) ([]UserIp, error) {
if value, ok := l.InboundInfo.Load(tag); ok {
inboundInfo := value.(*InboundInfo)
// Clear Speed Limiter bucket for users who are not online
@ -124,13 +125,13 @@ func (l *Limiter) GetOnlineUserIp(tag string) (*[]UserIp, error) {
if len(onlineUser) == 0 {
return nil, nil
}
return &onlineUser, nil
return onlineUser, nil
} else {
return nil, fmt.Errorf("no such inbound in limiter: %s", tag)
}
}
func (l *Limiter) UpdateOnlineUserIP(tag string, userIpList *[]UserIp) {
func (l *Limiter) UpdateOnlineUserIP(tag string, userIpList []UserIp) {
if v, ok := l.InboundInfo.Load(tag); ok {
inboundInfo := v.(*InboundInfo)
//Clear old IP
@ -139,12 +140,12 @@ func (l *Limiter) UpdateOnlineUserIP(tag string, userIpList *[]UserIp) {
return true
})
// Update User Online IP
for i := range *userIpList {
for i := range userIpList {
ipMap := new(sync.Map)
for _, userIp := range (*userIpList)[i].IPs {
for _, userIp := range (userIpList)[i].IPs {
ipMap.Store(userIp, false)
}
inboundInfo.UserOnlineIP.Store((*userIpList)[i].Uid, ipMap)
inboundInfo.UserOnlineIP.Store((userIpList)[i].Uid, ipMap)
}
}
}

View File

@ -46,7 +46,7 @@ func (r *Rule) UpdateProtocolRule(tag string, ruleList []string) error {
return nil
}
func (r *Rule) GetDetectResult(tag string) (*[]api.DetectResult, error) {
func (r *Rule) GetDetectResult(tag string) ([]api.DetectResult, error) {
detectResult := make([]api.DetectResult, 0)
if value, ok := r.InboundDetectResult.LoadAndDelete(tag); ok {
resultSet := value.(mapset.Set)
@ -55,7 +55,7 @@ func (r *Rule) GetDetectResult(tag string) (*[]api.DetectResult, error) {
detectResult = append(detectResult, result.(api.DetectResult))
}
}
return &detectResult, nil
return detectResult, nil
}
func (r *Rule) Detect(tag string, destination string, email string) (reject bool) {

View File

@ -1,2 +0,0 @@
// Package common contains common utilities that are shared among other packages.
package common

2
core/app/app.go Normal file
View File

@ -0,0 +1,2 @@
// Package app contains the third-party app used to replace the default app in xray-core
package app

View File

@ -5,8 +5,8 @@ package dispatcher
import (
"context"
"fmt"
"github.com/Yuzuki616/V2bX/common/limiter"
"github.com/Yuzuki616/V2bX/common/rule"
"github.com/Yuzuki616/V2bX/app/limiter"
"github.com/Yuzuki616/V2bX/app/rule"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/log"

View File

@ -1,11 +1,13 @@
package xray
package core
import (
"encoding/json"
"github.com/Yuzuki616/V2bX/app/dispatcher"
"github.com/Yuzuki616/V2bX/conf"
_ "github.com/Yuzuki616/V2bX/xray/distro/all"
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
_ "github.com/Yuzuki616/V2bX/core/distro/all"
"github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/app/proxyman/inbound"
"github.com/xtls/xray-core/app/proxyman/outbound"
"github.com/xtls/xray-core/app/stats"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/core"
@ -15,14 +17,17 @@ import (
"sync"
)
// Xray Structure
type Xray struct {
access sync.Mutex
Server *core.Instance
// Core Structure
type Core struct {
access sync.Mutex
Server *core.Instance
ihm *inbound.Manager
ohm *outbound.Manager
dispatcher *dispatcher.DefaultDispatcher
}
func New(c *conf.Conf) *Xray {
return &Xray{Server: getCore(c)}
func New(c *conf.Conf) *Core {
return &Core{Server: getCore(c)}
}
func parseConnectionConfig(c *conf.ConnetionConfig) (policy *coreConf.Policy) {
@ -117,7 +122,7 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
corePolicyConfig := &coreConf.PolicyConfig{}
corePolicyConfig.Levels = map[uint32]*coreConf.Policy{0: levelPolicyConfig}
policyConfig, _ := corePolicyConfig.Build()
// Build Xray conf
// Build Core conf
config := &core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(coreLogConfig.Build()),
@ -136,13 +141,13 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
if err != nil {
log.Panicf("failed to create instance: %s", err)
}
log.Printf("Xray Version: %s", core.Version())
log.Printf("Core Version: %s", core.Version())
return server
}
// Start the Xray
func (p *Xray) Start() {
// Start the Core
func (p *Core) Start() {
p.access.Lock()
defer p.access.Unlock()
log.Print("Start the panel..")
@ -153,7 +158,7 @@ func (p *Xray) Start() {
}
// Close the core
func (p *Xray) Close() {
func (p *Core) Close() {
p.access.Lock()
defer p.access.Unlock()
p.Server.Close()

View File

@ -5,7 +5,7 @@ import (
// Required features. Can't remove unless there is replacements.
// _ "github.com/xtls/xray-core/app/dispatcher"
_ "github.com/Yuzuki616/V2bX/app/dispatcher"
_ "github.com/Yuzuki616/V2bX/core/app/dispatcher"
_ "github.com/xtls/xray-core/app/proxyman/inbound"
_ "github.com/xtls/xray-core/app/proxyman/outbound"
@ -54,8 +54,8 @@ import (
// JSON & TOML & YAML
_ "github.com/xtls/xray-core/main/json"
_ "github.com/xtls/xray-core/main/toml"
_ "github.com/xtls/xray-core/main/yaml"
//_ "github.com/xtls/xray-core/main/toml"
//_ "github.com/xtls/xray-core/main/yaml"
// Load config from file or http(s)
_ "github.com/xtls/xray-core/main/confloader/external"

View File

@ -1,24 +1,22 @@
package xray
package core
import (
"context"
"fmt"
"github.com/Yuzuki616/V2bX/api"
"github.com/Yuzuki616/V2bX/app/dispatcher"
"github.com/Yuzuki616/V2bX/common/limiter"
"github.com/Yuzuki616/V2bX/app/limiter"
"github.com/Yuzuki616/V2bX/core/app/dispatcher"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/routing"
)
func (p *Xray) RemoveInbound(tag string) error {
inboundManager := p.Server.GetFeature(inbound.ManagerType()).(inbound.Manager)
err := inboundManager.RemoveHandler(context.Background(), tag)
func (p *Core) RemoveInbound(tag string) error {
err := p.ihm.RemoveHandler(context.Background(), tag)
return err
}
func (p *Xray) AddInbound(config *core.InboundHandlerConfig) error {
inboundManager := p.Server.GetFeature(inbound.ManagerType()).(inbound.Manager)
func (p *Core) AddInbound(config *core.InboundHandlerConfig) error {
rawHandler, err := core.CreateObject(p.Server, config)
if err != nil {
return err
@ -27,19 +25,19 @@ func (p *Xray) AddInbound(config *core.InboundHandlerConfig) error {
if !ok {
return fmt.Errorf("not an InboundHandler: %s", err)
}
if err := inboundManager.AddHandler(context.Background(), handler); err != nil {
if err := p.ihm.AddHandler(context.Background(), handler); err != nil {
return err
}
return nil
}
func (p *Xray) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList *[]api.UserInfo) error {
func (p *Core) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList []api.UserInfo) error {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
err := dispather.Limiter.AddInboundLimiter(tag, nodeInfo, userList)
return err
}
func (p *Xray) GetInboundLimiter(tag string) (*limiter.InboundInfo, error) {
func (p *Core) GetInboundLimiter(tag string) (*limiter.InboundInfo, error) {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
limit, ok := dispather.Limiter.InboundInfo.Load(tag)
if ok {
@ -48,13 +46,13 @@ func (p *Xray) GetInboundLimiter(tag string) (*limiter.InboundInfo, error) {
return nil, fmt.Errorf("not found limiter")
}
func (p *Xray) UpdateInboundLimiter(tag string, nodeInfo *api.NodeInfo, updatedUserList *[]api.UserInfo) error {
func (p *Core) UpdateInboundLimiter(tag string, nodeInfo *api.NodeInfo, updatedUserList []api.UserInfo) error {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
err := dispather.Limiter.UpdateInboundLimiter(tag, nodeInfo, updatedUserList)
return err
}
func (p *Xray) DeleteInboundLimiter(tag string) error {
func (p *Core) DeleteInboundLimiter(tag string) error {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
err := dispather.Limiter.DeleteInboundLimiter(tag)
return err

View File

@ -1,4 +1,4 @@
package xray
package core
import (
"context"
@ -7,13 +7,13 @@ import (
"github.com/xtls/xray-core/features/outbound"
)
func (p *Xray) RemoveOutbound(tag string) error {
func (p *Core) RemoveOutbound(tag string) error {
outboundManager := p.Server.GetFeature(outbound.ManagerType()).(outbound.Manager)
err := outboundManager.RemoveHandler(context.Background(), tag)
return err
}
func (p *Xray) AddOutbound(config *core.OutboundHandlerConfig) error {
func (p *Core) AddOutbound(config *core.OutboundHandlerConfig) error {
outboundManager := p.Server.GetFeature(outbound.ManagerType()).(outbound.Manager)
rawHandler, err := core.CreateObject(p.Server, config)
if err != nil {

19
core/rule.go Normal file
View File

@ -0,0 +1,19 @@
package core
import (
"github.com/Yuzuki616/V2bX/api"
)
func (p *Core) UpdateRule(tag string, newRuleList []api.DetectRule) error {
err := p.dispatcher.RuleManager.UpdateRule(tag, newRuleList)
return err
}
func (p *Core) UpdateProtocolRule(tag string, newRuleList []string) error {
err := p.dispatcher.RuleManager.UpdateProtocolRule(tag, newRuleList)
return err
}
func (p *Core) GetDetectResult(tag string) ([]api.DetectResult, error) {
return p.dispatcher.RuleManager.GetDetectResult(tag)
}

View File

@ -1,20 +1,16 @@
package xray
package core
import (
"context"
"fmt"
"github.com/Yuzuki616/V2bX/app/dispatcher"
"github.com/Yuzuki616/V2bX/common/limiter"
"github.com/Yuzuki616/V2bX/app/limiter"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/proxy"
)
func (p *Xray) GetUserManager(tag string) (proxy.UserManager, error) {
inboundManager := p.Server.GetFeature(inbound.ManagerType()).(inbound.Manager)
handler, err := inboundManager.GetHandler(context.Background(), tag)
func (p *Core) GetUserManager(tag string) (proxy.UserManager, error) {
handler, err := p.ihm.GetHandler(context.Background(), tag)
if err != nil {
return nil, fmt.Errorf("no such inbound tag: %s", err)
}
@ -29,7 +25,7 @@ func (p *Xray) GetUserManager(tag string) (proxy.UserManager, error) {
return userManager, nil
}
func (p *Xray) AddUsers(users []*protocol.User, tag string) error {
func (p *Core) AddUsers(users []*protocol.User, tag string) error {
userManager, err := p.GetUserManager(tag)
if err != nil {
return fmt.Errorf("get user manager error: %s", err)
@ -47,7 +43,7 @@ func (p *Xray) AddUsers(users []*protocol.User, tag string) error {
return nil
}
func (p *Xray) RemoveUsers(users []string, tag string) error {
func (p *Core) RemoveUsers(users []string, tag string) error {
userManager, err := p.GetUserManager(tag)
if err != nil {
return fmt.Errorf("get user manager error: %s", err)
@ -61,7 +57,7 @@ func (p *Xray) RemoveUsers(users []string, tag string) error {
return nil
}
func (p *Xray) GetUserTraffic(email string) (up int64, down int64) {
func (p *Core) GetUserTraffic(email string) (up int64, down int64) {
upName := "user>>>" + email + ">>>traffic>>>uplink"
downName := "user>>>" + email + ">>>traffic>>>downlink"
statsManager := p.Server.GetFeature(stats.ManagerType()).(stats.Manager)
@ -78,17 +74,14 @@ func (p *Xray) GetUserTraffic(email string) (up int64, down int64) {
return up, down
}
func (p *Xray) GetOnlineIps(tag string) (*[]limiter.UserIp, error) {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
return dispather.Limiter.GetOnlineUserIp(tag)
func (p *Core) GetOnlineIps(tag string) ([]limiter.UserIp, error) {
return p.dispatcher.Limiter.GetOnlineUserIp(tag)
}
func (p *Xray) UpdateOnlineIps(tag string, ips *[]limiter.UserIp) {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
dispather.Limiter.UpdateOnlineUserIP(tag, ips)
func (p *Core) UpdateOnlineIps(tag string, ips []limiter.UserIp) {
p.dispatcher.Limiter.UpdateOnlineUserIP(tag, ips)
}
func (p *Xray) ClearOnlineIps(tag string) {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
dispather.Limiter.ClearOnlineUserIP(tag)
func (p *Core) ClearOnlineIps(tag string) {
p.dispatcher.Limiter.ClearOnlineUserIP(tag)
}

View File

@ -5,8 +5,8 @@ import (
"fmt"
"github.com/Yuzuki616/V2bX/api"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/node"
"github.com/Yuzuki616/V2bX/xray"
"github.com/spf13/viper"
"log"
"os"
@ -25,7 +25,7 @@ var (
var (
version = "v0.0.4"
codename = "V2bX"
intro = "A V2board backend based on Xray"
intro = "A V2board backend based on Xray-core"
)
func showVersion() {
@ -58,7 +58,7 @@ func getConfig() *viper.Viper {
return config
}
func startNodes(nodes []*conf.NodeConfig, core *xray.Xray) error {
func startNodes(nodes []*conf.NodeConfig, core *core.Core) error {
for i, _ := range nodes {
var apiClient = api.New(nodes[i].ApiConfig)
// Register controller service
@ -82,7 +82,7 @@ func main() {
if err != nil {
log.Panicf("can't unmarshal config file: %s \n", err)
}
x := xray.New(c)
x := core.New(c)
x.Start()
defer x.Close()
err = startNodes(c.NodesConfig, x)

View File

@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/Yuzuki616/V2bX/api"
"github.com/Yuzuki616/V2bX/common/legoCmd"
"github.com/Yuzuki616/V2bX/app/legoCmd"
"github.com/Yuzuki616/V2bX/conf"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/uuid"

View File

@ -2,9 +2,9 @@ package node
import (
"fmt"
"github.com/Yuzuki616/V2bX/common/limiter"
"github.com/Yuzuki616/V2bX/app/limiter"
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/xray"
"github.com/Yuzuki616/V2bX/core"
"github.com/go-resty/resty/v2"
"github.com/goccy/go-json"
"log"
@ -14,26 +14,26 @@ import (
"time"
"github.com/Yuzuki616/V2bX/api"
"github.com/Yuzuki616/V2bX/common/legoCmd"
"github.com/Yuzuki616/V2bX/app/legoCmd"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/task"
)
type Node struct {
server *xray.Xray
server *core.Core
config *conf.ControllerConfig
clientInfo api.ClientInfo
apiClient api.API
nodeInfo *api.NodeInfo
Tag string
userList *[]api.UserInfo
userList []api.UserInfo
nodeInfoMonitorPeriodic *task.Periodic
userReportPeriodic *task.Periodic
onlineIpReportPeriodic *task.Periodic
}
// New return a Node service with default parameters.
func New(server *xray.Xray, api api.API, config *conf.ControllerConfig) *Node {
func New(server *core.Core, api api.API, config *conf.ControllerConfig) *Node {
controller := &Node{
server: server,
config: config,
@ -77,12 +77,12 @@ func (c *Node) Start() error {
if !c.config.DisableGetRule {
if ruleList, protocolRule, err := c.apiClient.GetNodeRule(); err != nil {
log.Printf("Get rule list filed: %s", err)
} else if len(*ruleList) > 0 {
if err := c.server.UpdateRule(c.Tag, *ruleList); err != nil {
} else if len(ruleList) > 0 {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
log.Print(err)
}
if len(*protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, *protocolRule); err != nil {
if len(protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, protocolRule); err != nil {
log.Print(err)
}
}
@ -189,12 +189,12 @@ func (c *Node) nodeInfoMonitor() (err error) {
if !c.config.DisableGetRule {
if ruleList, protocolRule, err := c.apiClient.GetNodeRule(); err != nil {
log.Printf("Get rule list filed: %s", err)
} else if len(*ruleList) > 0 {
if err := c.server.UpdateRule(c.Tag, *ruleList); err != nil {
} else if len(ruleList) > 0 {
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
log.Print(err)
}
if len(*protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, *protocolRule); err != nil {
if len(protocolRule) > 0 {
if err := c.server.UpdateProtocolRule(c.Tag, protocolRule); err != nil {
log.Print(err)
}
}
@ -209,7 +209,7 @@ func (c *Node) nodeInfoMonitor() (err error) {
if err != nil {
log.Print(err)
}
// Xray-core supports the OcspStapling certification hot renew
// Core-core supports the OcspStapling certification hot renew
_, _, err = lego.RenewCert(c.config.CertConfig.CertDomain, c.config.CertConfig.Email,
c.config.CertConfig.CertMode, c.config.CertConfig.Provider, c.config.CertConfig.DNSEnv)
if err != nil {
@ -239,19 +239,19 @@ func (c *Node) nodeInfoMonitor() (err error) {
runtime.GC()
} else {
deleted, added := compareUserList(c.userList, newUserInfo)
if len(*deleted) > 0 {
deletedEmail := make([]string, len(*deleted))
for i := range *deleted {
if len(deleted) > 0 {
deletedEmail := make([]string, len(deleted))
for i := range deleted {
deletedEmail[i] = fmt.Sprintf("%s|%s|%d", c.Tag,
(*deleted)[i].GetUserEmail(),
(*deleted)[i].UID)
(deleted)[i].GetUserEmail(),
(deleted)[i].UID)
}
err := c.server.RemoveUsers(deletedEmail, c.Tag)
if err != nil {
log.Print(err)
}
}
if len(*added) > 0 {
if len(added) > 0 {
err = c.addNewUser(added, newNodeInfo)
if err != nil {
log.Print(err)
@ -262,7 +262,7 @@ func (c *Node) nodeInfoMonitor() (err error) {
}
}
log.Printf("[%s: %d] %d user deleted, %d user added", c.nodeInfo.NodeType, c.nodeInfo.NodeId,
len(*deleted), len(*added))
len(deleted), len(added))
c.userList = newUserInfo
newUserInfo = nil
runtime.GC()
@ -305,14 +305,14 @@ func (c *Node) addNewTag(newNodeInfo *api.NodeInfo) (err error) {
return nil
}
func (c *Node) addNewUser(userInfo *[]api.UserInfo, nodeInfo *api.NodeInfo) (err error) {
func (c *Node) addNewUser(userInfo []api.UserInfo, nodeInfo *api.NodeInfo) (err error) {
users := make([]*protocol.User, 0)
if nodeInfo.NodeType == "V2ray" {
if nodeInfo.EnableVless {
users = c.buildVlessUsers(userInfo)
} else {
alterID := 0
alterID = (*userInfo)[0].V2rayUser.AlterId
alterID = (userInfo)[0].V2rayUser.AlterId
if alterID >= 0 && alterID < math.MaxUint16 {
users = c.buildVmessUsers(userInfo, uint16(alterID))
} else {
@ -331,32 +331,32 @@ func (c *Node) addNewUser(userInfo *[]api.UserInfo, nodeInfo *api.NodeInfo) (err
if err != nil {
return err
}
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(*userInfo))
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userInfo))
return nil
}
func compareUserList(old, new *[]api.UserInfo) (deleted, added *[]api.UserInfo) {
func compareUserList(old, new []api.UserInfo) (deleted, added []api.UserInfo) {
tmp := map[string]struct{}{}
tmp2 := map[string]struct{}{}
for i := range *old {
tmp[(*old)[i].GetUserEmail()] = struct{}{}
for i := range old {
tmp[(old)[i].GetUserEmail()] = struct{}{}
}
l := len(tmp)
for i := range *new {
e := (*new)[i].GetUserEmail()
for i := range new {
e := (new)[i].GetUserEmail()
tmp[e] = struct{}{}
tmp2[e] = struct{}{}
if l != len(tmp) {
*added = append(*added, (*new)[i])
added = append(added, (new)[i])
l++
}
}
tmp = nil
l = len(tmp2)
for i := range *old {
tmp2[(*old)[i].GetUserEmail()] = struct{}{}
for i := range old {
tmp2[(old)[i].GetUserEmail()] = struct{}{}
if l != len(tmp2) {
*deleted = append(*deleted, (*old)[i])
deleted = append(deleted, (old)[i])
l++
}
}
@ -366,17 +366,17 @@ func compareUserList(old, new *[]api.UserInfo) (deleted, added *[]api.UserInfo)
func (c *Node) userInfoMonitor() (err error) {
// Get User traffic
userTraffic := make([]api.UserTraffic, 0)
for i := range *c.userList {
up, down := c.server.GetUserTraffic(c.buildUserTag(&(*c.userList)[i]))
for i := range c.userList {
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]))
if up > 0 || down > 0 {
userTraffic = append(userTraffic, api.UserTraffic{
UID: (*c.userList)[i].UID,
UID: (c.userList)[i].UID,
Upload: up,
Download: down})
}
}
if len(userTraffic) > 0 && !c.config.DisableUploadTraffic {
err = c.apiClient.ReportUserTraffic(&userTraffic)
err = c.apiClient.ReportUserTraffic(userTraffic)
if err != nil {
log.Print(err)
} else {
@ -405,9 +405,9 @@ func (c *Node) onlineIpReport() (err error) {
c.server.ClearOnlineIps(c.Tag)
return nil
}
log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(*onlineIp))
log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp))
if rsp.StatusCode() == 200 {
onlineIp = &[]limiter.UserIp{}
onlineIp = []limiter.UserIp{}
err := json.Unmarshal(rsp.Body(), onlineIp)
if err != nil {
log.Print(err)
@ -415,7 +415,7 @@ func (c *Node) onlineIpReport() (err error) {
return nil
}
c.server.UpdateOnlineIps(c.Tag, onlineIp)
log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(*onlineIp))
log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp))
} else {
c.server.ClearOnlineIps(c.Tag)
}

View File

@ -13,9 +13,9 @@ import (
"github.com/xtls/xray-core/proxy/vless"
)
func (c *Node) buildVmessUsers(userInfo *[]api.UserInfo, serverAlterID uint16) (users []*protocol.User) {
users = make([]*protocol.User, len(*userInfo))
for i, user := range *userInfo {
func (c *Node) buildVmessUsers(userInfo []api.UserInfo, serverAlterID uint16) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i, user := range userInfo {
users[i] = c.buildVmessUser(&user, serverAlterID)
}
return users
@ -35,10 +35,10 @@ func (c *Node) buildVmessUser(userInfo *api.UserInfo, serverAlterID uint16) (use
return user
}
func (c *Node) buildVlessUsers(userInfo *[]api.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(*userInfo))
for i := range *userInfo {
users[i] = c.buildVlessUser(&(*userInfo)[i])
func (c *Node) buildVlessUsers(userInfo []api.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i := range userInfo {
users[i] = c.buildVlessUser(&(userInfo)[i])
}
return users
}
@ -56,10 +56,10 @@ func (c *Node) buildVlessUser(userInfo *api.UserInfo) (user *protocol.User) {
return user
}
func (c *Node) buildTrojanUsers(userInfo *[]api.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(*userInfo))
for i := range *userInfo {
users[i] = c.buildTrojanUser(&(*userInfo)[i])
func (c *Node) buildTrojanUsers(userInfo []api.UserInfo) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i := range userInfo {
users[i] = c.buildTrojanUser(&(userInfo)[i])
}
return users
}
@ -92,10 +92,10 @@ func getCipherFromString(c string) shadowsocks.CipherType {
}
}
func (c *Node) buildSSUsers(userInfo *[]api.UserInfo, cypher shadowsocks.CipherType) (users []*protocol.User) {
users = make([]*protocol.User, len(*userInfo))
for i := range *userInfo {
c.buildSSUser(&(*userInfo)[i], cypher)
func (c *Node) buildSSUsers(userInfo []api.UserInfo, cypher shadowsocks.CipherType) (users []*protocol.User) {
users = make([]*protocol.User, len(userInfo))
for i := range userInfo {
c.buildSSUser(&(userInfo)[i], cypher)
}
return users
}

View File

@ -1,24 +0,0 @@
package xray
import (
"github.com/Yuzuki616/V2bX/api"
"github.com/Yuzuki616/V2bX/app/dispatcher"
"github.com/xtls/xray-core/features/routing"
)
func (p *Xray) UpdateRule(tag string, newRuleList []api.DetectRule) error {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
err := dispather.RuleManager.UpdateRule(tag, newRuleList)
return err
}
func (p *Xray) UpdateProtocolRule(tag string, newRuleList []string) error {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
err := dispather.RuleManager.UpdateProtocolRule(tag, newRuleList)
return err
}
func (p *Xray) GetDetectResult(tag string) (*[]api.DetectResult, error) {
dispather := p.Server.GetFeature(routing.DispatcherType()).(*dispatcher.DefaultDispatcher)
return dispather.RuleManager.GetDetectResult(tag)
}