girl 0.87.0 → 0.91.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.

@@ -47,25 +47,25 @@ module Girl
47
47
  rs, ws = IO.select( @reads, @writes )
48
48
 
49
49
  @mutex.synchronize do
50
- ws.each do | sock |
51
- case @roles[ sock ]
52
- when :redir
53
- write_redir( sock )
54
- when :tun
55
- write_tun( sock )
56
- end
57
- end
58
-
59
50
  rs.each do | sock |
60
51
  case @roles[ sock ]
61
- when :dotr
52
+ when :dotr then
62
53
  read_dotr( sock )
63
- when :redir
54
+ when :redir then
64
55
  read_redir( sock )
65
- when :tun
56
+ when :tun then
66
57
  read_tun( sock )
67
58
  end
68
59
  end
60
+
61
+ ws.each do | sock |
62
+ case @roles[ sock ]
63
+ when :redir then
64
+ write_redir( sock )
65
+ when :tun then
66
+ write_tun( sock )
67
+ end
68
+ end
69
69
  end
70
70
  end
71
71
  end
@@ -82,20 +82,18 @@ module Girl
82
82
  sleep 30
83
83
 
84
84
  @mutex.synchronize do
85
- need_trigger = false
85
+ trigger = false
86
86
  now = Time.new
87
87
 
88
88
  @tun_infos.each do | tun, tun_info |
89
89
  # net.netfilter.nf_conntrack_udp_timeout_stream
90
- if now - tun_info[ :last_traff_at ] > 180
90
+ if now - tun_info[ :last_traff_at ] > 180 then
91
91
  set_is_closing( tun )
92
- need_trigger = true
92
+ trigger = true
93
93
  end
94
94
  end
95
95
 
96
- if need_trigger
97
- next_tick
98
- end
96
+ next_tick if trigger
99
97
  end
100
98
  end
101
99
  end
@@ -140,7 +138,7 @@ module Girl
140
138
  def add_tun_wbuff( tun, to_addr, data )
141
139
  tun_info = @tun_infos[ tun ]
142
140
 
143
- if to_addr
141
+ if to_addr then
144
142
  tun_info[ :wbuffs ] << [ to_addr, data ]
145
143
  add_write( tun )
146
144
  else
@@ -149,7 +147,7 @@ module Girl
149
147
  end
150
148
 
151
149
  def add_read( sock, role )
152
- unless @reads.include?( sock )
150
+ unless @reads.include?( sock ) then
153
151
  @reads << sock
154
152
  end
155
153
 
@@ -157,13 +155,13 @@ module Girl
157
155
  end
158
156
 
159
157
  def add_write( sock )
160
- unless @writes.include?( sock )
158
+ unless @writes.include?( sock ) then
161
159
  @writes << sock
162
160
  end
163
161
  end
164
162
 
165
163
  def set_is_closing( tun )
166
- if tun && !tun.closed?
164
+ if tun && !tun.closed? then
167
165
  # puts "debug1 set tun is closing"
168
166
 
169
167
  tun_info = @tun_infos[ tun ]
@@ -180,7 +178,7 @@ module Girl
180
178
  rescue IO::WaitWritable, Errno::EINTR
181
179
  return false
182
180
  rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
183
- if @roles[ sock ] == :tun
181
+ if @roles[ sock ] == :tun then
184
182
  puts "#{ Time.new } #{ e.class }, close tun"
185
183
  close_tun( sock )
186
184
  return false
@@ -198,23 +196,19 @@ module Girl
198
196
  tun_info = @tun_infos.delete( tun )
199
197
  @tuns.delete( [ tun_info[ :orig_src_addr ], tun_info[ :dst_addr ] ].join )
200
198
 
201
- if @mappings.include?( tun_info[ :src_addr ] )
199
+ if @mappings.include?( tun_info[ :src_addr ] ) then
202
200
  orig_src_addr, dst_addr, timeout, read_at = @mappings[ tun_info[ :src_addr ] ]
203
201
 
204
- if orig_src_addr == tun_info[ :orig_src_addr ] && dst_addr == tun_info[ :dst_addr ]
202
+ if orig_src_addr == tun_info[ :orig_src_addr ] && dst_addr == tun_info[ :dst_addr ] then
205
203
  @mappings.delete( tun_info[ :src_addr ] )
206
204
  end
207
205
  end
208
206
  end
209
207
 
210
208
  def write_redir( redir )
