Skip to content

Commit

Permalink
优化程序逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
fireinrain committed Mar 22, 2023
1 parent 7841817 commit 74995d2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion node-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vless://[email protected]:2083?encryption=none&security=tls&type=ws&host=&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
vless://[email protected]:2083?encryption=none&security=tls&type=ws&host=&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
vless://[email protected]:2083?encryption=none&security=tls&type=ws&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083

trojan://[email protected]:443#trojan.wefuckgfw.tk%3A443
trojan://[email protected]:443#trojan.wefuckgfw.tk%3A443
Expand Down
64 changes: 56 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,39 +264,87 @@ func DoReplaceCDNIpSet(inputNode string, ipResult []string) ([]string, error) {
//vless协议
if strings.HasPrefix(sampleNode, vlessPre) {
//vless: vless://[email protected]:539?type=tcp&security=tls&sni=b.a.tk&flow=xtls-rprx-direct#abc-vless-1
//vless://[email protected]:2083?encryption=none&security=tls&type=ws&host=&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
re := regexp.MustCompile(`@(.*?):`)

nodeHost := re.FindStringSubmatch(sampleNode)[1]

if strings.Index(sampleNode, "host=") != -1 {
re = regexp.MustCompile(`(host=)(.*?)(&)`)
sampleNode = re.ReplaceAllString(sampleNode, "$1"+nodeHost+"$3")

subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+nodeHost+subStrPart3)
} else {
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
sampleNode = re.ReplaceAllString(sampleNode, "$1$2$3$4$5host="+nodeHost+"&")
var subStrPart5 string
if strings.Contains(sampleNode, "?") {
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
subStrPart5 = re.FindStringSubmatch(sampleNode)[5]

} else {
re = regexp.MustCompile(`(@)(.*?)(:)(\d+)(.*?)`)
subStrPart5 = re.FindStringSubmatch(sampleNode)[5] + "?"
}
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart2 := re.FindStringSubmatch(sampleNode)[2]

subStrPart3 := re.FindStringSubmatch(sampleNode)[3]

subStrPart4 := re.FindStringSubmatch(sampleNode)[4]

//subStrPart5 := re.FindStringSubmatch(sampleNode)[5]
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+subStrPart2+subStrPart3+subStrPart4+subStrPart5+"host="+nodeHost+"&")

}

for _, ip := range ipResult {
re = regexp.MustCompile(`(@)(.*?)(:)`)
nodes = append(nodes, re.ReplaceAllString(sampleNode, "$1"+ip+"$3")+"\n")
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
nodes = append(nodes, re.ReplaceAllString(sampleNode, subStrPart1+ip+subStrPart3)+"\r")
}
return nodes, nil
}
if strings.HasPrefix(sampleNode, trojanPre) {
//trojan trojan://[email protected]:48857?type=tcp&security=tls&sni=a.b.tk&flow=xtls-rprx-direct#a.b.tk-trojan-2
re := regexp.MustCompile(`@(.*?):`)

nodeHost := re.FindStringSubmatch(sampleNode)[1]

if strings.Index(sampleNode, "host=") != -1 {
re = regexp.MustCompile(`(host=)(.*?)(&)`)
sampleNode = re.ReplaceAllString(sampleNode, "$1"+nodeHost+"$3")

subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+nodeHost+subStrPart3)
} else {
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
sampleNode = re.ReplaceAllString(sampleNode, "$1$2$3$4$5host="+nodeHost+"&")
//是否有额外参数
var subStrPart5 string
if strings.Contains(sampleNode, "?") {
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
subStrPart5 = re.FindStringSubmatch(sampleNode)[5]

} else {
re = regexp.MustCompile(`(@)(.*?)(:)(\d+)(.*?)`)
subStrPart5 = re.FindStringSubmatch(sampleNode)[5] + "?"

}
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart2 := re.FindStringSubmatch(sampleNode)[2]

subStrPart3 := re.FindStringSubmatch(sampleNode)[3]

subStrPart4 := re.FindStringSubmatch(sampleNode)[4]
//subStrPart5 := re.FindStringSubmatch(sampleNode)[5]

sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+subStrPart2+subStrPart3+subStrPart4+subStrPart5+"host="+nodeHost+"&")
}

for _, ip := range ipResult {
re = regexp.MustCompile(`(@)(.*?)(:)`)
nodes = append(nodes, re.ReplaceAllString(sampleNode, "$1"+ip+"$3")+"\n")
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
nodes = append(nodes, re.ReplaceAllString(sampleNode, subStrPart1+ip+subStrPart3)+"\r")
}
return nodes, nil
}
Expand Down
37 changes: 37 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package utils

import (
"fmt"
"proxy-node2more/config"
"regexp"
"strings"
"testing"
)

Expand Down Expand Up @@ -36,3 +39,37 @@ func TestCaculateNodesResult(t *testing.T) {
HandleError(err)
println(result)
}

func TestTrojanLink(t *testing.T) {
sampleNode := "trojan://[email protected]:80?security=tls&sni=ca1.trojanvh.xyz&type=tcp&headerType=none#org-org.org_Relay_-%F0%9F%87%A8%F0%9F%87%A6CA_36"
re := regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
fmt.Println(subStrPart1)

}

func TestTrojanLink2(t *testing.T) {
sampleNode := "trojan://[email protected]:443#trojan.wefuckgfw.tk%3A443"
var re *regexp.Regexp
if strings.Contains(sampleNode, "?") {
re := regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
fmt.Println(subStrPart1)
} else {
re := regexp.MustCompile(`(@)(.*?)(:)(\d+)(.*?)`)
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
subStrPart2 := re.FindStringSubmatch(sampleNode)[2]
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
subStrPart4 := re.FindStringSubmatch(sampleNode)[4]
subStrPart5 := re.FindStringSubmatch(sampleNode)[5]

fmt.Println(subStrPart1)
fmt.Println(subStrPart2)
fmt.Println(subStrPart3)
fmt.Println(subStrPart4)
fmt.Println(subStrPart5)

}
println(re)

}

0 comments on commit 74995d2

Please sign in to comment.