girl 3.9.0 → 4.4.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.

data/lib/girl/relay.rb CHANGED
@@ -3,10 +3,9 @@ require 'girl/concurrent_hash'
3
3
  require 'girl/head'
4
4
  require 'girl/proxy_custom'
5
5
  require 'girl/relay_worker'
6
- require 'girl/resolv_custom'
7
6
  require 'girl/version'
7
+ require 'ipaddr'
8
8
  require 'json'
9
- require 'openssl'
10
9
  require 'socket'
11
10
 
12
11
  ##
@@ -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
- @paused_tuns = []
22
+ @paused_btuns = []
24
23
  @resume_srcs = []
25
24
  @resume_dsts = []
26
- @resume_tuns = []
27
- @pending_srcs = [] # 还没配到tund,暂存的src
28
- @roles = ConcurrentHash.new # sock => :dotr / :resolv / :rsv / :redir / :proxy / :src / :dst / :tun / :pre_proxy / :pre_tun
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
- @tun_infos = ConcurrentHash.new # tun => {}
33
- @pre_tun_infos = ConcurrentHash.new # pre_tun => {}
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 :proxy then
67
- read_proxy( sock )
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 :tun then
73
- read_tun( sock )
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 :tun then
86
- write_tun( sock )
87
- when :pre_proxy then
88
- write_pre_proxy( sock )
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
- if @proxy && !@proxy.closed? then
104
- # puts "debug1 send tun fin"
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
- data = "#{ [ A_NEW_SOURCE, src_info[ :id ] ].pack( 'CQ>' ) }#{ domain_port }"
124
- add_ctlmsg( data )
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
- return if @proxy.nil? || @proxy.closed?
150
- @proxy_info[ :ctlmsgs ] << data
151
- add_write( @proxy )
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 "debug1 src.rbuff full"
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
- tun = src_info[ :tun ]
274
+ btun = src_info[ :btun ]
282
275
 
283
- if tun then
284
- puts "p#{ Process.pid } #{ Time.new } pause tun #{ src_info[ :destination_domain ] }"
285
- add_paused_tun( tun )
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 pre proxy
294
+ # close atun
316
295
  #
317
- def close_pre_proxy( pre_proxy )
318
- return if pre_proxy.closed?
319
- # puts "debug1 close pre proxy"
320
- close_sock( pre_proxy )
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 pre tun
309
+ # close btun
325
310
  #
326
- def close_pre_tun( pre_tun )
327
- return if pre_tun.closed?
328
- # puts "debug1 close pre tun"
329
- close_sock( pre_tun )
330
- @pre_tun_infos.delete( pre_tun )
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 proxy
319
+ # close ctl
335
320
  #
336
- def close_proxy( proxy )
337
- return if proxy.closed?
338
- # puts "debug1 close proxy"
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 "debug1 close read dst"
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
- dst_info = @dst_infos.delete( dst )
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 "debug1 close read src"
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
- src_info = del_src_info( src )
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 "debug1 close rsv"
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 "debug1 close src"
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
- @dst_infos.delete( dst )
391
+ del_dst_info( dst )
438
392
  else
439
- tun = src_info[ :tun ]
393
+ atun = src_info[ :atun ]
394
+ btun = src_info[ :btun ]
440
395
 
441
- if tun then
442
- close_tun( tun )
443
- else
444
- pre_tun = src_info[ :pre_tun ]
396
+ if atun then
397
+ close_sock( atun )
398
+ @atun_infos.delete( atun )
399
+ end
445
400
 
