mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
update to v1.1.0
change to uniproxy api refactor build inbound refactor limiter and rule add ss2022 support add speedlimit support and more...
This commit is contained in:
@@ -1,108 +1,47 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/Yuzuki616/V2bX/api/panel"
|
||||
"github.com/Yuzuki616/V2bX/conf"
|
||||
"github.com/Yuzuki616/V2bX/node/controller/legoCmd"
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/uuid"
|
||||
"github.com/xtls/xray-core/core"
|
||||
coreConf "github.com/xtls/xray-core/infra/conf"
|
||||
)
|
||||
|
||||
// buildInbound build Inbound config for different protocol
|
||||
func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag string) (*core.InboundHandlerConfig, error) {
|
||||
var proxySetting interface{}
|
||||
if nodeInfo.NodeType == "V2ray" {
|
||||
defer func() {
|
||||
//Clear v2ray config
|
||||
nodeInfo.V2ray = nil
|
||||
}()
|
||||
if nodeInfo.EnableVless {
|
||||
//Set vless
|
||||
nodeInfo.V2ray.Inbounds[0].Protocol = "vless"
|
||||
if config.EnableFallback {
|
||||
// Set fallback
|
||||
fallbackConfigs, err := buildVlessFallbacks(config.FallBackConfigs)
|
||||
if err == nil {
|
||||
proxySetting = &coreConf.VLessInboundConfig{
|
||||
Decryption: "none",
|
||||
Fallbacks: fallbackConfigs,
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
proxySetting = &coreConf.VLessInboundConfig{
|
||||
Decryption: "none",
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Set vmess
|
||||
nodeInfo.V2ray.Inbounds[0].Protocol = "vmess"
|
||||
proxySetting = &coreConf.VMessInboundConfig{}
|
||||
}
|
||||
} else if nodeInfo.NodeType == "Trojan" {
|
||||
defer func() {
|
||||
//clear trojan and v2ray config
|
||||
nodeInfo.V2ray = nil
|
||||
nodeInfo.Trojan = nil
|
||||
}()
|
||||
nodeInfo.V2ray = &panel.V2rayConfig{}
|
||||
nodeInfo.V2ray.Inbounds = make([]coreConf.InboundDetourConfig, 1)
|
||||
nodeInfo.V2ray.Inbounds[0].Protocol = "trojan"
|
||||
if config.EnableFallback {
|
||||
// Set fallback
|
||||
fallbackConfigs, err := buildTrojanFallbacks(config.FallBackConfigs)
|
||||
if err == nil {
|
||||
proxySetting = &coreConf.TrojanServerConfig{
|
||||
Fallbacks: fallbackConfigs,
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
proxySetting = &coreConf.TrojanServerConfig{}
|
||||
}
|
||||
nodeInfo.V2ray.Inbounds[0].PortList = &coreConf.PortList{
|
||||
Range: []coreConf.PortRange{{From: uint32(nodeInfo.Trojan.LocalPort), To: uint32(nodeInfo.Trojan.LocalPort)}},
|
||||
}
|
||||
t := coreConf.TransportProtocol(nodeInfo.Trojan.TransportProtocol)
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting = &coreConf.StreamConfig{Network: &t}
|
||||
} else if nodeInfo.NodeType == "Shadowsocks" {
|
||||
defer func() {
|
||||
//Clear v2ray config
|
||||
nodeInfo.V2ray = nil
|
||||
}()
|
||||
nodeInfo.V2ray = &panel.V2rayConfig{}
|
||||
nodeInfo.V2ray.Inbounds = []coreConf.InboundDetourConfig{{Protocol: "shadowsocks"}}
|
||||
proxySetting = &coreConf.ShadowsocksServerConfig{}
|
||||
randomPasswd := uuid.New()
|
||||
defaultSSuser := &coreConf.ShadowsocksUserConfig{
|
||||
Cipher: "aes-128-gcm",
|
||||
Password: randomPasswd.String(),
|
||||
}
|
||||
proxySetting, _ := proxySetting.(*coreConf.ShadowsocksServerConfig)
|
||||
proxySetting.Users = append(proxySetting.Users, defaultSSuser)
|
||||
proxySetting.NetworkList = &coreConf.NetworkList{"tcp", "udp"}
|
||||
proxySetting.IVCheck = true
|
||||
if config.DisableIVCheck {
|
||||
proxySetting.IVCheck = false
|
||||
}
|
||||
nodeInfo.V2ray.Inbounds[0].PortList = &coreConf.PortList{
|
||||
Range: []coreConf.PortRange{{From: uint32(nodeInfo.SS.Port), To: uint32(nodeInfo.SS.Port)}},
|
||||
}
|
||||
t := coreConf.TransportProtocol(nodeInfo.SS.TransportProtocol)
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting = &coreConf.StreamConfig{Network: &t}
|
||||
} else {
|
||||
inbound := &coreConf.InboundDetourConfig{}
|
||||
// Set network protocol
|
||||
t := coreConf.TransportProtocol(nodeInfo.Network)
|
||||
inbound.StreamSetting = &coreConf.StreamConfig{Network: &t}
|
||||
var err error
|
||||
switch nodeInfo.NodeType {
|
||||
case "V2ray":
|
||||
err = buildV2ray(config, nodeInfo, inbound)
|
||||
case "Trojan":
|
||||
err = buildTrojan(config, nodeInfo, inbound)
|
||||
case "Shadowsocks":
|
||||
err = buildShadowsocks(config, nodeInfo, inbound)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported node type: %s, Only support: V2ray, Trojan, Shadowsocks", nodeInfo.NodeType)
|
||||
}
|
||||
// Build Listen IP address
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Set server port
|
||||
inbound.PortList = &coreConf.PortList{
|
||||
Range: []coreConf.PortRange{{From: uint32(nodeInfo.ServerPort), To: uint32(nodeInfo.ServerPort)}},
|
||||
}
|
||||
// Set Listen IP address
|
||||
ipAddress := net.ParseAddress(config.ListenIP)
|
||||
nodeInfo.V2ray.Inbounds[0].ListenOn = &coreConf.Address{Address: ipAddress}
|
||||
// SniffingConfig
|
||||
inbound.ListenOn = &coreConf.Address{Address: ipAddress}
|
||||
// Set SniffingConfig
|
||||
sniffingConfig := &coreConf.SniffingConfig{
|
||||
Enabled: true,
|
||||
DestOverride: &coreConf.StringList{"http", "tls"},
|
||||
@@ -110,31 +49,23 @@ func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
|
||||
if config.DisableSniffing {
|
||||
sniffingConfig.Enabled = false
|
||||
}
|
||||
nodeInfo.V2ray.Inbounds[0].SniffingConfig = sniffingConfig
|
||||
|
||||
var setting json.RawMessage
|
||||
|
||||
// Build Protocol and Protocol setting
|
||||
setting, err := json.Marshal(proxySetting)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal proxy %s config fialed: %s", nodeInfo.NodeType, err)
|
||||
}
|
||||
if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network == "tcp" {
|
||||
if nodeInfo.V2ray.Inbounds[0].StreamSetting.TCPSettings != nil {
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.TCPSettings.AcceptProxyProtocol = config.EnableProxyProtocol
|
||||
inbound.SniffingConfig = sniffingConfig
|
||||
if nodeInfo.NodeType == "tcp" {
|
||||
if inbound.StreamSetting.TCPSettings != nil {
|
||||
inbound.StreamSetting.TCPSettings.AcceptProxyProtocol = config.EnableProxyProtocol
|
||||
} else {
|
||||
tcpSetting := &coreConf.TCPConfig{
|
||||
AcceptProxyProtocol: config.EnableProxyProtocol,
|
||||
} //Enable proxy protocol
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.TCPSettings = tcpSetting
|
||||
inbound.StreamSetting.TCPSettings = tcpSetting
|
||||
}
|
||||
} else if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network == "ws" {
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.WSSettings = &coreConf.WebSocketConfig{
|
||||
} else if nodeInfo.NodeType == "ws" {
|
||||
inbound.StreamSetting.WSSettings = &coreConf.WebSocketConfig{
|
||||
AcceptProxyProtocol: config.EnableProxyProtocol} //Enable proxy protocol
|
||||
}
|
||||
// Build TLS and XTLS settings
|
||||
// Set TLS and XTLS settings
|
||||
if nodeInfo.EnableTls && config.CertConfig.CertMode != "none" {
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.Security = nodeInfo.TLSType
|
||||
inbound.StreamSetting.Security = nodeInfo.TLSType
|
||||
certFile, keyFile, err := getCertFile(config.CertConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -144,7 +75,7 @@ func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
|
||||
RejectUnknownSNI: config.CertConfig.RejectUnknownSni,
|
||||
}
|
||||
tlsSettings.Certs = append(tlsSettings.Certs, &coreConf.TLSCertConfig{CertFile: certFile, KeyFile: keyFile, OcspStapling: 3600})
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.TLSSettings = tlsSettings
|
||||
inbound.StreamSetting.TLSSettings = tlsSettings
|
||||
} else if nodeInfo.TLSType == "xtls" {
|
||||
xtlsSettings := &coreConf.XTLSConfig{
|
||||
RejectUnknownSNI: config.CertConfig.RejectUnknownSni,
|
||||
@@ -153,23 +84,139 @@ func buildInbound(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, tag s
|
||||
CertFile: certFile,
|
||||
KeyFile: keyFile,
|
||||
OcspStapling: 3600})
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.XTLSSettings = xtlsSettings
|
||||
inbound.StreamSetting.XTLSSettings = xtlsSettings
|
||||
}
|
||||
} else if nodeInfo.NodeType == "V2ray" {
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.Security = "none"
|
||||
}
|
||||
// Support ProxyProtocol for any transport protocol
|
||||
if *nodeInfo.V2ray.Inbounds[0].StreamSetting.Network != "tcp" &&
|
||||
*nodeInfo.V2ray.Inbounds[0].StreamSetting.Network != "ws" &&
|
||||
if *inbound.StreamSetting.Network != "tcp" &&
|
||||
*inbound.StreamSetting.Network != "ws" &&
|
||||
config.EnableProxyProtocol {
|
||||
sockoptConfig := &coreConf.SocketConfig{
|
||||
AcceptProxyProtocol: config.EnableProxyProtocol,
|
||||
} //Enable proxy protocol
|
||||
nodeInfo.V2ray.Inbounds[0].StreamSetting.SocketSettings = sockoptConfig
|
||||
inbound.StreamSetting.SocketSettings = sockoptConfig
|
||||
}
|
||||
nodeInfo.V2ray.Inbounds[0].Settings = &setting
|
||||
nodeInfo.V2ray.Inbounds[0].Tag = tag
|
||||
return nodeInfo.V2ray.Inbounds[0].Build()
|
||||
inbound.Tag = tag
|
||||
return inbound.Build()
|
||||
}
|
||||
|
||||
func buildV2ray(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
|
||||
if nodeInfo.EnableVless {
|
||||
//Set vless
|
||||
inbound.Protocol = "vless"
|
||||
if config.EnableFallback {
|
||||
// Set fallback
|
||||
fallbackConfigs, err := buildVlessFallbacks(config.FallBackConfigs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s, err := json.Marshal(&coreConf.VLessInboundConfig{
|
||||
Decryption: "none",
|
||||
Fallbacks: fallbackConfigs,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal vless fallback config error: %s", err)
|
||||
}
|
||||
inbound.Settings = (*json.RawMessage)(&s)
|
||||
} else {
|
||||
var err error
|
||||
s, err := json.Marshal(&coreConf.VLessInboundConfig{
|
||||
Decryption: "none",
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal vless config error: %s", err)
|
||||
}
|
||||
inbound.Settings = (*json.RawMessage)(&s)
|
||||
}
|
||||
} else {
|
||||
// Set vmess
|
||||
inbound.Protocol = "vmess"
|
||||
var err error
|
||||
s, err := json.Marshal(&coreConf.VMessInboundConfig{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal vmess settings error: %s", err)
|
||||
}
|
||||
inbound.Settings = (*json.RawMessage)(&s)
|
||||
}
|
||||
switch nodeInfo.Network {
|
||||
case "tcp":
|
||||
err := json.Unmarshal(nodeInfo.NetworkSettings, &inbound.StreamSetting.TCPSettings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshal tcp settings error: %s", err)
|
||||
}
|
||||
case "ws":
|
||||
err := json.Unmarshal(nodeInfo.NetworkSettings, &inbound.StreamSetting.WSSettings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshal ws settings error: %s", err)
|
||||
}
|
||||
case "grpc":
|
||||
err := json.Unmarshal(nodeInfo.NetworkSettings, &inbound.StreamSetting.GRPCConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshal grpc settings error: %s", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildTrojan(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
|
||||
inbound.Protocol = "trojan"
|
||||
if config.EnableFallback {
|
||||
// Set fallback
|
||||
fallbackConfigs, err := buildTrojanFallbacks(config.FallBackConfigs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s, err := json.Marshal(&coreConf.TrojanServerConfig{
|
||||
Fallbacks: fallbackConfigs,
|
||||
})
|
||||
inbound.Settings = (*json.RawMessage)(&s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal trojan fallback config error: %s", err)
|
||||
}
|
||||
} else {
|
||||
s := []byte("{}")
|
||||
inbound.Settings = (*json.RawMessage)(&s)
|
||||
}
|
||||
t := coreConf.TransportProtocol(nodeInfo.Network)
|
||||
inbound.StreamSetting = &coreConf.StreamConfig{Network: &t}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildShadowsocks(config *conf.ControllerConfig, nodeInfo *panel.NodeInfo, inbound *coreConf.InboundDetourConfig) error {
|
||||
inbound.Protocol = "shadowsocks"
|
||||
settings := &coreConf.ShadowsocksServerConfig{
|
||||
Cipher: nodeInfo.Cipher,
|
||||
}
|
||||
p := make([]byte, 32)
|
||||
_, err := rand.Read(p)
|
||||
if err != nil {
|
||||
return fmt.Errorf("generate random password error: %s", err)
|
||||
}
|
||||
randomPasswd := hex.EncodeToString(p)
|
||||
cipher := nodeInfo.Cipher
|
||||
if nodeInfo.ServerKey != "" {
|
||||
settings.Password = nodeInfo.ServerKey
|
||||
randomPasswd = base64.StdEncoding.EncodeToString([]byte(randomPasswd))
|
||||
cipher = ""
|
||||
}
|
||||
defaultSSuser := &coreConf.ShadowsocksUserConfig{
|
||||
Cipher: cipher,
|
||||
Password: randomPasswd,
|
||||
}
|
||||
settings.Users = append(settings.Users, defaultSSuser)
|
||||
settings.NetworkList = &coreConf.NetworkList{"tcp", "udp"}
|
||||
settings.IVCheck = true
|
||||
if config.DisableIVCheck {
|
||||
settings.IVCheck = false
|
||||
}
|
||||
t := coreConf.TransportProtocol("tcp")
|
||||
inbound.StreamSetting = &coreConf.StreamConfig{Network: &t}
|
||||
s, err := json.Marshal(settings)
|
||||
inbound.Settings = (*json.RawMessage)(&s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal shadowsocks settings error: %s", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCertFile(certConfig *conf.CertConfig) (certFile string, keyFile string, err error) {
|
||||
@@ -199,7 +246,6 @@ func getCertFile(certConfig *conf.CertConfig) (certFile string, keyFile string,
|
||||
}
|
||||
return certPath, keyPath, err
|
||||
}
|
||||
|
||||
return "", "", fmt.Errorf("unsupported certmode: %s", certConfig.CertMode)
|
||||
}
|
||||
|
||||
@@ -207,14 +253,11 @@ func buildVlessFallbacks(fallbackConfigs []*conf.FallBackConfig) ([]*coreConf.VL
|
||||
if fallbackConfigs == nil {
|
||||
return nil, fmt.Errorf("you must provide FallBackConfigs")
|
||||
}
|
||||
|
||||
vlessFallBacks := make([]*coreConf.VLessInboundFallback, len(fallbackConfigs))
|
||||
for i, c := range fallbackConfigs {
|
||||
|
||||
if c.Dest == "" {
|
||||
return nil, fmt.Errorf("dest is required for fallback fialed")
|
||||
}
|
||||
|
||||
var dest json.RawMessage
|
||||
dest, err := json.Marshal(c.Dest)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,11 +3,11 @@ package cmd
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Yuzuki616/V2bX/node/controller/legoCmd/log"
|
||||
"github.com/goccy/go-json"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
@@ -3,8 +3,8 @@ package cmd
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"github.com/Yuzuki616/V2bX/node/controller/legoCmd/log"
|
||||
"github.com/goccy/go-json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/goccy/go-json"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
|
||||
type Node struct {
|
||||
server *core.Core
|
||||
config *conf.ControllerConfig
|
||||
clientInfo panel.ClientInfo
|
||||
apiClient panel.Panel
|
||||
nodeInfo *panel.NodeInfo
|
||||
@@ -25,14 +24,15 @@ type Node struct {
|
||||
userReportPeriodic *task.Periodic
|
||||
onlineIpReportPeriodic *task.Periodic
|
||||
DynamicSpeedLimitPeriodic *task.Periodic
|
||||
*conf.ControllerConfig
|
||||
}
|
||||
|
||||
// New return a Node service with default parameters.
|
||||
func New(server *core.Core, api panel.Panel, config *conf.ControllerConfig) *Node {
|
||||
controller := &Node{
|
||||
server: server,
|
||||
config: config,
|
||||
apiClient: api,
|
||||
server: server,
|
||||
ControllerConfig: config,
|
||||
apiClient: api,
|
||||
}
|
||||
return controller
|
||||
}
|
||||
@@ -64,71 +64,67 @@ func (c *Node) Start() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.server.AddInboundLimiter(c.Tag, c.nodeInfo); err != nil {
|
||||
if err := c.server.AddInboundLimiter(c.Tag, c.nodeInfo, c.userList); err != nil {
|
||||
return fmt.Errorf("add inbound limiter failed: %s", err)
|
||||
}
|
||||
// Add Rule Manager
|
||||
if !c.config.DisableGetRule {
|
||||
if ruleList, err := c.apiClient.GetNodeRule(); err != nil {
|
||||
log.Printf("Get rule list filed: %s", err)
|
||||
} else if ruleList != nil {
|
||||
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
|
||||
log.Printf("Update rule filed: %s", err)
|
||||
}
|
||||
if !c.DisableGetRule {
|
||||
if err := c.server.UpdateRule(c.Tag, newNodeInfo.Rules); err != nil {
|
||||
log.Printf("Update rule filed: %s", err)
|
||||
}
|
||||
}
|
||||
// fetch node info task
|
||||
c.nodeInfoMonitorPeriodic = &task.Periodic{
|
||||
Interval: time.Duration(c.config.UpdatePeriodic) * time.Second,
|
||||
Interval: time.Duration(c.nodeInfo.BaseConfig.PullInterval.(int)) * time.Second,
|
||||
Execute: c.nodeInfoMonitor,
|
||||
}
|
||||
// fetch user list task
|
||||
c.userReportPeriodic = &task.Periodic{
|
||||
Interval: time.Duration(c.config.UpdatePeriodic) * time.Second,
|
||||
Interval: time.Duration(c.nodeInfo.BaseConfig.PushInterval.(int)) * time.Second,
|
||||
Execute: c.reportUserTraffic,
|
||||
}
|
||||
log.Printf("[%s: %d] Start monitor node status", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
||||
// delay to start nodeInfoMonitor
|
||||
go func() {
|
||||
time.Sleep(time.Duration(c.config.UpdatePeriodic) * time.Second)
|
||||
time.Sleep(time.Duration(c.nodeInfo.BaseConfig.PullInterval.(int)) * time.Second)
|
||||
_ = c.nodeInfoMonitorPeriodic.Start()
|
||||
}()
|
||||
|
||||
log.Printf("[%s: %d] Start report node status", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
||||
// delay to start userReport
|
||||
go func() {
|
||||
time.Sleep(time.Duration(c.config.UpdatePeriodic) * time.Second)
|
||||
time.Sleep(time.Duration(c.nodeInfo.BaseConfig.PushInterval.(int)) * time.Second)
|
||||
_ = c.userReportPeriodic.Start()
|
||||
}()
|
||||
if c.config.EnableDynamicSpeedLimit {
|
||||
if c.EnableDynamicSpeedLimit {
|
||||
// Check dynamic speed limit task
|
||||
c.DynamicSpeedLimitPeriodic = &task.Periodic{
|
||||
Interval: time.Duration(c.config.DynamicSpeedLimitConfig.Periodic) * time.Second,
|
||||
Interval: time.Duration(c.DynamicSpeedLimitConfig.Periodic) * time.Second,
|
||||
Execute: c.dynamicSpeedLimit,
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(time.Duration(c.config.DynamicSpeedLimitConfig.Periodic) * time.Second)
|
||||
time.Sleep(time.Duration(c.DynamicSpeedLimitConfig.Periodic) * time.Second)
|
||||
_ = c.DynamicSpeedLimitPeriodic.Start()
|
||||
}()
|
||||
log.Printf("[%s: %d] Start dynamic speed limit", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
||||
}
|
||||
if c.config.EnableIpRecorder {
|
||||
switch c.config.IpRecorderConfig.Type {
|
||||
if c.EnableIpRecorder {
|
||||
switch c.IpRecorderConfig.Type {
|
||||
case "Recorder":
|
||||
c.ipRecorder = iprecoder.NewRecorder(c.config.IpRecorderConfig.RecorderConfig)
|
||||
c.ipRecorder = iprecoder.NewRecorder(c.IpRecorderConfig.RecorderConfig)
|
||||
case "Redis":
|
||||
c.ipRecorder = iprecoder.NewRedis(c.config.IpRecorderConfig.RedisConfig)
|
||||
c.ipRecorder = iprecoder.NewRedis(c.IpRecorderConfig.RedisConfig)
|
||||
default:
|
||||
log.Printf("recorder type: %s is not vail, disable recorder", c.config.IpRecorderConfig.Type)
|
||||
log.Printf("recorder type: %s is not vail, disable recorder", c.IpRecorderConfig.Type)
|
||||
return nil
|
||||
}
|
||||
// report and fetch online ip list task
|
||||
c.onlineIpReportPeriodic = &task.Periodic{
|
||||
Interval: time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second,
|
||||
Interval: time.Duration(c.IpRecorderConfig.Periodic) * time.Second,
|
||||
Execute: c.reportOnlineIp,
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(time.Duration(c.config.IpRecorderConfig.Periodic) * time.Second)
|
||||
time.Sleep(time.Duration(c.IpRecorderConfig.Periodic) * time.Second)
|
||||
_ = c.onlineIpReportPeriodic.Start()
|
||||
}()
|
||||
log.Printf("[%s: %d] Start report online ip", c.nodeInfo.NodeType, c.nodeInfo.NodeId)
|
||||
@@ -144,7 +140,6 @@ func (c *Node) Close() error {
|
||||
log.Panicf("node info periodic close failed: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if c.nodeInfoMonitorPeriodic != nil {
|
||||
err := c.userReportPeriodic.Close()
|
||||
if err != nil {
|
||||
@@ -167,5 +162,5 @@ func (c *Node) Close() error {
|
||||
}
|
||||
|
||||
func (c *Node) buildNodeTag() string {
|
||||
return fmt.Sprintf("%s_%s_%d", c.nodeInfo.NodeType, c.config.ListenIP, c.nodeInfo.NodeId)
|
||||
return fmt.Sprintf("%s_%s_%d", c.nodeInfo.NodeType, c.ListenIP, c.nodeInfo.NodeId)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Yuzuki616/V2bX/api/panel"
|
||||
conf2 "github.com/Yuzuki616/V2bX/conf"
|
||||
"github.com/goccy/go-json"
|
||||
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/core"
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/Yuzuki616/V2bX/node/controller/legoCmd"
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"log"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -21,53 +21,41 @@ func (c *Node) nodeInfoMonitor() (err error) {
|
||||
var nodeInfoChanged = false
|
||||
// If nodeInfo changed
|
||||
if newNodeInfo != nil {
|
||||
if c.nodeInfo.SS == nil || !reflect.DeepEqual(c.nodeInfo.SS, newNodeInfo.SS) {
|
||||
// Remove old tag
|
||||
oldTag := c.Tag
|
||||
err := c.removeOldTag(oldTag)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
// Add new tag
|
||||
c.nodeInfo = newNodeInfo
|
||||
c.Tag = c.buildNodeTag()
|
||||
err = c.addNewTag(newNodeInfo)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
nodeInfoChanged = true
|
||||
// Remove Old limiter
|
||||
if err = c.server.DeleteInboundLimiter(oldTag); err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
// Remove old tag
|
||||
oldTag := c.Tag
|
||||
err := c.removeOldTag(oldTag)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
// Add new tag
|
||||
c.nodeInfo = newNodeInfo
|
||||
c.Tag = c.buildNodeTag()
|
||||
err = c.addNewTag(newNodeInfo)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
nodeInfoChanged = true
|
||||
// Remove Old limiter
|
||||
if err = c.server.DeleteInboundLimiter(oldTag); err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
if err := c.server.UpdateRule(c.Tag, newNodeInfo.Rules); err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Check Rule
|
||||
if !c.config.DisableGetRule {
|
||||
if ruleList, err := c.apiClient.GetNodeRule(); err != nil {
|
||||
log.Printf("Get rule list filed: %s", err)
|
||||
} else if ruleList != nil {
|
||||
if err := c.server.UpdateRule(c.Tag, ruleList); err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Check Cert
|
||||
if c.nodeInfo.EnableTls && c.config.CertConfig.CertMode != "none" &&
|
||||
(c.config.CertConfig.CertMode == "dns" || c.config.CertConfig.CertMode == "http") {
|
||||
if c.nodeInfo.EnableTls && c.CertConfig.CertMode != "none" &&
|
||||
(c.CertConfig.CertMode == "dns" || c.CertConfig.CertMode == "http") {
|
||||
lego, err := legoCmd.New()
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
// 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)
|
||||
_, _, err = lego.RenewCert(c.CertConfig.CertDomain, c.CertConfig.Email,
|
||||
c.CertConfig.CertMode, c.CertConfig.Provider, c.CertConfig.DNSEnv)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
@@ -80,27 +68,42 @@ func (c *Node) nodeInfoMonitor() (err error) {
|
||||
}
|
||||
if nodeInfoChanged {
|
||||
c.userList = newUserInfo
|
||||
newUserInfo = nil
|
||||
err = c.addNewUser(c.userList, newNodeInfo)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
newNodeInfo = nil
|
||||
// Add Limiter
|
||||
if err := c.server.AddInboundLimiter(c.Tag, c.nodeInfo); err != nil {
|
||||
if err := c.server.AddInboundLimiter(c.Tag, newNodeInfo, newUserInfo); err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
}
|
||||
runtime.GC()
|
||||
// Check interval
|
||||
if c.nodeInfoMonitorPeriodic.Interval != time.Duration(newNodeInfo.BaseConfig.PullInterval.(int))*time.Second {
|
||||
c.nodeInfoMonitorPeriodic.Interval = time.Duration(newNodeInfo.BaseConfig.PullInterval.(int)) * time.Second
|
||||
_ = c.nodeInfoMonitorPeriodic.Close()
|
||||
go func() {
|
||||
time.Sleep(c.nodeInfoMonitorPeriodic.Interval)
|
||||
_ = c.nodeInfoMonitorPeriodic.Start()
|
||||
}()
|
||||
}
|
||||
if c.userReportPeriodic.Interval != time.Duration(newNodeInfo.BaseConfig.PushInterval.(int))*time.Second {
|
||||
c.userReportPeriodic.Interval = time.Duration(newNodeInfo.BaseConfig.PushInterval.(int)) * time.Second
|
||||
_ = c.userReportPeriodic.Close()
|
||||
go func() {
|
||||
time.Sleep(c.userReportPeriodic.Interval)
|
||||
_ = c.userReportPeriodic.Start()
|
||||
}()
|
||||
}
|
||||
} else {
|
||||
deleted, added := compareUserList(c.userList, newUserInfo)
|
||||
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)
|
||||
deletedEmail[i] = fmt.Sprintf("%s|%s|%d",
|
||||
c.Tag,
|
||||
(deleted)[i].Uuid,
|
||||
(deleted)[i].Id)
|
||||
}
|
||||
err := c.server.RemoveUsers(deletedEmail, c.Tag)
|
||||
if err != nil {
|
||||
@@ -114,16 +117,14 @@ func (c *Node) nodeInfoMonitor() (err error) {
|
||||
}
|
||||
}
|
||||
if len(added) > 0 || len(deleted) > 0 {
|
||||
defer runtime.GC()
|
||||
// Update Limiter
|
||||
if err := c.server.UpdateInboundLimiter(c.Tag, deleted); err != nil {
|
||||
if err := c.server.UpdateInboundLimiter(c.Tag, added, deleted); err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
}
|
||||
log.Printf("[%s: %d] %d user deleted, %d user added", c.nodeInfo.NodeType, c.nodeInfo.NodeId,
|
||||
len(deleted), len(added))
|
||||
c.userList = newUserInfo
|
||||
newUserInfo = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -141,24 +142,21 @@ func (c *Node) removeOldTag(oldTag string) (err error) {
|
||||
}
|
||||
|
||||
func (c *Node) addNewTag(newNodeInfo *panel.NodeInfo) (err error) {
|
||||
inboundConfig, err := buildInbound(c.config, newNodeInfo, c.Tag)
|
||||
inboundConfig, err := buildInbound(c.ControllerConfig, newNodeInfo, c.Tag)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("build inbound error: %s", err)
|
||||
}
|
||||
err = c.server.AddInbound(inboundConfig)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
return fmt.Errorf("add inbound error: %s", err)
|
||||
}
|
||||
outBoundConfig, err := buildOutbound(c.config, newNodeInfo, c.Tag)
|
||||
outBoundConfig, err := buildOutbound(c.ControllerConfig, newNodeInfo, c.Tag)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
return fmt.Errorf("build outbound error: %s", err)
|
||||
}
|
||||
err = c.server.AddOutbound(outBoundConfig)
|
||||
if err != nil {
|
||||
|
||||
return err
|
||||
return fmt.Errorf("add outbound error: %s", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -174,13 +172,13 @@ func (c *Node) addNewUser(userInfo []panel.UserInfo, nodeInfo *panel.NodeInfo) (
|
||||
} else if nodeInfo.NodeType == "Trojan" {
|
||||
users = c.buildTrojanUsers(userInfo)
|
||||
} else if nodeInfo.NodeType == "Shadowsocks" {
|
||||
users = c.buildSSUsers(userInfo, getCipherFromString(nodeInfo.SS.CypherMethod))
|
||||
users = c.buildSSUsers(userInfo, getCipherFromString(nodeInfo.Cipher))
|
||||
} else {
|
||||
return fmt.Errorf("unsupported node type: %s", nodeInfo.NodeType)
|
||||
}
|
||||
err = c.server.AddUsers(users, c.Tag)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("add users error: %s", err)
|
||||
}
|
||||
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userInfo))
|
||||
return nil
|
||||
@@ -190,24 +188,24 @@ func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo
|
||||
tmp := map[string]struct{}{}
|
||||
tmp2 := map[string]struct{}{}
|
||||
for i := range old {
|
||||
tmp[(old)[i].GetUserEmail()] = struct{}{}
|
||||
tmp[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
|
||||
}
|
||||
l := len(tmp)
|
||||
for i := range new {
|
||||
e := (new)[i].GetUserEmail()
|
||||
e := new[i].Uuid + strconv.Itoa(new[i].SpeedLimit)
|
||||
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{}{}
|
||||
tmp2[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
|
||||
if l != len(tmp2) {
|
||||
deleted = append(deleted, (old)[i])
|
||||
deleted = append(deleted, old[i])
|
||||
l++
|
||||
}
|
||||
}
|
||||
@@ -220,16 +218,16 @@ func (c *Node) reportUserTraffic() (err error) {
|
||||
for i := range c.userList {
|
||||
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]), true)
|
||||
if up > 0 || down > 0 {
|
||||
if c.config.EnableDynamicSpeedLimit {
|
||||
if c.EnableDynamicSpeedLimit {
|
||||
c.userList[i].Traffic += up + down
|
||||
}
|
||||
userTraffic = append(userTraffic, panel.UserTraffic{
|
||||
UID: (c.userList)[i].UID,
|
||||
UID: (c.userList)[i].Id,
|
||||
Upload: up,
|
||||
Download: down})
|
||||
}
|
||||
}
|
||||
if len(userTraffic) > 0 && !c.config.DisableUploadTraffic {
|
||||
if len(userTraffic) > 0 && !c.DisableUploadTraffic {
|
||||
err = c.apiClient.ReportUserTraffic(userTraffic)
|
||||
if err != nil {
|
||||
log.Printf("Report user traffic faild: %s", err)
|
||||
@@ -238,7 +236,7 @@ func (c *Node) reportUserTraffic() (err error) {
|
||||
}
|
||||
}
|
||||
userTraffic = nil
|
||||
if !c.config.EnableIpRecorder {
|
||||
if !c.EnableIpRecorder {
|
||||
c.server.ClearOnlineIp(c.Tag)
|
||||
}
|
||||
runtime.GC()
|
||||
@@ -256,7 +254,7 @@ func (c *Node) reportOnlineIp() (err error) {
|
||||
log.Print("Report online ip error: ", err)
|
||||
c.server.ClearOnlineIp(c.Tag)
|
||||
}
|
||||
if c.config.IpRecorderConfig.EnableIpSync {
|
||||
if c.IpRecorderConfig.EnableIpSync {
|
||||
c.server.UpdateOnlineIp(c.Tag, onlineIp)
|
||||
log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp))
|
||||
}
|
||||
@@ -265,14 +263,14 @@ func (c *Node) reportOnlineIp() (err error) {
|
||||
}
|
||||
|
||||
func (c *Node) dynamicSpeedLimit() error {
|
||||
if c.config.EnableDynamicSpeedLimit {
|
||||
if c.EnableDynamicSpeedLimit {
|
||||
for i := range c.userList {
|
||||
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]), false)
|
||||
if c.userList[i].Traffic+down+up/1024/1024 > c.config.DynamicSpeedLimitConfig.Traffic {
|
||||
if c.userList[i].Traffic+down+up/1024/1024 > c.DynamicSpeedLimitConfig.Traffic {
|
||||
err := c.server.AddUserSpeedLimit(c.Tag,
|
||||
&c.userList[i],
|
||||
c.config.DynamicSpeedLimitConfig.SpeedLimit,
|
||||
time.Now().Add(time.Second*time.Duration(c.config.DynamicSpeedLimitConfig.ExpireTime)).Unix())
|
||||
c.DynamicSpeedLimitConfig.SpeedLimit,
|
||||
time.Now().Add(time.Second*time.Duration(c.DynamicSpeedLimitConfig.ExpireTime)).Unix())
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Yuzuki616/V2bX/api/panel"
|
||||
"strings"
|
||||
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"github.com/xtls/xray-core/common/serial"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/proxy/shadowsocks"
|
||||
"github.com/xtls/xray-core/proxy/shadowsocks_2022"
|
||||
"github.com/xtls/xray-core/proxy/trojan"
|
||||
"github.com/xtls/xray-core/proxy/vless"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (c *Node) buildVmessUsers(userInfo []panel.UserInfo) (users []*protocol.User) {
|
||||
@@ -23,7 +23,7 @@ func (c *Node) buildVmessUsers(userInfo []panel.UserInfo) (users []*protocol.Use
|
||||
|
||||
func (c *Node) buildVmessUser(userInfo *panel.UserInfo, serverAlterID uint16) (user *protocol.User) {
|
||||
vmessAccount := &conf.VMessAccount{
|
||||
ID: userInfo.V2rayUser.Uuid,
|
||||
ID: userInfo.Uuid,
|
||||
AlterIds: serverAlterID,
|
||||
Security: "auto",
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func (c *Node) buildVlessUsers(userInfo []panel.UserInfo) (users []*protocol.Use
|
||||
|
||||
func (c *Node) buildVlessUser(userInfo *panel.UserInfo) (user *protocol.User) {
|
||||
vlessAccount := &vless.Account{
|
||||
Id: userInfo.V2rayUser.Uuid,
|
||||
Id: userInfo.Uuid,
|
||||
Flow: "xtls-rprx-direct",
|
||||
}
|
||||
return &protocol.User{
|
||||
@@ -64,7 +64,7 @@ func (c *Node) buildTrojanUsers(userInfo []panel.UserInfo) (users []*protocol.Us
|
||||
|
||||
func (c *Node) buildTrojanUser(userInfo *panel.UserInfo) (user *protocol.User) {
|
||||
trojanAccount := &trojan.Account{
|
||||
Password: userInfo.TrojanUser.Password,
|
||||
Password: userInfo.Uuid,
|
||||
Flow: "xtls-rprx-direct",
|
||||
}
|
||||
return &protocol.User{
|
||||
@@ -98,17 +98,28 @@ func (c *Node) buildSSUsers(userInfo []panel.UserInfo, cypher shadowsocks.Cipher
|
||||
}
|
||||
|
||||
func (c *Node) buildSSUser(userInfo *panel.UserInfo, cypher shadowsocks.CipherType) (user *protocol.User) {
|
||||
ssAccount := &shadowsocks.Account{
|
||||
Password: userInfo.Secret,
|
||||
CipherType: cypher,
|
||||
}
|
||||
return &protocol.User{
|
||||
Level: 0,
|
||||
Email: c.buildUserTag(userInfo),
|
||||
Account: serial.ToTypedMessage(ssAccount),
|
||||
if c.nodeInfo.ServerKey == "" {
|
||||
ssAccount := &shadowsocks.Account{
|
||||
Password: userInfo.Uuid,
|
||||
CipherType: cypher,
|
||||
}
|
||||
return &protocol.User{
|
||||
Level: 0,
|
||||
Email: c.buildUserTag(userInfo),
|
||||
Account: serial.ToTypedMessage(ssAccount),
|
||||
}
|
||||
} else {
|
||||
ssAccount := &shadowsocks_2022.User{
|
||||
Key: userInfo.Uuid,
|
||||
}
|
||||
return &protocol.User{
|
||||
Level: 0,
|
||||
Email: c.buildUserTag(userInfo),
|
||||
Account: serial.ToTypedMessage(ssAccount),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.Uuid, user.Id)
|
||||
}
|
||||
|
||||
@@ -18,9 +18,13 @@ func New() *Node {
|
||||
func (n *Node) Start(nodes []*conf.NodeConfig, core *core.Core) error {
|
||||
n.controllers = make([]*controller.Node, len(nodes))
|
||||
for i, c := range nodes {
|
||||
p, err := panel.New(c.ApiConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Register controller service
|
||||
n.controllers[i] = controller.New(core, panel.New(c.ApiConfig), c.ControllerConfig)
|
||||
err := n.controllers[i].Start()
|
||||
n.controllers[i] = controller.New(core, p, c.ControllerConfig)
|
||||
err = n.controllers[i].Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user