-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
227 lines (183 loc) · 5.26 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"check/validator"
"context"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub/executor"
chttp "github.com/Dreamacro/clash/listener/http"
)
var proxy constant.Proxy
type Args struct {
config_path, port, ctype, custom_url string
}
var args = Args{}
var proxy_url = "127.0.0.1:"
var fw os.File
var logger *log.Logger
func relay(l, r net.Conn) {
go io.Copy(l, r)
io.Copy(r, l)
}
func getIpInfo() string {
body := requestURL("http://myip.ipip.net")
if len(body) > 100 {
return strings.Replace(body[0:50], "\n", " ", -1)
} else {
return body
}
}
func requestURL(requrl string) string {
proxy, _ := url.Parse("http://" + proxy_url)
client := &http.Client{
Timeout: 5 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
Transport: &http.Transport{
// 设置代理
Proxy: http.ProxyURL(proxy),
},
}
req, _ := http.NewRequest("GET", requrl, nil)
req.Header.Set("USER-AGENT", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36")
resp, err := client.Do(req)
// resp, err := client.Get("http://myexternalip.com/raw")
if err != nil {
//return errors.New(strings.ReplaceAll(err.Error(), newrequrl, requrl))
return "Error"
}
defer resp.Body.Close()
content, _ := io.ReadAll(resp.Body)
return strings.Trim(string(content), "\n")
}
func init() {
flag.StringVar(&args.config_path, "c", "config.yaml", "config file;")
flag.StringVar(&args.port, "p", "18081", "proxy port;")
flag.StringVar(&args.ctype, "t", "0", "check type; \n\t0:check netflix;\n\t1:check google&youtube premium US\n\t2:check chatGPT\n")
flag.StringVar(&args.custom_url, "u", "", "custom probe url")
flag.Usage = func() {
flag.PrintDefaults()
fmt.Println("\ngenerate clash config:\ngoogle&youtube:\n----------\ngrep \"youtube:Y\" 1.check.log | cut -f 1 | cut -d \":\" -f 2 | sed 's/^/ -/g' | sort && echo \" \" && \\\ngrep \"google:Y\" 1.check.log | cut -f 1 | cut -d \":\" -f 2 | sed 's/^/ -/g' | sort\n----------\nnetflix:\n----------\ngrep \"netflix:Y\" 0.check.log | cut -f 1 | cut -d \":\" -f 2 | sed 's/^/ -/g' | sort\n----------\nchatGPT:\n----------\ngrep \"chatGPT:Y\" 2.check.log | cut -f 1 | cut -d \":\" -f 2 | sed 's/^/ -/g' | sort")
}
flag.Parse()
proxy_url += args.port
fmt.Println(proxy_url)
log.SetFlags(0)
fw, _ := os.OpenFile(args.ctype+".check.log", os.O_TRUNC|os.O_RDWR|os.O_CREATE, 0666)
logger = log.New(io.MultiWriter(os.Stdout, fw), "", 0)
}
func youtube_premium() string {
youtubeUrl := "https://www.youtube.com/premium"
content := requestURL(youtubeUrl)
if content == "Error" {
return "ERR"
}
is := strings.Contains(content, "Premium is not available in your country")
if is {
//存在
//fmt.Println(content)
return "N"
} else {
//不存在
return "Y"
}
}
func google() string {
googleUrl := "https://www.google.com"
content := requestURL(googleUrl)
if content == "Error" {
return "ERR"
}
// fmt.Println(content)
is := strings.Contains(content, "302 Moved")
if is {
//存在
//fmt.Println(content)
return "N"
} else {
//不存在
return "Y"
}
}
func main() {
_, err := os.Stat(args.config_path)
if nil != err {
fmt.Println("config illegal")
fmt.Println(args.config_path)
os.Exit(1)
}
config, err := executor.ParseWithPath(args.config_path)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
in := make(chan constant.ConnContext, 100)
defer close(in)
l, err := chttp.New(proxy_url, in)
if err != nil {
panic(err)
}
defer l.Close()
println("listen at:", l.Address())
go func() {
for c := range in {
conn := c
metadata := conn.Metadata()
go func() {
remote, err := proxy.DialContext(context.Background(), metadata)
if err != nil {
conn.Conn().Close()
// fmt.Println(err.Error())
return
}
relay(remote, conn.Conn())
}()
}
}()
index := 1
nodes := config.Proxies
// total := len(nodes)
for node, server := range nodes {
var (
res string
)
if server.Type() != constant.Shadowsocks && server.Type() != constant.ShadowsocksR && server.Type() != constant.Snell && server.Type() != constant.Socks5 && server.Type() != constant.Http && server.Type() != constant.Vmess && server.Type() != constant.Trojan {
continue
}
proxy = server
//落地机IP
ip := getIpInfo()
str := fmt.Sprintf("%d.node: %s", index, node)
if args.custom_url != "" {
vs := validator.NewVerify(proxy_url)
res = "\t" + args.custom_url + ":" + vs.CustomProbe(args.custom_url)
}
re := regexp.MustCompile("美|波特兰|达拉斯|俄勒冈|凤凰城|费利蒙|硅谷|拉斯维加斯|洛杉矶|圣何塞|圣克拉拉|西雅图|芝加哥|US|United States")
if args.ctype == "0" && !re.MatchString(node) {
vs := validator.NewVerify(proxy_url)
res = "\tnetflix:" + vs.Netflix()
}
if args.ctype == "1" {
res += "\tgoogle:" + google()
if re.MatchString(node) {
res += "\tyoutube:" + youtube_premium()
}
}
if args.ctype == "2" {
vs := validator.NewVerify(proxy_url)
res += "\tchatGPT:" + vs.ChatGPT()
}
logger.Printf("%s%s\t%s\n", str, res, ip)
index++
}
defer fw.Close()
}