girl 0.56.0 → 0.60.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.

@@ -3,7 +3,7 @@ require 'girl/version'
3
3
  require 'socket'
4
4
 
5
5
  ##
6
- # Girl::Resolv - dns查询得到正确的ip。近端。
6
+ # Girl::Resolv - dns查询得到正确的ip,近端。
7
7
  #
8
8
  # usage
9
9
  # =====
@@ -3,7 +3,7 @@ require 'girl/version'
3
3
  require 'socket'
4
4
 
5
5
  ##
6
- # Girl::Resolvd - dns查询得到正确的ip。远端。
6
+ # Girl::Resolvd - dns查询得到正确的ip,远端。
7
7
  #
8
8
  module Girl
9
9
  class Resolvd
@@ -4,7 +4,7 @@ require 'girl/version'
4
4
  require 'socket'
5
5
 
6
6
  ##
7
- # Girl::Tun - tcp流量正常的到达目的地。近端。
7
+ # Girl::Tun - tcp透明转发,近端。
8
8
  #
9
9
  # usage
10
10
  # =====
@@ -39,18 +39,6 @@ require 'socket'
39
39
  # 12 tund fin
40
40
  # 13 tun fin
41
41
  #
42
- # 两套关闭
43
- # ========
44
- #
45
- # 1-1. source.close -> ext.is_dest_closed ? no -> send fin1 loop
46
- # 1-2. recv got_fin1 -> break loop
47
- # 1-3. recv fin2 -> send got_fin2 -> del ext
48
- #
49
- # 2-1. recv fin1 -> send got_fin1 -> ext.is_dest_closed = true
50
- # 2-2. all sent && ext.biggest_dest_pack_id == ext.continue_dest_pack_id -> add closing source
51
- # 2-3. source.close -> ext.is_dest_closed ? yes -> del ext -> loop send fin2
52
- # 2-4. recv got_fin2 -> break loop
53
- #
54
42
  module Girl
55
43
  class Tun
56
44
  ##
@@ -96,6 +84,15 @@ module Girl
96
84
  rs, ws = IO.select( @reads, @writes )
97
85
 
98
86
  @mutex.synchronize do
87
+ ws.each do | sock |
88
+ case @roles[ sock ]
89
+ when :source
90
+ write_source( sock )
91
+ when :tun
92
+ write_tun( sock )
93
+ end
94
+ end
95
+
99
96
  rs.each do | sock |
100
97
  case @roles[ sock ]
101
98
  when :ctlr
@@ -108,15 +105,6 @@ module Girl
108
105
  read_tun( sock )
109
106
  end
110
107
  end
111
-
112
- ws.each do | sock |
113
- case @roles[ sock ]
114
- when :source
115
- write_source( sock )
116
- when :tun
117
- write_tun( sock )
118
- end
119
- end
120
108
  end
121
109
  end
122
110
  rescue Interrupt => e
@@ -707,13 +695,13 @@ module Girl
707
695
 
708
696
  def check_expire( tun )
709
697
  Thread.new do
710
- sleep HEARTBEAT_INTERVAL
698
+ sleep 3
711
699
 
712
- unless tun.closed?
713
- tun_info = @infos[ tun ]
700
+ @mutex.synchronize do
701
+ unless tun.closed?
702
+ tun_info = @infos[ tun ]
714
703
 
715
- unless tun_info[ :tund_addr ]
716
- @mutex.synchronize do
704
+ unless tun_info[ :tund_addr ]
717
705
  tun_id = @socks[ tun ]
718
706
  @ctlw.write( [ CTL_CLOSE, tun_id ].pack( 'CQ>' ) )
719
707
  end
@@ -739,26 +727,23 @@ module Girl
739
727
  def loop_check_expire( tun )
740
728
  Thread.new do
741
729
  loop do
742
- sleep 60
730
+ sleep CHECK_EXPIRE_INTERVAL
731
+
743
732
  break if tun.closed?
744
733
 
745
- now = Time.new
746
- tun_info = @infos[ tun ]
734
+ @mutex.synchronize do
735
+ now = Time.new
736
+ tun_info = @infos[ tun ]
747
737
 
