2022-10-09 23:31:15 -04:00
|
|
|
package controller
|
2022-09-14 21:55:11 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/Yuzuki616/V2bX/api/panel"
|
2022-10-09 23:31:15 -04:00
|
|
|
"github.com/Yuzuki616/V2bX/node/controller/legoCmd"
|
2022-09-14 21:55:11 -04:00
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
2022-12-18 10:31:06 -05:00
|
|
|
"strconv"
|
2022-09-14 21:55:11 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Node) nodeInfoMonitor() (err error) {
|
|
|
|
// First fetch Node Info
|
|
|
|
newNodeInfo, err := c.apiClient.GetNodeInfo()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var nodeInfoChanged = false
|
|
|
|
// If nodeInfo changed
|
|
|
|
if newNodeInfo != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
// Remove old tag
|
|
|
|
oldTag := c.Tag
|
|
|
|
err := c.removeOldTag(oldTag)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
// 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)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check Cert
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.nodeInfo.EnableTls && c.CertConfig.CertMode != "none" &&
|
|
|
|
(c.CertConfig.CertMode == "dns" || c.CertConfig.CertMode == "http") {
|
2022-09-14 21:55:11 -04:00
|
|
|
lego, err := legoCmd.New()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
// Core-core supports the OcspStapling certification hot renew
|
2022-12-18 10:31:06 -05:00
|
|
|
_, _, err = lego.RenewCert(c.CertConfig.CertDomain, c.CertConfig.Email,
|
|
|
|
c.CertConfig.CertMode, c.CertConfig.Provider, c.CertConfig.DNSEnv)
|
2022-09-14 21:55:11 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update User
|
|
|
|
newUserInfo, err := c.apiClient.GetUserList()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if nodeInfoChanged {
|
|
|
|
c.userList = newUserInfo
|
|
|
|
err = c.addNewUser(c.userList, newNodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Add Limiter
|
2022-12-18 10:31:06 -05:00
|
|
|
if err := c.server.AddInboundLimiter(c.Tag, newNodeInfo, newUserInfo); err != nil {
|
2022-09-14 21:55:11 -04:00
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
// 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()
|
|
|
|
}()
|
|
|
|
}
|
2022-09-14 21:55:11 -04:00
|
|
|
} else {
|
|
|
|
deleted, added := compareUserList(c.userList, newUserInfo)
|
|
|
|
if len(deleted) > 0 {
|
|
|
|
deletedEmail := make([]string, len(deleted))
|
|
|
|
for i := range deleted {
|
2022-12-18 10:31:06 -05:00
|
|
|
deletedEmail[i] = fmt.Sprintf("%s|%s|%d",
|
|
|
|
c.Tag,
|
|
|
|
(deleted)[i].Uuid,
|
|
|
|
(deleted)[i].Id)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
err := c.server.RemoveUsers(deletedEmail, c.Tag)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(added) > 0 {
|
|
|
|
err = c.addNewUser(added, c.nodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(added) > 0 || len(deleted) > 0 {
|
|
|
|
// Update Limiter
|
2022-12-18 10:31:06 -05:00
|
|
|
if err := c.server.UpdateInboundLimiter(c.Tag, added, deleted); err != nil {
|
2022-09-14 21:55:11 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Node) removeOldTag(oldTag string) (err error) {
|
|
|
|
err = c.server.RemoveInbound(oldTag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.server.RemoveOutbound(oldTag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Node) addNewTag(newNodeInfo *panel.NodeInfo) (err error) {
|
2022-12-18 10:31:06 -05:00
|
|
|
inboundConfig, err := buildInbound(c.ControllerConfig, newNodeInfo, c.Tag)
|
2022-09-14 21:55:11 -04:00
|
|
|
if err != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Errorf("build inbound error: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
err = c.server.AddInbound(inboundConfig)
|
|
|
|
if err != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Errorf("add inbound error: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
outBoundConfig, err := buildOutbound(c.ControllerConfig, newNodeInfo, c.Tag)
|
2022-09-14 21:55:11 -04:00
|
|
|
if err != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Errorf("build outbound error: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
err = c.server.AddOutbound(outBoundConfig)
|
|
|
|
if err != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Errorf("add outbound error: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Node) addNewUser(userInfo []panel.UserInfo, nodeInfo *panel.NodeInfo) (err error) {
|
|
|
|
users := make([]*protocol.User, 0)
|
|
|
|
if nodeInfo.NodeType == "V2ray" {
|
|
|
|
if nodeInfo.EnableVless {
|
|
|
|
users = c.buildVlessUsers(userInfo)
|
|
|
|
} else {
|
2022-10-09 20:59:00 -04:00
|
|
|
users = c.buildVmessUsers(userInfo)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
} else if nodeInfo.NodeType == "Trojan" {
|
|
|
|
users = c.buildTrojanUsers(userInfo)
|
|
|
|
} else if nodeInfo.NodeType == "Shadowsocks" {
|
2022-12-18 10:31:06 -05:00
|
|
|
users = c.buildSSUsers(userInfo, getCipherFromString(nodeInfo.Cipher))
|
2022-09-14 21:55:11 -04:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unsupported node type: %s", nodeInfo.NodeType)
|
|
|
|
}
|
|
|
|
err = c.server.AddUsers(users, c.Tag)
|
|
|
|
if err != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
return fmt.Errorf("add users error: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
log.Printf("[%s: %d] Added %d new users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userInfo))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
|
|
|
|
tmp := map[string]struct{}{}
|
|
|
|
tmp2 := map[string]struct{}{}
|
|
|
|
for i := range old {
|
2022-12-18 10:31:06 -05:00
|
|
|
tmp[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
|
|
|
l := len(tmp)
|
|
|
|
for i := range new {
|
2022-12-18 10:31:06 -05:00
|
|
|
e := new[i].Uuid + strconv.Itoa(new[i].SpeedLimit)
|
2022-09-14 21:55:11 -04:00
|
|
|
tmp[e] = struct{}{}
|
|
|
|
tmp2[e] = struct{}{}
|
|
|
|
if l != len(tmp) {
|
2022-12-18 10:31:06 -05:00
|
|
|
added = append(added, new[i])
|
2022-09-14 21:55:11 -04:00
|
|
|
l++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp = nil
|
|
|
|
l = len(tmp2)
|
|
|
|
for i := range old {
|
2022-12-18 10:31:06 -05:00
|
|
|
tmp2[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
|
2022-09-14 21:55:11 -04:00
|
|
|
if l != len(tmp2) {
|
2022-12-18 10:31:06 -05:00
|
|
|
deleted = append(deleted, old[i])
|
2022-09-14 21:55:11 -04:00
|
|
|
l++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deleted, added
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:59:00 -04:00
|
|
|
func (c *Node) reportUserTraffic() (err error) {
|
2022-09-14 21:55:11 -04:00
|
|
|
// Get User traffic
|
|
|
|
userTraffic := make([]panel.UserTraffic, 0)
|
|
|
|
for i := range c.userList {
|
|
|
|
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]), true)
|
|
|
|
if up > 0 || down > 0 {
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.EnableDynamicSpeedLimit {
|
2022-09-14 21:55:11 -04:00
|
|
|
c.userList[i].Traffic += up + down
|
|
|
|
}
|
|
|
|
userTraffic = append(userTraffic, panel.UserTraffic{
|
2022-12-18 10:31:06 -05:00
|
|
|
UID: (c.userList)[i].Id,
|
2022-09-14 21:55:11 -04:00
|
|
|
Upload: up,
|
|
|
|
Download: down})
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
if len(userTraffic) > 0 && !c.DisableUploadTraffic {
|
2022-09-14 21:55:11 -04:00
|
|
|
err = c.apiClient.ReportUserTraffic(userTraffic)
|
|
|
|
if err != nil {
|
2022-10-09 20:59:00 -04:00
|
|
|
log.Printf("Report user traffic faild: %s", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
} else {
|
|
|
|
log.Printf("[%s: %d] Report %d online users", c.nodeInfo.NodeType, c.nodeInfo.NodeId, len(userTraffic))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
userTraffic = nil
|
2022-12-18 10:31:06 -05:00
|
|
|
if !c.EnableIpRecorder {
|
2022-09-14 21:55:11 -04:00
|
|
|
c.server.ClearOnlineIp(c.Tag)
|
|
|
|
}
|
|
|
|
runtime.GC()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:59:00 -04:00
|
|
|
func (c *Node) reportOnlineIp() (err error) {
|
2022-09-14 21:55:11 -04:00
|
|
|
onlineIp, err := c.server.ListOnlineIp(c.Tag)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-20 23:07:04 -04:00
|
|
|
onlineIp, err = c.ipRecorder.SyncOnlineIp(onlineIp)
|
2022-09-14 21:55:11 -04:00
|
|
|
if err != nil {
|
2022-10-20 23:07:04 -04:00
|
|
|
log.Print("Report online ip error: ", err)
|
2022-09-14 21:55:11 -04:00
|
|
|
c.server.ClearOnlineIp(c.Tag)
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.IpRecorderConfig.EnableIpSync {
|
2022-10-20 23:07:04 -04:00
|
|
|
c.server.UpdateOnlineIp(c.Tag, onlineIp)
|
|
|
|
log.Printf("[Node: %d] Updated %d online ip", c.nodeInfo.NodeId, len(onlineIp))
|
2022-09-14 21:55:11 -04:00
|
|
|
}
|
2022-10-20 23:07:04 -04:00
|
|
|
log.Printf("[Node: %d] Report %d online ip", c.nodeInfo.NodeId, len(onlineIp))
|
2022-09-14 21:55:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-09 20:59:00 -04:00
|
|
|
func (c *Node) dynamicSpeedLimit() error {
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.EnableDynamicSpeedLimit {
|
2022-09-14 21:55:11 -04:00
|
|
|
for i := range c.userList {
|
|
|
|
up, down := c.server.GetUserTraffic(c.buildUserTag(&(c.userList)[i]), false)
|
2022-12-18 10:31:06 -05:00
|
|
|
if c.userList[i].Traffic+down+up/1024/1024 > c.DynamicSpeedLimitConfig.Traffic {
|
2022-09-14 21:55:11 -04:00
|
|
|
err := c.server.AddUserSpeedLimit(c.Tag,
|
|
|
|
&c.userList[i],
|
2022-12-18 10:31:06 -05:00
|
|
|
c.DynamicSpeedLimitConfig.SpeedLimit,
|
|
|
|
time.Now().Add(time.Second*time.Duration(c.DynamicSpeedLimitConfig.ExpireTime)).Unix())
|
2022-09-14 21:55:11 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.userList[i].Traffic = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|