girl 0.94.0 → 0.99.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4fbf6e091f1c0c19865e86a349ed046284ca1f807a171cd8f91b19066de3e28
4
- data.tar.gz: cb9077f3870ba5cfc2ed17bf66f9c97767d94d882836acf757d5b6addcc4860a
3
+ metadata.gz: a9256dd41425122dd6c8f579d7821c70213b042090c2ce3320b20931c6aa9f8e
4
+ data.tar.gz: 7af6d01468ee207a768d52ecaf96b3baadb89bb81130f37850776b4ed6bf0393
5
5
  SHA512:
6
- metadata.gz: 4bcb135b88eb5fd4d06f7b7cbeb063553a380efa8c3359c0420b69c2b5481a873ea5632333c8dd56899112d86fb1a11697fa8befc734a168cb2d03ef9eb5b2b2
7
- data.tar.gz: '07935ef257d9301b18a9e1b4ed943ac0499ca4b502434b27aebebcfb8fcbe3f7a576fc9e5e7a16e9ed37ab9cf75eb490b1802dcad7024dd33f7db1a32ae11d37'
6
+ metadata.gz: ff1e09096efc25f66473d85e34652b76841ffa533a538c75f63562c3593afd48089d40398f74abebd40338f4af12bfc2277669ec9550f0b599b30437b681ed51
7
+ data.tar.gz: 95c954a9651bc636c20298a94a39a1ec334867283bd24d0b53423528d0047a82e0a89ea6764015d6d0cfc1598210aba586ef9fdf7e75ceea4a50e3797f85f356
@@ -13,6 +13,13 @@ module Girl
13
13
  @mutex = Mutex.new
14
14
  @reads = []
15
15
  @writes = []
16
+ @closing_srcs = []
17
+ @paused_srcs = []
18
+ @paused_dsts = []
19
+ @paused_streams = []
20
+ @resume_srcs = []
21
+ @resume_dsts = []
22
+ @resume_streams = []
16
23
  @roles = {} # sock => :dotr / :proxy / :src / :dst / :tun / :stream
17
24
  @src_infos = {} # src => {}
18
25
  @dst_infos = {} # dst => {}
@@ -37,7 +44,6 @@ module Girl
37
44
  rs, ws = IO.select( @reads, @writes )
38
45
 
39
46
  @mutex.synchronize do
40
- # 先读,再写,避免打上关闭标记后读到
41
47
  rs.each do | sock |
42
48
  case @roles[ sock ]
43
49
  when :dotr then
@@ -90,6 +96,15 @@ module Girl
90
96
 
91
97
  private
92
98
 
99
+ ##
100
+ # add closing src
101
+ #
102
+ def add_closing_src( src )
103
+ return if src.closed? || @closing_srcs.include?( src )
104
+ @closing_srcs << src
105
+ next_tick
106
+ end
107
+
93
108
  ##
94
109
  # add ctlmsg
95
110
  #
@@ -101,14 +116,42 @@ module Girl
101
116
  if to_addr then
102
117
  @tun_info[ :ctlmsgs ] << [ data, to_addr ]
103
118
  add_write( @tun )
119
+ next_tick
104
120
  end
105
121
  end
106
122
 
123
+ ##
124
+ # add paused src
125
+ #
126
+ def add_paused_src( src )
127
+ return if src.closed? || @paused_srcs.include?( src )
128
+ @reads.delete( src )
129
+ @paused_srcs << src
130
+ end
131
+
132
+ ##
133
+ # add paused dst
134
+ #
135
+ def add_paused_dst( dst )
136
+ return if dst.closed? || @paused_dsts.include?( dst )
137
+ @reads.delete( dst )
138
+ @paused_dsts << dst
139
+ end
140
+
141
+ ##
142
+ # add paused stream
143
+ #
144
+ def add_paused_stream( stream )
145
+ return if stream.closed? || @paused_streams.include?( stream )
146
+ @reads.delete( stream )
147
+ @paused_streams << stream
148
+ end
149
+
107
150
  ##
108
151
  # add read
109
152
  #
110
153
  def add_read( sock, role = nil )
111
- unless @reads.include?( sock ) then
154
+ if !sock.closed? && !@reads.include?( sock ) then
112
155
  @reads << sock
