2022-06-04 00:05:46 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-06-05 01:19:39 -04:00
|
|
|
"bytes"
|
2022-06-04 00:05:46 -04:00
|
|
|
md52 "crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
"github.com/xtls/xray-core/infra/conf"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DetectRule struct {
|
|
|
|
ID int
|
|
|
|
Pattern *regexp.Regexp
|
|
|
|
}
|
|
|
|
type DetectResult struct {
|
|
|
|
UID int
|
|
|
|
RuleID int
|
|
|
|
}
|
|
|
|
|
|
|
|
// readLocalRuleList reads the local rule list file
|
|
|
|
func readLocalRuleList(path string) (LocalRuleList []DetectRule) {
|
|
|
|
LocalRuleList = make([]DetectRule, 0)
|
|
|
|
if path != "" {
|
|
|
|
// open the file
|
|
|
|
file, err := os.Open(path)
|
|
|
|
|
|
|
|
//handle errors while opening
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error when opening file: %s", err)
|
|
|
|
return LocalRuleList
|
|
|
|
}
|
|
|
|
|
|
|
|
fileScanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
// read line by line
|
|
|
|
for fileScanner.Scan() {
|
|
|
|
LocalRuleList = append(LocalRuleList, DetectRule{
|
|
|
|
ID: -1,
|
|
|
|
Pattern: regexp.MustCompile(fileScanner.Text()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// handle first encountered error while reading
|
|
|
|
if err := fileScanner.Err(); err != nil {
|
|
|
|
log.Fatalf("Error while reading file: %s", err)
|
|
|
|
return []DetectRule{}
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return LocalRuleList
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeInfo struct {
|
2022-06-06 05:07:21 -04:00
|
|
|
DeviceLimit int
|
|
|
|
SpeedLimit uint64
|
|
|
|
NodeType string
|
|
|
|
NodeId int
|
|
|
|
TLSType string
|
|
|
|
EnableVless bool
|
|
|
|
EnableTls bool
|
|
|
|
//EnableSS2022 bool
|
|
|
|
V2ray *V2rayConfig
|
|
|
|
Trojan *TrojanConfig
|
|
|
|
SS *SSConfig
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type SSConfig struct {
|
|
|
|
Port int `json:"port"`
|
|
|
|
TransportProtocol string `json:"transportProtocol"`
|
|
|
|
CypherMethod string `json:"cypher"`
|
|
|
|
}
|
|
|
|
type V2rayConfig struct {
|
|
|
|
Inbounds []conf.InboundDetourConfig `json:"inbounds"`
|
|
|
|
Routing *struct {
|
2022-06-12 09:10:20 -04:00
|
|
|
Rules json.RawMessage `json:"rules"`
|
2022-06-04 00:05:46 -04:00
|
|
|
} `json:"routing"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
InboundTag string `json:"inboundTag,omitempty"`
|
|
|
|
OutboundTag string `json:"outboundTag"`
|
|
|
|
Domain []string `json:"domain,omitempty"`
|
|
|
|
Protocol []string `json:"protocol,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TrojanConfig struct {
|
|
|
|
LocalPort int `json:"local_port"`
|
|
|
|
Password []interface{} `json:"password"`
|
|
|
|
TransportProtocol string
|
|
|
|
Ssl struct {
|
|
|
|
Sni string `json:"sni"`
|
|
|
|
} `json:"ssl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNodeInfo will pull NodeInfo Config from sspanel
|
|
|
|
func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
|
|
|
|
var path string
|
|
|
|
var res *resty.Response
|
|
|
|
switch c.NodeType {
|
|
|
|
case "V2ray":
|
|
|
|
path = "/api/v1/server/Deepbwork/config"
|
|
|
|
case "Trojan":
|
|
|
|
path = "/api/v1/server/TrojanTidalab/config"
|
|
|
|
case "Shadowsocks":
|
|
|
|
if nodeInfo, err = c.ParseSSNodeResponse(); err == nil {
|
|
|
|
return nodeInfo, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)
|
|
|
|
}
|
|
|
|
res, err = c.client.R().
|
|
|
|
SetQueryParam("local_port", "1").
|
|
|
|
ForceContentType("application/json").
|
|
|
|
Get(path)
|
|
|
|
err = c.checkResponse(res, path, err)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
|
|
|
switch c.NodeType {
|
|
|
|
case "V2ray":
|
2022-06-06 03:23:40 -04:00
|
|
|
i := bytes.Index(res.Body(), []byte("outbo"))
|
|
|
|
md := md52.Sum(res.Body()[:i])
|
2022-06-06 05:07:21 -04:00
|
|
|
nodeNotIsChange := true
|
|
|
|
if c.NodeInfoRspMd5 == [16]byte{} {
|
|
|
|
nodeNotIsChange = false
|
|
|
|
c.NodeInfoRspMd5 = md
|
|
|
|
} else {
|
2022-06-06 03:23:40 -04:00
|
|
|
if c.NodeInfoRspMd5 != md {
|
2022-06-06 05:07:21 -04:00
|
|
|
nodeNotIsChange = false
|
2022-06-06 03:23:40 -04:00
|
|
|
c.NodeInfoRspMd5 = md
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 01:19:39 -04:00
|
|
|
md2 := md52.Sum(res.Body()[i:])
|
2022-06-06 03:23:40 -04:00
|
|
|
ruleIsChange := false
|
|
|
|
if c.NodeRuleRspMd5 != md2 {
|
|
|
|
ruleIsChange = true
|
|
|
|
c.NodeRuleRspMd5 = md2
|
|
|
|
}
|
2022-06-06 05:07:21 -04:00
|
|
|
nodeInfo, err = c.ParseV2rayNodeResponse(res.Body(), nodeNotIsChange, ruleIsChange)
|
2022-06-04 00:05:46 -04:00
|
|
|
case "Trojan":
|
2022-06-06 03:23:40 -04:00
|
|
|
md := md52.Sum(res.Body())
|
|
|
|
if c.NodeInfoRspMd5 != [16]byte{} {
|
|
|
|
if c.NodeInfoRspMd5 == md {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-06-05 01:19:39 -04:00
|
|
|
}
|
2022-06-06 03:23:40 -04:00
|
|
|
c.NodeInfoRspMd5 = md
|
2022-06-04 00:05:46 -04:00
|
|
|
nodeInfo, err = c.ParseTrojanNodeResponse(res.Body())
|
|
|
|
}
|
|
|
|
return nodeInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-06-12 09:10:20 -04:00
|
|
|
func (c *Client) GetNodeRule() (*[]DetectRule, *[]string, error) {
|
2022-06-04 00:05:46 -04:00
|
|
|
ruleList := c.LocalRuleList
|
2022-06-05 01:19:39 -04:00
|
|
|
if c.NodeType != "V2ray" || c.RemoteRuleCache == nil {
|
2022-06-12 09:10:20 -04:00
|
|
|
return &ruleList, nil, nil
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
// V2board only support the rule for v2ray
|
|
|
|
// fix: reuse config response
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
2022-06-12 09:10:20 -04:00
|
|
|
if len(*c.RemoteRuleCache) >= 2 {
|
|
|
|
for i, rule := range (*c.RemoteRuleCache)[1].Domain {
|
|
|
|
ruleListItem := DetectRule{
|
|
|
|
ID: i,
|
|
|
|
Pattern: regexp.MustCompile(rule),
|
|
|
|
}
|
|
|
|
ruleList = append(ruleList, ruleListItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var protocolList []string
|
|
|
|
if len(*c.RemoteRuleCache) >= 3 {
|
|
|
|
for _, str := range (*c.RemoteRuleCache)[2].Protocol {
|
|
|
|
protocolList = append(protocolList, str)
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
}
|
2022-06-05 01:19:39 -04:00
|
|
|
c.RemoteRuleCache = nil
|
2022-06-12 09:10:20 -04:00
|
|
|
return &ruleList, &protocolList, nil
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseTrojanNodeResponse parse the response for the given nodeinfor format
|
|
|
|
func (c *Client) ParseTrojanNodeResponse(body []byte) (*NodeInfo, error) {
|
|
|
|
node := &NodeInfo{Trojan: &TrojanConfig{}}
|
|
|
|
var err = json.Unmarshal(body, node.Trojan)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal nodeinfo error: %s", err)
|
|
|
|
}
|
|
|
|
node.SpeedLimit = uint64(c.SpeedLimit * 1000000 / 8)
|
|
|
|
node.DeviceLimit = c.DeviceLimit
|
|
|
|
node.NodeId = c.NodeID
|
|
|
|
node.NodeType = c.NodeType
|
2022-06-06 05:07:21 -04:00
|
|
|
node.Trojan.TransportProtocol = "tcp"
|
2022-06-04 00:05:46 -04:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseSSNodeResponse parse the response for the given nodeinfor format
|
|
|
|
func (c *Client) ParseSSNodeResponse() (*NodeInfo, error) {
|
|
|
|
var port int
|
|
|
|
var method string
|
|
|
|
userInfo, err := c.GetUserList()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(*userInfo) > 0 {
|
|
|
|
port = (*userInfo)[0].Port
|
|
|
|
method = (*userInfo)[0].Cipher
|
2022-07-22 03:45:17 -04:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("shadowsocks node need a active user")
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node := &NodeInfo{
|
2022-06-04 05:12:28 -04:00
|
|
|
SpeedLimit: uint64(c.SpeedLimit * 1000000 / 8),
|
|
|
|
DeviceLimit: c.DeviceLimit,
|
|
|
|
//EnableSS2022: c.EnableSS2022,
|
|
|
|
NodeType: c.NodeType,
|
|
|
|
NodeId: c.NodeID,
|
2022-06-04 00:05:46 -04:00
|
|
|
SS: &SSConfig{
|
|
|
|
Port: port,
|
|
|
|
TransportProtocol: "tcp",
|
|
|
|
CypherMethod: method,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseV2rayNodeResponse parse the response for the given nodeinfor format
|
2022-06-05 01:19:39 -04:00
|
|
|
func (c *Client) ParseV2rayNodeResponse(body []byte, notParseNode, parseRule bool) (*NodeInfo, error) {
|
2022-06-06 05:07:21 -04:00
|
|
|
if notParseNode && !parseRule {
|
2022-06-06 03:29:01 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
2022-06-04 00:05:46 -04:00
|
|
|
node := &NodeInfo{V2ray: &V2rayConfig{}}
|
|
|
|
err := json.Unmarshal(body, node.V2ray)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal nodeinfo error: %s", err)
|
|
|
|
}
|
2022-06-05 01:19:39 -04:00
|
|
|
if parseRule {
|
2022-06-12 09:10:20 -04:00
|
|
|
c.RemoteRuleCache = &[]Rule{}
|
|
|
|
err := json.Unmarshal(node.V2ray.Routing.Rules, c.RemoteRuleCache)
|
2022-06-05 07:37:02 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-06-06 03:29:01 -04:00
|
|
|
if notParseNode {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-06-05 01:19:39 -04:00
|
|
|
}
|
|
|
|
node.V2ray.Routing = nil
|
2022-06-04 00:05:46 -04:00
|
|
|
node.SpeedLimit = uint64(c.SpeedLimit * 1000000 / 8)
|
|
|
|
node.DeviceLimit = c.DeviceLimit
|
|
|
|
node.NodeType = c.NodeType
|
|
|
|
node.NodeId = c.NodeID
|
|
|
|
if c.EnableXTLS {
|
|
|
|
node.TLSType = "xtls"
|
|
|
|
} else {
|
|
|
|
node.TLSType = "tls"
|
|
|
|
}
|
|
|
|
node.EnableVless = c.EnableVless
|
|
|
|
node.EnableTls = node.V2ray.Inbounds[0].StreamSetting.Security == "tls"
|
|
|
|
return node, nil
|
|
|
|
}
|