This commit is contained in:
yuzuki999
2022-06-03 01:22:56 +08:00
parent 358b5888b4
commit a5c6483402
7 changed files with 205 additions and 114 deletions

View File

@@ -59,15 +59,19 @@ type TrojanUserInfo struct {
Password string `json:"password"`
}
type UserInfo struct {
UID int `json:"id"`
DeviceLimit int `json:"device_limit"`
SpeedLimit uint64 `json:"speed_limit"`
UID int `json:"id"`
Port int `json:"port"`
Cipher string `json:"cipher"`
Secret string `json:"secret"`
V2rayUser *V2RayUserInfo `json:"v2ray_user"`
TrojanUser *TrojanUserInfo `json:"trojan_user"`
}
type UserListBody struct {
//Msg string `json:"msg"`
Data []UserInfo `json:"data"`
}
func (p *UserInfo) GetUserEmail() string {
if p.V2rayUser != nil {
@@ -79,14 +83,16 @@ func (p *UserInfo) GetUserEmail() string {
}
type NodeInfo struct {
NodeType string
NodeId int
TLSType string
EnableVless bool
EnableTls bool
V2ray *V2rayConfig
Trojan *TrojanConfig
SS *SSConfig
RspMd5 string
NodeType string
NodeId int
TLSType string
EnableVless bool
EnableTls bool
EnableSS2022 bool
V2ray *V2rayConfig
Trojan *TrojanConfig
SS *SSConfig
}
type SSConfig struct {

View File

@@ -2,10 +2,11 @@ package v2board
import (
"bufio"
md52 "crypto/md5"
"fmt"
"github.com/Yuzuki616/V2bX/api"
"github.com/go-resty/resty/v2"
json "github.com/goccy/go-json"
"github.com/goccy/go-json"
"log"
"os"
"regexp"
@@ -16,18 +17,21 @@ import (
// APIClient create an api client to the panel.
type APIClient struct {
client *resty.Client
APIHost string
NodeID int
Key string
NodeType string
EnableVless bool
EnableXTLS bool
SpeedLimit float64
DeviceLimit int
LocalRuleList []api.DetectRule
RemoteRuleCache *api.Rule
access sync.Mutex
client *resty.Client
APIHost string
NodeID int
Key string
NodeType string
EnableSS2022 bool
EnableVless bool
EnableXTLS bool
SpeedLimit float64
DeviceLimit int
LocalRuleList []api.DetectRule
RemoteRuleCache *api.Rule
access sync.Mutex
NodeInfoRspMd5 [16]byte
UserListCheckNum int
}
// New create an api instance
@@ -61,6 +65,7 @@ func New(apiConfig *api.Config) *APIClient {
Key: apiConfig.Key,
APIHost: apiConfig.APIHost,
NodeType: apiConfig.NodeType,
EnableSS2022: apiConfig.EnableSS2022,
EnableVless: apiConfig.EnableVless,
EnableXTLS: apiConfig.EnableXTLS,
SpeedLimit: apiConfig.SpeedLimit,
@@ -96,7 +101,7 @@ func readLocalRuleList(path string) (LocalRuleList []api.DetectRule) {
// handle first encountered error while reading
if err := fileScanner.Err(); err != nil {
log.Fatalf("Error while reading file: %s", err)
return make([]api.DetectRule, 0)
return []api.DetectRule{}
}
file.Close()
@@ -153,6 +158,13 @@ func (c *APIClient) GetNodeInfo() (nodeInfo *api.NodeInfo, err error) {
default:
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)
}
md := md52.Sum(res.Body())
if c.NodeInfoRspMd5 != [16]byte{} {
if c.NodeInfoRspMd5 == md {
return nil, nil
}
}
c.NodeInfoRspMd5 = md
res, err = c.client.R().
SetQueryParam("local_port", "1").
ForceContentType("application/json").
@@ -168,10 +180,6 @@ func (c *APIClient) GetNodeInfo() (nodeInfo *api.NodeInfo, err error) {
nodeInfo, err = c.ParseV2rayNodeResponse(res.Body())
case "Trojan":
nodeInfo, err = c.ParseTrojanNodeResponse(res.Body())
case "Shadowsocks":
nodeInfo, err = c.ParseSSNodeResponse()
default:
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)
}
return nodeInfo, nil
}
@@ -192,16 +200,25 @@ func (c *APIClient) GetUserList() (UserList *[]api.UserInfo, err error) {
res, err := c.client.R().
ForceContentType("application/json").
Get(path)
var userList []api.UserInfo
err = c.checkResponse(res, path, err)
if err != nil {
return nil, err
}
var userList *api.UserListBody
err = json.Unmarshal(res.Body(), &userList)
if err != nil {
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
return &userList, nil
checkNum := userList.Data[len(userList.Data)-1].UID +
userList.Data[len(userList.Data)/2-1].UID +
userList.Data[0].UID
if c.UserListCheckNum != 0 {
if c.UserListCheckNum == checkNum {
return nil, nil
}
}
c.UserListCheckNum = userList.Data[len(userList.Data)-1].UID
return &userList.Data, nil
}
// ReportUserTraffic reports the user traffic
@@ -286,8 +303,9 @@ func (c *APIClient) ParseSSNodeResponse() (*api.NodeInfo, error) {
return nil, err
}
node := &api.NodeInfo{
NodeType: c.NodeType,
NodeId: c.NodeID,
EnableSS2022: c.EnableSS2022,
NodeType: c.NodeType,
NodeId: c.NodeID,
SS: &api.SSConfig{
Port: port,
TransportProtocol: "tcp",
@@ -300,6 +318,10 @@ func (c *APIClient) ParseSSNodeResponse() (*api.NodeInfo, error) {
// ParseV2rayNodeResponse parse the response for the given nodeinfor format
func (c *APIClient) ParseV2rayNodeResponse(body []byte) (*api.NodeInfo, error) {
node := &api.NodeInfo{V2ray: &api.V2rayConfig{}}
err := json.Unmarshal(body, node.V2ray)
if err != nil {
return nil, fmt.Errorf("unmarshal nodeinfo error: %s", err)
}
node.NodeType = c.NodeType
node.NodeId = c.NodeID
c.RemoteRuleCache = &node.V2ray.Routing.Rules[0]