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