girl 3.7.0 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of girl might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/girl.gemspec +1 -1
- data/lib/girl/concurrent_hash.rb +4 -0
- data/lib/girl/{custom_dns_query.rb → custom.rb} +1 -1
- data/lib/girl/head.rb +11 -1
- data/lib/girl/proxy.rb +28 -31
- data/lib/girl/proxy_custom.rb +3 -0
- data/lib/girl/proxy_worker.rb +507 -604
- data/lib/girl/proxyd.rb +1 -15
- data/lib/girl/proxyd_custom.rb +3 -0
- data/lib/girl/proxyd_worker.rb +530 -466
- data/lib/girl/relay.rb +0 -2
- data/lib/girl/relay_worker.rb +475 -585
- data/lib/girl/resolv_custom.rb +2 -2
- data/lib/girl/resolvd_worker.rb +4 -4
- data/lib/girl/ssl.rb +1 -14
- data/lib/girl/ssl_worker.rb +133 -91
- data/lib/girl/version.rb +1 -1
- metadata +3 -3
data/lib/girl/relay.rb
CHANGED
data/lib/girl/relay_worker.rb
CHANGED
@@ -13,24 +13,23 @@ module Girl
|
|
13
13
|
@directs = directs
|
14
14
|
@remotes = remotes
|
15
15
|
@custom = Girl::ProxyCustom.new( im )
|
16
|
-
@resolv_custom = Girl::ResolvCustom.new
|
17
16
|
@reads = []
|
18
17
|
@writes = []
|
19
18
|
@closing_rsvs = []
|
20
19
|
@closing_srcs = []
|
21
20
|
@paused_srcs = []
|
22
21
|
@paused_dsts = []
|
23
|
-
@
|
22
|
+
@paused_btuns = []
|
24
23
|
@resume_srcs = []
|
25
24
|
@resume_dsts = []
|
26
|
-
@
|
27
|
-
@pending_srcs = [] #
|
28
|
-
@roles = ConcurrentHash.new # sock => :dotr / :resolv / :rsv / :redir / :proxy / :src / :dst / :
|
25
|
+
@resume_btuns = []
|
26
|
+
@pending_srcs = [] # 还没得到atund和btund地址,暂存的src
|
27
|
+
@roles = ConcurrentHash.new # sock => :dotr / :resolv / :rsv / :redir / :proxy / :src / :dst / :atun / :btun
|
29
28
|
@rsv_infos = ConcurrentHash.new # rsv => {}
|
30
29
|
@src_infos = ConcurrentHash.new # src => {}
|
31
30
|
@dst_infos = ConcurrentHash.new # dst => {}
|
32
|
-
@
|
33
|
-
@
|
31
|
+
@atun_infos = ConcurrentHash.new # atun => {}
|
32
|
+
@btun_infos = ConcurrentHash.new # btun => {}
|
34
33
|
@is_direct_caches = ConcurrentHash.new # ip => true / false
|
35
34
|
@srcs = ConcurrentHash.new # src_id => src
|
36
35
|
@ip_address_list = Socket.ip_address_list
|
@@ -63,31 +62,27 @@ module Girl
|
|
63
62
|
read_rsv( sock )
|
64
63
|
when :redir then
|
65
64
|
read_redir( sock )
|
66
|
-
when :
|
67
|
-
|
65
|
+
when :ctl then
|
66
|
+
read_ctl( sock )
|
68
67
|
when :src then
|
69
68
|
read_src( sock )
|
70
69
|
when :dst then
|
71
70
|
read_dst( sock )
|
72
|
-
when :
|
73
|
-
|
71
|
+
when :btun then
|
72
|
+
read_btun( sock )
|
74
73
|
end
|
75
74
|
end
|
76
75
|
|
77
76
|
ws.each do | sock |
|
78
77
|
case @roles[ sock ]
|
79
|
-
when :proxy then
|
80
|
-
write_proxy( sock )
|
81
78
|
when :src then
|
82
79
|
write_src( sock )
|
83
80
|
when :dst then
|
84
81
|
write_dst( sock )
|
85
|
-
when :
|
86
|
-
|
87
|
-
when :
|
88
|
-
|
89
|
-
when :pre_tun then
|
90
|
-
write_pre_tun( sock )
|
82
|
+
when :atun then
|
83
|
+
write_atun( sock )
|
84
|
+
when :btun then
|
85
|
+
write_btun( sock )
|
91
86
|
end
|
92
87
|
end
|
93
88
|
end
|
@@ -100,13 +95,8 @@ module Girl
|
|
100
95
|
# quit!
|
101
96
|
#
|
102
97
|
def quit!
|
103
|
-
|
104
|
-
|
105
|
-
data = [ TUN_FIN ].pack( 'C' )
|
106
|
-
@proxy.write( data )
|
107
|
-
end
|
108
|
-
|
109
|
-
# puts "debug1 exit"
|
98
|
+
# puts "debug exit"
|
99
|
+
send_ctlmsg( [ CTL_FIN ].pack( 'C' ) )
|
110
100
|
exit
|
111
101
|
end
|
112
102
|
|
@@ -120,8 +110,24 @@ module Girl
|
|
120
110
|
destination_domain = src_info[ :destination_domain ]
|
121
111
|
destination_port = src_info[ :destination_port ]
|
122
112
|
domain_port = [ destination_domain, destination_port ].join( ':' )
|
123
|
-
|
124
|
-
|
113
|
+
# puts "debug add a new source #{ src_info[ :id ] } #{ domain_port }"
|
114
|
+
key = [ A_NEW_SOURCE, src_info[ :id ] ].pack( 'CQ>' )
|
115
|
+
add_ctlmsg( key, domain_port )
|
116
|
+
end
|
117
|
+
|
118
|
+
##
|
119
|
+
# add atun wbuff
|
120
|
+
#
|
121
|
+
def add_atun_wbuff( atun, data )
|
122
|
+
return if atun.closed?
|
123
|
+
atun_info = @atun_infos[ atun ]
|
124
|
+
atun_info[ :wbuff ] << data
|
125
|
+
add_write( atun )
|
126
|
+
|
127
|
+
if atun_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
|
128
|
+
puts "p#{ Process.pid } #{ Time.new } pause tunnel src #{ atun_info[ :domain ] }"
|
129
|
+
add_paused_src( atun_info[ :src ] )
|
130
|
+
end
|
125
131
|
end
|
126
132
|
|
127
133
|
##
|
@@ -145,16 +151,18 @@ module Girl
|
|
145
151
|
##
|
146
152
|
# add ctlmsg
|
147
153
|
#
|
148
|
-
def add_ctlmsg( data )
|
149
|
-
|
150
|
-
|
151
|
-
|
154
|
+
def add_ctlmsg( key, data )
|
155
|
+
ctlmsg = "#{ key }#{ data }"
|
156
|
+
send_ctlmsg( ctlmsg )
|
157
|
+
@ctl_info[ :resends ][ key ] = 0
|
158
|
+
loop_resend_ctlmsg( key, ctlmsg )
|
152
159
|
end
|
153
160
|
|
154
161
|
##
|
155
162
|
# add dst wbuff
|
156
163
|
#
|
157
164
|
def add_dst_wbuff( dst, data )
|
165
|
+
return if dst.closed?
|
158
166
|
dst_info = @dst_infos[ dst ]
|
159
167
|
dst_info[ :wbuff ] << data
|
160
168
|
add_write( dst )
|
@@ -165,6 +173,15 @@ module Girl
|
|
165
173
|
end
|
166
174
|
end
|
167
175
|
|
176
|
+
##
|
177
|
+
# add paused btun
|
178
|
+
#
|
179
|
+
def add_paused_btun( btun )
|
180
|
+
return if btun.closed? || @paused_btuns.include?( btun )
|
181
|
+
@reads.delete( btun )
|
182
|
+
@paused_btuns << btun
|
183
|
+
end
|
184
|
+
|
168
185
|
##
|
169
186
|
# add paused dst
|
170
187
|
#
|
@@ -183,15 +200,6 @@ module Girl
|
|
183
200
|
@paused_srcs << src
|
184
201
|
end
|
185
202
|
|
186
|
-
##
|
187
|
-
# add paused tun
|
188
|
-
#
|
189
|
-
def add_paused_tun( tun )
|
190
|
-
return if tun.closed? || @paused_tuns.include?( tun )
|
191
|
-
@reads.delete( tun )
|
192
|
-
@paused_tuns << tun
|
193
|
-
end
|
194
|
-
|
195
203
|
##
|
196
204
|
# add read
|
197
205
|
#
|
@@ -206,6 +214,15 @@ module Girl
|
|
206
214
|
next_tick
|
207
215
|
end
|
208
216
|
|
217
|
+
##
|
218
|
+
# add resume btun
|
219
|
+
#
|
220
|
+
def add_resume_btun( btun )
|
221
|
+
return if @resume_btuns.include?( btun )
|
222
|
+
@resume_btuns << btun
|
223
|
+
next_tick
|
224
|
+
end
|
225
|
+
|
209
226
|
##
|
210
227
|
# add resume dst
|
211
228
|
#
|
@@ -224,30 +241,6 @@ module Girl
|
|
224
241
|
next_tick
|
225
242
|
end
|
226
243
|
|
227
|
-
##
|
228
|
-
# add resume tun
|
229
|
-
#
|
230
|
-
def add_resume_tun( tun )
|
231
|
-
return if @resume_tuns.include?( tun )
|
232
|
-
@resume_tuns << tun
|
233
|
-
next_tick
|
234
|
-
end
|
235
|
-
|
236
|
-
##
|
237
|
-
# add socks5 conn reply
|
238
|
-
#
|
239
|
-
def add_socks5_conn_reply( src )
|
240
|
-
# +----+-----+-------+------+----------+----------+
|
241
|
-
# |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
|
242
|
-
# +----+-----+-------+------+----------+----------+
|
243
|
-
# | 1 | 1 | X'00' | 1 | Variable | 2 |
|
244
|
-
# +----+-----+-------+------+----------+----------+
|
245
|
-
redir_ip, redir_port = @redir_local_address.ip_unpack
|
246
|
-
data = [ [ 5, 0, 0, 1 ].pack( 'C4' ), IPAddr.new( redir_ip ).hton, [ redir_port ].pack( 'n' ) ].join
|
247
|
-
# puts "debug1 add src.wbuff socks5 conn reply #{ data.inspect }"
|
248
|
-
add_src_wbuff( src, data )
|
249
|
-
end
|
250
|
-
|
251
244
|
##
|
252
245
|
# add src rbuff
|
253
246
|
#
|
@@ -256,7 +249,7 @@ module Girl
|
|
256
249
|
src_info[ :rbuff ] << data
|
257
250
|
|
258
251
|
if src_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
|
259
|
-
# puts "
|
252
|
+
# puts "debug src.rbuff full"
|
260
253
|
add_closing_src( src )
|
261
254
|
end
|
262
255
|
end
|
@@ -278,30 +271,16 @@ module Girl
|
|
278
271
|
puts "p#{ Process.pid } #{ Time.new } pause dst #{ src_info[ :destination_domain ] }"
|
279
272
|
add_paused_dst( dst )
|
280
273
|
else
|
281
|
-
|
274
|
+
btun = src_info[ :btun ]
|
282
275
|
|
283
|
-
if
|
284
|
-
puts "p#{ Process.pid } #{ Time.new } pause
|
285
|
-
|
276
|
+
if btun then
|
277
|
+
puts "p#{ Process.pid } #{ Time.new } pause btun #{ src_info[ :destination_domain ] }"
|
278
|
+
add_paused_btun( btun )
|
286
279
|
end
|
287
280
|
end
|
288
281
|
end
|
289
282
|
end
|
290
283
|
|
291
|
-
##
|
292
|
-
# add tun wbuff
|
293
|
-
#
|
294
|
-
def add_tun_wbuff( tun, data )
|
295
|
-
tun_info = @tun_infos[ tun ]
|
296
|
-
tun_info[ :wbuff ] << data
|
297
|
-
add_write( tun )
|
298
|
-
|
299
|
-
if tun_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
|
300
|
-
puts "p#{ Process.pid } #{ Time.new } pause tunnel src #{ tun_info[ :domain ] }"
|
301
|
-
add_paused_src( tun_info[ :src ] )
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
284
|
##
|
306
285
|
# add write
|
307
286
|
#
|
@@ -312,32 +291,36 @@ module Girl
|
|
312
291
|
end
|
313
292
|
|
314
293
|
##
|
315
|
-
# close
|
294
|
+
# close atun
|
316
295
|
#
|
317
|
-
def
|
318
|
-
return if
|
319
|
-
# puts "
|
320
|
-
close_sock(
|
296
|
+
def close_atun( atun )
|
297
|
+
return if atun.closed?
|
298
|
+
# puts "debug close atun"
|
299
|
+
close_sock( atun )
|
300
|
+
atun_info = @atun_infos.delete( atun )
|
301
|
+
src = atun_info[ :src ]
|
302
|
+
|
303
|
+
if src then
|
304
|
+
@paused_srcs.delete( src )
|
305
|
+
end
|
321
306
|
end
|
322
307
|
|
323
308
|
##
|
324
|
-
# close
|
309
|
+
# close btun
|
325
310
|
#
|
326
|
-
def
|
327
|
-
return if
|
328
|
-
# puts "
|
329
|
-
close_sock(
|
330
|
-
|
311
|
+
def close_btun( btun )
|
312
|
+
return if btun.closed?
|
313
|
+
# puts "debug close btun"
|
314
|
+
close_sock( btun )
|
315
|
+
del_btun_info( btun )
|
331
316
|
end
|
332
317
|
|
333
318
|
##
|
334
|
-
# close
|
319
|
+
# close ctl
|
335
320
|
#
|
336
|
-
def
|
337
|
-
|
338
|
-
|
339
|
-
close_sock( proxy )
|
340
|
-
close_pre_proxy( @pre_proxy )
|
321
|
+
def close_ctl( ctl )
|
322
|
+
close_sock( ctl )
|
323
|
+
@ctl_info[ :resends ].clear
|
341
324
|
end
|
342
325
|
|
343
326
|
##
|
@@ -345,20 +328,15 @@ module Girl
|
|
345
328
|
#
|
346
329
|
def close_read_dst( dst )
|
347
330
|
return if dst.closed?
|
348
|
-
# puts "
|
331
|
+
# puts "debug close read dst"
|
349
332
|
dst.close_read
|
350
333
|
@reads.delete( dst )
|
351
334
|
|
352
335
|
if dst.closed? then
|
353
|
-
# puts "debug1 delete dst info"
|
354
336
|
@writes.delete( dst )
|
355
337
|
@roles.delete( dst )
|
356
|
-
|
357
|
-
else
|
358
|
-
dst_info = @dst_infos[ dst ]
|
338
|
+
@dst_infos.delete( dst )
|
359
339
|
end
|
360
|
-
|
361
|
-
dst_info
|
362
340
|
end
|
363
341
|
|
364
342
|
##
|
@@ -366,46 +344,22 @@ module Girl
|
|
366
344
|
#
|
367
345
|
def close_read_src( src )
|
368
346
|
return if src.closed?
|
369
|
-
# puts "
|
347
|
+
# puts "debug close read src"
|
370
348
|
src.close_read
|
371
349
|
@reads.delete( src )
|
372
350
|
|
373
351
|
if src.closed? then
|
374
|
-
# puts "debug1 delete src info"
|
375
352
|
@writes.delete( src )
|
376
353
|
@roles.delete( src )
|
377
|
-
|
378
|
-
else
|
379
|
-
src_info = @src_infos[ src ]
|
354
|
+
del_src_info( src )
|
380
355
|
end
|
381
|
-
|
382
|
-
src_info
|
383
|
-
end
|
384
|
-
|
385
|
-
##
|
386
|
-
# close read tun
|
387
|
-
#
|
388
|
-
def close_read_tun( tun )
|
389
|
-
return if tun.closed?
|
390
|
-
# puts "debug1 close read tun"
|
391
|
-
tun_info = @tun_infos[ tun ]
|
392
|
-
tun_info[ :close_read ] = true
|
393
|
-
|
394
|
-
if tun_info[ :close_write ] then
|
395
|
-
# puts "debug1 close tun"
|
396
|
-
close_tun( tun )
|
397
|
-
else
|
398
|
-
@reads.delete( tun )
|
399
|
-
end
|
400
|
-
|
401
|
-
tun_info
|
402
356
|
end
|
403
357
|
|
404
358
|
##
|
405
359
|
# close rsv
|
406
360
|
#
|
407
361
|
def close_rsv( rsv )
|
408
|
-
# puts "
|
362
|
+
# puts "debug close rsv"
|
409
363
|
rsv.close
|
410
364
|
@reads.delete( rsv )
|
411
365
|
@roles.delete( rsv )
|
@@ -427,59 +381,44 @@ module Girl
|
|
427
381
|
#
|
428
382
|
def close_src( src )
|
429
383
|
return if src.closed?
|
430
|
-
# puts "
|
384
|
+
# puts "debug close src"
|
431
385
|
close_sock( src )
|
432
386
|
src_info = del_src_info( src )
|
433
387
|
dst = src_info[ :dst ]
|
434
388
|
|
435
389
|
if dst then
|
436
390
|
close_sock( dst )
|
437
|
-
|
391
|
+
del_dst_info( dst )
|
438
392
|
else
|
439
|
-
|
393
|
+
atun = src_info[ :atun ]
|
394
|
+
btun = src_info[ :btun ]
|
440
395
|
|
441
|
-
if
|
442
|
-
|
443
|
-
|
444
|
-
|
396
|
+
if atun then
|
397
|
+
close_sock( atun )
|
398
|
+
@atun_infos.delete( atun )
|
399
|
+
end
|
445
400
|
|
446
|
-
|
447
|
-
|
448
|
-
|
401
|
+
if btun then
|
402
|
+
close_sock( btun )
|
403
|
+
del_btun_info( btun )
|
449
404
|
end
|
450
405
|
end
|
451
406
|
end
|
452
407
|
|
453
|
-
##
|
454
|
-
# close tun
|
455
|
-
#
|
456
|
-
def close_tun( tun )
|
457
|
-
return if tun.closed?
|
458
|
-
# puts "debug1 close tun"
|
459
|
-
close_sock( tun )
|
460
|
-
tun_info = @tun_infos.delete( tun )
|
461
|
-
close_pre_tun( tun_info[ :pre_tun ] )
|
462
|
-
end
|
463
|
-
|
464
408
|
##
|
465
409
|
# close write dst
|
466
410
|
#
|
467
411
|
def close_write_dst( dst )
|
468
412
|
return if dst.closed?
|
469
|
-
# puts "
|
413
|
+
# puts "debug close write dst"
|
470
414
|
dst.close_write
|
471
415
|
@writes.delete( dst )
|
472
416
|
|
473
417
|
if dst.closed? then
|
474
|
-
# puts "debug1 delete dst info"
|
475
418
|
@reads.delete( dst )
|
476
419
|
@roles.delete( dst )
|
477
|
-
|
478
|
-
else
|
479
|
-
dst_info = @dst_infos[ dst ]
|
420
|
+
del_dst_info( dst )
|
480
421
|
end
|
481
|
-
|
482
|
-
dst_info
|
483
422
|
end
|
484
423
|
|
485
424
|
##
|
@@ -487,39 +426,15 @@ module Girl
|
|
487
426
|
#
|
488
427
|
def close_write_src( src )
|
489
428
|
return if src.closed?
|
490
|
-
# puts "
|
429
|
+
# puts "debug close write src"
|
491
430
|
src.close_write
|
492
431
|
@writes.delete( src )
|
493
432
|
|
494
433
|
if src.closed? then
|
495
|
-
# puts "debug1 delete src info"
|
496
434
|
@reads.delete( src )
|
497
435
|
@roles.delete( src )
|
498
|
-
|
499
|
-
else
|
500
|
-
src_info = @src_infos[ src ]
|
436
|
+
del_src_info( src )
|
501
437
|
end
|
502
|
-
|
503
|
-
src_info
|
504
|
-
end
|
505
|
-
|
506
|
-
##
|
507
|
-
# close write tun
|
508
|
-
#
|
509
|
-
def close_write_tun( tun )
|
510
|
-
return if tun.closed?
|
511
|
-
# puts "debug1 close write tun"
|
512
|
-
tun_info = @tun_infos[ tun ]
|
513
|
-
tun_info[ :close_write ] = true
|
514
|
-
|
515
|
-
if tun_info[ :close_read ] then
|
516
|
-
# puts "debug1 close tun"
|
517
|
-
close_tun( tun )
|
518
|
-
else
|
519
|
-
@writes.delete( tun )
|
520
|
-
end
|
521
|
-
|
522
|
-
tun_info
|
523
438
|
end
|
524
439
|
|
525
440
|
##
|
@@ -529,9 +444,8 @@ module Girl
|
|
529
444
|
return if src.closed?
|
530
445
|
src_info = @src_infos[ src ]
|
531
446
|
|
532
|
-
if
|
533
|
-
|| ip_info.
|
534
|
-
|| ( ( @ip_address_list.any? { | addrinfo | addrinfo.ip_address == ip_info.ip_address } ) && ( src_info[ :destination_port ] == @redir_port ) ) then
|
447
|
+
if ( ( @ip_address_list.any? { | addrinfo | addrinfo.ip_address == ip_info.ip_address } ) && ( src_info[ :destination_port ] == @redir_port ) ) \
|
448
|
+
|| ( ( ip_info.ip_address == @proxyd_host ) && ( src_info[ :destination_port ] == @proxyd_port ) ) then
|
535
449
|
puts "p#{ Process.pid } #{ Time.new } ignore #{ ip_info.ip_address }:#{ src_info[ :destination_port ] }"
|
536
450
|
add_closing_src( src )
|
537
451
|
return
|
@@ -548,29 +462,49 @@ module Girl
|
|
548
462
|
is_direct = @is_direct_caches[ ip_info.ip_address ]
|
549
463
|
else
|
550
464
|
is_direct = @directs.any? { | direct | direct.include?( ip_info.ip_address ) }
|
551
|
-
# 判断直连耗时较长(树莓派 0.27秒),这里可能切去主线程,回来src可能已关闭
|
552
465
|
puts "p#{ Process.pid } #{ Time.new } cache is direct #{ ip_info.ip_address } #{ is_direct }"
|
553
466
|
@is_direct_caches[ ip_info.ip_address ] = is_direct
|
554
467
|
end
|
555
468
|
|
556
469
|
if is_direct then
|
557
|
-
# puts "
|
470
|
+
# puts "debug #{ ip_info.inspect } hit directs"
|
558
471
|
new_a_dst( src, ip_info )
|
559
472
|
else
|
560
|
-
#
|
561
|
-
# puts "debug1 #{ ip_info.inspect } go tunnel"
|
473
|
+
# puts "debug #{ ip_info.inspect } go tunnel"
|
562
474
|
set_proxy_type_tunnel( src )
|
563
475
|
end
|
564
476
|
end
|
565
477
|
|
478
|
+
##
|
479
|
+
# del btun info
|
480
|
+
#
|
481
|
+
def del_btun_info( btun )
|
482
|
+
@btun_infos.delete( btun )
|
483
|
+
@paused_btuns.delete( btun )
|
484
|
+
@resume_btuns.delete( btun )
|
485
|
+
end
|
486
|
+
|
487
|
+
##
|
488
|
+
# del dst info
|
489
|
+
#
|
490
|
+
def del_dst_info( dst )
|
491
|
+
# puts "debug delete dst info"
|
492
|
+
dst_info = @dst_infos.delete( dst )
|
493
|
+
@paused_dsts.delete( dst )
|
494
|
+
@resume_dsts.delete( dst )
|
495
|
+
dst_info
|
496
|
+
end
|
497
|
+
|
566
498
|
##
|
567
499
|
# del src info
|
568
500
|
#
|
569
501
|
def del_src_info( src )
|
502
|
+
# puts "debug delete src info"
|
570
503
|
src_info = @src_infos.delete( src )
|
571
504
|
@srcs.delete( src_info[ :id ] )
|
572
505
|
@pending_srcs.delete( src )
|
573
|
-
|
506
|
+
@paused_srcs.delete( src )
|
507
|
+
@resume_srcs.delete( src )
|
574
508
|
src_info
|
575
509
|
end
|
576
510
|
|
@@ -583,28 +517,14 @@ module Girl
|
|
583
517
|
sleep CHECK_EXPIRE_INTERVAL
|
584
518
|
now = Time.new
|
585
519
|
|
586
|
-
if @
|
587
|
-
|
588
|
-
last_recv_at = @proxy_info[ :last_recv_at ]
|
589
|
-
expire_after = EXPIRE_AFTER
|
590
|
-
else
|
591
|
-
last_recv_at = @proxy_info[ :created_at ]
|
592
|
-
expire_after = EXPIRE_NEW
|
593
|
-
end
|
520
|
+
if @ctl && !@ctl.closed? then
|
521
|
+
last_recv_at = @ctl_info[ :last_recv_at ] || @ctl_info[ :created_at ]
|
594
522
|
|
595
|
-
if now - last_recv_at >=
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
else
|
600
|
-
# puts "debug1 #{ Time.new } send heartbeat"
|
601
|
-
data = [ HEARTBEAT ].pack( 'C' )
|
602
|
-
add_ctlmsg( data )
|
523
|
+
if now - last_recv_at >= EXPIRE_AFTER then
|
524
|
+
puts "p#{ Process.pid } #{ Time.new } expire ctl"
|
525
|
+
@ctl_info[ :closing ] = true
|
526
|
+
next_tick
|
603
527
|
end
|
604
|
-
elsif @pre_proxy && !@pre_proxy.closed? && ( now - @pre_proxy_info[ :created_at ] > EXPIRE_NEW ) then
|
605
|
-
puts "p#{ Process.pid } #{ Time.new } expire pre proxy"
|
606
|
-
@pre_proxy_info[ :closing ] = true
|
607
|
-
next_tick
|
608
528
|
end
|
609
529
|
|
610
530
|
@rsv_infos.each do | rsv, rsv_info |
|
@@ -620,7 +540,7 @@ module Girl
|
|
620
540
|
expire_after = ( src_info[ :dst ] || src_info[ :tun ] ) ? EXPIRE_AFTER : EXPIRE_NEW
|
621
541
|
|
622
542
|
if ( now - last_recv_at >= expire_after ) && ( now - last_sent_at >= expire_after ) then
|
623
|
-
puts "p#{ Process.pid } #{ Time.new } expire src #{ expire_after } #{ src_info[ :destination_domain ] }"
|
543
|
+
puts "p#{ Process.pid } #{ Time.new } expire src #{ expire_after } #{ src_info[ :id ] } #{ src_info[ :destination_domain ] }"
|
624
544
|
add_closing_src( src )
|
625
545
|
|
626
546
|
unless src_info[ :rbuff ].empty? then
|
@@ -641,24 +561,25 @@ module Girl
|
|
641
561
|
sleep CHECK_RESUME_INTERVAL
|
642
562
|
|
643
563
|
@paused_srcs.each do | src |
|
644
|
-
|
645
|
-
|
646
|
-
else
|
647
|
-
src_info = @src_infos[ src ]
|
648
|
-
dst = src_info[ :dst ]
|
564
|
+
src_info = @src_infos[ src ]
|
565
|
+
dst = src_info[ :dst ]
|
649
566
|
|
650
|
-
|
567
|
+
if dst then
|
568
|
+
if !dst.closed? then
|
651
569
|
dst_info = @dst_infos[ dst ]
|
652
570
|
|
653
571
|
if dst_info[ :wbuff ].size < RESUME_BELOW then
|
654
572
|
puts "p#{ Process.pid } #{ Time.new } resume direct src #{ src_info[ :destination_domain ] }"
|
655
573
|
add_resume_src( src )
|
656
574
|
end
|
657
|
-
|
658
|
-
|
659
|
-
|
575
|
+
end
|
576
|
+
else
|
577
|
+
atun = src_info[ :atun ]
|
660
578
|
|
661
|
-
|
579
|
+
if atun && !atun.closed? then
|
580
|
+
atun_info = @atun_infos[ atun ]
|
581
|
+
|
582
|
+
if atun_info[ :wbuff ].size < RESUME_BELOW then
|
662
583
|
puts "p#{ Process.pid } #{ Time.new } resume tunnel src #{ src_info[ :destination_domain ] }"
|
663
584
|
add_resume_src( src )
|
664
585
|
end
|
@@ -667,11 +588,10 @@ module Girl
|
|
667
588
|
end
|
668
589
|
|
669
590
|
@paused_dsts.each do | dst |
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
src = dst_info[ :src ]
|
591
|
+
dst_info = @dst_infos[ dst ]
|
592
|
+
src = dst_info[ :src ]
|
593
|
+
|
594
|
+
if src && !src.closed? then
|
675
595
|
src_info = @src_infos[ src ]
|
676
596
|
|
677
597
|
if src_info[ :wbuff ].size < RESUME_BELOW then
|
@@ -681,17 +601,16 @@ module Girl
|
|
681
601
|
end
|
682
602
|
end
|
683
603
|
|
684
|
-
@
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
src = tun_info[ :src ]
|
604
|
+
@paused_btuns.each do | btun |
|
605
|
+
btun_info = @btun_infos[ btun ]
|
606
|
+
src = btun_info[ :src ]
|
607
|
+
|
608
|
+
if src && !src.closed? then
|
690
609
|
src_info = @src_infos[ src ]
|
691
610
|
|
692
611
|
if src_info[ :wbuff ].size < RESUME_BELOW then
|
693
|
-
puts "p#{ Process.pid } #{ Time.new } resume
|
694
|
-
|
612
|
+
puts "p#{ Process.pid } #{ Time.new } resume btun #{ btun_info[ :domain ] }"
|
613
|
+
add_resume_btun( btun )
|
695
614
|
end
|
696
615
|
end
|
697
616
|
end
|
@@ -699,6 +618,31 @@ module Girl
|
|
699
618
|
end
|
700
619
|
end
|
701
620
|
|
621
|
+
##
|
622
|
+
# loop resend ctlmsg
|
623
|
+
#
|
624
|
+
def loop_resend_ctlmsg( key, ctlmsg )
|
625
|
+
Thread.new do
|
626
|
+
loop do
|
627
|
+
sleep RESEND_INTERVAL
|
628
|
+
|
629
|
+
resend = @ctl_info[ :resends ][ key ]
|
630
|
+
break unless resend
|
631
|
+
|
632
|
+
puts "p#{ Process.pid } #{ Time.new } resend #{ ctlmsg.inspect }"
|
633
|
+
send_ctlmsg( ctlmsg )
|
634
|
+
resend += 1
|
635
|
+
|
636
|
+
if resend >= RESEND_LIMIT then
|
637
|
+
@ctl_info[ :resends ].delete( key )
|
638
|
+
break
|
639
|
+
end
|
640
|
+
|
641
|
+
@ctl_info[ :resends ][ key ] = resend
|
642
|
+
end
|
643
|
+
end
|
644
|
+
end
|
645
|
+
|
702
646
|
##
|
703
647
|
# new a dst
|
704
648
|
#
|
@@ -721,7 +665,7 @@ module Girl
|
|
721
665
|
return
|
722
666
|
end
|
723
667
|
|
724
|
-
# puts "
|
668
|
+
# puts "debug a new dst #{ dst.local_address.inspect }"
|
725
669
|
dst_info = {
|
726
670
|
src: src, # 对应src
|
727
671
|
domain: domain, # 目的地
|
@@ -735,72 +679,38 @@ module Girl
|
|
735
679
|
src_info[ :dst ] = dst
|
736
680
|
|
737
681
|
if src_info[ :rbuff ] then
|
738
|
-
# puts "
|
682
|
+
# puts "debug move src.rbuff to dst.wbuff"
|
739
683
|
dst_info[ :wbuff ] << src_info[ :rbuff ]
|
740
684
|
add_write( dst )
|
741
685
|
end
|
742
686
|
end
|
743
687
|
|
744
688
|
##
|
745
|
-
# new a
|
689
|
+
# new a ctl
|
746
690
|
#
|
747
|
-
def
|
748
|
-
|
749
|
-
pre_proxy.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
|
691
|
+
def new_a_ctl
|
692
|
+
ctl = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
|
750
693
|
|
751
694
|
if RUBY_PLATFORM.include?( 'linux' ) then
|
752
|
-
|
753
|
-
pre_proxy.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
|
695
|
+
ctl.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1 )
|
754
696
|
end
|
755
697
|
|
756
|
-
|
757
|
-
|
758
|
-
rescue IO::WaitWritable
|
759
|
-
rescue Exception => e
|
760
|
-
puts "p#{ Process.pid } #{ Time.new } connect proxyd #{ e.class }, close pre proxy"
|
761
|
-
pre_proxy.close
|
762
|
-
return
|
763
|
-
end
|
698
|
+
@ctl = ctl
|
699
|
+
add_read( ctl, :ctl )
|
764
700
|
|
765
|
-
@
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
701
|
+
@ctl_info = {
|
702
|
+
resends: ConcurrentHash.new, # key => count
|
703
|
+
atund_addr: nil, # atund地址,src->dst
|
704
|
+
btund_addr: nil, # btund地址,dst->src
|
705
|
+
closing: false, # 准备关闭
|
706
|
+
created_at: Time.new, # 创建时间
|
707
|
+
last_recv_at: nil # 最近一次收到数据时间
|
770
708
|
}
|
771
|
-
add_write( pre_proxy )
|
772
|
-
end
|
773
|
-
|
774
|
-
##
|
775
|
-
# new a pre tun
|
776
|
-
#
|
777
|
-
def new_a_pre_tun( src_id, dst_id )
|
778
|
-
src = @srcs[ src_id ]
|
779
|
-
return if src.nil? || src.closed?
|
780
|
-
src_info = @src_infos[ src ]
|
781
|
-
return if src_info[ :dst_id ]
|
782
|
-
|
783
|
-
pre_tun = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
784
|
-
pre_tun.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 ) if RUBY_PLATFORM.include?( 'linux' )
|
785
|
-
|
786
|
-
begin
|
787
|
-
pre_tun.connect_nonblock( @proxy_info[ :tund_addr ] )
|
788
|
-
rescue IO::WaitWritable
|
789
|
-
rescue Exception => e
|
790
|
-
puts "p#{ Process.pid } #{ Time.new } connect tund #{ e.class }, close pre tun"
|
791
|
-
pre_tun.close
|
792
|
-
return
|
793
|
-
end
|
794
709
|
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
destination_domain: src_info[ :destination_domain ],
|
800
|
-
created_at: Time.new
|
801
|
-
}
|
802
|
-
@roles[ pre_tun ] = :pre_tun
|
803
|
-
add_write( pre_tun )
|
710
|
+
hello = @custom.hello
|
711
|
+
puts "p#{ Process.pid } #{ Time.new } hello i'm #{ hello.inspect }"
|
712
|
+
key = [ HELLO ].pack( 'C' )
|
713
|
+
add_ctlmsg( key, hello )
|
804
714
|
end
|
805
715
|
|
806
716
|
##
|
@@ -810,7 +720,6 @@ module Girl
|
|
810
720
|
redir = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
811
721
|
redir.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1 )
|
812
722
|
redir.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1 )
|
813
|
-
redir.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
|
814
723
|
redir.bind( Socket.sockaddr_in( redir_port, '0.0.0.0' ) )
|
815
724
|
redir.listen( 127 )
|
816
725
|
puts "p#{ Process.pid } #{ Time.new } redir listen on #{ redir_port }"
|
@@ -841,7 +750,7 @@ module Girl
|
|
841
750
|
rsv.bind( Socket.sockaddr_in( 0, '0.0.0.0' ) )
|
842
751
|
|
843
752
|
if @qnames.any? { | qname | data.include?( qname ) } then
|
844
|
-
data = @
|
753
|
+
data = @custom.encode( data )
|
845
754
|
to_addr = @resolvd_addr
|
846
755
|
else
|
847
756
|
to_addr = @nameserver_addr
|
@@ -857,6 +766,76 @@ module Girl
|
|
857
766
|
send_data( rsv, to_addr, data )
|
858
767
|
end
|
859
768
|
|
769
|
+
##
|
770
|
+
# new tuns
|
771
|
+
#
|
772
|
+
def new_tuns( src_id, dst_id )
|
773
|
+
return if @ctl_info[ :atund_addr ].nil? || @ctl_info[ :btund_addr ].nil?
|
774
|
+
src = @srcs[ src_id ]
|
775
|
+
return if src.nil? || src.closed?
|
776
|
+
src_info = @src_infos[ src ]
|
777
|
+
return if src_info[ :dst_id ]
|
778
|
+
|
779
|
+
# puts "debug new atun and btun"
|
780
|
+
atun = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
781
|
+
|
782
|
+
begin
|
783
|
+
atun.connect_nonblock( @ctl_info[ :atund_addr ] )
|
784
|
+
rescue IO::WaitWritable
|
785
|
+
rescue Exception => e
|
786
|
+
puts "p#{ Process.pid } #{ Time.new } connect atund #{ e.class }, close atun"
|
787
|
+
atun.close
|
788
|
+
return
|
789
|
+
end
|
790
|
+
|
791
|
+
btun = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
|
792
|
+
|
793
|
+
begin
|
794
|
+
btun.connect_nonblock( @ctl_info[ :btund_addr ] )
|
795
|
+
rescue IO::WaitWritable
|
796
|
+
rescue Exception => e
|
797
|
+
puts "p#{ Process.pid } #{ Time.new } connect btund #{ e.class }, close btun"
|
798
|
+
btun.close
|
799
|
+
return
|
800
|
+
end
|
801
|
+
|
802
|
+
domain = src_info[ :destination_domain ]
|
803
|
+
atun_wbuff = [ dst_id ].pack( 'n' )
|
804
|
+
|
805
|
+
until src_info[ :rbuff ].empty? do
|
806
|
+
data = src_info[ :rbuff ][ 0, 65535 ]
|
807
|
+
data_size = data.bytesize
|
808
|
+
# puts "debug move src.rbuff #{ data_size } to atun.wbuff"
|
809
|
+
atun_wbuff << pack_a_chunk( data )
|
810
|
+
src_info[ :rbuff ] = src_info[ :rbuff ][ data_size..-1 ]
|
811
|
+
end
|
812
|
+
|
813
|
+
@atun_infos[ atun ] = {
|
814
|
+
src: src, # 对应src
|
815
|
+
domain: domain, # 目的地
|
816
|
+
wbuff: atun_wbuff, # 写前
|
817
|
+
closing: false # 准备关闭
|
818
|
+
}
|
819
|
+
|
820
|
+
btun_wbuff = [ dst_id ].pack( 'n' )
|
821
|
+
|
822
|
+
@btun_infos[ btun ] = {
|
823
|
+
src: src, # 对应src
|
824
|
+
domain: domain, # 目的地
|
825
|
+
wbuff: btun_wbuff, # 写前
|
826
|
+
rbuff: '', # 暂存当前块没收全的流量
|
827
|
+
wait_bytes: 0 # 还差多少字节收全当前块
|
828
|
+
}
|
829
|
+
|
830
|
+
src_info[ :dst_id ] = dst_id
|
831
|
+
src_info[ :atun ] = atun
|
832
|
+
src_info[ :btun ] = btun
|
833
|
+
add_read( atun, :atun )
|
834
|
+
add_read( btun, :btun )
|
835
|
+
add_write( atun )
|
836
|
+
add_write( btun )
|
837
|
+
end
|
838
|
+
|
860
839
|
##
|
861
840
|
# next tick
|
862
841
|
#
|
@@ -864,15 +843,50 @@ module Girl
|
|
864
843
|
@dotw.write( '.' )
|
865
844
|
end
|
866
845
|
|
846
|
+
##
|
847
|
+
# pack a chunk
|
848
|
+
#
|
849
|
+
def pack_a_chunk( data )
|
850
|
+
# puts "debug pack a chunk"
|
851
|
+
data = @custom.encode( data )
|
852
|
+
"#{ [ data.bytesize ].pack( 'n' ) }#{ data }"
|
853
|
+
end
|
854
|
+
|
855
|
+
##
|
856
|
+
# send ctlmsg
|
857
|
+
#
|
858
|
+
def send_ctlmsg( data )
|
859
|
+
return if @ctl.nil? || @ctl.closed?
|
860
|
+
data = @custom.encode( data )
|
861
|
+
|
862
|
+
begin
|
863
|
+
@ctl.sendmsg( data, 0, @proxyd_addr )
|
864
|
+
@ctl_info[ :last_sent_at ] = Time.new
|
865
|
+
rescue Exception => e
|
866
|
+
puts "p#{ Process.pid } #{ Time.new } sendmsg #{ e.class }"
|
867
|
+
close_ctl( @ctl )
|
868
|
+
end
|
869
|
+
end
|
870
|
+
|
871
|
+
##
|
872
|
+
# set atun closing
|
873
|
+
#
|
874
|
+
def set_atun_closing( atun )
|
875
|
+
return if atun.closed?
|
876
|
+
atun_info = @atun_infos[ atun ]
|
877
|
+
return if atun_info[ :closing ]
|
878
|
+
# puts "debug set atun closing"
|
879
|
+
atun_info[ :closing ] = true
|
880
|
+
add_write( atun )
|
881
|
+
end
|
882
|
+
|
867
883
|
##
|
868
884
|
# send data
|
869
885
|
#
|
870
886
|
def send_data( sock, to_addr, data )
|
871
887
|
begin
|
872
888
|
sock.sendmsg( data, 0, to_addr )
|
873
|
-
rescue
|
874
|
-
print 'w'
|
875
|
-
rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
|
889
|
+
rescue Exception => e
|
876
890
|
puts "p#{ Process.pid } #{ Time.new } sendmsg to #{ to_addr.ip_unpack.inspect } #{ e.class }"
|
877
891
|
end
|
878
892
|
end
|
@@ -897,7 +911,7 @@ module Girl
|
|
897
911
|
src_info[ :proxy_type ] = :tunnel
|
898
912
|
src_id = src_info[ :id ]
|
899
913
|
|
900
|
-
if @
|
914
|
+
if @ctl && !@ctl.closed? && @ctl_info[ :atund_addr ] then
|
901
915
|
add_a_new_source( src )
|
902
916
|
else
|
903
917
|
@pending_srcs << src
|
@@ -915,48 +929,15 @@ module Girl
|
|
915
929
|
add_write( src )
|
916
930
|
end
|
917
931
|
|
918
|
-
##
|
919
|
-
# set tun closing write
|
920
|
-
#
|
921
|
-
def set_tun_closing_write( tun )
|
922
|
-
return if tun.closed?
|
923
|
-
tun_info = @tun_infos[ tun ]
|
924
|
-
return if tun_info[ :closing_write ]
|
925
|
-
tun_info[ :closing_write ] = true
|
926
|
-
add_write( tun )
|
927
|
-
end
|
928
|
-
|
929
|
-
##
|
930
|
-
# sub http request
|
931
|
-
#
|
932
|
-
def sub_http_request( data )
|
933
|
-
lines = data.split( "\r\n" )
|
934
|
-
|
935
|
-
return [ data, nil ] if lines.empty?
|
936
|
-
|
937
|
-
method, url, proto = lines.first.split( ' ' )
|
938
|
-
|
939
|
-
if proto && url && proto[ 0, 4 ] == 'HTTP' && url[ 0, 7 ] == 'http://' then
|
940
|
-
domain_port = url.split( '/' )[ 2 ]
|
941
|
-
data = data.sub( "http://#{ domain_port }", '' )
|
942
|
-
# puts "debug1 subed #{ data.inspect } #{ domain_port }"
|
943
|
-
end
|
944
|
-
|
945
|
-
[ data, domain_port ]
|
946
|
-
end
|
947
|
-
|
948
932
|
##
|
949
933
|
# read dotr
|
950
934
|
#
|
951
935
|
def read_dotr( dotr )
|
952
|
-
dotr.read_nonblock(
|
936
|
+
dotr.read_nonblock( 65535 )
|
953
937
|
|
954
|
-
if @
|
955
|
-
|
956
|
-
|
957
|
-
end
|
958
|
-
elsif @pre_proxy && !@pre_proxy.closed? && @pre_proxy_info[ :closing ] then
|
959
|
-
close_pre_proxy( @pre_proxy )
|
938
|
+
if @ctl_info && @ctl_info[ :closing ] then
|
939
|
+
send_ctlmsg( [ CTL_FIN ].pack( 'C' ) )
|
940
|
+
close_ctl( @ctl )
|
960
941
|
end
|
961
942
|
|
962
943
|
if @closing_rsvs.any? then
|
@@ -987,13 +968,13 @@ module Girl
|
|
987
968
|
@resume_dsts.clear
|
988
969
|
end
|
989
970
|
|
990
|
-
if @
|
991
|
-
@
|
992
|
-
add_read(
|
993
|
-
@
|
971
|
+
if @resume_btuns.any? then
|
972
|
+
@resume_btuns.each do | btun |
|
973
|
+
add_read( btun )
|
974
|
+
@paused_btuns.delete( btun )
|
994
975
|
end
|
995
976
|
|
996
|
-
@
|
977
|
+
@resume_btuns.clear
|
997
978
|
end
|
998
979
|
end
|
999
980
|
|
@@ -1002,7 +983,7 @@ module Girl
|
|
1002
983
|
#
|
1003
984
|
def read_resolv( resolv )
|
1004
985
|
data, addrinfo, rflags, *controls = resolv.recvmsg
|
1005
|
-
# puts "
|
986
|
+
# puts "debug resolv recvmsg #{ addrinfo.ip_unpack.inspect } #{ data.inspect }"
|
1006
987
|
new_a_rsv( addrinfo.to_sockaddr, data )
|
1007
988
|
end
|
1008
989
|
|
@@ -1011,10 +992,10 @@ module Girl
|
|
1011
992
|
#
|
1012
993
|
def read_rsv( rsv )
|
1013
994
|
data, addrinfo, rflags, *controls = rsv.recvmsg
|
1014
|
-
# puts "
|
995
|
+
# puts "debug rsv recvmsg #{ addrinfo.ip_unpack.inspect } #{ data.inspect }"
|
1015
996
|
|
1016
997
|
if addrinfo.to_sockaddr == @resolvd_addr then
|
1017
|
-
data = @
|
998
|
+
data = @custom.decode( data )
|
1018
999
|
end
|
1019
1000
|
|
1020
1001
|
rsv_info = @rsv_infos[ rsv ]
|
@@ -1047,7 +1028,7 @@ module Girl
|
|
1047
1028
|
dest_ip = dest_addrinfo.ip_address
|
1048
1029
|
|
1049
1030
|
src_id = rand( ( 2 ** 64 ) - 2 ) + 1
|
1050
|
-
# puts "
|
1031
|
+
# puts "debug accept a src #{ addrinfo.ip_unpack.inspect } to #{ dest_ip }:#{ dest_port } #{ src_id }"
|
1051
1032
|
|
1052
1033
|
@srcs[ src_id ] = src
|
1053
1034
|
@src_infos[ src ] = {
|
@@ -1057,10 +1038,11 @@ module Girl
|
|
1057
1038
|
destination_port: dest_port, # 目的地端口
|
1058
1039
|
rbuff: '', # 读到的流量
|
1059
1040
|
dst: nil, # :direct的场合,对应的dst
|
1060
|
-
|
1061
|
-
|
1041
|
+
ctl: nil, # :tunnel的场合,对应的ctl
|
1042
|
+
atun: nil, # :tunnel的场合,对应的atun
|
1043
|
+
btun: nil, # :tunnel的场合,对应的btun
|
1062
1044
|
dst_id: nil, # 远端dst id
|
1063
|
-
wbuff: '', # 从dst/
|
1045
|
+
wbuff: '', # 从dst/btun读到的流量
|
1064
1046
|
created_at: Time.new, # 创建时间
|
1065
1047
|
last_recv_at: nil, # 上一次收到新流量(由dst收到,或者由tun收到)的时间
|
1066
1048
|
last_sent_at: nil, # 上一次发出流量(由dst发出,或者由tun发出)的时间
|
@@ -1069,67 +1051,53 @@ module Girl
|
|
1069
1051
|
|
1070
1052
|
add_read( src, :src )
|
1071
1053
|
|
1072
|
-
|
1073
|
-
|
1074
|
-
new_a_pre_proxy
|
1054
|
+
if @ctl.nil? || @ctl.closed? then
|
1055
|
+
new_a_ctl
|
1075
1056
|
end
|
1076
1057
|
|
1077
1058
|
deal_with_destination_ip( src, dest_addrinfo )
|
1078
1059
|
end
|
1079
1060
|
|
1080
1061
|
##
|
1081
|
-
# read
|
1062
|
+
# read ctl
|
1082
1063
|
#
|
1083
|
-
def
|
1084
|
-
if proxy.closed? then
|
1085
|
-
puts "p#{ Process.pid } #{ Time.new } read proxy but proxy closed?"
|
1086
|
-
return
|
1087
|
-
end
|
1088
|
-
|
1064
|
+
def read_ctl( ctl )
|
1089
1065
|
begin
|
1090
|
-
data =
|
1091
|
-
rescue IO::WaitReadable
|
1092
|
-
return
|
1093
|
-
rescue Errno::EINTR
|
1094
|
-
puts e.class
|
1095
|
-
return
|
1066
|
+
data, addrinfo, rflags, *controls = ctl.recvmsg
|
1096
1067
|
rescue Exception => e
|
1097
|
-
|
1098
|
-
|
1068
|
+
puts "p#{ Process.pid } #{ Time.new } recvmsg #{ e.class }"
|
1069
|
+
close_ctl( ctl )
|
1099
1070
|
return
|
1100
1071
|
end
|
1101
1072
|
|
1102
|
-
|
1103
|
-
|
1104
|
-
data.split( SEPARATE ).each do | ctlmsg |
|
1105
|
-
next unless ctlmsg[ 0 ]
|
1106
|
-
|
1107
|
-
ctl_num = ctlmsg[ 0 ].unpack( 'C' ).first
|
1073
|
+
data = @custom.decode( data )
|
1074
|
+
ctl_num = data[ 0 ].unpack( 'C' ).first
|
1108
1075
|
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1076
|
+
case ctl_num
|
1077
|
+
when TUND_PORT then
|
1078
|
+
return if @ctl_info[ :atund_addr ] || data.size != 5
|
1079
|
+
atund_port, btund_port = data[ 1, 4 ].unpack( 'nn' )
|
1080
|
+
puts "p#{ Process.pid } #{ Time.new } got tund port #{ atund_port } #{ btund_port }"
|
1081
|
+
@ctl_info[ :resends ].delete( [ HELLO ].pack( 'C' ) )
|
1082
|
+
@ctl_info[ :atund_addr ] = Socket.sockaddr_in( atund_port, @proxyd_host )
|
1083
|
+
@ctl_info[ :btund_addr ] = Socket.sockaddr_in( btund_port, @proxyd_host )
|
1084
|
+
@ctl_info[ :last_recv_at ] = Time.new
|
1115
1085
|
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
end
|
1121
|
-
when PAIRED then
|
1122
|
-
next if ctlmsg.size != 11
|
1123
|
-
src_id, dst_id = ctlmsg[ 1, 10 ].unpack( 'Q>n' )
|
1124
|
-
# puts "debug1 got paired #{ src_id } #{ dst_id }"
|
1125
|
-
new_a_pre_tun( src_id, dst_id )
|
1126
|
-
when TUND_FIN then
|
1127
|
-
puts "p#{ Process.pid } #{ Time.new } got tund fin"
|
1128
|
-
close_proxy( proxy )
|
1129
|
-
return
|
1130
|
-
when HEARTBEAT
|
1131
|
-
# puts "debug1 #{ Time.new } got heartbeat"
|
1086
|
+
if @pending_srcs.any? then
|
1087
|
+
puts "p#{ Process.pid } #{ Time.new } send pending sources"
|
1088
|
+
@pending_srcs.each { | src | add_a_new_source( src ) }
|
1089
|
+
@pending_srcs.clear
|
1132
1090
|
end
|
1091
|
+
when PAIRED then
|
1092
|
+
return if data.size != 11
|
1093
|
+
src_id, dst_id = data[ 1, 10 ].unpack( 'Q>n' )
|
1094
|
+
# puts "debug got paired #{ src_id } #{ dst_id }"
|
1095
|
+
@ctl_info[ :resends ].delete( [ A_NEW_SOURCE, src_id ].pack( 'CQ>' ) )
|
1096
|
+
@ctl_info[ :last_recv_at ] = Time.new
|
1097
|
+
new_tuns( src_id, dst_id )
|
1098
|
+
when UNKNOWN_CTL_ADDR then
|
1099
|
+
puts "p#{ Process.pid } #{ Time.new } got unknown ctl addr, close ctl"
|
1100
|
+
close_ctl( ctl )
|
1133
1101
|
end
|
1134
1102
|
end
|
1135
1103
|
|
@@ -1142,55 +1110,53 @@ module Girl
|
|
1142
1110
|
return
|
1143
1111
|
end
|
1144
1112
|
|
1113
|
+
src_info = @src_infos[ src ]
|
1114
|
+
|
1145
1115
|
begin
|
1146
|
-
data = src.read_nonblock(
|
1147
|
-
rescue IO::WaitReadable
|
1116
|
+
data = src.read_nonblock( 65535 )
|
1117
|
+
rescue IO::WaitReadable
|
1148
1118
|
print 'r'
|
1149
1119
|
return
|
1150
1120
|
rescue Exception => e
|
1151
|
-
# puts "
|
1152
|
-
|
1121
|
+
# puts "debug read src #{ e.class }"
|
1122
|
+
close_read_src( src )
|
1153
1123
|
dst = src_info[ :dst ]
|
1154
1124
|
|
1155
1125
|
if dst then
|
1156
1126
|
set_dst_closing_write( dst )
|
1157
1127
|
else
|
1158
|
-
|
1159
|
-
|
1128
|
+
atun = src_info[ :atun ]
|
1129
|
+
|
1130
|
+
if atun then
|
1131
|
+
set_atun_closing( atun )
|
1132
|
+
end
|
1160
1133
|
end
|
1161
1134
|
|
1162
1135
|
return
|
1163
1136
|
end
|
1164
1137
|
|
1165
|
-
src_info = @src_infos[ src ]
|
1166
1138
|
proxy_type = src_info[ :proxy_type ]
|
1167
1139
|
|
1168
1140
|
case proxy_type
|
1169
1141
|
when :checking then
|
1170
|
-
# puts "
|
1142
|
+
# puts "debug add src rbuff before resolved #{ data.inspect }"
|
1171
1143
|
src_info[ :rbuff ] << data
|
1172
1144
|
when :tunnel then
|
1173
|
-
|
1145
|
+
atun = src_info[ :atun ]
|
1174
1146
|
|
1175
|
-
if
|
1176
|
-
|
1177
|
-
# puts "debug2 add tun.wbuff #{ data.bytesize }"
|
1178
|
-
add_tun_wbuff( tun, data )
|
1179
|
-
end
|
1147
|
+
if atun then
|
1148
|
+
add_atun_wbuff( atun, pack_a_chunk( data ) )
|
1180
1149
|
else
|
1181
|
-
# puts "
|
1150
|
+
# puts "debug add src.rbuff #{ data.bytesize }"
|
1182
1151
|
add_src_rbuff( src, data )
|
1183
1152
|
end
|
1184
1153
|
when :direct then
|
1185
1154
|
dst = src_info[ :dst ]
|
1186
1155
|
|
1187
1156
|
if dst then
|
1188
|
-
|
1189
|
-
# puts "debug2 add dst.wbuff #{ data.bytesize }"
|
1190
|
-
add_dst_wbuff( dst, data )
|
1191
|
-
end
|
1157
|
+
add_dst_wbuff( dst, data )
|
1192
1158
|
else
|
1193
|
-
# puts "
|
1159
|
+
# puts "debug add src.rbuff #{ data.bytesize }"
|
1194
1160
|
add_src_rbuff( src, data )
|
1195
1161
|
end
|
1196
1162
|
end
|
@@ -1205,82 +1171,88 @@ module Girl
|
|
1205
1171
|
return
|
1206
1172
|
end
|
1207
1173
|
|
1174
|
+
dst_info = @dst_infos[ dst ]
|
1175
|
+
src = dst_info[ :src ]
|
1176
|
+
|
1208
1177
|
begin
|
1209
|
-
data = dst.read_nonblock(
|
1210
|
-
rescue IO::WaitReadable
|
1178
|
+
data = dst.read_nonblock( 65535 )
|
1179
|
+
rescue IO::WaitReadable
|
1211
1180
|
print 'r'
|
1212
1181
|
return
|
1213
1182
|
rescue Exception => e
|
1214
|
-
# puts "
|
1215
|
-
|
1216
|
-
src = dst_info[ :src ]
|
1183
|
+
# puts "debug read dst #{ e.class }"
|
1184
|
+
close_read_dst( dst )
|
1217
1185
|
set_src_closing_write( src )
|
1218
1186
|
return
|
1219
1187
|
end
|
1220
1188
|
|
1221
|
-
|
1222
|
-
src = dst_info[ :src ]
|
1189
|
+
# puts "debug read dst #{ data.bytesize }"
|
1223
1190
|
add_src_wbuff( src, data )
|
1224
1191
|
end
|
1225
1192
|
|
1226
1193
|
##
|
1227
|
-
# read
|
1194
|
+
# read btun
|
1228
1195
|
#
|
1229
|
-
def
|
1230
|
-
if
|
1231
|
-
puts "p#{ Process.pid } #{ Time.new } read
|
1196
|
+
def read_btun( btun )
|
1197
|
+
if btun.closed? then
|
1198
|
+
puts "p#{ Process.pid } #{ Time.new } read btun but btun closed?"
|
1232
1199
|
return
|
1233
1200
|
end
|
1234
1201
|
|
1202
|
+
btun_info = @btun_infos[ btun ]
|
1203
|
+
src = btun_info[ :src ]
|
1204
|
+
|
1235
1205
|
begin
|
1236
|
-
data =
|
1237
|
-
rescue IO::WaitReadable
|
1238
|
-
return
|
1239
|
-
rescue Errno::EINTR
|
1240
|
-
puts e.class
|
1241
|
-
return
|
1206
|
+
data = btun.read_nonblock( READ_SIZE )
|
1242
1207
|
rescue Exception => e
|
1243
|
-
# puts "
|
1244
|
-
|
1245
|
-
src = tun_info[ :src ]
|
1246
|
-
set_src_closing_write( src )
|
1247
|
-
return
|
1248
|
-
end
|
1208
|
+
# puts "debug read btun #{ btun_info[ :im ] } #{ e.class }"
|
1209
|
+
close_btun( btun )
|
1249
1210
|
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
add_src_wbuff( src, data )
|
1254
|
-
end
|
1211
|
+
if src then
|
1212
|
+
set_src_closing_write( src )
|
1213
|
+
end
|
1255
1214
|
|
1256
|
-
##
|
1257
|
-
# write proxy
|
1258
|
-
#
|
1259
|
-
def write_proxy( proxy )
|
1260
|
-
if proxy.closed? then
|
1261
|
-
puts "p#{ Process.pid } #{ Time.new } write proxy but proxy closed?"
|
1262
1215
|
return
|
1263
1216
|
end
|
1264
1217
|
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1218
|
+
until data.empty? do
|
1219
|
+
rbuff = btun_info[ :rbuff ]
|
1220
|
+
wait_bytes = btun_info[ :wait_bytes ]
|
1221
|
+
|
1222
|
+
if wait_bytes > 0 then
|
1223
|
+
len = wait_bytes
|
1224
|
+
# puts "debug wait bytes #{ len }"
|
1225
|
+
else
|
1226
|
+
if data.bytesize <= 2 then
|
1227
|
+
# puts "debug unexpect data length #{ data.bytesize }"
|
1228
|
+
close_btun( btun )
|
1229
|
+
return
|
1230
|
+
end
|
1231
|
+
|
1232
|
+
len = data[ 0, 2 ].unpack( 'n' ).first
|
1233
|
+
# puts "debug read len #{ len }"
|
1234
|
+
data = data[ 2..-1 ]
|
1278
1235
|
end
|
1279
1236
|
|
1280
|
-
|
1281
|
-
|
1237
|
+
chunk = data[ 0, len ]
|
1238
|
+
chunk_size = chunk.bytesize
|
1239
|
+
|
1240
|
+
if chunk_size == len then
|
1241
|
+
# 取完整了
|
1242
|
+
chunk = @custom.decode( "#{ rbuff }#{ chunk }" )
|
1243
|
+
# puts "debug read btun decoded #{ chunk.bytesize }"
|
1244
|
+
add_src_wbuff( src, chunk )
|
1245
|
+
btun_info[ :rbuff ] = ''
|
1246
|
+
btun_info[ :wait_bytes ] = 0
|
1247
|
+
else
|
1248
|
+
# 暂存
|
1249
|
+
# puts "debug add btun.rbuff #{ chunk_size } wait bytes #{ len - chunk_size }"
|
1250
|
+
btun_info[ :rbuff ] << chunk
|
1251
|
+
btun_info[ :wait_bytes ] = len - chunk_size
|
1252
|
+
end
|
1282
1253
|
|
1283
|
-
|
1254
|
+
data = data[ chunk_size..-1 ]
|
1255
|
+
end
|
1284
1256
|
end
|
1285
1257
|
|
1286
1258
|
##
|
@@ -1314,20 +1286,23 @@ module Girl
|
|
1314
1286
|
print 'w'
|
1315
1287
|
return
|
1316
1288
|
rescue Exception => e
|
1317
|
-
# puts "
|
1289
|
+
# puts "debug write src #{ e.class }"
|
1318
1290
|
close_write_src( src )
|
1319
1291
|
|
1320
1292
|
if dst then
|
1321
1293
|
close_read_dst( dst )
|
1322
1294
|
else
|
1323
|
-
|
1324
|
-
|
1295
|
+
btun = src_info[ :btun ]
|
1296
|
+
|
1297
|
+
if btun then
|
1298
|
+
close_btun( btun )
|
1299
|
+
end
|
1325
1300
|
end
|
1326
1301
|
|
1327
1302
|
return
|
1328
1303
|
end
|
1329
1304
|
|
1330
|
-
# puts "
|
1305
|
+
# puts "debug write src #{ written }"
|
1331
1306
|
data = data[ written..-1 ]
|
1332
1307
|
src_info[ :wbuff ] = data
|
1333
1308
|
end
|
@@ -1363,7 +1338,7 @@ module Girl
|
|
1363
1338
|
print 'w'
|
1364
1339
|
return
|
1365
1340
|
rescue Exception => e
|
1366
|
-
# puts "
|
1341
|
+
# puts "debug write dst #{ e.class }"
|
1367
1342
|
close_write_dst( dst )
|
1368
1343
|
close_read_src( src )
|
1369
1344
|
return
|
@@ -1379,24 +1354,24 @@ module Girl
|
|
1379
1354
|
end
|
1380
1355
|
|
1381
1356
|
##
|
1382
|
-
# write
|
1357
|
+
# write atun
|
1383
1358
|
#
|
1384
|
-
def
|
1385
|
-
if
|
1386
|
-
puts "p#{ Process.pid } #{ Time.new } write
|
1359
|
+
def write_atun( atun )
|
1360
|
+
if atun.closed? then
|
1361
|
+
puts "p#{ Process.pid } #{ Time.new } write atun but atun closed?"
|
1387
1362
|
return
|
1388
1363
|
end
|
1389
1364
|
|
1390
|
-
|
1391
|
-
src =
|
1392
|
-
data =
|
1365
|
+
atun_info = @atun_infos[ atun ]
|
1366
|
+
src = atun_info[ :src ]
|
1367
|
+
data = atun_info[ :wbuff ]
|
1393
1368
|
|
1394
1369
|
# 写前为空,处理关闭写
|
1395
1370
|
if data.empty? then
|
1396
|
-
if
|
1397
|
-
|
1371
|
+
if atun_info[ :closing ] then
|
1372
|
+
close_atun( atun )
|
1398
1373
|
else
|
1399
|
-
@writes.delete(
|
1374
|
+
@writes.delete( atun )
|
1400
1375
|
end
|
1401
1376
|
|
1402
1377
|
return
|
@@ -1404,23 +1379,20 @@ module Girl
|
|
1404
1379
|
|
1405
1380
|
# 写入
|
1406
1381
|
begin
|
1407
|
-
written =
|
1408
|
-
rescue IO::
|
1409
|
-
# OpenSSL::SSL::SSLErrorWaitReadable
|
1410
|
-
return
|
1411
|
-
rescue IO::WaitWritable, Errno::EINTR
|
1382
|
+
written = atun.write_nonblock( data )
|
1383
|
+
rescue IO::WaitWritable
|
1412
1384
|
print 'w'
|
1413
1385
|
return
|
1414
1386
|
rescue Exception => e
|
1415
|
-
# puts "
|
1416
|
-
|
1387
|
+
# puts "debug write atun #{ e.class }"
|
1388
|
+
close_atun( atun )
|
1417
1389
|
close_read_src( src )
|
1418
1390
|
return
|
1419
1391
|
end
|
1420
1392
|
|
1421
|
-
# puts "
|
1393
|
+
# puts "debug write atun #{ written }"
|
1422
1394
|
data = data[ written..-1 ]
|
1423
|
-
|
1395
|
+
atun_info[ :wbuff ] = data
|
1424
1396
|
|
1425
1397
|
unless src.closed? then
|
1426
1398
|
src_info = @src_infos[ src ]
|
@@ -1429,112 +1401,30 @@ module Girl
|
|
1429
1401
|
end
|
1430
1402
|
|
1431
1403
|
##
|
1432
|
-
# write
|
1404
|
+
# write btun
|
1433
1405
|
#
|
1434
|
-
def
|
1435
|
-
if
|
1436
|
-
puts "p#{ Process.pid } #{ Time.new } write
|
1406
|
+
def write_btun( btun )
|
1407
|
+
if btun.closed? then
|
1408
|
+
puts "p#{ Process.pid } #{ Time.new } write btun but btun closed?"
|
1437
1409
|
return
|
1438
1410
|
end
|
1439
1411
|
|
1440
|
-
|
1412
|
+
btun_info = @btun_infos[ btun ]
|
1413
|
+
data = btun_info[ :wbuff ]
|
1441
1414
|
|
1415
|
+
# 写入dst id
|
1442
1416
|
begin
|
1443
|
-
|
1444
|
-
rescue IO::WaitReadable
|
1445
|
-
rescue IO::WaitWritable
|
1446
|
-
rescue Exception => e
|
1447
|
-
puts "p#{ Process.pid } #{ Time.new } proxy connect #{ e.class }, close proxy"
|
1448
|
-
proxy.close
|
1449
|
-
close_pre_proxy( pre_proxy )
|
1450
|
-
return
|
1451
|
-
end
|
1452
|
-
|
1453
|
-
@proxy = proxy
|
1454
|
-
@proxy_info = {
|
1455
|
-
ctlmsgs: [], # ctlmsg
|
1456
|
-
tund_addr: nil, # tund地址
|
1457
|
-
created_at: Time.new, # 创建时间
|
1458
|
-
last_recv_at: nil, # 上一次收到流量的时间
|
1459
|
-
closing: false # 是否准备关闭
|
1460
|
-
}
|
1461
|
-
add_read( proxy, :proxy )
|
1462
|
-
hello = @custom.hello
|
1463
|
-
puts "p#{ Process.pid } #{ Time.new } tunnel #{ hello.inspect }"
|
1464
|
-
data = "#{ [ HELLO ].pack( 'C' ) }#{ hello }"
|
1465
|
-
add_ctlmsg( data )
|
1466
|
-
|
1467
|
-
@roles.delete( pre_proxy )
|
1468
|
-
@writes.delete( pre_proxy )
|
1469
|
-
end
|
1470
|
-
|
1471
|
-
##
|
1472
|
-
# write pre tun
|
1473
|
-
#
|
1474
|
-
def write_pre_tun( pre_tun )
|
1475
|
-
if pre_tun.closed? then
|
1476
|
-
puts "p#{ Process.pid } #{ Time.new } write pre tun but pre tun closed?"
|
1477
|
-
return
|
1478
|
-
end
|
1479
|
-
|
1480
|
-
pre_tun_info = @pre_tun_infos[ pre_tun ]
|
1481
|
-
src = pre_tun_info[ :src ]
|
1482
|
-
|
1483
|
-
if src.closed? then
|
1484
|
-
puts "p#{ Process.pid } #{ Time.new } new a tun but src closed?"
|
1485
|
-
return
|
1486
|
-
end
|
1487
|
-
|
1488
|
-
tun = OpenSSL::SSL::SSLSocket.new pre_tun
|
1489
|
-
|
1490
|
-
begin
|
1491
|
-
tun.connect_nonblock
|
1492
|
-
rescue IO::WaitReadable
|
1493
|
-
rescue IO::WaitWritable
|
1417
|
+
written = btun.write( data )
|
1494
1418
|
rescue Exception => e
|
1495
|
-
puts "
|
1496
|
-
|
1497
|
-
|
1419
|
+
# puts "debug write btun #{ e.class }"
|
1420
|
+
src = btun_info[ :src ]
|
1421
|
+
close_btun( btun )
|
1422
|
+
add_closing_src( src )
|
1498
1423
|
return
|
1499
1424
|
end
|
1500
1425
|
|
1501
|
-
|
1502
|
-
|
1503
|
-
# puts "debug1 set tun.wbuff #{ dst_id }"
|
1504
|
-
data = [ dst_id ].pack( 'n' )
|
1505
|
-
|
1506
|
-
unless src_info[ :rbuff ].empty? then
|
1507
|
-
# puts "debug1 move src.rbuff to tun.wbuff"
|
1508
|
-
data << src_info[ :rbuff ]
|
1509
|
-
end
|
1510
|
-
|
1511
|
-
domain = src_info[ :destination_domain ]
|
1512
|
-
@tun_infos[ tun ] = {
|
1513
|
-
pre_tun: pre_tun, # 对应pre tun
|
1514
|
-
src: src, # 对应src
|
1515
|
-
domain: domain, # 目的地
|
1516
|
-
wbuff: data, # 写前
|
1517
|
-
closing_write: false, # 准备关闭写
|
1518
|
-
close_read: false, # 已经关闭读
|
1519
|
-
close_write: false # 已经关闭写
|
1520
|
-
}
|
1521
|
-
|
1522
|
-
src_info[ :tun ] = tun
|
1523
|
-
add_read( tun, :tun )
|
1524
|
-
add_write( tun )
|
1525
|
-
|
1526
|
-
if src_info[ :proxy_proto ] == :http then
|
1527
|
-
if src_info[ :is_connect ] then
|
1528
|
-
# puts "debug1 add src.wbuff http ok"
|
1529
|
-
add_src_wbuff( src, HTTP_OK )
|
1530
|
-
end
|
1531
|
-
elsif src_info[ :proxy_proto ] == :socks5 then
|
1532
|
-
add_socks5_conn_reply( src )
|
1533
|
-
end
|
1534
|
-
|
1535
|
-
@pre_tun_infos.delete( pre_tun )
|
1536
|
-
@roles.delete( pre_tun )
|
1537
|
-
@writes.delete( pre_tun )
|
1426
|
+
# puts "debug write btun #{ written }"
|
1427
|
+
@writes.delete( btun )
|
1538
1428
|
end
|
1539
1429
|
|
1540
1430
|
end
|