446
- if pre_tun then
447
- close_pre_tun( pre_tun )
448
- end
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 "debug1 close write dst"
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
- dst_info = @dst_infos.delete( dst )
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 "debug1 close write src"
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
- src_info = del_src_info( src )
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 ip_info.ipv4_loopback? \
533
- || ip_info.ipv6_loopback? \
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 "debug1 #{ ip_info.inspect } hit directs"
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 @proxy && !@proxy.closed? then
587
- if @proxy_info[ :last_recv_at ] then
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 >= expire_after then
596
- puts "p#{ Process.pid } #{ Time.new } expire proxy"
597
- @proxy_info[ :closing ] = true
598
- next_tick
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
- if src.closed? then
645
- add_resume_src( src )
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
- if dst then
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
- else
658
- tun = src_info[ :tun ]
659
- tun_info = @tun_infos[ tun ]
575
+ end
576
+ else
577
+ atun = src_info[ :atun ]
578
+
579
+ if atun && !atun.closed? then
580
+ atun_info = @atun_infos[ atun ]
660
581
 
661
- if tun_info[ :wbuff ].size < RESUME_BELOW then
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
- if dst.closed? then
671
- add_resume_dst( dst )
672
- else
673
- dst_info = @dst_infos[ dst ]
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
- @paused_tuns.each do | tun |
685
- if tun.closed? then
686
- add_resume_tun( tun )
687
- else
688
- tun_info = @tun_infos[ tun ]
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 tun #{ tun_info[ :domain ] }"
694
- add_resume_tun( tun )
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 "debug1 a new dst #{ dst.local_address.inspect }"
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 "debug1 move src.rbuff to dst.wbuff"
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 pre proxy
689
+ # new a ctl
746
690
  #
747
- def new_a_pre_proxy
748
- pre_proxy = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
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
- pre_proxy.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1 )
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
- begin
757
- pre_proxy.connect_nonblock( @proxyd_addr )
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
- @pre_proxy = pre_proxy
766
- @roles[ pre_proxy ] = :pre_proxy
767
- @pre_proxy_info = {
768
- closing: false,
769
- created_at: Time.new
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
709
 
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
-
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 )
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 = @resolv_custom.encode( data )
753
+ data = @custom.encode( data )
845
754
  to_addr = @resolvd_addr
846
755
  else
847
756
  to_addr = @nameserver_addr
@@ -857,6 +766,77 @@ 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
+ lbuff: '' # 流量截断在长度前缀处
829
+ }
830
+
831
+ src_info[ :dst_id ] = dst_id
832
+ src_info[ :atun ] = atun
833
+ src_info[ :btun ] = btun
834
+ add_read( atun, :atun )
835
+ add_read( btun, :btun )
836
+ add_write( atun )
837
+ add_write( btun )
838
+ end
839
+
860
840
  ##
861
841
  # next tick
862
842
  #
@@ -864,15 +844,50 @@ module Girl
864
844
  @dotw.write( '.' )
865
845
  end
866
846
 
847
+ ##
848
+ # pack a chunk
849
+ #
850
+ def pack_a_chunk( data )
851
+ # puts "debug pack a chunk"
852
+ data = @custom.encode( data )
853
+ "#{ [ data.bytesize ].pack( 'n' ) }#{ data }"
854
+ end
855
+
856
+ ##
857
+ # send ctlmsg
858
+ #
859
+ def send_ctlmsg( data )
860
+ return if @ctl.nil? || @ctl.closed?
861
+ data = @custom.encode( data )
862
+
863
+ begin
864
+ @ctl.sendmsg( data, 0, @proxyd_addr )
865
+ @ctl_info[ :last_sent_at ] = Time.new
866
+ rescue Exception => e
867
+ puts "p#{ Process.pid } #{ Time.new } sendmsg #{ e.class }"
868
+ close_ctl( @ctl )
869
+ end
870
+ end
871
+
872
+ ##
873
+ # set atun closing
874
+ #
875
+ def set_atun_closing( atun )
876
+ return if atun.closed?
877
+ atun_info = @atun_infos[ atun ]
878
+ return if atun_info[ :closing ]
879
+ # puts "debug set atun closing"
880
+ atun_info[ :closing ] = true
881
+ add_write( atun )
882
+ end
883
+
867
884
  ##
868
885
  # send data
869
886
  #
870
887
  def send_data( sock, to_addr, data )
871
888
  begin
872
889
  sock.sendmsg( data, 0, to_addr )