211
- while @redir_wbuffs.any?
209
+ while @redir_wbuffs.any? do
212
210
  to_addr, data = @redir_wbuffs.first
213
-
214
- unless send_data( redir, data, to_addr )
215
- return
216
- end
217
-
211
+ return unless send_data( redir, data, to_addr )
218
212
  @redir_wbuffs.shift
219
213
  end
220
214
 
@@ -224,18 +218,14 @@ module Girl
224
218
  def write_tun( tun )
225
219
  tun_info = @tun_infos[ tun ]
226
220
 
227
- if tun_info[ :is_closing ]
221
+ if tun_info[ :is_closing ] then
228
222
  close_tun( tun )
229
223
  return
230
224
  end
231
225
 
232
- while tun_info[ :wbuffs ].any?
226
+ while tun_info[ :wbuffs ].any? do
233
227
  to_addr, data = tun_info[ :wbuffs ].first
234
-
235
- unless send_data( tun, data, to_addr )
236
- return
237
- end
238
-
228
+ return unless send_data( tun, data, to_addr )
239
229
  tun_info[ :wbuffs ].shift
240
230
  end
241
231
 
@@ -253,10 +243,10 @@ module Girl
253
243
  now = Time.new
254
244
  # puts "debug redir recv #{ data.inspect } from #{ addrinfo.inspect }"
255
245
 
256
- if @mappings.include?( src_addr )
246
+ if @mappings.include?( src_addr ) then
257
247
  orig_src_addr, dst_addr, timeout, read_at = @mappings[ src_addr ]
258
248
 
259
- if now - read_at < timeout
249
+ if now - read_at < timeout then
260
250
  # puts "debug hit cache #{ addrinfo.inspect }"
261
251
  is_hit_cache = true
262
252
  else
@@ -265,14 +255,14 @@ module Girl
265
255
  end
266
256
  end
267
257
 
268
- unless is_hit_cache
258
+ unless is_hit_cache then
269
259
  # 2 udp 4 timeout 5 src 7 sport 9 [UNREPLIED] 11 dst 13 dport
270
260
  # 2 udp 4 timeout 5 src 7 sport 10 dst 12 dport
271
261
  bin = IO.binread( '/proc/net/nf_conntrack' )
272
262
  rows = bin.split( "\n" ).map { | line | line.split( ' ' ) }
273
263
  row = rows.find { | _row | _row[ 2 ] == 'udp' && ( ( _row[ 10 ].split( '=' )[ 1 ] == addrinfo.ip_address && _row[ 12 ].split( '=' )[ 1 ].to_i == addrinfo.ip_port ) || ( _row[ 9 ] == '[UNREPLIED]' && _row[ 11 ].split( '=' )[ 1 ] == addrinfo.ip_address && _row[ 13 ].split( '=' )[ 1 ].to_i == addrinfo.ip_port ) ) }
274
264
 
275
- unless row
265
+ unless row then
276
266
  puts "miss conntrack #{ addrinfo.inspect } #{ Time.new }"
277
267
  IO.binwrite( '/tmp/nf_conntrack', bin )
278
268
  return
@@ -286,7 +276,7 @@ module Girl
286
276
  orig_src_addr = Socket.sockaddr_in( orig_src_port, orig_src_ip )
287
277
  dst_addr = Socket.sockaddr_in( dst_port, dst_ip )
288
278
 
289
- if Addrinfo.new( dst_addr ).ipv4_private?
279
+ if Addrinfo.new( dst_addr ).ipv4_private? then
290
280
  puts "dst is private? #{ Addrinfo.new( dst_addr ).inspect } #{ Addrinfo.new( src_addr ).inspect } #{ Addrinfo.new( orig_src_addr ).inspect } #{ Time.new }"
291
281
  add_redir_wbuff( redir, dst_addr, data )
292
282
  return
@@ -298,7 +288,7 @@ module Girl
298
288
 
299
289
  tun = @tuns[ [ orig_src_addr, dst_addr ].join ]
300
290
 
301
- unless tun
291
+ unless tun then
302
292
  tun = new_a_tun( orig_src_addr, dst_addr, src_addr )
303
293
 
304
294
  # puts "debug tun send to udpd #{ Addrinfo.new( orig_src_addr ).inspect } #{ Addrinfo.new( dst_addr ).inspect }"
@@ -316,18 +306,18 @@ module Girl
316
306
  tun_info = @tun_infos[ tun ]