748
- if now - tun_info[ :last_traffic_at ] > EXPIRE_AFTER
749
- @mutex.synchronize do
738
+ if now - tun_info[ :last_traffic_at ] > EXPIRE_AFTER
750
739
  tun_id = @socks[ tun ]
751
740
  # puts "debug ctlw close tun #{ tun_id } #{ Time.new } p#{ Process.pid }"
752
741
  @ctlw.write( [ CTL_CLOSE, tun_id ].pack( 'CQ>' ) )
753
742
  end
754
743
 
755
- break
756
- end
757
-
758
- exts = tun_info[ :source_exts ].select{ | _, ext | now - ext[ :created_at ] > 5 }
744
+ exts = tun_info[ :source_exts ].select{ | _, ext | now - ext[ :created_at ] > 5 }
759
745
 
760
- if exts.any?
761
- @mutex.synchronize do
746
+ if exts.any?
762
747
  exts.each do | source_id, ext |
763
748
  if ext[ :last_recv_at ].nil? || ( now - ext[ :last_recv_at ] > EXPIRE_AFTER )
764
749
  # puts "debug ctlw close source #{ source_id } #{ Time.new } p#{ Process.pid }"
@@ -781,10 +766,10 @@ module Girl
781
766
  break
782
767
  end
783
768
 
784
- tun_info = @infos[ tun ]
769
+ @mutex.synchronize do
770
+ tun_info = @infos[ tun ]
785
771
 
786
- if tun_info[ :source_exts ].any?
787
- @mutex.synchronize do
772
+ if tun_info[ :source_exts ].any?
788
773
  now = Time.new
789
774
 
790
775
  tun_info[ :source_exts ].each do | source_id, ext |
@@ -801,10 +786,8 @@ module Girl
801
786
  end
802
787
  end
803
788
  end
804
- end
805
789
 
806
- if tun_info[ :paused ] && ( tun_info[ :source_exts ].map{ | _, ext | ext[ :wmems ].size }.sum < RESUME_BELOW )
807
- @mutex.synchronize do
790
+ if tun_info[ :paused ] && ( tun_info[ :source_exts ].map{ | _, ext | ext[ :wmems ].size }.sum < RESUME_BELOW )
808
791
  tun_id = @socks[ tun ]
809
792
  puts "ctlw resume #{ tun_id } #{ Time.new } p#{ Process.pid }"
810
793
  @ctlw.write( [ CTL_RESUME, tun_id ].pack( 'CQ>' ) )
@@ -817,7 +800,7 @@ module Girl
817
800
 
818
801
  def loop_send_a_new_source( source, original_dst )
819
802
  Thread.new do
820
- 100.times do
803
+ 30.times do
821
804
  break if source.closed?
822
805
 
823
806
  source_info = @infos[ source ]
@@ -834,12 +817,12 @@ module Girl
834
817
  # puts "debug break a new source loop #{ Time.new } p#{ Process.pid }"
835
818
  break
836
819
  end
820
+ end
837
821
 
838
- @mutex.synchronize do
839
- ctlmsg = "#{ [ 0, A_NEW_SOURCE, source_id ].pack( 'Q>CQ>' ) }#{ original_dst }"
840
- # puts "debug send a new source #{ Time.new } p#{ Process.pid }"
841
- send_pack( tun, ctlmsg, tun_info[ :tund_addr ] )
842
- end
822
+ @mutex.synchronize do
823
+ ctlmsg = "#{ [ 0, A_NEW_SOURCE, source_id ].pack( 'Q>CQ>' ) }#{ original_dst }"
824
+ # puts "debug send a new source #{ Time.new } p#{ Process.pid }"
825
+ send_pack( tun, ctlmsg, tun_info[ :tund_addr ] )
843
826
  end
844
827
 
845
828
  sleep 1
@@ -849,7 +832,7 @@ module Girl
849
832
 
850
833
  def loop_send_fin1( tun, source_id )
851
834
  Thread.new do
852
- 100.times do
835
+ 30.times do
853
836
  break if tun.closed?
854
837
 
855
838
  tun_info = @infos[ tun ]
