From 27b97927c1e5c6480485f59693d0ca5b8b2f484e Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Tue, 16 Aug 2022 13:04:33 +0800 Subject: [PATCH] change project structure, fix online ip report bug --- api/{ => panel}/api.go | 6 +-- api/{ => panel}/node.go | 2 +- api/{interface.go => panel/panel.go} | 4 +- api/{ => panel}/user.go | 2 +- api/{ => panel}/utils.go | 2 +- app/app.go | 2 - app/limiter/errors.go | 9 ----- app/limiter/rate.go | 31 --------------- app/rule/errors.go | 9 ----- core/app/dispatcher/default.go | 10 ++--- .../app/dispatcher}/limiter.go | 38 +++++++++++++++---- {app/rule => core/app/dispatcher}/rule.go | 22 +++++------ core/inbound.go | 12 +++--- core/rule.go | 6 +-- core/user.go | 6 +-- main.go | 4 +- node/inboundbuilder.go | 10 ++--- node/inboundbuilder_test.go | 9 ++--- {app => node}/legoCmd/cmd/account.go | 0 {app => node}/legoCmd/cmd/accounts_storage.go | 2 +- {app => node}/legoCmd/cmd/certs_storage.go | 2 +- {app => node}/legoCmd/cmd/cmd.go | 0 {app => node}/legoCmd/cmd/cmd_before.go | 2 +- {app => node}/legoCmd/cmd/cmd_dnshelp.go | 0 {app => node}/legoCmd/cmd/cmd_list.go | 0 {app => node}/legoCmd/cmd/cmd_renew.go | 2 +- {app => node}/legoCmd/cmd/cmd_renew_test.go | 0 {app => node}/legoCmd/cmd/cmd_revoke.go | 2 +- {app => node}/legoCmd/cmd/cmd_run.go | 2 +- {app => node}/legoCmd/cmd/flags.go | 0 {app => node}/legoCmd/cmd/hook.go | 0 {app => node}/legoCmd/cmd/setup.go | 2 +- {app => node}/legoCmd/cmd/setup_challenges.go | 2 +- .../legoCmd/cmd/zz_gen_cmd_dnshelp.go | 0 {app => node}/legoCmd/lego.go | 8 ++-- {app => node}/legoCmd/lego_test.go | 3 +- {app => node}/legoCmd/log/log.go | 0 node/node.go | 35 +++++++++-------- node/node_test.go | 3 +- node/outboundbuilder.go | 4 +- node/userbuilder.go | 20 +++++----- 41 files changed, 121 insertions(+), 152 deletions(-) rename api/{ => panel}/api.go (94%) rename api/{ => panel}/node.go (99%) rename api/{interface.go => panel/panel.go} (87%) rename api/{ => panel}/user.go (99%) rename api/{ => panel}/utils.go (98%) delete mode 100644 app/app.go delete mode 100644 app/limiter/errors.go delete mode 100644 app/limiter/rate.go delete mode 100644 app/rule/errors.go rename {app/limiter => core/app/dispatcher}/limiter.go (87%) rename {app/rule => core/app/dispatcher}/rule.go (79%) rename {app => node}/legoCmd/cmd/account.go (100%) rename {app => node}/legoCmd/cmd/accounts_storage.go (99%) rename {app => node}/legoCmd/cmd/certs_storage.go (99%) rename {app => node}/legoCmd/cmd/cmd.go (100%) rename {app => node}/legoCmd/cmd/cmd_before.go (91%) rename {app => node}/legoCmd/cmd/cmd_dnshelp.go (100%) rename {app => node}/legoCmd/cmd/cmd_list.go (100%) rename {app => node}/legoCmd/cmd/cmd_renew.go (99%) rename {app => node}/legoCmd/cmd/cmd_renew_test.go (100%) rename {app => node}/legoCmd/cmd/cmd_revoke.go (96%) rename {app => node}/legoCmd/cmd/cmd_run.go (99%) rename {app => node}/legoCmd/cmd/flags.go (100%) rename {app => node}/legoCmd/cmd/hook.go (100%) rename {app => node}/legoCmd/cmd/setup.go (98%) rename {app => node}/legoCmd/cmd/setup_challenges.go (98%) rename {app => node}/legoCmd/cmd/zz_gen_cmd_dnshelp.go (100%) rename {app => node}/legoCmd/lego.go (96%) rename {app => node}/legoCmd/lego_test.go (97%) rename {app => node}/legoCmd/log/log.go (100%) diff --git a/api/api.go b/api/panel/api.go similarity index 94% rename from api/api.go rename to api/panel/api.go index 5f697e9..64251ff 100644 --- a/api/api.go +++ b/api/panel/api.go @@ -1,7 +1,7 @@ // Package api contains all the api used by XrayR // To implement an api , one needs to implement the interface below. -package api +package panel import ( "github.com/Yuzuki616/V2bX/conf" @@ -12,7 +12,7 @@ import ( "time" ) -// API is the interface for different panel's api. +// Panel is the interface for different panel's api. type ClientInfo struct { APIHost string @@ -39,7 +39,7 @@ type Client struct { NodeRuleRspMd5 [16]byte } -func New(apiConfig *conf.ApiConfig) API { +func New(apiConfig *conf.ApiConfig) Panel { client := resty.New() client.SetRetryCount(3) if apiConfig.Timeout > 0 { diff --git a/api/node.go b/api/panel/node.go similarity index 99% rename from api/node.go rename to api/panel/node.go index 39b2ef3..3e37f92 100644 --- a/api/node.go +++ b/api/panel/node.go @@ -1,4 +1,4 @@ -package api +package panel import ( "bufio" diff --git a/api/interface.go b/api/panel/panel.go similarity index 87% rename from api/interface.go rename to api/panel/panel.go index 0892e02..d6acb78 100644 --- a/api/interface.go +++ b/api/panel/panel.go @@ -1,6 +1,6 @@ -package api +package panel -type API interface { +type Panel interface { GetNodeInfo() (nodeInfo *NodeInfo, err error) GetUserList() (userList []UserInfo, err error) ReportUserTraffic(userTraffic []UserTraffic) (err error) diff --git a/api/user.go b/api/panel/user.go similarity index 99% rename from api/user.go rename to api/panel/user.go index 6bba2ba..e2c5075 100644 --- a/api/user.go +++ b/api/panel/user.go @@ -1,4 +1,4 @@ -package api +package panel import ( "fmt" diff --git a/api/utils.go b/api/panel/utils.go similarity index 98% rename from api/utils.go rename to api/panel/utils.go index 6669389..5887bff 100644 --- a/api/utils.go +++ b/api/panel/utils.go @@ -1,4 +1,4 @@ -package api +package panel import ( "fmt" diff --git a/app/app.go b/app/app.go deleted file mode 100644 index 33a269c..0000000 --- a/app/app.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package app contains feature implementations of XrayR. -package app diff --git a/app/limiter/errors.go b/app/limiter/errors.go deleted file mode 100644 index 6f1d1fa..0000000 --- a/app/limiter/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -package limiter - -import "github.com/xtls/xray-core/common/errors" - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} diff --git a/app/limiter/rate.go b/app/limiter/rate.go deleted file mode 100644 index a9284a7..0000000 --- a/app/limiter/rate.go +++ /dev/null @@ -1,31 +0,0 @@ -package limiter - -import ( - "io" - - "github.com/juju/ratelimit" - "github.com/xtls/xray-core/common" - "github.com/xtls/xray-core/common/buf" -) - -type Writer struct { - writer buf.Writer - limiter *ratelimit.Bucket - w io.Writer -} - -func (l *Limiter) RateWriter(writer buf.Writer, limiter *ratelimit.Bucket) buf.Writer { - return &Writer{ - writer: writer, - limiter: limiter, - } -} - -func (w *Writer) Close() error { - return common.Close(w.writer) -} - -func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error { - w.limiter.Wait(int64(mb.Len())) - return w.writer.WriteMultiBuffer(mb) -} diff --git a/app/rule/errors.go b/app/rule/errors.go deleted file mode 100644 index ab39cce..0000000 --- a/app/rule/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -package rule - -import "github.com/xtls/xray-core/common/errors" - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} diff --git a/core/app/dispatcher/default.go b/core/app/dispatcher/default.go index eed9874..69ff0b0 100644 --- a/core/app/dispatcher/default.go +++ b/core/app/dispatcher/default.go @@ -5,8 +5,6 @@ package dispatcher import ( "context" "fmt" - "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" @@ -96,8 +94,8 @@ type DefaultDispatcher struct { stats stats.Manager dns dns.Client fdns dns.FakeDNSEngine - Limiter *limiter.Limiter - RuleManager *rule.Rule + Limiter *Limiter + RuleManager *Rule } func init() { @@ -121,8 +119,8 @@ func (d *DefaultDispatcher) Init(config *Config, om outbound.Manager, router rou d.router = router d.policy = pm d.stats = sm - d.Limiter = limiter.New() - d.RuleManager = rule.New() + d.Limiter = NewLimiter() + d.RuleManager = NewRule() d.dns = dns return nil } diff --git a/app/limiter/limiter.go b/core/app/dispatcher/limiter.go similarity index 87% rename from app/limiter/limiter.go rename to core/app/dispatcher/limiter.go index 003f9df..fbc1830 100644 --- a/app/limiter/limiter.go +++ b/core/app/dispatcher/limiter.go @@ -1,13 +1,15 @@ // Package limiter is to control the links that go into the dispather -package limiter +package dispatcher import ( "fmt" + "github.com/Yuzuki616/V2bX/api/panel" + "github.com/juju/ratelimit" + "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/buf" + "io" "sync" "time" - - "github.com/Yuzuki616/V2bX/api" - "github.com/juju/ratelimit" ) type UserInfo struct { @@ -28,13 +30,13 @@ type Limiter struct { InboundInfo *sync.Map // Key: Tag, Value: *InboundInfo } -func New() *Limiter { +func NewLimiter() *Limiter { return &Limiter{ InboundInfo: new(sync.Map), } } -func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList []api.UserInfo) error { +func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *panel.NodeInfo, userList []panel.UserInfo) error { inboundInfo := &InboundInfo{ Tag: tag, NodeSpeedLimit: nodeInfo.SpeedLimit, @@ -61,7 +63,7 @@ func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList return nil } -func (l *Limiter) UpdateInboundLimiter(tag string, nodeInfo *api.NodeInfo, updatedUserList []api.UserInfo) error { +func (l *Limiter) UpdateInboundLimiter(tag string, nodeInfo *panel.NodeInfo, updatedUserList []panel.UserInfo) error { if value, ok := l.InboundInfo.Load(tag); ok { inboundInfo := value.(*InboundInfo) // Update User info @@ -211,6 +213,28 @@ func (l *Limiter) GetUserBucket(tag string, email string, ip string) (limiter *r } } +type Writer struct { + writer buf.Writer + limiter *ratelimit.Bucket + w io.Writer +} + +func (l *Limiter) RateWriter(writer buf.Writer, limiter *ratelimit.Bucket) buf.Writer { + return &Writer{ + writer: writer, + limiter: limiter, + } +} + +func (w *Writer) Close() error { + return common.Close(w.writer) +} + +func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error { + w.limiter.Wait(int64(mb.Len())) + return w.writer.WriteMultiBuffer(mb) +} + // determineRate returns the minimum non-zero rate func determineRate(nodeLimit, userLimit uint64) (limit uint64) { if nodeLimit == 0 || userLimit == 0 { diff --git a/app/rule/rule.go b/core/app/dispatcher/rule.go similarity index 79% rename from app/rule/rule.go rename to core/app/dispatcher/rule.go index ac47992..91e8ea8 100644 --- a/app/rule/rule.go +++ b/core/app/dispatcher/rule.go @@ -1,14 +1,14 @@ // Package rule is to control the audit rule behaviors -package rule +package dispatcher import ( "fmt" + "github.com/Yuzuki616/V2bX/api/panel" "reflect" "strconv" "strings" "sync" - "github.com/Yuzuki616/V2bX/api" mapset "github.com/deckarep/golang-set" ) @@ -18,7 +18,7 @@ type Rule struct { InboundDetectResult *sync.Map // key: Tag, Value: mapset.NewSet []api.DetectResult } -func New() *Rule { +func NewRule() *Rule { return &Rule{ InboundRule: new(sync.Map), InboundProtocolRule: new(sync.Map), @@ -26,9 +26,9 @@ func New() *Rule { } } -func (r *Rule) UpdateRule(tag string, newRuleList []api.DetectRule) error { +func (r *Rule) UpdateRule(tag string, newRuleList []panel.DetectRule) error { if value, ok := r.InboundRule.LoadOrStore(tag, newRuleList); ok { - oldRuleList := value.([]api.DetectRule) + oldRuleList := value.([]panel.DetectRule) if !reflect.DeepEqual(oldRuleList, newRuleList) { r.InboundRule.Store(tag, newRuleList) } @@ -46,13 +46,13 @@ func (r *Rule) UpdateProtocolRule(tag string, ruleList []string) error { return nil } -func (r *Rule) GetDetectResult(tag string) ([]api.DetectResult, error) { - detectResult := make([]api.DetectResult, 0) +func (r *Rule) GetDetectResult(tag string) ([]panel.DetectResult, error) { + detectResult := make([]panel.DetectResult, 0) if value, ok := r.InboundDetectResult.LoadAndDelete(tag); ok { resultSet := value.(mapset.Set) it := resultSet.Iterator() for result := range it.C { - detectResult = append(detectResult, result.(api.DetectResult)) + detectResult = append(detectResult, result.(panel.DetectResult)) } } return detectResult, nil @@ -63,7 +63,7 @@ func (r *Rule) Detect(tag string, destination string, email string) (reject bool var hitRuleID = -1 // If we have some rule for this inbound if value, ok := r.InboundRule.Load(tag); ok { - ruleList := value.([]api.DetectRule) + ruleList := value.([]panel.DetectRule) for _, r := range ruleList { if r.Pattern.Match([]byte(destination)) { hitRuleID = r.ID @@ -79,12 +79,12 @@ func (r *Rule) Detect(tag string, destination string, email string) (reject bool newError(fmt.Sprintf("Record illegal behavior failed! Cannot find user's uid: %s", email)).AtDebug().WriteToLog() return reject } - newSet := mapset.NewSetWith(api.DetectResult{UID: uid, RuleID: hitRuleID}) + newSet := mapset.NewSetWith(panel.DetectResult{UID: uid, RuleID: hitRuleID}) // If there are any hit history if v, ok := r.InboundDetectResult.LoadOrStore(tag, newSet); ok { resultSet := v.(mapset.Set) // If this is a new record - if resultSet.Add(api.DetectResult{UID: uid, RuleID: hitRuleID}) { + if resultSet.Add(panel.DetectResult{UID: uid, RuleID: hitRuleID}) { r.InboundDetectResult.Store(tag, resultSet) } } diff --git a/core/inbound.go b/core/inbound.go index f4306a2..dfd9044 100644 --- a/core/inbound.go +++ b/core/inbound.go @@ -3,8 +3,8 @@ package core import ( "context" "fmt" - "github.com/Yuzuki616/V2bX/api" - "github.com/Yuzuki616/V2bX/app/limiter" + "github.com/Yuzuki616/V2bX/api/panel" + "github.com/Yuzuki616/V2bX/core/app/dispatcher" "github.com/xtls/xray-core/core" "github.com/xtls/xray-core/features/inbound" ) @@ -29,20 +29,20 @@ func (p *Core) AddInbound(config *core.InboundHandlerConfig) error { return nil } -func (p *Core) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList []api.UserInfo) error { +func (p *Core) AddInboundLimiter(tag string, nodeInfo *panel.NodeInfo, userList []panel.UserInfo) error { err := p.dispatcher.Limiter.AddInboundLimiter(tag, nodeInfo, userList) return err } -func (p *Core) GetInboundLimiter(tag string) (*limiter.InboundInfo, error) { +func (p *Core) GetInboundLimiter(tag string) (*dispatcher.InboundInfo, error) { limit, ok := p.dispatcher.Limiter.InboundInfo.Load(tag) if ok { - return limit.(*limiter.InboundInfo), nil + return limit.(*dispatcher.InboundInfo), nil } return nil, fmt.Errorf("not found limiter") } -func (p *Core) UpdateInboundLimiter(tag string, nodeInfo *api.NodeInfo, updatedUserList []api.UserInfo) error { +func (p *Core) UpdateInboundLimiter(tag string, nodeInfo *panel.NodeInfo, updatedUserList []panel.UserInfo) error { err := p.dispatcher.Limiter.UpdateInboundLimiter(tag, nodeInfo, updatedUserList) return err } diff --git a/core/rule.go b/core/rule.go index 8cda5bb..d213a0c 100644 --- a/core/rule.go +++ b/core/rule.go @@ -1,10 +1,10 @@ package core import ( - "github.com/Yuzuki616/V2bX/api" + "github.com/Yuzuki616/V2bX/api/panel" ) -func (p *Core) UpdateRule(tag string, newRuleList []api.DetectRule) error { +func (p *Core) UpdateRule(tag string, newRuleList []panel.DetectRule) error { return p.dispatcher.RuleManager.UpdateRule(tag, newRuleList) } @@ -13,6 +13,6 @@ func (p *Core) UpdateProtocolRule(tag string, newRuleList []string) error { return p.dispatcher.RuleManager.UpdateProtocolRule(tag, newRuleList) } -func (p *Core) GetDetectResult(tag string) ([]api.DetectResult, error) { +func (p *Core) GetDetectResult(tag string) ([]panel.DetectResult, error) { return p.dispatcher.RuleManager.GetDetectResult(tag) } diff --git a/core/user.go b/core/user.go index b85495f..ab73d66 100644 --- a/core/user.go +++ b/core/user.go @@ -3,7 +3,7 @@ package core import ( "context" "fmt" - "github.com/Yuzuki616/V2bX/app/limiter" + "github.com/Yuzuki616/V2bX/core/app/dispatcher" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/proxy" @@ -74,11 +74,11 @@ func (p *Core) GetUserTraffic(email string) (up int64, down int64) { return up, down } -func (p *Core) GetOnlineIps(tag string) ([]limiter.UserIp, error) { +func (p *Core) GetOnlineIps(tag string) ([]dispatcher.UserIp, error) { return p.dispatcher.Limiter.GetOnlineUserIp(tag) } -func (p *Core) UpdateOnlineIps(tag string, ips []limiter.UserIp) { +func (p *Core) UpdateOnlineIps(tag string, ips []dispatcher.UserIp) { p.dispatcher.Limiter.UpdateOnlineUserIP(tag, ips) } diff --git a/main.go b/main.go index 24766da..27733cb 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "flag" "fmt" - "github.com/Yuzuki616/V2bX/api" + "github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/core" "github.com/Yuzuki616/V2bX/node" @@ -60,7 +60,7 @@ func getConfig() *viper.Viper { func startNodes(nodes []*conf.NodeConfig, core *core.Core) error { for i, _ := range nodes { - var apiClient = api.New(nodes[i].ApiConfig) + var apiClient = panel.New(nodes[i].ApiConfig) // Register controller service err := node.New(core, apiClient, nodes[i].ControllerConfig).Start() if err != nil { diff --git a/node/inboundbuilder.go b/node/inboundbuilder.go index b25ec80..43d7788 100644 --- a/node/inboundbuilder.go +++ b/node/inboundbuilder.go @@ -4,9 +4,9 @@ package node import ( "encoding/json" "fmt" - "github.com/Yuzuki616/V2bX/api" - "github.com/Yuzuki616/V2bX/app/legoCmd" + "github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/conf" + "github.com/Yuzuki616/V2bX/node/legoCmd" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/uuid" "github.com/xtls/xray-core/core" @@ -14,7 +14,7 @@ import ( ) //InboundBuilder build Inbound config for different protocol -func InboundBuilder(config *conf.ControllerConfig, nodeInfo *api.NodeInfo, tag string) (*core.InboundHandlerConfig, error) { +func InboundBuilder(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag string) (*core.InboundHandlerConfig, error) { var proxySetting interface{} if nodeInfo.NodeType == "V2ray" { defer func() { @@ -47,7 +47,7 @@ func InboundBuilder(config *conf.ControllerConfig, nodeInfo *api.NodeInfo, tag s nodeInfo.V2ray = nil nodeInfo.Trojan = nil }() - nodeInfo.V2ray = &api.V2rayConfig{} + nodeInfo.V2ray = &panel.V2rayConfig{} nodeInfo.V2ray.Inbounds = make([]coreConf.InboundDetourConfig, 1) nodeInfo.V2ray.Inbounds[0].Protocol = "trojan" // Enable fallback @@ -72,7 +72,7 @@ func InboundBuilder(config *conf.ControllerConfig, nodeInfo *api.NodeInfo, tag s defer func() { nodeInfo.V2ray = nil }() - nodeInfo.V2ray = &api.V2rayConfig{} + nodeInfo.V2ray = &panel.V2rayConfig{} nodeInfo.V2ray.Inbounds = []coreConf.InboundDetourConfig{{Protocol: "shadowsocks"}} proxySetting = &coreConf.ShadowsocksServerConfig{} randomPasswd := uuid.New() diff --git a/node/inboundbuilder_test.go b/node/inboundbuilder_test.go index 5c1f605..e114b7c 100644 --- a/node/inboundbuilder_test.go +++ b/node/inboundbuilder_test.go @@ -1,14 +1,13 @@ package node_test import ( + "github.com/Yuzuki616/V2bX/api/panel" . "github.com/Yuzuki616/V2bX/node" "testing" - - "github.com/Yuzuki616/V2bX/api" ) func TestBuildV2ray(t *testing.T) { - nodeInfo := &api.NodeInfo{ + nodeInfo := &panel.NodeInfo{ NodeType: "V2ray", NodeID: 1, Port: 1145, @@ -36,7 +35,7 @@ func TestBuildV2ray(t *testing.T) { } func TestBuildTrojan(t *testing.T) { - nodeInfo := &api.NodeInfo{ + nodeInfo := &panel.NodeInfo{ NodeType: "Trojan", NodeID: 1, Port: 1145, @@ -68,7 +67,7 @@ func TestBuildTrojan(t *testing.T) { } func TestBuildSS(t *testing.T) { - nodeInfo := &api.NodeInfo{ + nodeInfo := &panel.NodeInfo{ NodeType: "Shadowsocks", NodeID: 1, Port: 1145, diff --git a/app/legoCmd/cmd/account.go b/node/legoCmd/cmd/account.go similarity index 100% rename from app/legoCmd/cmd/account.go rename to node/legoCmd/cmd/account.go diff --git a/app/legoCmd/cmd/accounts_storage.go b/node/legoCmd/cmd/accounts_storage.go similarity index 99% rename from app/legoCmd/cmd/accounts_storage.go rename to node/legoCmd/cmd/accounts_storage.go index 84ba5f3..9e9fd65 100644 --- a/app/legoCmd/cmd/accounts_storage.go +++ b/node/legoCmd/cmd/accounts_storage.go @@ -7,13 +7,13 @@ import ( "encoding/pem" "errors" "fmt" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "io/ioutil" "net/url" "os" "path/filepath" "strings" - "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" diff --git a/app/legoCmd/cmd/certs_storage.go b/node/legoCmd/cmd/certs_storage.go similarity index 99% rename from app/legoCmd/cmd/certs_storage.go rename to node/legoCmd/cmd/certs_storage.go index 29e8532..6f56b5f 100644 --- a/app/legoCmd/cmd/certs_storage.go +++ b/node/legoCmd/cmd/certs_storage.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/x509" "encoding/json" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "io/ioutil" "os" "path/filepath" @@ -11,7 +12,6 @@ import ( "strings" "time" - "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" diff --git a/app/legoCmd/cmd/cmd.go b/node/legoCmd/cmd/cmd.go similarity index 100% rename from app/legoCmd/cmd/cmd.go rename to node/legoCmd/cmd/cmd.go diff --git a/app/legoCmd/cmd/cmd_before.go b/node/legoCmd/cmd/cmd_before.go similarity index 91% rename from app/legoCmd/cmd/cmd_before.go rename to node/legoCmd/cmd/cmd_before.go index a7d5176..ce3b078 100644 --- a/app/legoCmd/cmd/cmd_before.go +++ b/node/legoCmd/cmd/cmd_before.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/Yuzuki616/V2bX/app/legoCmd/log" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "github.com/urfave/cli" ) diff --git a/app/legoCmd/cmd/cmd_dnshelp.go b/node/legoCmd/cmd/cmd_dnshelp.go similarity index 100% rename from app/legoCmd/cmd/cmd_dnshelp.go rename to node/legoCmd/cmd/cmd_dnshelp.go diff --git a/app/legoCmd/cmd/cmd_list.go b/node/legoCmd/cmd/cmd_list.go similarity index 100% rename from app/legoCmd/cmd/cmd_list.go rename to node/legoCmd/cmd/cmd_list.go diff --git a/app/legoCmd/cmd/cmd_renew.go b/node/legoCmd/cmd/cmd_renew.go similarity index 99% rename from app/legoCmd/cmd/cmd_renew.go rename to node/legoCmd/cmd/cmd_renew.go index 258bd17..a25b470 100644 --- a/app/legoCmd/cmd/cmd_renew.go +++ b/node/legoCmd/cmd/cmd_renew.go @@ -3,9 +3,9 @@ package cmd import ( "crypto" "crypto/x509" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "time" - "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" diff --git a/app/legoCmd/cmd/cmd_renew_test.go b/node/legoCmd/cmd/cmd_renew_test.go similarity index 100% rename from app/legoCmd/cmd/cmd_renew_test.go rename to node/legoCmd/cmd/cmd_renew_test.go diff --git a/app/legoCmd/cmd/cmd_revoke.go b/node/legoCmd/cmd/cmd_revoke.go similarity index 96% rename from app/legoCmd/cmd/cmd_revoke.go rename to node/legoCmd/cmd/cmd_revoke.go index 690c6d1..44dd152 100644 --- a/app/legoCmd/cmd/cmd_revoke.go +++ b/node/legoCmd/cmd/cmd_revoke.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/Yuzuki616/V2bX/app/legoCmd/log" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "github.com/urfave/cli" ) diff --git a/app/legoCmd/cmd/cmd_run.go b/node/legoCmd/cmd/cmd_run.go similarity index 99% rename from app/legoCmd/cmd/cmd_run.go rename to node/legoCmd/cmd/cmd_run.go index 147a1b5..7aa5c1c 100644 --- a/app/legoCmd/cmd/cmd_run.go +++ b/node/legoCmd/cmd/cmd_run.go @@ -3,10 +3,10 @@ package cmd import ( "bufio" "fmt" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "os" "strings" - "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" diff --git a/app/legoCmd/cmd/flags.go b/node/legoCmd/cmd/flags.go similarity index 100% rename from app/legoCmd/cmd/flags.go rename to node/legoCmd/cmd/flags.go diff --git a/app/legoCmd/cmd/hook.go b/node/legoCmd/cmd/hook.go similarity index 100% rename from app/legoCmd/cmd/hook.go rename to node/legoCmd/cmd/hook.go diff --git a/app/legoCmd/cmd/setup.go b/node/legoCmd/cmd/setup.go similarity index 98% rename from app/legoCmd/cmd/setup.go rename to node/legoCmd/cmd/setup.go index 62d7db1..60a040a 100644 --- a/app/legoCmd/cmd/setup.go +++ b/node/legoCmd/cmd/setup.go @@ -4,12 +4,12 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "io/ioutil" "os" "strings" "time" - "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" diff --git a/app/legoCmd/cmd/setup_challenges.go b/node/legoCmd/cmd/setup_challenges.go similarity index 98% rename from app/legoCmd/cmd/setup_challenges.go rename to node/legoCmd/cmd/setup_challenges.go index 2029429..1c61045 100644 --- a/app/legoCmd/cmd/setup_challenges.go +++ b/node/legoCmd/cmd/setup_challenges.go @@ -1,11 +1,11 @@ package cmd import ( + "github.com/Yuzuki616/V2bX/node/legoCmd/log" "net" "strings" "time" - "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" diff --git a/app/legoCmd/cmd/zz_gen_cmd_dnshelp.go b/node/legoCmd/cmd/zz_gen_cmd_dnshelp.go similarity index 100% rename from app/legoCmd/cmd/zz_gen_cmd_dnshelp.go rename to node/legoCmd/cmd/zz_gen_cmd_dnshelp.go diff --git a/app/legoCmd/lego.go b/node/legoCmd/lego.go similarity index 96% rename from app/legoCmd/lego.go rename to node/legoCmd/lego.go index 249c14c..ecbeb4c 100644 --- a/app/legoCmd/lego.go +++ b/node/legoCmd/lego.go @@ -5,13 +5,13 @@ package legoCmd import ( "errors" "fmt" + cmd2 "github.com/Yuzuki616/V2bX/node/legoCmd/cmd" "os" "path" "path/filepath" "runtime" "strings" - "github.com/Yuzuki616/V2bX/app/legoCmd/cmd" "github.com/urfave/cli" ) @@ -47,11 +47,11 @@ func New() (*LegoCMD, error) { defaultPath = filepath.Join(pathTemp, "cert") - app.Flags = cmd.CreateFlags(defaultPath) + app.Flags = cmd2.CreateFlags(defaultPath) - app.Before = cmd.Before + app.Before = cmd2.Before - app.Commands = cmd.CreateCommands() + app.Commands = cmd2.CreateCommands() lego := &LegoCMD{ cmdClient: app, diff --git a/app/legoCmd/lego_test.go b/node/legoCmd/lego_test.go similarity index 97% rename from app/legoCmd/lego_test.go rename to node/legoCmd/lego_test.go index 2282e26..345d7bc 100644 --- a/app/legoCmd/lego_test.go +++ b/node/legoCmd/lego_test.go @@ -1,9 +1,8 @@ package legoCmd_test import ( + "github.com/Yuzuki616/V2bX/node/legoCmd" "testing" - - "github.com/Yuzuki616/V2bX/app/legoCmd" ) func TestLegoClient(t *testing.T) { diff --git a/app/legoCmd/log/log.go b/node/legoCmd/log/log.go similarity index 100% rename from app/legoCmd/log/log.go rename to node/legoCmd/log/log.go diff --git a/node/node.go b/node/node.go index 8294b24..f31c018 100644 --- a/node/node.go +++ b/node/node.go @@ -2,38 +2,37 @@ package node import ( "fmt" - "github.com/Yuzuki616/V2bX/app/limiter" + "github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/core" + "github.com/Yuzuki616/V2bX/core/app/dispatcher" + "github.com/Yuzuki616/V2bX/node/legoCmd" "github.com/go-resty/resty/v2" "github.com/goccy/go-json" + "github.com/xtls/xray-core/common/protocol" + "github.com/xtls/xray-core/common/task" "log" "math" "reflect" "runtime" "time" - - "github.com/Yuzuki616/V2bX/api" - "github.com/Yuzuki616/V2bX/app/legoCmd" - "github.com/xtls/xray-core/common/protocol" - "github.com/xtls/xray-core/common/task" ) type Node struct { server *core.Core config *conf.ControllerConfig - clientInfo api.ClientInfo - apiClient api.API - nodeInfo *api.NodeInfo + clientInfo panel.ClientInfo + apiClient panel.Panel + nodeInfo *panel.NodeInfo Tag string - userList []api.UserInfo + userList []panel.UserInfo nodeInfoMonitorPeriodic *task.Periodic userReportPeriodic *task.Periodic onlineIpReportPeriodic *task.Periodic } // New return a Node service with default parameters. -func New(server *core.Core, api api.API, config *conf.ControllerConfig) *Node { +func New(server *core.Core, api panel.Panel, config *conf.ControllerConfig) *Node { controller := &Node{ server: server, config: config, @@ -280,7 +279,7 @@ func (c *Node) removeOldTag(oldtag string) (err error) { return nil } -func (c *Node) addNewTag(newNodeInfo *api.NodeInfo) (err error) { +func (c *Node) addNewTag(newNodeInfo *panel.NodeInfo) (err error) { inboundConfig, err := InboundBuilder(c.config, newNodeInfo, c.Tag) if err != nil { return err @@ -303,7 +302,7 @@ 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 []panel.UserInfo, nodeInfo *panel.NodeInfo) (err error) { users := make([]*protocol.User, 0) if nodeInfo.NodeType == "V2ray" { if nodeInfo.EnableVless { @@ -333,7 +332,7 @@ func (c *Node) addNewUser(userInfo []api.UserInfo, nodeInfo *api.NodeInfo) (err return nil } -func compareUserList(old, new []api.UserInfo) (deleted, added []api.UserInfo) { +func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) { tmp := map[string]struct{}{} tmp2 := map[string]struct{}{} for i := range old { @@ -363,11 +362,11 @@ func compareUserList(old, new []api.UserInfo) (deleted, added []api.UserInfo) { func (c *Node) userInfoMonitor() (err error) { // Get User traffic - userTraffic := make([]api.UserTraffic, 0) + userTraffic := make([]panel.UserTraffic, 0) 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{ + userTraffic = append(userTraffic, panel.UserTraffic{ UID: (c.userList)[i].UID, Upload: up, Download: down}) @@ -406,8 +405,8 @@ func (c *Node) onlineIpReport() (err error) { } log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp)) if rsp.StatusCode() == 200 { - onlineIp = []limiter.UserIp{} - err := json.Unmarshal(rsp.Body(), onlineIp) + onlineIp = []dispatcher.UserIp{} + err := json.Unmarshal(rsp.Body(), &onlineIp) if err != nil { log.Print(err) c.server.ClearOnlineIps(c.Tag) diff --git a/node/node_test.go b/node/node_test.go index 92325dc..5a2f3f7 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -2,6 +2,7 @@ package node_test import ( "fmt" + "github.com/Yuzuki616/V2bX/api/panel" . "github.com/Yuzuki616/V2bX/node" "os" "os/signal" @@ -60,7 +61,7 @@ func TestController(t *testing.T) { NodeID: 41, NodeType: "V2ray", } - apiclient := api.New(apiConfig) + apiclient := panel.New(apiConfig) c := New(server, apiclient, controlerconfig) fmt.Println("Sleep 1s") err = c.Start() diff --git a/node/outboundbuilder.go b/node/outboundbuilder.go index 3a24631..27c25b6 100644 --- a/node/outboundbuilder.go +++ b/node/outboundbuilder.go @@ -3,16 +3,16 @@ package node import ( "encoding/json" "fmt" + "github.com/Yuzuki616/V2bX/api/panel" conf2 "github.com/Yuzuki616/V2bX/conf" - "github.com/Yuzuki616/V2bX/api" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/core" "github.com/xtls/xray-core/infra/conf" ) //OutboundBuilder build freedom outbund config for addoutbound -func OutboundBuilder(config *conf2.ControllerConfig, nodeInfo *api.NodeInfo, tag string) (*core.OutboundHandlerConfig, error) { +func OutboundBuilder(config *conf2.ControllerConfig, nodeInfo *panel.NodeInfo, tag string) (*core.OutboundHandlerConfig, error) { outboundDetourConfig := &conf.OutboundDetourConfig{} outboundDetourConfig.Protocol = "freedom" outboundDetourConfig.Tag = tag diff --git a/node/userbuilder.go b/node/userbuilder.go index 50099b4..2475f05 100644 --- a/node/userbuilder.go +++ b/node/userbuilder.go @@ -2,9 +2,9 @@ package node import ( "fmt" + "github.com/Yuzuki616/V2bX/api/panel" "strings" - "github.com/Yuzuki616/V2bX/api" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/infra/conf" @@ -13,7 +13,7 @@ import ( "github.com/xtls/xray-core/proxy/vless" ) -func (c *Node) buildVmessUsers(userInfo []api.UserInfo, serverAlterID uint16) (users []*protocol.User) { +func (c *Node) buildVmessUsers(userInfo []panel.UserInfo, serverAlterID uint16) (users []*protocol.User) { users = make([]*protocol.User, len(userInfo)) for i, user := range userInfo { users[i] = c.buildVmessUser(&user, serverAlterID) @@ -21,7 +21,7 @@ func (c *Node) buildVmessUsers(userInfo []api.UserInfo, serverAlterID uint16) (u return users } -func (c *Node) buildVmessUser(userInfo *api.UserInfo, serverAlterID uint16) (user *protocol.User) { +func (c *Node) buildVmessUser(userInfo *panel.UserInfo, serverAlterID uint16) (user *protocol.User) { vmessAccount := &conf.VMessAccount{ ID: userInfo.V2rayUser.Uuid, AlterIds: serverAlterID, @@ -35,7 +35,7 @@ func (c *Node) buildVmessUser(userInfo *api.UserInfo, serverAlterID uint16) (use return user } -func (c *Node) buildVlessUsers(userInfo []api.UserInfo) (users []*protocol.User) { +func (c *Node) buildVlessUsers(userInfo []panel.UserInfo) (users []*protocol.User) { users = make([]*protocol.User, len(userInfo)) for i := range userInfo { users[i] = c.buildVlessUser(&(userInfo)[i]) @@ -43,7 +43,7 @@ func (c *Node) buildVlessUsers(userInfo []api.UserInfo) (users []*protocol.User) return users } -func (c *Node) buildVlessUser(userInfo *api.UserInfo) (user *protocol.User) { +func (c *Node) buildVlessUser(userInfo *panel.UserInfo) (user *protocol.User) { vlessAccount := &vless.Account{ Id: userInfo.V2rayUser.Uuid, Flow: "xtls-rprx-direct", @@ -56,7 +56,7 @@ func (c *Node) buildVlessUser(userInfo *api.UserInfo) (user *protocol.User) { return user } -func (c *Node) buildTrojanUsers(userInfo []api.UserInfo) (users []*protocol.User) { +func (c *Node) buildTrojanUsers(userInfo []panel.UserInfo) (users []*protocol.User) { users = make([]*protocol.User, len(userInfo)) for i := range userInfo { users[i] = c.buildTrojanUser(&(userInfo)[i]) @@ -64,7 +64,7 @@ func (c *Node) buildTrojanUsers(userInfo []api.UserInfo) (users []*protocol.User return users } -func (c *Node) buildTrojanUser(userInfo *api.UserInfo) (user *protocol.User) { +func (c *Node) buildTrojanUser(userInfo *panel.UserInfo) (user *protocol.User) { trojanAccount := &trojan.Account{ Password: userInfo.TrojanUser.Password, Flow: "xtls-rprx-direct", @@ -92,7 +92,7 @@ func getCipherFromString(c string) shadowsocks.CipherType { } } -func (c *Node) buildSSUsers(userInfo []api.UserInfo, cypher shadowsocks.CipherType) (users []*protocol.User) { +func (c *Node) buildSSUsers(userInfo []panel.UserInfo, cypher shadowsocks.CipherType) (users []*protocol.User) { users = make([]*protocol.User, len(userInfo)) for i := range userInfo { c.buildSSUser(&(userInfo)[i], cypher) @@ -100,7 +100,7 @@ func (c *Node) buildSSUsers(userInfo []api.UserInfo, cypher shadowsocks.CipherTy return users } -func (c *Node) buildSSUser(userInfo *api.UserInfo, cypher shadowsocks.CipherType) (user *protocol.User) { +func (c *Node) buildSSUser(userInfo *panel.UserInfo, cypher shadowsocks.CipherType) (user *protocol.User) { ssAccount := &shadowsocks.Account{ Password: userInfo.Secret, CipherType: cypher, @@ -113,6 +113,6 @@ func (c *Node) buildSSUser(userInfo *api.UserInfo, cypher shadowsocks.CipherType return user } -func (c *Node) buildUserTag(user *api.UserInfo) string { +func (c *Node) buildUserTag(user *panel.UserInfo) string { return fmt.Sprintf("%s|%s|%d", c.Tag, user.GetUserEmail(), user.UID) }