873
- rescue IO::WaitWritable, Errno::EINTR
874
- print 'w'
875
- rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
890
+ rescue Exception => e
876
891
  puts "p#{ Process.pid } #{ Time.new } sendmsg to #{ to_addr.ip_unpack.inspect } #{ e.class }"
877
892
  end
878
893
  end
@@ -897,7 +912,7 @@ module Girl
897
912
  src_info[ :proxy_type ] = :tunnel
898
913
  src_id = src_info[ :id ]
899
914
 
900
- if @proxy && !@proxy.closed? && @proxy_info[ :tund_addr ] then
915
+ if @ctl && !@ctl.closed? && @ctl_info[ :atund_addr ] then
901
916
  add_a_new_source( src )
902
917
  else
903
918
  @pending_srcs << src
@@ -915,48 +930,15 @@ module Girl
915
930
  add_write( src )
916
931
  end
917
932
 
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
933
  ##
949
934
  # read dotr
950
935
  #
951
936
  def read_dotr( dotr )
952
- dotr.read_nonblock( READ_SIZE )
937
+ dotr.read_nonblock( 65535 )
953
938
 
954
- if @proxy && !@proxy.closed? then
955
- if @proxy_info[ :closing ] then
956
- close_proxy( @proxy )
957
- end
958
- elsif @pre_proxy && !@pre_proxy.closed? && @pre_proxy_info[ :closing ] then
959
- close_pre_proxy( @pre_proxy )
939
+ if @ctl_info && @ctl_info[ :closing ] then
940
+ send_ctlmsg( [ CTL_FIN ].pack( 'C' ) )
941
+ close_ctl( @ctl )
960
942
  end
961
943
 
962
944
  if @closing_rsvs.any? then
@@ -987,13 +969,13 @@ module Girl
987
969
  @resume_dsts.clear
988
970
  end
989
971
 
990
- if @resume_tuns.any? then
991
- @resume_tuns.each do | tun |
992
- add_read( tun )
993
- @paused_tuns.delete( tun )
972
+ if @resume_btuns.any? then
973
+ @resume_btuns.each do | btun |
974
+ add_read( btun )
975
+ @paused_btuns.delete( btun )
994
976
  end
995
977
 
996
- @resume_tuns.clear
978
+ @resume_btuns.clear
997
979
  end
998
980
  end
999
981
 
@@ -1002,7 +984,7 @@ module Girl
1002
984
  #
1003
985
  def read_resolv( resolv )
1004
986
  data, addrinfo, rflags, *controls = resolv.recvmsg
1005
- # puts "debug1 resolv recvmsg #{ addrinfo.ip_unpack.inspect } #{ data.inspect }"
987
+ # puts "debug resolv recvmsg #{ addrinfo.ip_unpack.inspect } #{ data.inspect }"
1006
988
  new_a_rsv( addrinfo.to_sockaddr, data )
1007
989
  end
1008
990
 
@@ -1011,10 +993,10 @@ module Girl
1011
993
  #
1012
994
  def read_rsv( rsv )
1013
995
  data, addrinfo, rflags, *controls = rsv.recvmsg
1014
- # puts "debug1 rsv recvmsg #{ addrinfo.ip_unpack.inspect } #{ data.inspect }"
996
+ # puts "debug rsv recvmsg #{ addrinfo.ip_unpack.inspect } #{ data.inspect }"
1015
997
 
1016
998
  if addrinfo.to_sockaddr == @resolvd_addr then
1017
- data = @resolv_custom.decode( data )
999
+ data = @custom.decode( data )
1018
1000
  end
1019
1001
 
1020
1002
  rsv_info = @rsv_infos[ rsv ]
@@ -1047,7 +1029,7 @@ module Girl
1047
1029
  dest_ip = dest_addrinfo.ip_address
1048
1030
 
1049
1031
  src_id = rand( ( 2 ** 64 ) - 2 ) + 1
1050
- # puts "debug1 accept a src #{ addrinfo.ip_unpack.inspect } to #{ dest_ip }:#{ dest_port } #{ src_id }"
1032
+ # puts "debug accept a src #{ addrinfo.ip_unpack.inspect } to #{ dest_ip }:#{ dest_port } #{ src_id }"
1051
1033
 