317
307
  tun_info[ :last_traff_at ] = Time.new
318
308
 
319
- if from_addr == @udpd_addr
309
+ if from_addr == @udpd_addr then
320
310
  tund_port = data[ 0, 2 ].unpack( 'n' ).first
321
311
  tund_addr = Socket.sockaddr_in( tund_port, @udpd_host )
322
312
  tun_info[ :tund_addr ] = tund_addr
323
313
 
324
- if tun_info[ :rbuffs ].any?
314
+ if tun_info[ :rbuffs ].any? then
325
315
  tun_info[ :wbuffs ] += tun_info[ :rbuffs ].map{ | rbuff | [ tund_addr, rbuff ] }
326
316
  tun_info[ :rbuffs ].clear
327
317
  add_write( tun )
328
318
  end
329
319
 
330
- elsif from_addr == tun_info[ :tund_addr ]
320
+ elsif from_addr == tun_info[ :tund_addr ] then
331
321
  add_redir_wbuff( @redir, tun_info[ :src_addr ], data )
332
322
  end
333
323
  end
@@ -39,20 +39,20 @@ module Girl
39
39
  @mutex.synchronize do
40
40
  ws.each do | sock |
41
41
  case @roles[ sock ]
42
- when :udpd
42
+ when :udpd then
43
43
  write_udpd( sock )
44
- when :tund
44
+ when :tund then
45
45
  write_tund( sock )
46
46
  end
47
47
  end
48
48
 
49
49
  rs.each do | sock |
50
50
  case @roles[ sock ]
51
- when :dotr
51
+ when :dotr then
52
52
  read_dotr( sock )
53
- when :udpd
53
+ when :udpd then
54
54
  read_udpd( sock )
55
- when :tund
55
+ when :tund then
56
56
  read_tund( sock )
57
57
  end
58
58
  end
@@ -72,20 +72,18 @@ module Girl
72
72
  sleep 30
73
73
 
74
74
  @mutex.synchronize do
75
- need_trigger = false
75
+ trigger = false
76
76
  now = Time.new
77
77
 
78
78
  @tund_infos.each do | tund, tund_info |
79
79
  # net.netfilter.nf_conntrack_udp_timeout_stream
80
- if now - tund_info[ :last_traff_at ] > 180
80
+ if now - tund_info[ :last_traff_at ] > 180 then
81
81
  set_is_closing( tund )
82
- need_trigger = true
82
+ trigger = true
83
83
  end
84
84
  end
85
85
 
86
- if need_trigger
87
- next_tick
88
- end
86
+ next_tick if trigger
89
87
  end
90
88
  end
91
89
  end
@@ -105,7 +103,7 @@ module Girl
105
103
  td_addr = [ tun_addr, dst_addr ].join
106
104
  tund = @tunds[ from_addr ]
107
105
 
108
- if tund
106
+ if tund then
109
107
  tund_info = @tund_infos[ tund ]
110
108
  tund_info[ :dst_addrs ][ tun_addr ] = dst_addr
111
109
  tund_info[ :tun_addrs ][ dst_addr ] = tun_addr
@@ -145,7 +143,7 @@ module Girl
145
143
  end
146
144
 
147
145
  def add_read( sock, role )
148
- unless @reads.include?( sock )
146
+ unless @reads.include?( sock ) then
149
147
  @reads << sock
150
148
  end
151
149
 
@@ -153,13 +151,13 @@ module Girl
153
151
  end
154
152
 
155
153
  def add_write( sock )
156
- unless @writes.include?( sock )
154
+ unless @writes.include?( sock ) then
157
155
  @writes << sock
158
156
  end
159
157
  end
160
158
 
161
159
  def set_is_closing( tund )
162
- if tund && !tund.closed?
160
+ if tund && !tund.closed? then
163
161
  # puts "debug1 set tund is closing"
164
162
 
165
163
  tund_info = @tund_infos[ tund ]
@@ -176,7 +174,7 @@ module Girl
176
174
  rescue IO::WaitWritable, Errno::EINTR
177
175
  return false
178
176
  rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN => e
179
- if @roles[ sock ] == :tund
177
+ if @roles[ sock ] == :tund then
180
178
  puts "#{ Time.new } #{ e.class }, close tund"
181
179
  close_tund( sock )
182
180
  return false
@@ -200,13 +198,9 @@ module Girl
200
198
  end
201
199
 
202
200
  def write_udpd( udpd )