113
156
 
114
157
  if role then
@@ -117,11 +160,65 @@ module Girl
117
160
  end
118
161
  end
119
162
 
163
+ ##
164
+ # add resume src
165
+ #
166
+ def add_resume_src( src )
167
+ return if @resume_srcs.include?( src )
168
+ @resume_srcs << src
169
+ next_tick
170
+ end
171
+
172
+ ##
173
+ # add resume dst
174
+ #
175
+ def add_resume_dst( dst )
176
+ return if @resume_dsts.include?( dst )
177
+ @resume_dsts << dst
178
+ next_tick
179
+ end
180
+
181
+ ##
182
+ # add resume stream
183
+ #
184
+ def add_resume_stream( stream )
185
+ return if @resume_streams.include?( stream )
186
+ @resume_streams << stream
187
+ next_tick
188
+ end
189
+
190
+ ##
191
+ # add dst wbuff
192
+ #
193
+ def add_dst_wbuff( dst, data )
194
+ dst_info = @dst_infos[ dst ]
195
+ dst_info[ :wbuff ] << data
196
+ add_write( dst )
197
+
198
+ if dst_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
199
+ puts "p#{ Process.pid } #{ Time.new } pause direct src #{ dst_info[ :domain ] }"
200
+ add_paused_src( dst_info[ :src ] )
201
+ end
202
+ end
203
+
204
+ ##
205
+ # add src rbuff
206
+ #
207
+ def add_src_rbuff( src, data )
208
+ src_info = @src_infos[ src ]
209
+ src_info[ :rbuff ] << data
210
+
211
+ if src_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
212
+ # puts "debug1 src.rbuff full"
213
+ add_closing_src( src )
214
+ end
215
+ end
216
+
120
217
  ##
121
218
  # add src wbuff
122
219
  #
123
220
  def add_src_wbuff( src, data )
124
- return if src.closed?
221
+ return if src.closed? || @closing_srcs.include?( src )
125
222
  src_info = @src_infos[ src ]
126
223
  src_info[ :wbuff ] << data
127
224
  src_info[ :last_recv_at ] = Time.new
@@ -131,23 +228,33 @@ module Girl
131
228
  dst = src_info[ :dst ]
132
229
 
133
230
  if dst then
134
- dst_info = @dst_infos[ dst ]
135
- puts "p#{ Process.pid } #{ Time.new } pause dst #{ dst_info[ :domain ] }"
136
- dst_info[ :paused ] = true
137
- @reads.delete( dst )
231
+ puts "p#{ Process.pid } #{ Time.new } pause dst #{ src_info[ :destination_domain ] }"
232
+ add_paused_dst( dst )
138
233
  else
139
234
  stream = src_info[ :stream ]
140
235
 
141
236
  if stream then
142
- stream_info = @stream_infos[ stream ]
143
- puts "p#{ Process.pid } #{ Time.new } pause stream #{ stream_info[ :domain ] }"
144
- stream_info[ :paused ] = true
145
- @reads.delete( stream )
237
+ puts "p#{ Process.pid } #{ Time.new } pause stream #{ src_info[ :destination_domain ] }"
238
+ add_paused_stream( stream )
146
239
  end
147
240
  end
148
241
  end
149
242
  end
150
243
 
244
+ ##
245
+ # add stream wbuff
246
+ #
247
+ def add_stream_wbuff( stream, data )
248
+ stream_info = @stream_infos[ stream ]
249
+ stream_info[ :wbuff ] << data
250
+ add_write( stream )
251
+
252
+ if stream_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
253
+ puts "p#{ Process.pid } #{ Time.new } pause tunnel src #{ stream_info[ :domain ] }"
254
+ add_paused_src( stream_info[ :src ] )
255
+ end
256
+ end
257
+
151
258
  ##
152
259
  # add src wbuff socks5 conn reply
153
260
  #
@@ -167,7 +274,7 @@ module Girl
167
274
  # add write
168
275
  #
169
276
  def add_write( sock )
170
- unless @writes.include?( sock ) then
277
+ if !sock.closed? && !@writes.include?( sock ) then
171
278
  @writes << sock
172
279
  end
173
280
  end
