Skip to content

Commit 74995d2

Browse files
committed
优化程序逻辑
1 parent 7841817 commit 74995d2

File tree

3 files changed

+94
-9
lines changed

3 files changed

+94
-9
lines changed

node-test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vless://[email protected]:2083?encryption=none&security=tls&type=ws&host=&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
2-
vless://[email protected]:2083?encryption=none&security=tls&type=ws&host=&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
2+
vless://[email protected]:2083?encryption=none&security=tls&type=ws&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
33

44
trojan://[email protected]:443#trojan.wefuckgfw.tk%3A443
55
trojan://[email protected]:443#trojan.wefuckgfw.tk%3A443

utils/utils.go

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,39 +264,87 @@ func DoReplaceCDNIpSet(inputNode string, ipResult []string) ([]string, error) {
264264
//vless协议
265265
if strings.HasPrefix(sampleNode, vlessPre) {
266266
//vless: vless://[email protected]:539?type=tcp&security=tls&sni=b.a.tk&flow=xtls-rprx-direct#abc-vless-1
267+
//vless://[email protected]:2083?encryption=none&security=tls&type=ws&host=&path=/4TLoIulw/#xray.wefuckgfw.tk%3A2083
267268
re := regexp.MustCompile(`@(.*?):`)
269+
268270
nodeHost := re.FindStringSubmatch(sampleNode)[1]
269271

270272
if strings.Index(sampleNode, "host=") != -1 {
271273
re = regexp.MustCompile(`(host=)(.*?)(&)`)
272-
sampleNode = re.ReplaceAllString(sampleNode, "$1"+nodeHost+"$3")
274+
275+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
276+
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
277+
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+nodeHost+subStrPart3)
273278
} else {
274-
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
275-
sampleNode = re.ReplaceAllString(sampleNode, "$1$2$3$4$5host="+nodeHost+"&")
279+
var subStrPart5 string
280+
if strings.Contains(sampleNode, "?") {
281+
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
282+
subStrPart5 = re.FindStringSubmatch(sampleNode)[5]
283+
284+
} else {
285+
re = regexp.MustCompile(`(@)(.*?)(:)(\d+)(.*?)`)
286+
subStrPart5 = re.FindStringSubmatch(sampleNode)[5] + "?"
287+
}
288+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
289+
subStrPart2 := re.FindStringSubmatch(sampleNode)[2]
290+
291+
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
292+
293+
subStrPart4 := re.FindStringSubmatch(sampleNode)[4]
294+
295+
//subStrPart5 := re.FindStringSubmatch(sampleNode)[5]
296+
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+subStrPart2+subStrPart3+subStrPart4+subStrPart5+"host="+nodeHost+"&")
297+
276298
}
277299

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

289314
if strings.Index(sampleNode, "host=") != -1 {
290315
re = regexp.MustCompile(`(host=)(.*?)(&)`)
291-
sampleNode = re.ReplaceAllString(sampleNode, "$1"+nodeHost+"$3")
316+
317+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
318+
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
319+
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+nodeHost+subStrPart3)
292320
} else {
293-
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
294-
sampleNode = re.ReplaceAllString(sampleNode, "$1$2$3$4$5host="+nodeHost+"&")
321+
//是否有额外参数
322+
var subStrPart5 string
323+
if strings.Contains(sampleNode, "?") {
324+
re = regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
325+
subStrPart5 = re.FindStringSubmatch(sampleNode)[5]
326+
327+
} else {
328+
re = regexp.MustCompile(`(@)(.*?)(:)(\d+)(.*?)`)
329+
subStrPart5 = re.FindStringSubmatch(sampleNode)[5] + "?"
330+
331+
}
332+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
333+
subStrPart2 := re.FindStringSubmatch(sampleNode)[2]
334+
335+
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
336+
337+
subStrPart4 := re.FindStringSubmatch(sampleNode)[4]
338+
//subStrPart5 := re.FindStringSubmatch(sampleNode)[5]
339+
340+
sampleNode = re.ReplaceAllString(sampleNode, subStrPart1+subStrPart2+subStrPart3+subStrPart4+subStrPart5+"host="+nodeHost+"&")
295341
}
296342

297343
for _, ip := range ipResult {
298344
re = regexp.MustCompile(`(@)(.*?)(:)`)
299-
nodes = append(nodes, re.ReplaceAllString(sampleNode, "$1"+ip+"$3")+"\n")
345+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
346+
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
347+
nodes = append(nodes, re.ReplaceAllString(sampleNode, subStrPart1+ip+subStrPart3)+"\r")
300348
}
301349
return nodes, nil
302350
}

utils/utils_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package utils
22

33
import (
4+
"fmt"
45
"proxy-node2more/config"
6+
"regexp"
7+
"strings"
58
"testing"
69
)
710

@@ -36,3 +39,37 @@ func TestCaculateNodesResult(t *testing.T) {
3639
HandleError(err)
3740
println(result)
3841
}
42+
43+
func TestTrojanLink(t *testing.T) {
44+
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"
45+
re := regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
46+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
47+
fmt.Println(subStrPart1)
48+
49+
}
50+
51+
func TestTrojanLink2(t *testing.T) {
52+
sampleNode := "trojan://[email protected]:443#trojan.wefuckgfw.tk%3A443"
53+
var re *regexp.Regexp
54+
if strings.Contains(sampleNode, "?") {
55+
re := regexp.MustCompile(`(@)(.*?)(:)(.*?)(\?)`)
56+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
57+
fmt.Println(subStrPart1)
58+
} else {
59+
re := regexp.MustCompile(`(@)(.*?)(:)(\d+)(.*?)`)
60+
subStrPart1 := re.FindStringSubmatch(sampleNode)[1]
61+
subStrPart2 := re.FindStringSubmatch(sampleNode)[2]
62+
subStrPart3 := re.FindStringSubmatch(sampleNode)[3]
63+
subStrPart4 := re.FindStringSubmatch(sampleNode)[4]
64+
subStrPart5 := re.FindStringSubmatch(sampleNode)[5]
65+
66+
fmt.Println(subStrPart1)
67+
fmt.Println(subStrPart2)
68+
fmt.Println(subStrPart3)
69+
fmt.Println(subStrPart4)
70+
fmt.Println(subStrPart5)
71+
72+
}
73+
println(re)
74+
75+
}

0 commit comments

Comments
 (0)