@@ -878,7 +861,7 @@ module Girl
878
861
 
879
862
  def loop_send_fin2( tun, source_id )
880
863
  Thread.new do
881
- 100.times do
864
+ 30.times do
882
865
  break if tun.closed?
883
866
 
884
867
  tun_info = @infos[ tun ]
@@ -4,19 +4,7 @@ require 'girl/version'
4
4
  require 'socket'
5
5
 
6
6
  ##
7
- # Girl::Tund - tcp流量正常的到达目的地。远端。
8
- #
9
- # 两套关闭
10
- # ========
11
- #
12
- # 1-1. dest.close -> ext.is_source_closed ? no -> send fin1 loop
13
- # 1-2. recv got_fin1 -> break loop
14
- # 1-3. recv fin2 -> send got_fin2 -> del ext
15
- #
16
- # 2-1. recv fin1 -> send got_fin1 -> ext.is_source_closed = true
17
- # 2-2. all sent && ext.biggest_source_pack_id == ext.continue_source_pack_id -> add closing dest
18
- # 2-3. dest.close -> ext.is_source_closed ? yes -> del ext -> loop send fin2
19
- # 2-4. recv got_fin2 -> break loop
7
+ # Girl::Tund - tcp透明转发,远端。
20
8
  #
21
9
  module Girl
22
10
  class Tund
@@ -54,6 +42,15 @@ module Girl
54
42
  rs, ws = IO.select( @reads, @writes )
55
43
 
56
44
  @mutex.synchronize do
45
+ ws.each do | sock |
46
+ case @roles[ sock ]
47
+ when :dest
48
+ write_dest( sock )
49
+ when :tund
50
+ write_tund( sock )
51
+ end
52
+ end
53
+
57
54
  rs.each do | sock |
58
55
  case @roles[ sock ]
59
56
  when :ctlr
@@ -66,15 +63,6 @@ module Girl
66
63
  read_tund( sock )
67
64
  end
68
65
  end
69
-
70
- ws.each do | sock |
71
- case @roles[ sock ]
72
- when :dest
73
- write_dest( sock )
74
- when :tund
75
- write_tund( sock )
76
- end
77
- end
78
66
  end
79
67
  end
80
68
  rescue Interrupt => e
@@ -660,13 +648,13 @@ module Girl
660
648
 
661
649
  def check_expire( tund )
662
650
  Thread.new do
663
- sleep HEARTBEAT_INTERVAL
651
+ sleep 3
664
652
 
665
- unless tund.closed?
666
- tund_info = @infos[ tund ]
653
+ @mutex.synchronize do
654
+ unless tund.closed?
655
+ tund_info = @infos[ tund ]
667
656
 
668
- unless tund_info[ :tun_addr ]
669
- @mutex.synchronize do
657
+ unless tund_info[ :tun_addr ]
670
658
  tund_id = @socks[ tund ]
671
659
  @ctlw.write( [ CTL_CLOSE, tund_id ].pack( 'CQ>' ) )
672
660
  end
@@ -678,25 +666,22 @@ module Girl
678
666
  def loop_check_expire( tund )
679
667
  Thread.new do
680
668
  loop do
681
- sleep 60
669
+ sleep CHECK_EXPIRE_INTERVAL
670
+
682
671
  break if tund.closed?
683
672
 
684
- now = Time.new
685
- tund_info = @infos[ tund ]
673
+ @mutex.synchronize do
674
+ now = Time.new
675
+ tund_info = @infos[ tund ]
686
676
 
687
- if now - tund_info[ :last_traffic_at ] > EXPIRE_AFTER
688
- @mutex.synchronize do
677
+ if now - tund_info[ :last_traffic_at ] > EXPIRE_AFTER
689
678
  tund_id = @socks[ tund ]
690
679
  @ctlw.write( [ CTL_CLOSE, tund_id ].pack( 'CQ>' ) )
691
680
  end
692
681
 
693
- break
694
- end
695
-
696
- exts = tund_info[ :dest_exts ].select{ | _, ext | now - ext[ :created_at ] > 5 }
682
+ exts = tund_info[ :dest_exts ].select{ | _, ext | now - ext[ :created_at ] > 5 }
697
683
 