@@ -182,18 +289,6 @@ module Girl
182
289
  @roles.delete( sock )
183
290
  end
184
291
 
185
- ##
186
- # close dst
187
- #
188
- def close_dst( dst )
189
- # puts "debug1 close dst"
190
- close_sock( dst )
191
- dst_info = @dst_infos.delete( dst )
192
- src = dst_info[ :src ]
193
- close_read_src( src )
194
- set_src_closing_write( src )
195
- end
196
-
197
292
  ##
198
293
  # close read src
199
294
  #
@@ -211,7 +306,6 @@ module Girl
211
306
  src_info = @src_infos[ src ]
212
307
  end
213
308
 
214
- src_info[ :paused ] = false
215
309
  src_info
216
310
  end
217
311
 
@@ -259,6 +353,7 @@ module Girl
259
353
  # close src
260
354
  #
261
355
  def close_src( src )
356
+ return if src.closed?
262
357
  # puts "debug1 close src"
263
358
  close_sock( src )
264
359
  src_info = del_src_info( src )
@@ -277,25 +372,14 @@ module Girl
277
372
  end
278
373
  end
279
374
 
280
- ##
281
- # close stream
282
- #
283
- def close_stream( stream )
284
- # puts "debug1 close stream"
285
- close_sock( stream )
286
- stream_info = @stream_infos.delete( stream )
287
- src = stream_info[ :src ]
288
- close_read_src( src )
289
- set_src_closing_write( src )
290
- end
291
-
292
375
  ##
293
376
  # close tun
294
377
  #
295
378
  def close_tun( tun )
296
379
  # puts "debug1 close tun"
297
380
  close_sock( tun )
298
- @tun_info[ :srcs ].each{ | _, src | set_src_closing( src ) }
381
+ @tun_info[ :ctlmsgs ].clear
382
+ @tun_info[ :srcs ].each{ | _, src | close_src( src ) }
299
383
  end
300
384
 
301
385
  ##
@@ -315,7 +399,6 @@ module Girl
315
399
  src_info = @src_infos[ src ]
316
400
  end
317
401
 
318
- src_info[ :closed_write ] = true
319
402
  src_info
320
403
  end
321
404
 
@@ -336,7 +419,6 @@ module Girl
336
419
  dst_info = @dst_infos[ dst ]
337
420
  end
338
421
 
339
- dst_info[ :closed_write ] = true
340
422
  dst_info
341
423
  end
342
424
 
@@ -357,7 +439,6 @@ module Girl
357
439
  stream_info = @stream_infos[ stream ]
358
440
  end
359
441
 
360
- stream_info[ :closed_write ] = true
361
442
  stream_info
362
443
  end
363
444
 
@@ -384,7 +465,7 @@ module Girl
384
465
  def del_src_info( src )
385
466
  src_info = @src_infos.delete( src )
386
467
 
387
- if ( src_info[ :proxy_type ] == :tunnel ) && @tun && !@tun.closed? then
468
+ if ( src_info[ :proxy_type ] == :tunnel ) && @tun then
388
469
  @tun_info[ :srcs ].delete( src_info[ :id ] )
389
470
  end
390
471
 
@@ -400,7 +481,6 @@ module Girl
400
481
  sleep CHECK_EXPIRE_INTERVAL
401
482
 
402
483
  @mutex.synchronize do
403
- trigger = false
404
484
  now = Time.new
405
485
 
406
486
  if @tun && !@tun.closed? then
@@ -414,8 +494,6 @@ module Girl
414
494
  data = [ 0, HEARTBEAT ].pack( 'Q>C' )
415
495
  add_ctlmsg( data )
416
496
  end
417
-
418
- trigger = true
419
497
  end
420
498
 
421
499
  @src_infos.each do | src, src_info |
@@ -424,12 +502,9 @@ module Girl
424
502
 
425
503
  if ( now - last_recv_at >= EXPIRE_AFTER ) && ( now - last_sent_at >= EXPIRE_AFTER ) then
426
504
  puts "p#{ Process.pid } #{ Time.new } expire src #{ src_info[ :destination_domain ] }"
427
- set_src_closing( src )
428
- trigger = true
505
+ add_closing_src( src )
429
506
  end
430
507
  end
