change project structure, fix online ip report bug

This commit is contained in:
yuzuki999 2022-08-16 13:04:33 +08:00
parent ca180a63c9
commit 27b97927c1
41 changed files with 121 additions and 152 deletions

View File

@ -1,7 +1,7 @@
// Package api contains all the api used by XrayR // Package api contains all the api used by XrayR
// To implement an api , one needs to implement the interface below. // To implement an api , one needs to implement the interface below.
package api package panel
import ( import (
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
@ -12,7 +12,7 @@ import (
"time" "time"
) )
// API is the interface for different panel's api. // Panel is the interface for different panel's api.
type ClientInfo struct { type ClientInfo struct {
APIHost string APIHost string
@ -39,7 +39,7 @@ type Client struct {
NodeRuleRspMd5 [16]byte NodeRuleRspMd5 [16]byte
} }
func New(apiConfig *conf.ApiConfig) API { func New(apiConfig *conf.ApiConfig) Panel {
client := resty.New() client := resty.New()
client.SetRetryCount(3) client.SetRetryCount(3)
if apiConfig.Timeout > 0 { if apiConfig.Timeout > 0 {

View File

@ -1,4 +1,4 @@
package api package panel
import ( import (
"bufio" "bufio"

View File

@ -1,6 +1,6 @@
package api package panel
type API interface { type Panel interface {
GetNodeInfo() (nodeInfo *NodeInfo, err error) GetNodeInfo() (nodeInfo *NodeInfo, err error)
GetUserList() (userList []UserInfo, err error) GetUserList() (userList []UserInfo, err error)
ReportUserTraffic(userTraffic []UserTraffic) (err error) ReportUserTraffic(userTraffic []UserTraffic) (err error)

View File

@ -1,4 +1,4 @@
package api package panel
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package api package panel
import ( import (
"fmt" "fmt"

View File

@ -1,2 +0,0 @@
// Package app contains feature implementations of XrayR.
package app

View File

@ -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{})
}

View File

@ -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)
}

View File

@ -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{})
}

View File

@ -5,8 +5,6 @@ package dispatcher
import ( import (
"context" "context"
"fmt" "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"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/log"
@ -96,8 +94,8 @@ type DefaultDispatcher struct {
stats stats.Manager stats stats.Manager
dns dns.Client dns dns.Client
fdns dns.FakeDNSEngine fdns dns.FakeDNSEngine
Limiter *limiter.Limiter Limiter *Limiter
RuleManager *rule.Rule RuleManager *Rule
} }
func init() { func init() {
@ -121,8 +119,8 @@ func (d *DefaultDispatcher) Init(config *Config, om outbound.Manager, router rou
d.router = router d.router = router
d.policy = pm d.policy = pm
d.stats = sm d.stats = sm
d.Limiter = limiter.New() d.Limiter = NewLimiter()
d.RuleManager = rule.New() d.RuleManager = NewRule()
d.dns = dns d.dns = dns
return nil return nil
} }

View File

@ -1,13 +1,15 @@
// Package limiter is to control the links that go into the dispather // Package limiter is to control the links that go into the dispather
package limiter package dispatcher
import ( import (
"fmt" "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" "sync"
"time" "time"
"github.com/Yuzuki616/V2bX/api"
"github.com/juju/ratelimit"
) )
type UserInfo struct { type UserInfo struct {
@ -28,13 +30,13 @@ type Limiter struct {
InboundInfo *sync.Map // Key: Tag, Value: *InboundInfo InboundInfo *sync.Map // Key: Tag, Value: *InboundInfo
} }
func New() *Limiter { func NewLimiter() *Limiter {
return &Limiter{ return &Limiter{
InboundInfo: new(sync.Map), 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{ inboundInfo := &InboundInfo{
Tag: tag, Tag: tag,
NodeSpeedLimit: nodeInfo.SpeedLimit, NodeSpeedLimit: nodeInfo.SpeedLimit,
@ -61,7 +63,7 @@ func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *api.NodeInfo, userList
return nil 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 { if value, ok := l.InboundInfo.Load(tag); ok {
inboundInfo := value.(*InboundInfo) inboundInfo := value.(*InboundInfo)
// Update User info // 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 // determineRate returns the minimum non-zero rate
func determineRate(nodeLimit, userLimit uint64) (limit uint64) { func determineRate(nodeLimit, userLimit uint64) (limit uint64) {
if nodeLimit == 0 || userLimit == 0 { if nodeLimit == 0 || userLimit == 0 {

View File

@ -1,14 +1,14 @@
// Package rule is to control the audit rule behaviors // Package rule is to control the audit rule behaviors
package rule package dispatcher
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"github.com/Yuzuki616/V2bX/api"
mapset "github.com/deckarep/golang-set" mapset "github.com/deckarep/golang-set"
) )
@ -18,7 +18,7 @@ type Rule struct {
InboundDetectResult *sync.Map // key: Tag, Value: mapset.NewSet []api.DetectResult InboundDetectResult *sync.Map // key: Tag, Value: mapset.NewSet []api.DetectResult
} }
func New() *Rule { func NewRule() *Rule {
return &Rule{ return &Rule{
InboundRule: new(sync.Map), InboundRule: new(sync.Map),
InboundProtocolRule: 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 { if value, ok := r.InboundRule.LoadOrStore(tag, newRuleList); ok {
oldRuleList := value.([]api.DetectRule) oldRuleList := value.([]panel.DetectRule)
if !reflect.DeepEqual(oldRuleList, newRuleList) { if !reflect.DeepEqual(oldRuleList, newRuleList) {
r.InboundRule.Store(tag, newRuleList) r.InboundRule.Store(tag, newRuleList)
} }
@ -46,13 +46,13 @@ func (r *Rule) UpdateProtocolRule(tag string, ruleList []string) error {
return nil return nil
} }
func (r *Rule) GetDetectResult(tag string) ([]api.DetectResult, error) { func (r *Rule) GetDetectResult(tag string) ([]panel.DetectResult, error) {
detectResult := make([]api.DetectResult, 0) detectResult := make([]panel.DetectResult, 0)
if value, ok := r.InboundDetectResult.LoadAndDelete(tag); ok { if value, ok := r.InboundDetectResult.LoadAndDelete(tag); ok {
resultSet := value.(mapset.Set) resultSet := value.(mapset.Set)
it := resultSet.Iterator() it := resultSet.Iterator()
for result := range it.C { for result := range it.C {
detectResult = append(detectResult, result.(api.DetectResult)) detectResult = append(detectResult, result.(panel.DetectResult))
} }
} }
return detectResult, nil return detectResult, nil
@ -63,7 +63,7 @@ func (r *Rule) Detect(tag string, destination string, email string) (reject bool
var hitRuleID = -1 var hitRuleID = -1
// If we have some rule for this inbound // If we have some rule for this inbound
if value, ok := r.InboundRule.Load(tag); ok { if value, ok := r.InboundRule.Load(tag); ok {
ruleList := value.([]api.DetectRule) ruleList := value.([]panel.DetectRule)
for _, r := range ruleList { for _, r := range ruleList {
if r.Pattern.Match([]byte(destination)) { if r.Pattern.Match([]byte(destination)) {
hitRuleID = r.ID 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() newError(fmt.Sprintf("Record illegal behavior failed! Cannot find user's uid: %s", email)).AtDebug().WriteToLog()
return reject 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 there are any hit history
if v, ok := r.InboundDetectResult.LoadOrStore(tag, newSet); ok { if v, ok := r.InboundDetectResult.LoadOrStore(tag, newSet); ok {
resultSet := v.(mapset.Set) resultSet := v.(mapset.Set)
// If this is a new record // 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) r.InboundDetectResult.Store(tag, resultSet)
} }
} }

View File

@ -3,8 +3,8 @@ package core
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api" "github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/app/limiter" "github.com/Yuzuki616/V2bX/core/app/dispatcher"
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/inbound" "github.com/xtls/xray-core/features/inbound"
) )
@ -29,20 +29,20 @@ func (p *Core) AddInbound(config *core.InboundHandlerConfig) error {
return nil 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) err := p.dispatcher.Limiter.AddInboundLimiter(tag, nodeInfo, userList)
return err 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) limit, ok := p.dispatcher.Limiter.InboundInfo.Load(tag)
if ok { if ok {
return limit.(*limiter.InboundInfo), nil return limit.(*dispatcher.InboundInfo), nil
} }
return nil, fmt.Errorf("not found limiter") 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) err := p.dispatcher.Limiter.UpdateInboundLimiter(tag, nodeInfo, updatedUserList)
return err return err
} }

View File

@ -1,10 +1,10 @@
package core package core
import ( 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) 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) 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) return p.dispatcher.RuleManager.GetDetectResult(tag)
} }

View File

@ -3,7 +3,7 @@ package core
import ( import (
"context" "context"
"fmt" "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/common/protocol"
"github.com/xtls/xray-core/features/stats" "github.com/xtls/xray-core/features/stats"
"github.com/xtls/xray-core/proxy" "github.com/xtls/xray-core/proxy"
@ -74,11 +74,11 @@ func (p *Core) GetUserTraffic(email string) (up int64, down int64) {
return up, down 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) 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) p.dispatcher.Limiter.UpdateOnlineUserIP(tag, ips)
} }

View File

@ -3,7 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api" "github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core" "github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/node" "github.com/Yuzuki616/V2bX/node"
@ -60,7 +60,7 @@ func getConfig() *viper.Viper {
func startNodes(nodes []*conf.NodeConfig, core *core.Core) error { func startNodes(nodes []*conf.NodeConfig, core *core.Core) error {
for i, _ := range nodes { for i, _ := range nodes {
var apiClient = api.New(nodes[i].ApiConfig) var apiClient = panel.New(nodes[i].ApiConfig)
// Register controller service // Register controller service
err := node.New(core, apiClient, nodes[i].ControllerConfig).Start() err := node.New(core, apiClient, nodes[i].ControllerConfig).Start()
if err != nil { if err != nil {

View File

@ -4,9 +4,9 @@ package node
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api" "github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/app/legoCmd"
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/node/legoCmd"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/uuid" "github.com/xtls/xray-core/common/uuid"
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
@ -14,7 +14,7 @@ import (
) )
//InboundBuilder build Inbound config for different protocol //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{} var proxySetting interface{}
if nodeInfo.NodeType == "V2ray" { if nodeInfo.NodeType == "V2ray" {
defer func() { defer func() {
@ -47,7 +47,7 @@ func InboundBuilder(config *conf.ControllerConfig, nodeInfo *api.NodeInfo, tag s
nodeInfo.V2ray = nil nodeInfo.V2ray = nil
nodeInfo.Trojan = nil nodeInfo.Trojan = nil
}() }()
nodeInfo.V2ray = &api.V2rayConfig{} nodeInfo.V2ray = &panel.V2rayConfig{}
nodeInfo.V2ray.Inbounds = make([]coreConf.InboundDetourConfig, 1) nodeInfo.V2ray.Inbounds = make([]coreConf.InboundDetourConfig, 1)
nodeInfo.V2ray.Inbounds[0].Protocol = "trojan" nodeInfo.V2ray.Inbounds[0].Protocol = "trojan"
// Enable fallback // Enable fallback
@ -72,7 +72,7 @@ func InboundBuilder(config *conf.ControllerConfig, nodeInfo *api.NodeInfo, tag s
defer func() { defer func() {
nodeInfo.V2ray = nil nodeInfo.V2ray = nil
}() }()
nodeInfo.V2ray = &api.V2rayConfig{} nodeInfo.V2ray = &panel.V2rayConfig{}
nodeInfo.V2ray.Inbounds = []coreConf.InboundDetourConfig{{Protocol: "shadowsocks"}} nodeInfo.V2ray.Inbounds = []coreConf.InboundDetourConfig{{Protocol: "shadowsocks"}}
proxySetting = &coreConf.ShadowsocksServerConfig{} proxySetting = &coreConf.ShadowsocksServerConfig{}
randomPasswd := uuid.New() randomPasswd := uuid.New()

View File

@ -1,14 +1,13 @@
package node_test package node_test
import ( import (
"github.com/Yuzuki616/V2bX/api/panel"
. "github.com/Yuzuki616/V2bX/node" . "github.com/Yuzuki616/V2bX/node"
"testing" "testing"
"github.com/Yuzuki616/V2bX/api"
) )
func TestBuildV2ray(t *testing.T) { func TestBuildV2ray(t *testing.T) {
nodeInfo := &api.NodeInfo{ nodeInfo := &panel.NodeInfo{
NodeType: "V2ray", NodeType: "V2ray",
NodeID: 1, NodeID: 1,
Port: 1145, Port: 1145,
@ -36,7 +35,7 @@ func TestBuildV2ray(t *testing.T) {
} }
func TestBuildTrojan(t *testing.T) { func TestBuildTrojan(t *testing.T) {
nodeInfo := &api.NodeInfo{ nodeInfo := &panel.NodeInfo{
NodeType: "Trojan", NodeType: "Trojan",
NodeID: 1, NodeID: 1,
Port: 1145, Port: 1145,
@ -68,7 +67,7 @@ func TestBuildTrojan(t *testing.T) {
} }
func TestBuildSS(t *testing.T) { func TestBuildSS(t *testing.T) {
nodeInfo := &api.NodeInfo{ nodeInfo := &panel.NodeInfo{
NodeType: "Shadowsocks", NodeType: "Shadowsocks",
NodeID: 1, NodeID: 1,
Port: 1145, Port: 1145,

View File

@ -7,13 +7,13 @@ import (
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/node/legoCmd/log"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration" "github.com/go-acme/lego/v4/registration"

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"crypto/x509" "crypto/x509"
"encoding/json" "encoding/json"
"github.com/Yuzuki616/V2bX/node/legoCmd/log"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -11,7 +12,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/certificate"
"github.com/urfave/cli" "github.com/urfave/cli"

View File

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

View File

@ -3,9 +3,9 @@ package cmd
import ( import (
"crypto" "crypto"
"crypto/x509" "crypto/x509"
"github.com/Yuzuki616/V2bX/node/legoCmd/log"
"time" "time"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"

View File

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

View File

@ -3,10 +3,10 @@ package cmd
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/node/legoCmd/log"
"os" "os"
"strings" "strings"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/certificate"
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration" "github.com/go-acme/lego/v4/registration"

View File

@ -4,12 +4,12 @@ import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/node/legoCmd/log"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/certcrypto" "github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration" "github.com/go-acme/lego/v4/registration"

View File

@ -1,11 +1,11 @@
package cmd package cmd
import ( import (
"github.com/Yuzuki616/V2bX/node/legoCmd/log"
"net" "net"
"strings" "strings"
"time" "time"
"github.com/Yuzuki616/V2bX/app/legoCmd/log"
"github.com/go-acme/lego/v4/challenge" "github.com/go-acme/lego/v4/challenge"
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/challenge/http01" "github.com/go-acme/lego/v4/challenge/http01"

View File

@ -5,13 +5,13 @@ package legoCmd
import ( import (
"errors" "errors"
"fmt" "fmt"
cmd2 "github.com/Yuzuki616/V2bX/node/legoCmd/cmd"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"github.com/Yuzuki616/V2bX/app/legoCmd/cmd"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -47,11 +47,11 @@ func New() (*LegoCMD, error) {
defaultPath = filepath.Join(pathTemp, "cert") 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{ lego := &LegoCMD{
cmdClient: app, cmdClient: app,

View File

@ -1,9 +1,8 @@
package legoCmd_test package legoCmd_test
import ( import (
"github.com/Yuzuki616/V2bX/node/legoCmd"
"testing" "testing"
"github.com/Yuzuki616/V2bX/app/legoCmd"
) )
func TestLegoClient(t *testing.T) { func TestLegoClient(t *testing.T) {

View File

@ -2,38 +2,37 @@ package node
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/app/limiter" "github.com/Yuzuki616/V2bX/api/panel"
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core" "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/go-resty/resty/v2"
"github.com/goccy/go-json" "github.com/goccy/go-json"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/task"
"log" "log"
"math" "math"
"reflect" "reflect"
"runtime" "runtime"
"time" "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 { type Node struct {
server *core.Core server *core.Core
config *conf.ControllerConfig config *conf.ControllerConfig
clientInfo api.ClientInfo clientInfo panel.ClientInfo
apiClient api.API apiClient panel.Panel
nodeInfo *api.NodeInfo nodeInfo *panel.NodeInfo
Tag string Tag string
userList []api.UserInfo userList []panel.UserInfo
nodeInfoMonitorPeriodic *task.Periodic nodeInfoMonitorPeriodic *task.Periodic
userReportPeriodic *task.Periodic userReportPeriodic *task.Periodic
onlineIpReportPeriodic *task.Periodic onlineIpReportPeriodic *task.Periodic
} }
// New return a Node service with default parameters. // 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{ controller := &Node{
server: server, server: server,
config: config, config: config,
@ -280,7 +279,7 @@ func (c *Node) removeOldTag(oldtag string) (err error) {
return nil 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) inboundConfig, err := InboundBuilder(c.config, newNodeInfo, c.Tag)
if err != nil { if err != nil {
return err return err
@ -303,7 +302,7 @@ func (c *Node) addNewTag(newNodeInfo *api.NodeInfo) (err error) {
return nil 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) users := make([]*protocol.User, 0)
if nodeInfo.NodeType == "V2ray" { if nodeInfo.NodeType == "V2ray" {
if nodeInfo.EnableVless { if nodeInfo.EnableVless {
@ -333,7 +332,7 @@ func (c *Node) addNewUser(userInfo []api.UserInfo, nodeInfo *api.NodeInfo) (err
return nil 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{}{} tmp := map[string]struct{}{}
tmp2 := map[string]struct{}{} tmp2 := map[string]struct{}{}
for i := range old { for i := range old {
@ -363,11 +362,11 @@ func compareUserList(old, new []api.UserInfo) (deleted, added []api.UserInfo) {
func (c *Node) userInfoMonitor() (err error) { func (c *Node) userInfoMonitor() (err error) {
// Get User traffic // Get User traffic
userTraffic := make([]api.UserTraffic, 0) userTraffic := make([]panel.UserTraffic, 0)
for i := range c.userList { for i := range c.userList {
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i])) up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]))
if up > 0 || down > 0 { if up > 0 || down > 0 {
userTraffic = append(userTraffic, api.UserTraffic{ userTraffic = append(userTraffic, panel.UserTraffic{
UID: (c.userList)[i].UID, UID: (c.userList)[i].UID,
Upload: up, Upload: up,
Download: down}) 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)) log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp))
if rsp.StatusCode() == 200 { if rsp.StatusCode() == 200 {
onlineIp = []limiter.UserIp{} onlineIp = []dispatcher.UserIp{}
err := json.Unmarshal(rsp.Body(), onlineIp) err := json.Unmarshal(rsp.Body(), &onlineIp)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
c.server.ClearOnlineIps(c.Tag) c.server.ClearOnlineIps(c.Tag)

View File

@ -2,6 +2,7 @@ package node_test
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api/panel"
. "github.com/Yuzuki616/V2bX/node" . "github.com/Yuzuki616/V2bX/node"
"os" "os"
"os/signal" "os/signal"
@ -60,7 +61,7 @@ func TestController(t *testing.T) {
NodeID: 41, NodeID: 41,
NodeType: "V2ray", NodeType: "V2ray",
} }
apiclient := api.New(apiConfig) apiclient := panel.New(apiConfig)
c := New(server, apiclient, controlerconfig) c := New(server, apiclient, controlerconfig)
fmt.Println("Sleep 1s") fmt.Println("Sleep 1s")
err = c.Start() err = c.Start()

View File

@ -3,16 +3,16 @@ package node
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api/panel"
conf2 "github.com/Yuzuki616/V2bX/conf" conf2 "github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/api"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/core" "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/infra/conf" "github.com/xtls/xray-core/infra/conf"
) )
//OutboundBuilder build freedom outbund config for addoutbound //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 := &conf.OutboundDetourConfig{}
outboundDetourConfig.Protocol = "freedom" outboundDetourConfig.Protocol = "freedom"
outboundDetourConfig.Tag = tag outboundDetourConfig.Tag = tag

View File

@ -2,9 +2,9 @@ package node
import ( import (
"fmt" "fmt"
"github.com/Yuzuki616/V2bX/api/panel"
"strings" "strings"
"github.com/Yuzuki616/V2bX/api"
"github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/infra/conf" "github.com/xtls/xray-core/infra/conf"
@ -13,7 +13,7 @@ import (
"github.com/xtls/xray-core/proxy/vless" "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)) users = make([]*protocol.User, len(userInfo))
for i, user := range userInfo { for i, user := range userInfo {
users[i] = c.buildVmessUser(&user, serverAlterID) users[i] = c.buildVmessUser(&user, serverAlterID)
@ -21,7 +21,7 @@ func (c *Node) buildVmessUsers(userInfo []api.UserInfo, serverAlterID uint16) (u
return users 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{ vmessAccount := &conf.VMessAccount{
ID: userInfo.V2rayUser.Uuid, ID: userInfo.V2rayUser.Uuid,
AlterIds: serverAlterID, AlterIds: serverAlterID,
@ -35,7 +35,7 @@ func (c *Node) buildVmessUser(userInfo *api.UserInfo, serverAlterID uint16) (use
return user 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)) users = make([]*protocol.User, len(userInfo))
for i := range userInfo { for i := range userInfo {
users[i] = c.buildVlessUser(&(userInfo)[i]) users[i] = c.buildVlessUser(&(userInfo)[i])
@ -43,7 +43,7 @@ func (c *Node) buildVlessUsers(userInfo []api.UserInfo) (users []*protocol.User)
return users 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{ vlessAccount := &vless.Account{
Id: userInfo.V2rayUser.Uuid, Id: userInfo.V2rayUser.Uuid,
Flow: "xtls-rprx-direct", Flow: "xtls-rprx-direct",
@ -56,7 +56,7 @@ func (c *Node) buildVlessUser(userInfo *api.UserInfo) (user *protocol.User) {
return 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)) users = make([]*protocol.User, len(userInfo))
for i := range userInfo { for i := range userInfo {
users[i] = c.buildTrojanUser(&(userInfo)[i]) users[i] = c.buildTrojanUser(&(userInfo)[i])
@ -64,7 +64,7 @@ func (c *Node) buildTrojanUsers(userInfo []api.UserInfo) (users []*protocol.User
return users 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{ trojanAccount := &trojan.Account{
Password: userInfo.TrojanUser.Password, Password: userInfo.TrojanUser.Password,
Flow: "xtls-rprx-direct", 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)) users = make([]*protocol.User, len(userInfo))
for i := range userInfo { for i := range userInfo {
c.buildSSUser(&(userInfo)[i], cypher) c.buildSSUser(&(userInfo)[i], cypher)
@ -100,7 +100,7 @@ func (c *Node) buildSSUsers(userInfo []api.UserInfo, cypher shadowsocks.CipherTy
return users 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{ ssAccount := &shadowsocks.Account{
Password: userInfo.Secret, Password: userInfo.Secret,
CipherType: cypher, CipherType: cypher,
@ -113,6 +113,6 @@ func (c *Node) buildSSUser(userInfo *api.UserInfo, cypher shadowsocks.CipherType
return user 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) return fmt.Sprintf("%s|%s|%d", c.Tag, user.GetUserEmail(), user.UID)
} }