-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse.go
122 lines (114 loc) · 3.16 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package prox5
import (
"strconv"
"strings"
"github.com/miekg/dns"
"net/netip"
)
func filterv6(in string) (filtered string, ok bool) {
split := strings.Split(in, "]:")
if len(split) < 2 {
return "", false
}
split2 := strings.Split(split[1], ":")
switch len(split2) {
case 0:
combo, err := netip.ParseAddrPort(buildProxyString("", "", split[0], split2[0], true))
if err == nil {
return combo.String(), true
}
case 1:
concat := buildProxyString("", "", split[0], split2[0], true)
combo, err := netip.ParseAddrPort(concat)
if err == nil {
return combo.String(), true
}
default:
_, err := netip.ParseAddrPort(buildProxyString("", "", split[0], split2[0], true))
if err == nil {
return buildProxyString(split2[1], split2[2], split[0], split2[0], true), true
}
}
return "", true
}
func isNumber(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
func buildProxyString(username, password, address, port string, v6 bool) (result string) {
builder := strs.Get()
defer strs.MustPut(builder)
if username != "" && password != "" {
builder.MustWriteString(username)
builder.MustWriteString(":")
builder.MustWriteString(password)
builder.MustWriteString("@")
}
builder.MustWriteString(address)
if v6 {
builder.MustWriteString("]")
}
builder.MustWriteString(":")
builder.MustWriteString(port)
return builder.String()
}
func filter(in string) (filtered string, ok bool) { //nolint:cyclop
if !strings.Contains(in, ":") {
return "", false
}
split := strings.Split(in, ":")
if len(split) < 2 {
return "", false
}
switch len(split) {
case 2:
_, isDomain := dns.IsDomainName(split[0])
if isDomain && isNumber(split[1]) {
return in, true
}
combo, err := netip.ParseAddrPort(in)
if err != nil {
return "", false
}
return combo.String(), true
case 3:
if !strings.Contains(in, "@") {
return "", false
}
split := strings.Split(in, "@")
if !strings.Contains(split[0], ":") {
return "", false
}
splitAuth := strings.Split(split[0], ":")
splitServ := strings.Split(split[1], ":")
_, isDomain := dns.IsDomainName(splitServ[0])
if isDomain && isNumber(splitServ[1]) {
return buildProxyString(splitAuth[0], splitAuth[1],
splitServ[0], splitServ[1], false), true
}
if _, err := netip.ParseAddrPort(split[1]); err == nil {
return buildProxyString(splitAuth[0], splitAuth[1],
splitServ[0], splitServ[1], false), true
}
case 4:
_, isDomain := dns.IsDomainName(split[0])
if isDomain && isNumber(split[1]) {
return buildProxyString(split[2], split[3], split[0], split[1], false), true
}
_, isDomain = dns.IsDomainName(split[2])
if isDomain && isNumber(split[3]) {
return buildProxyString(split[0], split[1], split[2], split[3], false), true
}
if _, err := netip.ParseAddrPort(split[2] + ":" + split[3]); err == nil {
return buildProxyString(split[0], split[1], split[2], split[3], false), true
}
if _, err := netip.ParseAddrPort(split[0] + ":" + split[1]); err == nil {
return buildProxyString(split[2], split[3], split[0], split[1], false), true
}
default:
if !strings.Contains(in, "[") || !strings.Contains(in, "]:") {
return "", false
}
}
return filterv6(in)
}