431
-
432
- next_tick if trigger
433
508
  end
434
509
  end
435
510
  end
@@ -444,56 +519,61 @@ module Girl
444
519
  sleep CHECK_RESUME_INTERVAL
445
520
 
446
521
  @mutex.synchronize do
447
- trigger = false
448
-
449
- @src_infos.select{ | _, src_info | src_info[ :paused ] }.each do | src, src_info |
450
- dst = src_info[ :dst ]
451
-
452
- if dst then
453
- dst_info = @dst_infos[ dst ]
454
-
455
- if dst_info[ :wbuff ].size < RESUME_BELOW then
456
- puts "p#{ Process.pid } #{ Time.new } dst.wbuff below #{ RESUME_BELOW }, resume src #{ src_info[ :destination_domain ] }"
457
- resume_src( src )
458
- trigger = true
459
- end
522
+ @paused_srcs.each do | src |
523
+ if src.closed? then
524
+ add_resume_src( src )
460
525
  else
461
- stream = src_info[ :stream ]
462
- stream_info = @stream_infos[ stream ]
463
-
464
- if stream_info[ :wbuff ].size < RESUME_BELOW then
465
- puts "p#{ Process.pid } #{ Time.new } stream.wbuff below #{ RESUME_BELOW }, resume src #{ src_info[ :destination_domain ] }"
466
- resume_src( src )
467
- trigger = true
526
+ src_info = @src_infos[ src ]
527
+ dst = src_info[ :dst ]
528
+
529
+ if dst then
530
+ dst_info = @dst_infos[ dst ]
531
+
532
+ if dst_info[ :wbuff ].size < RESUME_BELOW then
533
+ puts "p#{ Process.pid } #{ Time.new } resume direct src #{ src_info[ :destination_domain ] }"
534
+ add_resume_src( src )
535
+ end
536
+ else
537
+ stream = src_info[ :stream ]
538
+ stream_info = @stream_infos[ stream ]
539
+
540
+ if stream_info[ :wbuff ].size < RESUME_BELOW then
541
+ puts "p#{ Process.pid } #{ Time.new } resume tunnel src #{ src_info[ :destination_domain ] }"
542
+ add_resume_src( src )
543
+ end
468
544
  end
469
545
  end
470
546
  end
471
547
 
472
- @dst_infos.select{ | _, dst_info | dst_info[ :paused ] }.each do | dst, dst_info |
473
- src = dst_info[ :src ]
474
- src_info = @src_infos[ src ]
548
+ @paused_dsts.each do | dst |
549
+ if dst.closed? then
550
+ add_resume_dst( dst )
551
+ else
552
+ dst_info = @dst_infos[ dst ]
553
+ src = dst_info[ :src ]
554
+ src_info = @src_infos[ src ]
475
555
 
476
- if src_info[ :wbuff ].size < RESUME_BELOW then
477
- puts "p#{ Process.pid } #{ Time.new } src.wbuff below #{ RESUME_BELOW }, resume dst #{ dst_info[ :domain ] }"
478
- dst_info[ :paused ] = false
479
- add_read( dst )
480
- trigger = true
556
+ if src_info[ :wbuff ].size < RESUME_BELOW then
557
+ puts "p#{ Process.pid } #{ Time.new } resume dst #{ dst_info[ :domain ] }"
558
+ add_resume_dst( dst )
559
+ end
481
560
  end
482
561
  end
483
562
 
484
- @stream_infos.select{ | _, stream_info | stream_info[ :paused ] }.each do | stream, stream_info |
485
- src = stream_info[ :src ]
486
- src_info = @src_infos[ src ]
563
+ @paused_streams.each do | stream |
564
+ if stream.closed? then
565
+ add_resume_stream( stream )
566
+ else
567
+ stream_info = @stream_infos[ stream ]
568
+ src = stream_info[ :src ]
569
+ src_info = @src_infos[ src ]
487
570
 
488
- if src_info[ :wbuff ].size < RESUME_BELOW then
489
- puts "p#{ Process.pid } #{ Time.new } src.wbuff below #{ RESUME_BELOW }, resume stream #{ stream_info[ :domain ] }"
490
- stream_info[ :paused ] = false
491
- add_read( stream )
492
- trigger = true
571
+ if src_info[ :wbuff ].size < RESUME_BELOW then
572
+ puts "p#{ Process.pid } #{ Time.new } resume stream #{ stream_info[ :domain ] }"
573
+ add_resume_stream( stream )
574
+ end
493
575
  end