698
- if exts.any?
699
- @mutex.synchronize do
684
+ if exts.any?
700
685
  exts.each do | dest_id, ext |
701
686
  if ext[ :last_recv_at ].nil? || ( now - ext[ :last_recv_at ] > EXPIRE_AFTER )
702
687
  # puts "debug ctlw close dest #{ dest_id } #{ Time.new } p#{ Process.pid }"
@@ -719,10 +704,10 @@ module Girl
719
704
  break
720
705
  end
721
706
 
722
- tund_info = @infos[ tund ]
707
+ @mutex.synchronize do
708
+ tund_info = @infos[ tund ]
723
709
 
724
- if tund_info[ :dest_exts ].any?
725
- @mutex.synchronize do
710
+ if tund_info[ :dest_exts ].any?
726
711
  now = Time.new
727
712
 
728
713
  tund_info[ :dest_exts ].each do | dest_id, ext |
@@ -739,10 +724,8 @@ module Girl
739
724
  end
740
725
  end
741
726
  end
742
- end
743
727
 
744
- if tund_info[ :paused ] && ( tund_info[ :dest_exts ].map{ | _, ext | ext[ :wmems ].size }.sum < RESUME_BELOW )
745
- @mutex.synchronize do
728
+ if tund_info[ :paused ] && ( tund_info[ :dest_exts ].map{ | _, ext | ext[ :wmems ].size }.sum < RESUME_BELOW )
746
729
  tund_id = @socks[ tund ]
747
730
  puts "ctlw resume #{ tund_id } #{ Time.new } p#{ Process.pid }"
748
731
  @ctlw.write( [ CTL_RESUME, tund_id ].pack( 'CQ>' ) )
@@ -755,7 +738,7 @@ module Girl
755
738
 
756
739
  def loop_send_fin1( tund, dest_id )
757
740
  Thread.new do
758
- 100.times do
741
+ 30.times do
759
742
  break if tund.closed?
760
743
 
761
744
  tund_info = @infos[ tund ]
@@ -784,7 +767,7 @@ module Girl
784
767
 
785
768
  def loop_send_fin2( tund, dest_id )
786
769
  Thread.new do
787
- 100.times do
770
+ 30.times do
788
771
  break if tund.closed?
789
772
 
790
773
  tund_info = @infos[ tund ]
@@ -2,7 +2,7 @@ require 'girl/version'
2
2
  require 'socket'
3
3
 
4
4
  ##
5
- # Girl::Udp - 转发udp。近端。
5
+ # Girl::Udp - udp透明转发,近端。
6
6
  #
7
7
  # usage
8
8
  # ======
@@ -2,7 +2,7 @@ require 'girl/version'
2
2
  require 'socket'
3
3
 
4
4
  ##
5
- # Girl::Udpd - 转发udp。远端。
5
+ # Girl::Udpd - udp透明转发,远端。
6
6
  #
7
7
  module Girl
8
8
  class Udpd
@@ -34,6 +34,7 @@ module Girl
34
34
  # wbuffs: [] # [ to_addr, data ] ...
35
35
  # dst_addrs: { tun_addr => dst_addr }
36
36
  # tun_addrs: { dst_addr => tun_addr }
37
+ # is_tunneleds: { [ tun_addr dst_addr ] => false }
37
38
  # unpaired_dst_rbuffs: { dst_addr => [] }
38
39
  # last_traff_at: now
39
40
  end
@@ -116,8 +117,23 @@ module Girl
116
117
 
117
118
  if to_addr
118
119
  # 来自tun,发给dst。
120
+ td_addr = [ from_addr, to_addr ].join
121
+ is_tunneled = tund_info[ :is_tunneleds ][ td_addr ]
122
+
123
+ unless is_tunneled
124
+ # puts "debug first traffic from tun #{ addrinfo.inspect } to #{ Addrinfo.new( to_addr ).inspect }"
125
+ # 发暂存
126
+ if tund_info[ :unpaired_dst_rbuffs ].include?( to_addr )
127
+ rbuffs = tund_info[ :unpaired_dst_rbuffs ].delete( to_addr )
128
+ # puts "debug move tund.dst.rbuffs to tund.wbuffs #{ rbuffs.inspect }"
129
+ tund_info[ :wbuffs ] += rbuffs.map{ | rbuff | [ from_addr, rbuff ] }
130
+ end
131
+
132
+ tund_info[ :is_tunneleds ][ td_addr ] = true
133
+ end
134
+
119
135
  # 如果对面没来过流量,且在nat里,nat规则是只对去过的目的地做接收,那么,先过去的流量会撞死。
