-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockc.go
247 lines (211 loc) · 5.25 KB
/
sockc.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bufio"
"context"
"crypto/tls"
"fmt"
"net"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"unicode"
"github.com/sloweax/argparse"
_ "github.com/sloweax/sockc/shadowsocks"
"golang.org/x/net/proxy"
)
type Program struct {
Append bool `name:"a" alias:"append" description:"open output file in append mode"`
Detail bool `name:"d" alias:"detail" description:"include the time to establish connection in the fragment of the proxy url (in seconds)"`
Workers uint `name:"j" alias:"workers" metavar:"num" description:"number of concurrent workers (default: 8)"`
Network string `name:"n" alias:"network" metavar:"val" description:"network to connect to -t (default: tcp)"`
OutputFile string `name:"o" alias:"output" metavar:"file" description:"valid proxy output file (default: stdout)"`
Target string `name:"t" alias:"target" metavar:"host" description:"target host to check proxy validity. see \"-v\" option (default: google.com:443)"`
Unique bool `name:"u" alias:"unique" description:"don't scan the same proxy url twice"`
Validity string `name:"v" alias:"validity" description:"connect: validity by just connecting, tls: validity by succesfull tls handshake (default: tls)"`
Timeout uint `name:"w" alias:"timeout" metavar:"seconds" description:"proxy connection timeout. 0 for no timeout (default: 10)"`
Proxy *string `name:"x" alias:"proxy" metavar:"url" description:"use proxy to connect to proxies"`
ProxyFiles []string `name:"file" type:"positional" metavar:"file..." description:"check proxies from file. if no file is provided it is read from stdin"`
proxyUrl *url.URL
scannedmu *sync.Mutex
scanned map[string]struct{}
fout *os.File
pchan chan *url.URL
}
func (p *Program) Init() error {
if p.Unique {
p.scanned = make(map[string]struct{})
p.scannedmu = new(sync.Mutex)
}
switch p.Validity {
case "connect", "tls":
default:
return fmt.Errorf(`unsupported value for option -v %q`, p.Validity)
}
p.pchan = make(chan *url.URL)
if p.Proxy != nil {
url, err := url.Parse(*p.Proxy)
if err != nil {
return err
}
p.proxyUrl = url
}
if p.OutputFile == "-" {
p.fout = os.Stdout
} else {
flags := os.O_WRONLY | os.O_CREATE
if p.Append {
flags |= os.O_APPEND
}
if f, err := os.OpenFile(p.OutputFile, flags, 0644); err != nil {
return err
} else {
p.fout = f
}
}
return nil
}
func (p *Program) LoadFile(f *os.File) error {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
proxy := strings.TrimFunc(scanner.Text(), unicode.IsSpace)
if len(proxy) == 0 {
continue
}
url, err := url.Parse(proxy)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid url %s\n", proxy)
continue
}
if p.Unique {
key := url.String()
p.scannedmu.Lock()
_, ok := p.scanned[key]
if !ok {
p.scanned[key] = struct{}{}
p.scannedmu.Unlock()
} else {
p.scannedmu.Unlock()
continue
}
}
p.pchan <- url
}
return scanner.Err()
}
func (p *Program) Run() error {
if err := p.Init(); err != nil {
return err
}
wg := &sync.WaitGroup{}
for i := uint(0); i < p.Workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
p.Worker()
}()
}
if len(p.ProxyFiles) == 0 {
p.ProxyFiles = append(p.ProxyFiles, "-")
}
for _, fname := range p.ProxyFiles {
var err error
var f *os.File
if fname == "-" {
f = os.Stdin
} else {
f, err = os.Open(fname)
if err != nil {
return err
}
}
err = p.LoadFile(f)
f.Close()
if err != nil {
return err
}
}
close(p.pchan)
wg.Wait()
return p.fout.Close()
}
func (p *Program) CheckProxy(url *url.URL) error {
var pd proxy.Dialer
if p.Proxy != nil {
tmp, err := proxy.FromURL(p.proxyUrl, nil)
if err != nil {
return err
}
pd = tmp
}
d, err := proxy.FromURL(url, pd)
if err != nil {
return err
}
dc, ok := d.(proxy.ContextDialer)
if !ok {
return fmt.Errorf("%s does not implement proxy.ContextDialer", url.Scheme)
}
var ctx context.Context
var cancel context.CancelFunc
if p.Timeout == 0 {
ctx = context.Background()
} else {
ctx, cancel = context.WithTimeout(context.Background(), time.Second*time.Duration(p.Timeout))
defer cancel()
}
conn, err := dc.DialContext(ctx, p.Network, p.Target)
if err != nil {
return err
}
defer conn.Close()
switch p.Validity {
case "tls":
host, _, err := net.SplitHostPort(p.Target)
if err != nil {
return err
}
tlsconn := tls.Client(conn, &tls.Config{ServerName: host})
conn = tlsconn
if err := tlsconn.HandshakeContext(ctx); err != nil {
return err
}
case "connect":
}
return nil
}
func (p *Program) Worker() {
for proxy := range p.pchan {
start := time.Now()
err := p.CheckProxy(proxy)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
if p.Detail {
proxy.Fragment = strconv.FormatFloat(time.Now().Sub(start).Seconds(), 'f', -1, 64)
}
fmt.Fprintln(p.fout, proxy.String())
}
}
func main() {
p := &Program{
Network: "tcp",
Target: "google.com:443",
OutputFile: "-",
Validity: "tls",
Workers: 8,
Timeout: 10,
}
parser := argparse.FromStruct(p)
if err := parser.ParseArgs(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := p.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}