494
576
  end
495
-
496
- next_tick if trigger
497
577
  end
498
578
  end
499
579
  end
@@ -513,7 +593,7 @@ module Girl
513
593
 
514
594
  Thread.new do
515
595
  SEND_HELLO_COUNT.times do | i |
516
- if @tun.nil? || @tun.closed? || src.closed? || src_info[ :stream ] then
596
+ if @tun.nil? || @tun.closed? || @tun_info[ :closing ] || src.closed? || src_info[ :stream ] then
517
597
  # puts "debug1 break loop send a new source #{ src_info[ :dst_port ] }"
518
598
  break
519
599
  end
@@ -524,7 +604,6 @@ module Girl
524
604
  end
525
605
 
526
606
  add_ctlmsg( data )
527
- next_tick
528
607
  end
529
608
 
530
609
  sleep SEND_HELLO_INTERVAL
@@ -549,21 +628,16 @@ module Girl
549
628
  # connect nonblock 必抛 wait writable
550
629
  rescue Exception => e
551
630
  puts "p#{ Process.pid } #{ Time.new } dst connect destination #{ domain } #{ src_info[ :destination_port ] } #{ ip_info.ip_address } #{ e.class }, close src"
552
- set_src_closing( src )
631
+ add_closing_src( src )
553
632
  return
554
633
  end
555
634
 
556
635
  # puts "debug1 a new dst #{ dst.local_address.inspect }"
557
- local_port = dst.local_address.ip_port
558
636
  dst_info = {
559
- local_port: local_port, # 本地端口
560
- src: src, # 对应src
561
- domain: domain, # 目的地
562
- wbuff: '', # 写前,从src读到的流量
563
- paused: false, # 是否已暂停读
564
- closing: false, # 准备关闭
565
- closing_write: false, # 准备关闭写
566
- closed_write: false # 已关闭写
637
+ src: src, # 对应src
638
+ domain: domain, # 目的地
639
+ wbuff: '', # 写前,从src读到的流量
640
+ closing_write: false # 准备关闭写
567
641
  }
568
642
 
569
643
  @dst_infos[ dst ] = dst_info
@@ -597,7 +671,7 @@ module Girl
597
671
 
598
672
  if dst_id == 0 then
599
673
  puts "p#{ Process.pid } #{ Time.new } remote dst already closed"
600
- set_src_closing( src )
674
+ add_closing_src( src )
601
675
  return
602
676
  end
603
677
 
@@ -625,10 +699,7 @@ module Girl
625
699
  src: src, # 对应src
626
700
  domain: domain, # 目的地
627
701
  wbuff: data, # 写前,写往远端streamd
628
- paused: false, # 是否已暂停读
629
- closing: false, # 准备关闭
630
- closing_write: false, # 准备关闭写
631
- closed_write: false # 已关闭写
702
+ closing_write: false # 准备关闭写
632
703
  }
633
704
 
634
705
  src_info[ :dst_id ] = dst_id
@@ -692,7 +763,7 @@ module Girl
692
763
 
693
764
  Thread.new do
694
765
  SEND_HELLO_COUNT.times do | i |
695
- if @tun.nil? || @tun.closed? || @tun_info[ :tund_addr ] then
766
+ if @tun.nil? || @tun.closed? || @tun_info[ :closing ] || @tun_info[ :tund_addr ] then
696
767
  # puts "debug1 break loop send hello"
697
768
  break
698
769
  end
@@ -703,7 +774,6 @@ module Girl
703
774
  # puts "debug1 #{ data.inspect }"
704
775
 
705
776
  add_ctlmsg( data, @proxyd_addr )
706
- next_tick
707
777
  end
708
778
 
709
779
  sleep SEND_HELLO_INTERVAL
@@ -760,41 +830,15 @@ module Girl
760
830
  unless src.closed? then
761
831
  puts "p#{ Process.pid } #{ Time.new } resolved #{ domain } #{ ip_info.ip_address }"
