@@ -42,76 +42,61 @@ func ToNetwork(protocol string) string {
42
42
}
43
43
}
44
44
45
- // ExtractSocksAddress parse socks5 address,
46
- // support two formats: socks5://127.0.0.1:1080 and 127.0.0.1:1080
47
- func ExtractSocksAddress (rawAddress string ) (string , error ) {
45
+ // support two formats: scheme://127.0.0.1:1080 or 127.0.0.1:1080
46
+ func extractUrl (rawAddress string , protocol string ) (host string , port string , err error ) {
47
+
48
+ if ! strings .Contains (rawAddress , "://" ) {
49
+ rawAddress = protocol + "://" + rawAddress
50
+ }
51
+
48
52
uri , err := url .Parse (rawAddress )
49
53
if err != nil {
50
- // socks5 address format is 127.0.0.1:1080
51
- _ , _ , err = net .SplitHostPort (rawAddress )
52
- isJustIP := isJustIP (rawAddress )
53
- if err != nil && ! isJustIP {
54
- log .Warnf ("socks5 address %s is invalid" , rawAddress )
55
- return "" , errors .New ("socks5 address is invalid" )
56
- }
57
- if isJustIP {
58
- rawAddress = rawAddress + ":" + getDefaultPort ("socks5" )
59
- }
60
- return rawAddress , nil
54
+ log .Warnf ("url %s is invalid" , rawAddress )
55
+ return "" , "" , errors .New ("url is invalid" )
61
56
}
62
- // socks5://127.0.0.1:1080
63
- if len (uri .Scheme ) == 0 || uri .Scheme != "socks5" {
64
- return "" , errors .New ("socks5 address is invalid" )
57
+ host = uri .Hostname ()
58
+
59
+ if len (uri .Scheme ) == 0 || uri .Scheme != protocol {
60
+ return "" , "" , errors .New ("url is invalid" )
65
61
}
66
- port := uri .Port ()
62
+
63
+ port = uri .Port ()
67
64
if len (port ) == 0 {
68
- port = "1080"
65
+ port = getDefaultPort ( protocol )
69
66
}
70
- address := net .JoinHostPort (uri .Hostname (), port )
71
- return address , nil
67
+ return
72
68
}
73
69
74
- // ExtractTLSDNSAddress parse tcp-tls format: dns.google:853@8.8.8.8
75
- func ExtractTLSDNSAddress (rawAddress string ) (host string , port string , ip string , err error ) {
70
+ func ExtractFullUrl (rawAddress string , protocol string ) (string , error ) {
71
+ host , port , err := extractUrl (rawAddress , protocol )
72
+ return net .JoinHostPort (host , port ), err
73
+ }
74
+
75
+ func extractTLSDNSAddress (rawAddress string , protocol string ) (host string , port string , err error ) {
76
+ rawAddress = protocol + "://" + rawAddress
76
77
s := strings .Split (rawAddress , "@" )
77
- host , port , err = net .SplitHostPort (s [0 ])
78
- isJustHost := len (rawAddress ) > 0
79
- if err != nil && ! isJustHost {
80
- log .Warnf ("dns server address %s is invalid" , rawAddress )
81
- return "" , "" , "" , errors .New ("dns up server address is invalid" )
82
- }
83
- if err != nil && isJustHost {
84
- host = s [0 ]
85
- if isJustIP (host ) {
86
- host = generateLiteralIPv6AddressIfNecessary (host )
87
- }
88
- port = getDefaultPort ("tcp-tls" )
89
- }
90
78
91
- ip = s [1 ]
92
- if isJustIP (ip ) {
93
- ip = generateLiteralIPv6AddressIfNecessary (ip )
94
- } else {
95
- log .Warnf ("dns server address %s is invalid" , rawAddress )
96
- return "" , "" , "" , errors .New ("dns up server address is invalid" )
79
+ host , port , err = extractUrl (s [0 ], protocol )
80
+
81
+ if err != nil {
82
+ return "" , "" , nil
97
83
}
98
- return host , port , ip , nil
99
- }
100
84
101
- // extractNormalDNSAddress parse normal format: 8.8.8.8:53
102
- func extractNormalDNSAddress (rawAddress string , protocol string ) (host string , port string , err error ) {
103
- host , port , err = net .SplitHostPort (rawAddress )
104
- isJustIP := isJustIP (rawAddress )
105
- if err != nil && ! isJustIP {
85
+ if len (s ) == 2 && isJustIP (s [1 ]) {
86
+ host = generateLiteralIPv6AddressIfNecessary (s [1 ])
87
+ } else {
106
88
log .Warnf ("dns server address %s is invalid" , rawAddress )
107
89
return "" , "" , errors .New ("dns up server address is invalid" )
108
90
}
109
- if isJustIP {
110
- host = generateLiteralIPv6AddressIfNecessary (rawAddress )
111
- port = getDefaultPort (protocol )
112
- }
113
91
return host , port , nil
92
+ }
114
93
94
+ func ExtractTLSDNSHostName (rawAddress string ) (host string , err error ) {
95
+ rawAddress = "tcp-tls" + "://" + rawAddress
96
+ s := strings .Split (rawAddress , "@" )
97
+
98
+ host , _ , err = extractUrl (s [0 ], "tcp-tls" )
99
+ return host , err
115
100
}
116
101
117
102
func isJustIP (rawAddress string ) bool {
@@ -128,37 +113,13 @@ func generateLiteralIPv6AddressIfNecessary(rawAddress string) string {
128
113
return rawAddress
129
114
}
130
115
131
- // extractHTTPSAddress parse https format: https://dns.google/dns-query
132
- func extractHTTPSAddress (rawAddress string ) (host string , port string , err error ) {
133
- uri , err := url .Parse (rawAddress )
134
- if err != nil {
135
- return "" , "" , err
136
- }
137
- host = uri .Hostname ()
138
- port = uri .Port ()
139
- if len (port ) == 0 {
140
- port = getDefaultPort ("https" )
141
- }
142
- return host , port , nil
143
-
144
- }
145
-
146
116
// ExtractDNSAddress parse all format, return literal IPv6 address
147
117
func ExtractDNSAddress (rawAddress string , protocol string ) (host string , port string , err error ) {
148
118
switch protocol {
149
- case "https" :
150
- host , port , err = extractHTTPSAddress (rawAddress )
151
119
case "tcp-tls" :
152
- _host , _port , _ip , _err := ExtractTLSDNSAddress (rawAddress )
153
- if len (_ip ) > 0 {
154
- host = _ip
155
- } else {
156
- host = _host
157
- }
158
- port = _port
159
- err = _err
120
+ host , port , err = extractTLSDNSAddress (rawAddress , protocol )
160
121
default :
161
- host , port , err = extractNormalDNSAddress (rawAddress , protocol )
122
+ host , port , err = extractUrl (rawAddress , protocol )
162
123
}
163
124
return host , port , err
164
125
}
0 commit comments