1052
1034
  @srcs[ src_id ] = src
1053
1035
  @src_infos[ src ] = {
@@ -1057,10 +1039,11 @@ module Girl
1057
1039
  destination_port: dest_port, # 目的地端口
1058
1040
  rbuff: '', # 读到的流量
1059
1041
  dst: nil, # :direct的场合,对应的dst
1060
- pre_tun: nil, # :tunnel的场合,对应的pre tun
1061
- tun: nil, # :tunnel的场合,对应的tun
1042
+ ctl: nil, # :tunnel的场合,对应的ctl
1043
+ atun: nil, # :tunnel的场合,对应的atun
1044
+ btun: nil, # :tunnel的场合,对应的btun
1062
1045
  dst_id: nil, # 远端dst id
1063
- wbuff: '', # 从dst/tun读到的流量
1046
+ wbuff: '', # 从dst/btun读到的流量
1064
1047
  created_at: Time.new, # 创建时间
1065
1048
  last_recv_at: nil, # 上一次收到新流量(由dst收到,或者由tun收到)的时间
1066
1049
  last_sent_at: nil, # 上一次发出流量(由dst发出,或者由tun发出)的时间
@@ -1069,67 +1052,53 @@ module Girl
1069
1052
 
1070
1053
  add_read( src, :src )
1071
1054
 
1072
- # 避免多线程重复建proxy,在accept到src时就建。
1073
- if @pre_proxy.nil? || @pre_proxy.closed? then
1074
- new_a_pre_proxy
1055
+ if @ctl.nil? || @ctl.closed? then
1056
+ new_a_ctl
1075
1057
  end
1076
1058
 
1077
1059
  deal_with_destination_ip( src, dest_addrinfo )
1078
1060
  end
1079
1061
 
1080
1062
  ##
1081
- # read proxy
1063
+ # read ctl
1082
1064
  #
1083
- def read_proxy( proxy )
1084
- if proxy.closed? then
1085
- puts "p#{ Process.pid } #{ Time.new } read proxy but proxy closed?"
1086
- return
1087
- end
1088
-
1065
+ def read_ctl( ctl )
1089
1066
  begin
1090
- data = proxy.read_nonblock( READ_SIZE )
1091
- rescue IO::WaitReadable
1092
- return
1093
- rescue Errno::EINTR
1094
- puts e.class
1095
- return
1067
+ data, addrinfo, rflags, *controls = ctl.recvmsg
1096
1068
  rescue Exception => e
1097
- # puts "debug1 read proxy #{ e.class }"
1098
- close_proxy( proxy )
1069
+ puts "p#{ Process.pid } #{ Time.new } recvmsg #{ e.class }"
1070
+ close_ctl( ctl )
1099
1071
  return
1100
1072
  end
1101
1073
 
1102
- @proxy_info[ :last_recv_at ] = Time.new
1103
-
1104
- data.split( SEPARATE ).each do | ctlmsg |
1105
- next unless ctlmsg[ 0 ]
1074
+ data = @custom.decode( data )
1075
+ ctl_num = data[ 0 ].unpack( 'C' ).first
1106
1076
 
1107
- ctl_num = ctlmsg[ 0 ].unpack( 'C' ).first
1077
+ case ctl_num
1078
+ when TUND_PORT then
1079
+ return if @ctl_info[ :atund_addr ] || data.size != 5
1080
+ atund_port, btund_port = data[ 1, 4 ].unpack( 'nn' )
1081
+ puts "p#{ Process.pid } #{ Time.new } got tund port #{ atund_port } #{ btund_port }"
1082
+ @ctl_info[ :resends ].delete( [ HELLO ].pack( 'C' ) )
1083
+ @ctl_info[ :atund_addr ] = Socket.sockaddr_in( atund_port, @proxyd_host )
1084
+ @ctl_info[ :btund_addr ] = Socket.sockaddr_in( btund_port, @proxyd_host )
1085
+ @ctl_info[ :last_recv_at ] = Time.new
1108
1086
 
1109
- case ctl_num
1110
- when TUND_PORT then
1111
- next if @proxy_info[ :tund_addr ] || ( ctlmsg.size != 3 )
1112
- tund_port = ctlmsg[ 1, 2 ].unpack( 'n' ).first
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"
1087
+ if @pending_srcs.any? then
1088
+ puts "p#{ Process.pid } #{ Time.new } send pending sources"
1089
+ @pending_srcs.each { | src | add_a_new_source( src ) }
1090
+ @pending_srcs.clear
1132
1091
  end
1092
+ when PAIRED then
1093
+ return if data.size != 11
1094
+ src_id, dst_id = data[ 1, 10 ].unpack( 'Q>n' )
1095
+ # puts "debug got paired #{ src_id } #{ dst_id }"
1096
+ @ctl_info[ :resends ].delete( [ A_NEW_SOURCE, src_id ].pack( 'CQ>' ) )
1097
+ @ctl_info[ :last_recv_at ] = Time.new
1098
+ new_tuns( src_id, dst_id )
1099
+ when UNKNOWN_CTL_ADDR then
1100
+ puts "p#{ Process.pid } #{ Time.new } got unknown ctl addr, close ctl"
1101
+ close_ctl( ctl )
1133
1102
  end
1134
1103
  end
1135
1104
 
@@ -1142,55 +1111,53 @@ module Girl
1142
1111
  return
1143
1112
  end
1144
1113
 
1114
+ src_info = @src_infos[ src ]
1115
+
1145
1116
  begin
1146
- data = src.read_nonblock( READ_SIZE )
1147
- rescue IO::WaitReadable, Errno::EINTR
1117
+ data = src.read_nonblock( 65535 )
1118
+ rescue IO::WaitReadable
1148
1119
  print 'r'
1149
1120
  return
1150
1121
  rescue Exception => e
1151
- # puts "debug1 read src #{ e.class }"
1152
- src_info = close_read_src( src )
1122
+ # puts "debug read src #{ e.class }"
1123
+ close_read_src( src )
1153
1124
  dst = src_info[ :dst ]
1154
1125
 
1155
1126
  if dst then
1156
1127
  set_dst_closing_write( dst )
1157
1128
  else
1158
- tun = src_info[ :tun ]
1159
- set_tun_closing_write( tun ) if tun
1129
+ atun = src_info[ :atun ]
1130
+
1131
+ if atun then
1132
+ set_atun_closing( atun )
1133
+ end
1160
1134
  end
1161
1135
 
1162
1136
  return
1163
1137
  end
1164
1138
 
1165
- src_info = @src_infos[ src ]
1166
1139
  proxy_type = src_info[ :proxy_type ]
1167
1140
 
1168
1141
  case proxy_type
1169
1142
  when :checking then
1170
- # puts "debug1 add src rbuff before resolved #{ data.inspect }"
1143
+ # puts "debug add src rbuff before resolved #{ data.inspect }"
1171
1144
  src_info[ :rbuff ] << data
1172
1145
  when :tunnel then
1173
- tun = src_info[ :tun ]
1146
+ atun = src_info[ :atun ]
1174
1147
 
1175
- if tun then
1176
- unless tun.closed? then
1177
- # puts "debug2 add tun.wbuff #{ data.bytesize }"
1178
- add_tun_wbuff( tun, data )
1179
- end
1148
+ if atun then
1149
+ add_atun_wbuff( atun, pack_a_chunk( data ) )
1180
1150
  else
1181
- # puts "debug1 tun not ready, save data to src.rbuff"
1151
+ # puts "debug add src.rbuff #{ data.bytesize }"
1182
1152
  add_src_rbuff( src, data )
1183
1153
  end
1184
1154
  when :direct then
1185
1155
  dst = src_info[ :dst ]
1186
1156
 
1187
1157
  if dst then
1188
- unless dst.closed? then
1189
- # puts "debug2 add dst.wbuff #{ data.bytesize }"
1190
- add_dst_wbuff( dst, data )
1191
- end
1158
+ add_dst_wbuff( dst, data )
1192
1159
  else
1193
- # puts "debug1 dst not ready, save data to src.rbuff"
1160
+ # puts "debug add src.rbuff #{ data.bytesize }"
1194
1161
  add_src_rbuff( src, data )
1195
1162
  end
1196
1163
  end
@@ -1205,82 +1172,110 @@ module Girl
1205
1172
  return
1206
1173
  end
1207
1174
 
1175
+ dst_info = @dst_infos[ dst ]
1176
+ src = dst_info[ :src ]
1177
+
1208
1178
  begin
1209
- data = dst.read_nonblock( READ_SIZE )
1210
- rescue IO::WaitReadable, Errno::EINTR
1179
+ data = dst.read_nonblock( 65535 )
1180
+ rescue IO::WaitReadable
1211
1181
  print 'r'
1212
1182
  return
1213
1183
  rescue Exception => e
1214
- # puts "debug1 read dst #{ e.class }"
1215
- dst_info = close_read_dst( dst )
1216
- src = dst_info[ :src ]
1184
+ # puts "debug read dst #{ e.class }"
1185
+ close_read_dst( dst )
1217
1186
  set_src_closing_write( src )
1218
1187
  return
1219
1188
  end
1220
1189
 
1221
- dst_info = @dst_infos[ dst ]
1222
- src = dst_info[ :src ]
1190
+ # puts "debug read dst #{ data.bytesize }"
1223
1191
  add_src_wbuff( src, data )
1224
1192
  end
1225
1193
 
1226
1194
  ##
1227
- # read tun
1195
+ # read btun
1228
1196
  #
1229
- def read_tun( tun )
1230
- if tun.closed? then
1231
- puts "p#{ Process.pid } #{ Time.new } read tun but tun closed?"
1197
+ def read_btun( btun )
1198
+ if btun.closed? then
1199
+ puts "p#{ Process.pid } #{ Time.new } read btun but btun closed?"
1232
1200
  return
1233
1201
  end
1234
1202
 
1203
+ btun_info = @btun_infos[ btun ]
1204
+ src = btun_info[ :src ]
1205
+
1235
1206
  begin
1236
- data = tun.read_nonblock( READ_SIZE )
1237
- rescue IO::WaitReadable
1238
- return
1239
- rescue Errno::EINTR
1240
- puts e.class
1241
- return
1207
+ data = btun.read_nonblock( READ_SIZE )
1242
1208
  rescue Exception => e
1243
- # puts "debug1 read tun #{ e.class }"
1244
- tun_info = close_read_tun( tun )
1245
- src = tun_info[ :src ]
1246
- set_src_closing_write( src )
1247
- return
1248
- end
1209
+ # puts "debug read btun #{ btun_info[ :im ] } #{ e.class }"
1210
+ close_btun( btun )
1249
1211
 
1250
- tun_info = @tun_infos[ tun ]
1251
- src = tun_info[ :src ]
1252
- # puts "debug2 add src.wbuff #{ data.bytesize }"
1253
- add_src_wbuff( src, data )
1254
- end
1212
+ if src then
1213
+ set_src_closing_write( src )
1214
+ end
1255
1215
 
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
1216
  return
1263
1217
  end
1264
1218
 
1265
- if @proxy_info[ :ctlmsgs ].any? then
1266
- data = @proxy_info[ :ctlmsgs ].map{ | ctlmsg | "#{ ctlmsg }#{ SEPARATE }" }.join
1267
-
1268
- # 写入
1269
- begin
1270
- written = proxy.write( data )
1271
- rescue IO::WaitWritable, Errno::EINTR
1272
- print 'w'
1273
- return
1274
- rescue Exception => e
1275
- # puts "debug1 write proxy #{ e.class }"
1276
- close_proxy( proxy )
1277
- return
1219
+ until data.empty? do
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
+ lbuff = btun_info[ :lbuff ]
1227
+
1228
+ if lbuff.empty? then
1229
+ # 长度缓存为空,从读到的流量里取长度
1230
+ # 两个字节以下,记进长度缓存
1231
+ if data.bytesize <= 2 then
1232
+ # puts "debug set btun.lbuff #{ data.inspect }"
1233
+ btun_info[ :lbuff ] = data
1234
+ return
1235
+ end
1236
+
1237
+ len = data[ 0, 2 ].unpack( 'n' ).first
1238
+ data = data[ 2..-1 ]
1239
+ elsif lbuff.bytesize == 1 then
1240
+ # 长度缓存记有一个字节,补一个字节
1241
+ lbuff = "#{ lbuff }#{ data[ 0 ] }"
1242
+
1243
+ if data.bytesize == 1 then
1244
+ # puts "debug add btun.lbuff a byte #{ data.inspect }"
1245
+ btun_info[ :lbuff ] = lbuff
1246
+ return
1247
+ end
1248
+
1249
+ # 使用长度缓存
1250
+ len = lbuff.unpack( 'n' ).first
1251
+ btun_info[ :lbuff ].clear
1252
+ data = data[ 1..-1 ]
1253
+ else
1254
+ # 使用长度缓存
1255
+ len = lbuff.unpack( 'n' ).first
1256
+ btun_info[ :lbuff ].clear
1257
+ end
1278
1258
  end
1279
1259
 
1280
- @proxy_info[ :ctlmsgs ].clear
1281
- end
1260
+ chunk = data[ 0, len ]
1261
+ chunk_size = chunk.bytesize
1262
+
1263
+ if chunk_size == len then
1264
+ # 取完整了
1265
+ chunk = @custom.decode( "#{ btun_info[ :rbuff ] }#{ chunk }" )
1266
+ # puts "debug read btun decoded #{ chunk.bytesize }"
1267
+ add_src_wbuff( src, chunk )
1268
+ btun_info[ :rbuff ] = ''
1269
+ btun_info[ :wait_bytes ] = 0
1270
+ else
1271
+ # 暂存
1272
+ # puts "debug add btun.rbuff #{ chunk_size } wait bytes #{ len - chunk_size }"
1273
+ btun_info[ :rbuff ] << chunk
1274
+ btun_info[ :wait_bytes ] = len - chunk_size
1275
+ end
1282
1276
 
1283
- @writes.delete( proxy )
1277
+ data = data[ chunk_size..-1 ]
1278
+ end
1284
1279
  end
1285
1280
 
1286
1281
  ##
@@ -1314,20 +1309,23 @@ module Girl
1314
1309
  print 'w'
1315
1310
  return
1316
1311
  rescue Exception => e
1317
- # puts "debug1 write src #{ e.class }"
1312
+ # puts "debug write src #{ e.class }"
1318
1313
  close_write_src( src )
1319
1314
 
1320
1315
  if dst then
1321
1316
  close_read_dst( dst )
1322
1317
  else
1323
- tun = src_info[ :tun ]
1324
- close_read_tun( tun ) if tun
1318
+ btun = src_info[ :btun ]
1319
+
1320
+ if btun then
1321
+ close_btun( btun )
1322
+ end
1325
1323
  end
1326
1324
 
1327
1325
  return
1328
1326
  end
1329
1327
 
1330
- # puts "debug2 written src #{ written }"
1328
+ # puts "debug write src #{ written }"
1331
1329
  data = data[ written..-1 ]
1332
1330
  src_info[ :wbuff ] = data
1333
1331
  end
@@ -1363,7 +1361,7 @@ module Girl
1363
1361
  print 'w'
1364
1362
  return
1365
1363
  rescue Exception => e
1366
- # puts "debug1 write dst #{ e.class }"
1364
+ # puts "debug write dst #{ e.class }"
1367
1365
  close_write_dst( dst )
1368
1366
  close_read_src( src )
1369
1367
  return
@@ -1379,24 +1377,24 @@ module Girl
1379
1377
  end
1380
1378
 
1381
1379
  ##
1382
- # write tun
1380
+ # write atun
1383
1381
  #
1384
- def write_tun( tun )
1385
- if tun.closed? then
1386
- puts "p#{ Process.pid } #{ Time.new } write tun but tun closed?"
1382
+ def write_atun( atun )
1383
+ if atun.closed? then
1384
+ puts "p#{ Process.pid } #{ Time.new } write atun but atun closed?"
1387
1385
  return
1388
1386
  end
1389
1387
 
1390
- tun_info = @tun_infos[ tun ]
1391
- src = tun_info[ :src ]
1392
- data = tun_info[ :wbuff ]
1388
+ atun_info = @atun_infos[ atun ]
1389
+ src = atun_info[ :src ]
1390
+ data = atun_info[ :wbuff ]
1393
1391
 
1394
1392
  # 写前为空,处理关闭写
1395
1393
  if data.empty? then
1396
- if tun_info[ :closing_write ] then
1397
- close_write_tun( tun )
1394
+ if atun_info[ :closing ] then
1395
+ close_atun( atun )
1398
1396
  else
1399
- @writes.delete( tun )
1397
+ @writes.delete( atun )
1400
1398
  end
1401
1399
 
1402
1400
  return
@@ -1404,23 +1402,20 @@ module Girl
1404
1402
 
1405
1403
  # 写入
1406
1404
  begin
1407
- written = tun.write_nonblock( data )
1408
- rescue IO::WaitReadable
1409
- # OpenSSL::SSL::SSLErrorWaitReadable
1410
- return
1411
- rescue IO::WaitWritable, Errno::EINTR
1405
+ written = atun.write_nonblock( data )
1406
+ rescue IO::WaitWritable
1412
1407
  print 'w'
1413
1408
  return
1414
1409
  rescue Exception => e
1415
- # puts "debug1 write tun #{ e.class }"
1416
- close_write_tun( tun )
1410
+ # puts "debug write atun #{ e.class }"
1411
+ close_atun( atun )
1417
1412
  close_read_src( src )
1418
1413
  return
1419
1414
  end
1420
1415
 
1421
- # puts "debug2 written tun #{ written }"
1416
+ # puts "debug write atun #{ written }"
1422
1417
  data = data[ written..-1 ]
1423
- tun_info[ :wbuff ] = data
1418
+ atun_info[ :wbuff ] = data
1424
1419
 
1425
1420
  unless src.closed? then
1426
1421
  src_info = @src_infos[ src ]
@@ -1429,112 +1424,30 @@ module Girl
1429
1424
  end
1430
1425
 
1431
1426
  ##
1432
- # write pre proxy
1427
+ # write btun
1433
1428
  #
1434
- def write_pre_proxy( pre_proxy )
1435
- if pre_proxy.closed? then
1436
- puts "p#{ Process.pid } #{ Time.new } write pre proxy but pre proxy closed?"
1429
+ def write_btun( btun )
1430
+ if btun.closed? then
1431
+ puts "p#{ Process.pid } #{ Time.new } write btun but btun closed?"
1437
1432
  return
1438
1433
  end
1439
1434
 
1440
- proxy = OpenSSL::SSL::SSLSocket.new pre_proxy
1435
+ btun_info = @btun_infos[ btun ]
1436
+ data = btun_info[ :wbuff ]
1441
1437
 
1438
+ # 写入dst id
1442
1439
  begin
1443
- proxy.connect_nonblock
1444
- rescue IO::WaitReadable
1445
- rescue IO::WaitWritable
1440
+ written = btun.write( data )
1446
1441
  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
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 )
1442
+ # puts "debug write btun #{ e.class }"
1443
+ src = btun_info[ :src ]
1444
+ close_btun( btun )
1445
+ add_closing_src( src )
1498
1446
  return
1499
1447
  end
1500
1448
 
1501
- src_info = @src_infos[ src ]
1502
- dst_id = src_info[ :dst_id ]
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 )
1449
+ # puts "debug write btun #{ written }"
1450
+ @writes.delete( btun )
1538
1451
  end
1539
1452
 
1540
1453
  end