762
832
  deal_with_destination_ip( src, ip_info )
833
+ next_tick
763
834
  end
764
835
  else
765
- set_src_closing( src )
836
+ add_closing_src( src )
766
837
  end
767
-
768
- next_tick
769
838
  end
770
839
  end
771
840
  end
772
841
 
773
- ##
774
- # resume src
775
- #
776
- def resume_src( src )
777
- src_info = @src_infos[ src ]
778
- src_info[ :paused ] = false
779
- add_read( src )
780
- end
781
-
782
- ##
783
- # set dst closing
784
- #
785
- def set_dst_closing( dst )
786
- return if dst.closed?
787
- dst_info = @dst_infos[ dst ]
788
-
789
- if dst_info[ :closed_write ] then
790
- close_read_dst( dst )
791
- else
792
- dst_info[ :closing ] = true
793
- @reads.delete( dst )
794
- add_write( dst )
795
- end
796
- end
797
-
798
842
  ##
799
843
  # set dst closing write
800
844
  #
@@ -802,37 +846,20 @@ module Girl
802
846
  return if dst.closed?
803
847
 
804
848
  dst_info = @dst_infos[ dst ]
805
- return if dst_info[ :closed_write ]
849
+ return if dst_info[ :closing_write ]
806
850
 
807
851
  dst_info[ :closing_write ] = true
808
852
  add_write( dst )
809
853
  end
810
854
 
811
- ##
812
- # set src is closing
813
- #
814
- def set_src_closing( src )
815
- return if src.closed?
816
-
817
- src_info = @src_infos[ src ]
818
-
819
- if src_info[ :closed_write ] then
820
- close_read_src( src )
821
- else
822
- @reads.delete( src )
823
- src_info[ :closing ] = true
824
- add_write( src )
825
- end
826
- end
827
-
828
855
  ##
829
856
  # set src closing write
830
857
  #
831
858
  def set_src_closing_write( src )
832
- return if src.closed?
859
+ return if src.closed? || @closing_srcs.include?( src )
833
860
 
834
861
  src_info = @src_infos[ src ]
835
- return if src_info[ :closed_write ]
862
+ return if src_info[ :closing_write ]
836
863
 
837
864
  src_info[ :closing_write ] = true
838
865
  add_write( src )
@@ -842,10 +869,6 @@ module Girl
842
869
  # set src proxy type tunnel
843
870
  #
844
871
  def set_src_proxy_type_tunnel( src )
845
- if @tun.nil? || @tun.closed? then
846
- new_a_tun
847
- end
848
-
849
872
  src_info = @src_infos[ src ]
850
873
  src_info[ :proxy_type ] = :tunnel
851
874
  src_id = src_info[ :id ]
@@ -865,7 +888,7 @@ module Girl
865
888
  return if stream.closed?
866
889
 
867
890
  stream_info = @stream_infos[ stream ]
868
- return if stream_info[ :closed_write ]
891
+ return if stream_info[ :closing_write ]
869
892
 
870
893
  stream_info[ :closing_write ] = true
871
894
  add_write( stream )
@@ -875,10 +898,11 @@ module Girl
875
898
  # set tun is closing
876
899
  #
877
900
  def set_tun_closing
878
- return if @tun.closed?
901
+ return if @tun.closed? || @tun_info[ :closing ]
879
902
  @tun_info[ :closing ] = true
880
903
  @reads.delete( @tun )
881
- add_write( @tun )
904
+ @writes.delete( @tun )
905
+ next_tick
882
906
  end
883
907
 
884
908
  ##
@@ -904,7 +928,43 @@ module Girl
904
928
  # read dotr
905
929
  #
906
930
  def read_dotr( dotr )