203
- while @udpd_wbuffs.any?
201
+ while @udpd_wbuffs.any? do
204
202
  to_addr, data = @udpd_wbuffs.first
205
-
206
- unless send_data( udpd, data, to_addr )
207
- return
208
- end
209
-
203
+ return unless send_data( udpd, data, to_addr )
210
204
  @udpd_wbuffs.shift
211
205
  end
212
206
 
@@ -216,18 +210,14 @@ module Girl
216
210
  def write_tund( tund )
217
211
  tund_info = @tund_infos[ tund ]
218
212
 
219
- if tund_info[ :is_closing ]
213
+ if tund_info[ :is_closing ] then
220
214
  close_tund( tund )
221
215
  return
222
216
  end
223
217
 
224
- while tund_info[ :wbuffs ].any?
218
+ while tund_info[ :wbuffs ].any? do
225
219
  to_addr, data = tund_info[ :wbuffs ].first
226
-
227
- unless send_data( tund, data, to_addr )
228
- return
229
- end
230
-
220
+ return unless send_data( tund, data, to_addr )
231
221
  tund_info[ :wbuffs ].shift
232
222
  end
233
223
 
@@ -269,15 +259,15 @@ module Girl
269
259
  tund_info[ :last_traff_at ] = Time.new
270
260
  to_addr = tund_info[ :dst_addrs ][ from_addr ]
271
261
 
272
- if to_addr
262
+ if to_addr then
273
263
  # 来自tun,发给dst。
274
264
  td_addr = [ from_addr, to_addr ].join
275
265
  is_tunneled = tund_info[ :is_tunneleds ][ td_addr ]
276
266
 
277
- unless is_tunneled
267
+ unless is_tunneled then
278
268
  # puts "debug first traffic from tun #{ addrinfo.inspect } to #{ Addrinfo.new( to_addr ).inspect }"
279
269
  # 发暂存
280
- if tund_info[ :unpaired_dst_rbuffs ].include?( to_addr )
270
+ if tund_info[ :unpaired_dst_rbuffs ].include?( to_addr ) then
281
271
  rbuffs = tund_info[ :unpaired_dst_rbuffs ].delete( to_addr )
282
272
  # puts "debug move tund.dst.rbuffs to tund.wbuffs #{ rbuffs.inspect }"
283
273
  tund_info[ :wbuffs ] += rbuffs.map{ | rbuff | [ from_addr, rbuff ] }
@@ -295,14 +285,14 @@ module Girl
295
285
 
296
286
  to_addr = tund_info[ :tun_addrs ][ from_addr ]
297
287
 
298
- if to_addr
288
+ if to_addr then
299
289
  # 来自dst,发给tun。
300
290
  # puts "debug #{ data.inspect } from #{ addrinfo.inspect } to #{ Addrinfo.new( to_addr ).inspect }"
301
291
 
302
292
  td_addr = [ to_addr, from_addr ].join
303
293
  is_tunneled = tund_info[ :is_tunneleds ][ td_addr ]
304
294
 
305
- if is_tunneled
295
+ if is_tunneled then
306
296
  add_tund_wbuff( tund, to_addr, data )
307
297
  return
308
298
  end
@@ -311,12 +301,12 @@ module Girl
311
301
  end
312
302
 
313
303
  # 来自未知的地方,或者对应的tun还没来流量,记暂存
314
- unless tund_info[ :unpaired_dst_rbuffs ][ from_addr ]
304
+ unless tund_info[ :unpaired_dst_rbuffs ][ from_addr ] then
315
305
  tund_info[ :unpaired_dst_rbuffs ][ from_addr ] = []
316
306
  end
317
307
 
318
308
  # 暂存5条(连发打洞数据,不需要存多)。
319
- if tund_info[ :unpaired_dst_rbuffs ][ from_addr ].size < 5
309
+ if tund_info[ :unpaired_dst_rbuffs ][ from_addr ].size < 5 then
320
310
  # puts "debug save other dst rbuff #{ addrinfo.inspect } #{ data.inspect }"
321
311
  tund_info[ :unpaired_dst_rbuffs ][ from_addr ] << data
322
312
  end
@@ -1,3 +1,3 @@
1
1
  module Girl
2
- VERSION = '0.87.0'.freeze
2
+ VERSION = '0.91.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: girl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.87.0
4
+ version: 0.91.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takafan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-20 00:00:00.000000000 Z
11
+ date: 2020-10-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: escape evil.
14
14
  email: