From 15efffba0d0322b95279d14b4b4be086dc9ac539 Mon Sep 17 00:00:00 2001 From: Yuzuki616 Date: Fri, 14 Jul 2023 12:54:09 +0800 Subject: [PATCH] move extra to NetworkSettings --- api/panel/node.go | 25 +++++++++++++------------ api/panel/panel.go | 11 ++++++----- common/crypt/aes.go | 30 ++++++++++++++++++++++++++++++ common/crypt/sha.go | 11 +++++++++++ 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 common/crypt/aes.go create mode 100644 common/crypt/sha.go diff --git a/api/panel/node.go b/api/panel/node.go index 4d7dac1..32c546b 100644 --- a/api/panel/node.go +++ b/api/panel/node.go @@ -2,13 +2,16 @@ package panel import ( "fmt" - "github.com/Yuzuki616/V2bX/conf" - "github.com/goccy/go-json" "reflect" "regexp" "strconv" "strings" "time" + + "github.com/Yuzuki616/V2bX/common/crypt" + + "github.com/Yuzuki616/V2bX/conf" + "github.com/goccy/go-json" ) type CommonNodeRsp struct { @@ -97,7 +100,6 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { if err != nil { return nil, fmt.Errorf("decode common params error: %s", err) } - var extra []byte for i := range common.Routes { // parse rules from routes var matchs []string if _, ok := common.Routes[i].Match.(string); ok { @@ -117,10 +119,6 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { node.Rules = append(node.Rules, regexp.MustCompile(v)) } case "dns": - if matchs[0] != "extra" { - break - } - extra = []byte(strings.Join(matchs[1:], "")) } } node.ServerName = common.ServerName @@ -142,11 +140,14 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { if rsp.Tls == 1 { node.Tls = true } - if len(extra) != 0 { - err = json.Unmarshal(extra, &node.ExtraConfig) - if err != nil { - return nil, fmt.Errorf("decode v2ray extra error: %s", err) - } + err = json.Unmarshal(rsp.NetworkSettings, &node.ExtraConfig) + if err != nil { + return nil, fmt.Errorf("decode v2ray extra error: %s", err) + } + if node.ExtraConfig.RealityConfig.PrivateKey != "" { + temp := crypt.GenShaHash([]byte(c.APIHost + c.Token)) + temp, err = crypt.AesDecrypt(node.ExtraConfig.RealityConfig.PrivateKey, []byte(temp)) + node.ExtraConfig.RealityConfig.PrivateKey = temp } case "shadowsocks": rsp := ShadowsocksNodeRsp{} diff --git a/api/panel/panel.go b/api/panel/panel.go index f6e90dd..c1acaa2 100644 --- a/api/panel/panel.go +++ b/api/panel/panel.go @@ -3,14 +3,15 @@ package panel import ( "bufio" "fmt" - "github.com/Yuzuki616/V2bX/conf" - "github.com/go-resty/resty/v2" "log" "os" "regexp" "strconv" "strings" "time" + + "github.com/Yuzuki616/V2bX/conf" + "github.com/go-resty/resty/v2" ) // Panel is the interface for different panel's api. @@ -18,7 +19,7 @@ import ( type Client struct { client *resty.Client APIHost string - Key string + Token string NodeType string NodeId int LocalRuleList []*regexp.Regexp @@ -48,7 +49,7 @@ func New(c *conf.ApiConfig) (*Client, error) { default: return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType) } - // Create Key for each requests + // set params client.SetQueryParams(map[string]string{ "node_type": c.NodeType, "node_id": strconv.Itoa(c.NodeID), @@ -58,7 +59,7 @@ func New(c *conf.ApiConfig) (*Client, error) { localRuleList := readLocalRuleList(c.RuleListPath) return &Client{ client: client, - Key: c.Key, + Token: c.Key, APIHost: c.APIHost, NodeType: c.NodeType, NodeId: c.NodeID, diff --git a/common/crypt/aes.go b/common/crypt/aes.go new file mode 100644 index 0000000..8aae7f7 --- /dev/null +++ b/common/crypt/aes.go @@ -0,0 +1,30 @@ +package crypt + +import ( + "crypto/aes" + "encoding/base64" +) + +func AesEncrypt(data []byte, key []byte) (string, error) { + a, err := aes.NewCipher(key) + if err != nil { + return "", err + } + en := make([]byte, 0, len(data)) + a.Encrypt(en, data) + return base64.StdEncoding.EncodeToString(en), nil +} + +func AesDecrypt(data string, key []byte) (string, error) { + d, err := base64.StdEncoding.DecodeString(data) + if err != nil { + return "", err + } + a, err := aes.NewCipher(key) + if err != nil { + return "", err + } + de := make([]byte, 0, len(data)) + a.Decrypt(de, d) + return string(de), nil +} diff --git a/common/crypt/sha.go b/common/crypt/sha.go new file mode 100644 index 0000000..05ddbeb --- /dev/null +++ b/common/crypt/sha.go @@ -0,0 +1,11 @@ +package crypt + +import ( + "crypto/sha256" + "encoding/hex" +) + +func GenShaHash(data []byte) string { + d := sha256.Sum256(data) + return hex.EncodeToString(d[:]) +}