907
- dotr.read( 1 )
931
+ dotr.read_nonblock( READ_SIZE )
932
+
933
+ if @tun && !@tun.closed? && @tun_info[ :closing ] then
934
+ close_tun( @tun )
935
+ end
936
+
937
+ if @closing_srcs.any? then
938
+ @closing_srcs.each{ | src | close_src( src ) }
939
+ @closing_srcs.clear
940
+ end
941
+
942
+ if @resume_srcs.any? then
943
+ @resume_srcs.each do | src |
944
+ add_read( src )
945
+ @paused_srcs.delete( src )
946
+ end
947
+
948
+ @resume_srcs.clear
949
+ end
950
+
951
+ if @resume_dsts.any? then
952
+ @resume_dsts.each do | dst |
953
+ add_read( dst )
954
+ @paused_dsts.delete( dst )
955
+ end
956
+
957
+ @resume_dsts.clear
958
+ end
959
+
960
+ if @resume_streams.any? then
961
+ @resume_streams.each do | stream |
962
+ add_read( stream )
963
+ @paused_streams.delete( stream )
964
+ end
965
+
966
+ @resume_streams.clear
967
+ end
908
968
  end
909
969
 
910
970
  ##
@@ -936,19 +996,23 @@ module Girl
936
996
  created_at: Time.new, # 创建时间
937
997
  last_recv_at: nil, # 上一次收到新流量(由dst收到,或者由stream收到)的时间
938
998
  last_sent_at: nil, # 上一次发出流量(由dst发出,或者由stream发出)的时间
939
- paused: false, # 是否已暂停读
940
- closing: false, # 准备关闭
941
- closing_write: false, # 准备关闭写
942
- closed_write: false # 已关闭写
999
+ closing_write: false # 准备关闭写
943
1000
  }
944
1001
 
945
1002
  add_read( src, :src )
1003
+
1004
+ # 用到tun,是在解析目的地域名得出是国外ip之后。但解析域名是额外的线程,为避免多线程重复建tun,在accept到src时就建。
1005
+ if @tun.nil? || @tun.closed? then
1006
+ new_a_tun
1007
+ end
946
1008
  end
947
1009
 
948
1010
  ##
949
1011
  # read tun
950
1012
  #
951
1013
  def read_tun( tun )
1014
+ return if tun.closed?
1015
+
952
1016
  begin
953
1017
  data, addrinfo, rflags, *controls = tun.recvmsg_nonblock
954
1018
  rescue IO::WaitReadable, Errno::EINTR
@@ -1039,7 +1103,7 @@ module Girl
1039
1103
 
1040
1104
  unless domain_port then
1041
1105
  puts "p#{ Process.pid } #{ Time.new } CONNECT miss domain"
1042
- set_src_closing( src )
1106
+ add_closing_src( src )
1043
1107
  return
1044
1108
  end
1045
1109
  elsif data[ 0 ].unpack( 'C' ).first == 5 then
@@ -1057,7 +1121,7 @@ module Girl
1057
1121
 
1058
1122
  unless methods.include?( 0 ) then
1059
1123
  puts "p#{ Process.pid } #{ Time.new } miss method 00"
1060
- set_src_closing( src )
1124
+ add_closing_src( src )
1061
1125
  return
1062
1126
  end
1063
1127
 
@@ -1077,7 +1141,7 @@ module Girl
1077
1141
 
1078
1142
  unless host_line then
1079
1143
  # puts "debug1 not found host line"
1080
- set_src_closing( src )
1144
+ add_closing_src( src )
1081
1145
  return
1082
1146
  end
1083
1147
 
@@ -1089,7 +1153,7 @@ module Girl
1089
1153
 
1090
1154
  unless domain_port then
1091
1155
  puts "p#{ Process.pid } #{ Time.new } Host line miss domain"
1092
- set_src_closing( src )
1156
+ add_closing_src( src )
1093
1157
  return
1094
1158
  end
1095
1159
  end
@@ -1154,26 +1218,13 @@ module Girl
1154
1218
  data, _ = sub_http_request( data )
1155
1219
  end
1156
1220
 
1157
- stream_info = @stream_infos[ stream ]
1158
1221
  data = @custom.encode( data )
1159
1222
  # puts "debug2 add stream.wbuff encoded #{ data.bytesize }"
1160
- stream_info[ :wbuff ] << data
1161
- add_write( stream )
1162
-
1163
- if stream_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
1164
- puts "p#{ Process.pid } #{ Time.new } pause tunnel src #{ src_info[ :destination_domain ] }"
1165
- src_info[ :paused ] = true
1166
- @reads.delete( src )
1167
- end
1223
+ add_stream_wbuff( stream, data )
1168
1224
  end
1169
1225
  else