120
- # 没关系,撞死的流量通常是打洞数据,撞死在应用掌控之内,打洞数据通常是连发的。
136
+ # 没关系,撞死的流量通常是打洞数据,在应用计算之内,打洞数据通常是连发的。
121
137
  # puts "debug #{ data.inspect } from #{ addrinfo.inspect } to #{ Addrinfo.new( to_addr ).inspect }"
122
138
  add_tund_wbuff( tund, to_addr, data )
123
139
  return
@@ -127,19 +143,20 @@ module Girl
127
143
 
128
144
  if to_addr
129
145
  # 来自dst,发给tun。
130
- # 先发暂存
131
- if tund_info[ :unpaired_dst_rbuffs ].include?( from_addr )
132
- rbuffs = tund_info[ :unpaired_dst_rbuffs ].delete( from_addr )
133
- # puts "debug move tund.dst.rbuffs to tund.wbuffs #{ rbuffs.inspect }"
134
- tund_info[ :wbuffs ] += rbuffs.map{ | rbuff | [ to_addr, rbuff ] }
146
+ # puts "debug #{ data.inspect } from #{ addrinfo.inspect } to #{ Addrinfo.new( to_addr ).inspect }"
147
+
148
+ td_addr = [ to_addr, from_addr ].join
149
+ is_tunneled = tund_info[ :is_tunneleds ][ td_addr ]
150
+
151
+ if is_tunneled
152
+ add_tund_wbuff( tund, to_addr, data )
153
+ return
135
154
  end
136
155
 
137
- # puts "debug #{ data.inspect } from #{ addrinfo.inspect } to #{ Addrinfo.new( to_addr ).inspect }"
138
- add_tund_wbuff( tund, to_addr, data )
139
- return
156
+ # puts "debug #{ Addrinfo.new( to_addr ).inspect } #{ addrinfo.inspect } not tunneled"
140
157
  end
141
158
 
142
- # 来自未知的地方,看做是p2p对面先到。
159
+ # 来自未知的地方,或者对应的tun还没来流量,记暂存
143
160
  unless tund_info[ :unpaired_dst_rbuffs ][ from_addr ]
144
161
  tund_info[ :unpaired_dst_rbuffs ][ from_addr ] = []
145
162
  end
@@ -211,12 +228,14 @@ module Girl
211
228
 
212
229
  def pair_tund( tun_addr, tun_ip_addr, orig_src_addr, dst_addr )
213
230
  from_addr = [ tun_ip_addr, orig_src_addr ].join
231
+ td_addr = [ tun_addr, dst_addr ].join
214
232
  tund = @tunds[ from_addr ]
215
233
 
216
234
  if tund
217
235
  tund_info = @tund_infos[ tund ]
218
236
  tund_info[ :dst_addrs ][ tun_addr ] = dst_addr
219
237
  tund_info[ :tun_addrs ][ dst_addr ] = tun_addr
238
+ tund_info[ :is_tunneleds ][ td_addr ] = false
220
239
  else
221
240
  tund = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
222
241
  tund.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1 )
@@ -231,6 +250,7 @@ module Girl
231
250
  wbuffs: [],
232
251
  dst_addrs: { tun_addr => dst_addr },
233
252
  tun_addrs: { dst_addr => tun_addr },
253
+ is_tunneleds: { td_addr => false },
234
254
  unpaired_dst_rbuffs: {},
235
255
  last_traff_at: Time.new
236
256
  }
@@ -1,3 +1,3 @@
1
1
  module Girl
2
- VERSION = '0.56.0'.freeze
2
+ VERSION = '0.60.0'.freeze
3
3
  end