1170
1226
  # puts "debug1 stream not ready, save data to src.rbuff"
1171
- src_info[ :rbuff ] << data
1172
-
1173
- if src_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
1174
- # puts "debug1 tunnel src.rbuff full"
1175
- set_src_closing( src )
1176
- end
1227
+ add_src_rbuff( src, data )
1177
1228
  end
1178
1229
  when :direct then
1179
1230
  dst = src_info[ :dst ]
@@ -1184,25 +1235,12 @@ module Girl
1184
1235
  data, _ = sub_http_request( data )
1185
1236
  end
1186
1237
 
1187
- dst_info = @dst_infos[ dst ]
1188
1238
  # puts "debug2 add dst.wbuff #{ data.bytesize }"
1189
- dst_info[ :wbuff ] << data
1190
- add_write( dst )
1191
-
1192
- if dst_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
1193
- puts "p#{ Process.pid } #{ Time.new } pause direct src #{ src_info[ :destination_domain ] }"
1194
- src_info[ :paused ] = true
1195
- @reads.delete( src )
1196
- end
1239
+ add_dst_wbuff( dst, data )
1197
1240
  end
1198
1241
  else
1199
1242
  # puts "debug1 dst not ready, save data to src.rbuff"
1200
- src_info[ :rbuff ] << data
1201
-
1202
- if src_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
1203
- # puts "debug1 direct src.rbuff full"
1204
- set_src_closing( src )
1205
- end
1243
+ add_src_rbuff( src, data )
1206
1244
  end
1207
1245
  end
1208
1246
  end
@@ -1261,13 +1299,7 @@ module Girl
1261
1299
  # write tun
1262
1300
  #
1263
1301
  def write_tun( tun )
1264
- # 处理关闭
1265
- if @tun_info[ :closing ] then
1266
- close_tun( tun )
1267
- return
1268
- end
1269
-
1270
- now = Time.new
1302
+ return if tun.closed?
1271
1303
 
1272
1304
  # 发ctlmsg
1273
1305
  while @tun_info[ :ctlmsgs ].any? do
@@ -1279,8 +1311,8 @@ module Girl
1279
1311
  puts "p#{ Process.pid } #{ Time.new } wait send ctlmsg, left #{ @tun_info[ :ctlmsgs ].size }"
1280
1312
  return
1281
1313
  rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
1282
- puts "p#{ Process.pid } #{ Time.new } sendmsg #{ e.class }, close tun"
1283
- close_tun( tun )
1314
+ puts "p#{ Process.pid } #{ Time.new } sendmsg #{ e.class }, set tun closing"
1315
+ set_tun_closing
1284
1316
  return
1285
1317
  end
1286
1318
 
@@ -1297,14 +1329,6 @@ module Girl
1297
1329
  return if src.closed?
1298
1330
  src_info = @src_infos[ src ]
1299
1331
  dst = src_info[ :dst ]
1300
-
1301
- # 处理关闭
1302
- if src_info[ :closing ] then
1303
- close_src( src )
1304
- return
1305
- end
1306
-
1307
- # 处理wbuff
1308
1332
  data = src_info[ :wbuff ]
1309
1333
 
1310
1334
  # 写前为空,处理关闭写
@@ -1350,13 +1374,6 @@ module Girl
1350
1374
  return if dst.closed?
1351
1375
  dst_info = @dst_infos[ dst ]
1352
1376
  src = dst_info[ :src ]
1353
-
1354
- # 处理关闭
1355
- if dst_info[ :closing ] then
1356
- close_dst( dst )
1357
- return
1358
- end
1359
-
1360
1377
  data = dst_info[ :wbuff ]
1361
1378
 
1362
1379
  # 写前为空,处理关闭写
@@ -1399,13 +1416,6 @@ module Girl
1399
1416
  return if stream.closed?
1400
1417
  stream_info = @stream_infos[ stream ]
1401
1418
  src = stream_info[ :src ]
1402
-
1403
- # 处理关闭
1404
- if stream_info[ :closing ] then
1405
- close_stream( stream )
1406
- return
1407
- end
1408
-
1409
1419
  data = stream_info[ :wbuff ]
1410
1420
 
1411
1421
  # 写前为空,处理关闭写