girl 0.64.1 → 0.65.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: 5f4e1bba2efaa28dcf93f09ed4e913b449165cdd55febb11d5b59d88f374e49f
4
- data.tar.gz: e23ceb94f65c8edf04b53fb8fd369b47d1aecd602bbdd4057f0498fda63bfeb8
3
+ metadata.gz: a6d217fe38a61d01846b2cbf57e0a43a5c456406237007c852d6e4f2d69be3bc
4
+ data.tar.gz: 61641d8b22ee04b1e0daf71d410fe420939e6e6fd6c6ff65c49641a1f502baaf
5
5
  SHA512:
6
- metadata.gz: d8f9d02746902687b25c2c17346cae8bda8e493c4b7325afa0c64a6417769dd3c39941cf2668b41041ce5f74205399cdc3ce990808602ff6e748012b5462a284
7
- data.tar.gz: b4b52b30693c3d6afca6bd9fee85ac7782afba4b58f475aa73961d3fc8fb3ba21299c745f827631e732dc9e63b2faca5e506dd9f5201e134edd52d38f5fc6097
6
+ metadata.gz: cf51f0e222f05a488ca6c5e437bdd3a6231b1ece0aa184b8907d14f0fe1ff2b3c5daa66bca56c376b1e02ca8162744f1156721f8ad48c73b9b4f3118a73a3cf5
7
+ data.tar.gz: b373766e42e420240a47eb5b54f4d3ea6d4c74c630b6a137fc5b91706486635f06717d6e92a1881e92bc841a640f851cb6b8c018e9b2540d16898a8104ae5d24
@@ -1,8 +1,6 @@
1
1
  module Girl
2
- PACK_SIZE = 1328 # 包大小 1400(console MTU) - 8(PPPoE header) - 40(IPv6 header) - 8(UDP header) - 8(source/dest id) - 8(pack id) = 1328
2
+ PACK_SIZE = 1328 # 包大小 1400(console MTU) - 8(PPPoE header) - 40(IPv6 header) - 8(UDP header) - 8(pack id) - 8(src id) = 1328
3
3
  CHUNK_SIZE = PACK_SIZE * 1000 # 块大小
4
- PROXY_PACK_SIZE = 1320 # 1400(console MTU) - 8(PPPoE header) - 40(IPv6 header) - 8(UDP header) - 8(pack id) - 16(src_addr) = 1320
5
- PROXY_CHUNK_SIZE = PROXY_PACK_SIZE * 1000 # proxy块大小
6
4
  WBUFFS_LIMIT = 1000 # 写前上限,超过上限结一个块
7
5
  WMEMS_LIMIT = 100_000 # 写后上限,达到上限暂停写
8
6
  RESUME_BELOW = 50_000 # 降到多少以下恢复写
@@ -24,19 +24,19 @@ require 'socket'
24
24
  # tun-tund:
25
25
  #
26
26
  # Q>: 0 ctlmsg -> C: 2 heartbeat -> C: random char
27
- # 3 a new src -> src_addr -> encoded destination address
28
- # 4 paired -> src_addr -> n: dst_port
27
+ # 3 a new src -> Q>: src_id -> encoded destination address
28
+ # 4 paired -> Q>: src_id -> n: dst_port
29
29
  # 5 dst status -> n: dst_port -> Q>Q>: biggest_dst_pack_id continue_src_pack_id
30
- # 6 src status -> src_addr -> Q>Q>: biggest_src_pack_id continue_dst_pack_id
31
- # 7 miss -> src_addr/n: dst_port -> Q>Q>: pack_id_begin pack_id_end
32
- # 8 fin1 -> src_addr/n: dst_port -> Q>Q>: biggest_src_pack_id continue_dst_pack_id / biggest_dst_pack_id continue_src_pack_id
30
+ # 6 src status -> Q>: src_id -> Q>Q>: biggest_src_pack_id continue_dst_pack_id
31
+ # 7 miss -> Q>: src_id/n: dst_port -> Q>Q>: pack_id_begin pack_id_end
32
+ # 8 fin1 -> Q>: src_id/n: dst_port -> Q>Q>: biggest_src_pack_id continue_dst_pack_id / biggest_dst_pack_id continue_src_pack_id
33
33
  # 9 not use
34
- # 10 fin2 -> src_addr/n: dst_port
34
+ # 10 fin2 -> Q>: src_id/n: dst_port
35
35
  # 11 not use
36
36
  # 12 tund fin
37
37
  # 13 tun fin
38
38
  #
39
- # Q>: 1+ pack_id -> src_addr/n: dst_port -> traffic
39
+ # Q>: 1+ pack_id -> Q>: src_id/n: dst_port -> traffic
40
40
  #
41
41
  # close logic
42
42
  # ===========
@@ -111,10 +111,10 @@ module Girl
111
111
  data = [ 0, HEARTBEAT, rand( 128 ) ].pack( 'Q>CC' )
112
112
  add_tun_ctlmsg( data )
113
113
 
114
- @tun_info[ :src_exts ].each do | src_addr, src_ext |
114
+ @tun_info[ :src_exts ].each do | src_id, src_ext |
115
115
  if src_ext[ :src ].closed? && ( now - src_ext[ :last_continue_at ] > EXPIRE_AFTER )
116
- puts "p#{ Process.pid } #{ Time.new } expire src ext"
117
- del_src_ext( src_addr )
116
+ puts "p#{ Process.pid } #{ Time.new } expire src ext #{ src_id }"
117
+ del_src_ext( src_id )
118
118
  end
119
119
  end
120
120
  end
@@ -165,10 +165,10 @@ module Girl
165
165
  if @tun_info[ :src_exts ].any?
166
166
  now = Time.new
167
167
 
168
- @tun_info[ :src_exts ].each do | src_addr, src_ext |
168
+ @tun_info[ :src_exts ].each do | src_id, src_ext |
169
169
  if src_ext[ :dst_port ] && ( now - src_ext[ :last_continue_at ] < SEND_STATUS_UNTIL )
170
170
  # puts "debug2 ctl send status biggest #{ src_ext[ :biggest_pack_id ] } continue dst #{ src_ext[ :continue_dst_pack_id ] }"
171
- data = [ [ 0, SOURCE_STATUS ].pack( 'Q>C' ), src_addr, [ src_ext[ :biggest_pack_id ], src_ext[ :continue_dst_pack_id ] ].pack( 'Q>Q>' ) ].join
171
+ data = [ 0, SOURCE_STATUS, src_id, src_ext[ :biggest_pack_id ], src_ext[ :continue_dst_pack_id ] ].pack( 'Q>CQ>Q>Q>' )
172
172
  add_tun_ctlmsg( data )
173
173
  need_trigger = true
174
174
  end
@@ -311,20 +311,20 @@ module Girl
311
311
  def new_a_tun
312
312
  tun = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
313
313
  tun.bind( Socket.sockaddr_in( 0, '0.0.0.0' ) )
314
- port = tun.local_address.ip_unpack.last
314
+ port = tun.local_address.ip_port
315
315
  tun_info = {
316
316
  port: port, # 端口
317
317
  ctlmsg_rbuffs: [], # 还没配上tund,暂存的ctlmsg
318
318
  ctlmsgs: [], # [ to_addr, data ]
319
- wbuffs: [], # 写前缓存 [ src_addr, data ]
320
- caches: [], # 块读出缓存 [ src_addr, data ]
319
+ wbuffs: [], # 写前缓存 [ src_id, data ]
320
+ caches: [], # 块读出缓存 [ src_id, data ]
321
321
  chunks: [], # 块队列 filename
322
322
  spring: 0, # 块后缀,结块时,如果块队列不为空,则自增,为空,则置为0
323
323
  tund_addr: nil, # tund地址
324
- src_exts: {}, # src额外信息 src_addr => {}
325
- src_addrs: {}, # dst_port => src_addr
324
+ src_exts: {}, # src额外信息 src_id => {}
325
+ src_ids: {}, # dst_port => src_id
326
326
  paused: false, # 是否暂停写
327
- resendings: [], # 重传队列 [ src_addr, pack_id ]
327
+ resendings: [], # 重传队列 [ src_id, pack_id ]
328
328
  created_at: Time.new, # 创建时间
329
329
  last_recv_at: nil, # 上一次收到流量的时间,过期关闭
330
330
  is_closing: false # 是否准备关闭
@@ -363,7 +363,7 @@ module Girl
363
363
  end
364
364
 
365
365
  # puts "debug1 a new dst #{ dst.local_address.inspect }"
366
- local_port = dst.local_address.ip_unpack.last
366
+ local_port = dst.local_address.ip_port
367
367
  @dsts[ local_port ] = dst
368
368
  @dst_infos[ dst ] = {
369
369
  local_port: local_port, # 本地端口
@@ -424,14 +424,14 @@ module Girl
424
424
  }
425
425
 
426
426
  src_info = @src_infos[ src ]
427
- src_addr = src_info[ :src_addr ]
428
- @tun_info[ :src_exts ][ src_addr ] = src_ext
427
+ src_id = src_info[ :id ]
428
+ @tun_info[ :src_exts ][ src_id ] = src_ext
429
429
  src_info[ :proxy_type ] = :tunnel
430
430
 
431
431
  destination_port = src_info[ :destination_port ]
432
432
  destination_domain = src_info[ :destination_domain ]
433
433
  destination_domain_port = [ destination_domain, destination_port ].join( ':' )
434
- data = [ [ 0, A_NEW_SOURCE ].pack( 'Q>C' ), src_addr, @custom.encode( destination_domain_port ) ].join
434
+ data = [ [ 0, A_NEW_SOURCE, src_id ].pack( 'Q>CQ>' ), @custom.encode( destination_domain_port ) ].join
435
435
  loop_send_a_new_source( src_ext, data )
436
436
  end
437
437
 
@@ -490,14 +490,14 @@ module Girl
490
490
  ##
491
491
  # add tun wbuff
492
492
  #
493
- def add_tun_wbuff( src_addr, data )
494
- @tun_info[ :wbuffs ] << [ src_addr, data ]
493
+ def add_tun_wbuff( src_id, data )
494
+ @tun_info[ :wbuffs ] << [ src_id, data ]
495
495
 
496
496
  if @tun_info[ :wbuffs ].size >= WBUFFS_LIMIT
497
497
  spring = @tun_info[ :chunks ].size > 0 ? ( @tun_info[ :spring ] + 1 ) : 0
498
498
  filename = "#{ Process.pid }-#{ @tun_info[ :port ] }.#{ spring }"
499
499
  chunk_path = File.join( @tun_chunk_dir, filename )
500
- wbuffs = @tun_info[ :wbuffs ].map{ | _src_addr, _data | [ _src_addr, [ _data.bytesize ].pack( 'n' ), _data ].join }
500
+ wbuffs = @tun_info[ :wbuffs ].map{ | _src_id, _data | [ [ _src_id, _data.bytesize ].pack( 'Q>n' ), _data ].join }
501
501
 
502
502
  begin
503
503
  IO.binwrite( chunk_path, wbuffs.join )
@@ -522,9 +522,9 @@ module Girl
522
522
  src_info = @src_infos[ src ]
523
523
  src_info[ :wbuff ] << data
524
524
 
525
- if src_info[ :wbuff ].bytesize >= PROXY_CHUNK_SIZE
525
+ if src_info[ :wbuff ].bytesize >= CHUNK_SIZE
526
526
  spring = src_info[ :chunks ].size > 0 ? ( src_info[ :spring ] + 1 ) : 0
527
- filename = "#{ Process.pid }-#{ Addrinfo.new( src_info[ :src_addr ] ).ip_unpack.join( '-' ) }.#{ spring }"
527
+ filename = "#{ Process.pid }-#{ src_info[ :id ] }.#{ spring }"
528
528
  chunk_path = File.join( @src_chunk_dir, filename )
529
529
 
530
530
  begin
@@ -550,7 +550,7 @@ module Girl
550
550
  dst_info = @dst_infos[ dst ]
551
551
  dst_info[ :wbuff ] << data
552
552
 
553
- if dst_info[ :wbuff ].bytesize >= PROXY_CHUNK_SIZE
553
+ if dst_info[ :wbuff ].bytesize >= CHUNK_SIZE
554
554
  spring = dst_info[ :chunks ].size > 0 ? ( dst_info[ :spring ] + 1 ) : 0
555
555
  filename = "#{ Process.pid }-#{ dst_info[ :local_port ] }.#{ spring }"
556
556
  chunk_path = File.join( @dst_chunk_dir, filename )
@@ -630,22 +630,22 @@ module Girl
630
630
  end
631
631
  end
632
632
 
633
- src_addr = src_info[ :src_addr ]
633
+ src_id = src_info[ :id ]
634
634
 
635
635
  if src_info[ :proxy_type ] == :tunnel
636
636
  return if @tun.closed?
637
637
 
638
- src_ext = @tun_info[ :src_exts ][ src_addr ]
638
+ src_ext = @tun_info[ :src_exts ][ src_id ]
639
639
  return if src_ext.nil? || src_ext[ :dst_port ].nil?
640
640
 
641
641
  if src_ext[ :is_dst_closed ]
642
642
  # puts "debug1 2-2. after close src -> dst closed ? yes -> del src ext -> send fin2"
643
- del_src_ext( src_addr )
644
- data = [ [ 0, FIN2 ].pack( 'Q>C' ), src_addr ].join
643
+ del_src_ext( src_id )
644
+ data = [ 0, FIN2, src_id ].pack( 'Q>CQ>' )
645
645
  add_tun_ctlmsg( data )
646
646
  else
647
647
  # puts "debug1 1-1. after close src -> dst closed ? no -> send fin1"
648
- data = [ [ 0, FIN1 ].pack( 'Q>C' ), src_addr, [ src_ext[ :biggest_pack_id ], src_ext[ :continue_dst_pack_id ] ].pack( 'Q>Q>' ) ].join
648
+ data = [ 0, FIN1, src_id, src_ext[ :biggest_pack_id ], src_ext[ :continue_dst_pack_id ] ].pack( 'Q>CQ>Q>Q>' )
649
649
  add_tun_ctlmsg( data )
650
650
  end
651
651
  elsif src_info[ :proxy_type ] == :direct
@@ -702,11 +702,11 @@ module Girl
702
702
  ##
703
703
  # del src ext
704
704
  #
705
- def del_src_ext( src_addr )
706
- src_ext = @tun_info[ :src_exts ].delete( src_addr )
705
+ def del_src_ext( src_id )
706
+ src_ext = @tun_info[ :src_exts ].delete( src_id )
707
707
 
708
708
  if src_ext
709
- @tun_info[ :src_addrs ].delete( src_ext[ :dst_port ] )
709
+ @tun_info[ :src_ids ].delete( src_ext[ :dst_port ] )
710
710
  end
711
711
  end
712
712
 
@@ -861,8 +861,8 @@ module Girl
861
861
 
862
862
  # 重传
863
863
  while @tun_info[ :resendings ].any?
864
- src_addr, pack_id = @tun_info[ :resendings ].first
865
- src_ext = @tun_info[ :src_exts ][ src_addr ]
864
+ src_id, pack_id = @tun_info[ :resendings ].first
865
+ src_ext = @tun_info[ :src_exts ][ src_id ]
866
866
 
867
867
  if src_ext
868
868
  data = src_ext[ :wmems ][ pack_id ]
@@ -893,7 +893,7 @@ module Girl
893
893
 
894
894
  # 取写前
895
895
  if @tun_info[ :caches ].any?
896
- src_addr, data = @tun_info[ :caches ].first
896
+ src_id, data = @tun_info[ :caches ].first
897
897
  from = :caches
898
898
  elsif @tun_info[ :chunks ].any?
899
899
  path = File.join( @tun_chunk_dir, @tun_info[ :chunks ].shift )
@@ -910,24 +910,23 @@ module Girl
910
910
  caches = []
911
911
 
912
912
  until data.empty?
913
- _src_addr = data[ 0, 16 ]
914
- pack_size = data[ 16, 2 ].unpack( 'n' ).first
915
- caches << [ _src_addr, data[ 18, pack_size ] ]
916
- data = data[ ( 18 + pack_size )..-1 ]
913
+ _src_id, pack_size = data[ 0, 10 ].unpack( 'Q>n' )
914
+ caches << [ _src_id, data[ 10, pack_size ] ]
915
+ data = data[ ( 10 + pack_size )..-1 ]
917
916
  end
918
917
 
919
918
  @tun_info[ :caches ] = caches
920
- src_addr, data = caches.first
919
+ src_id, data = caches.first
921
920
  from = :caches
922
921
  elsif @tun_info[ :wbuffs ].any?
923
- src_addr, data = @tun_info[ :wbuffs ].first
922
+ src_id, data = @tun_info[ :wbuffs ].first
924
923
  from = :wbuffs
925
924
  else
926
925
  @writes.delete( tun )
927
926
  return
928
927
  end
929
928
 
930
- src_ext = @tun_info[ :src_exts ][ src_addr ]
929
+ src_ext = @tun_info[ :src_exts ][ src_id ]
931
930
 
932
931
  if src_ext
933
932
  pack_id = src_ext[ :biggest_pack_id ] + 1
@@ -937,7 +936,7 @@ module Girl
937
936
  # puts "debug1 encoded pack #{ pack_id }"
938
937
  end
939
938
 
940
- data = [ [ pack_id ].pack( 'Q>' ), src_addr, data ].join
939
+ data = [ [ pack_id, src_id ].pack( 'Q>Q>' ), data ].join
941
940
 
942
941
  begin
943
942
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
@@ -972,10 +971,11 @@ module Girl
972
971
  return
973
972
  end
974
973
 
975
- # puts "debug1 accept a src #{ addrinfo.inspect }"
976
- src_addr = addrinfo.to_sockaddr
974
+ id = rand( ( 2 ** 64 ) - 1 ) + 1
975
+ # puts "debug1 accept a src #{ addrinfo.inspect } #{ id }"
976
+
977
977
  @src_infos[ src ] = {
978
- src_addr: src_addr, # src地址
978
+ id: id, # id
979
979
  proxy_proto: :uncheck, # :uncheck / :http / :socks5
980
980
  proxy_type: :uncheck, # :uncheck / :checking / :direct / :tunnel / :negotiation
981
981
  dst: nil, # :direct的场合,对应的dst
@@ -999,7 +999,7 @@ module Girl
999
999
  #
1000
1000
  def read_src( src )
1001
1001
  begin
1002
- data = src.read_nonblock( PROXY_PACK_SIZE )
1002
+ data = src.read_nonblock( PACK_SIZE )
1003
1003
  rescue IO::WaitReadable, Errno::EINTR
1004
1004
  return
1005
1005
  rescue Exception => e
@@ -1129,8 +1129,8 @@ module Girl
1129
1129
  puts "p#{ Process.pid } #{ Time.new } socks5 cmd #{ cmd } not implement"
1130
1130
  end
1131
1131
  when :tunnel
1132
- src_addr = src_info[ :src_addr ]
1133
- src_ext = @tun_info[ :src_exts ][ src_addr ]
1132
+ src_id = src_info[ :id ]
1133
+ src_ext = @tun_info[ :src_exts ][ src_id ]
1134
1134
 
1135
1135
  unless src_ext
1136
1136
  # puts "debug1 not found src ext"
@@ -1149,7 +1149,7 @@ module Girl
1149
1149
  data, _ = sub_http_request( data )
1150
1150
  end
1151
1151
 
1152
- add_tun_wbuff( src_addr, data )
1152
+ add_tun_wbuff( src_id, data )
1153
1153
  else
1154
1154
  # puts "debug1 remote dst not ready, save data to src rbuff"
1155
1155
  src_info[ :rbuffs ] << data
@@ -1181,7 +1181,7 @@ module Girl
1181
1181
  #
1182
1182
  def read_dst( dst )
1183
1183
  begin
1184
- data = dst.read_nonblock( PROXY_PACK_SIZE )
1184
+ data = dst.read_nonblock( PACK_SIZE )
1185
1185
  rescue IO::WaitReadable, Errno::EINTR
1186
1186
  return
1187
1187
  rescue Exception => e
@@ -1212,6 +1212,7 @@ module Girl
1212
1212
  from_addr = addrinfo.to_sockaddr
1213
1213
  now = Time.new
1214
1214
  pack_id = data[ 0, 8 ].unpack( 'Q>' ).first
1215
+ @tun_info[ :last_recv_at ] = now
1215
1216
 
1216
1217
  if pack_id == 0
1217
1218
  ctl_num = data[ 8 ].unpack( 'C' ).first
@@ -1220,7 +1221,6 @@ module Girl
1220
1221
  when TUND_PORT
1221
1222
  return if ( from_addr != @proxyd_addr ) || @tun_info[ :tund_addr ]
1222
1223
 
1223
- @tun_info[ :last_recv_at ] = now
1224
1224
  tund_port = data[ 9, 2 ].unpack( 'n' ).first
1225
1225
 
1226
1226
  # puts "debug1 got tund port #{ tund_port }"
@@ -1236,18 +1236,15 @@ module Girl
1236
1236
  when PAIRED
1237
1237
  return if from_addr != @tun_info[ :tund_addr ]
1238
1238
 
1239
- src_addr = data[ 9, 16 ]
1240
- dst_port = data[ 25, 2 ].unpack( 'n' ).first
1239
+ src_id, dst_port = data[ 9, 10 ].unpack( 'Q>n' )
1241
1240
 
1242
- src_ext = @tun_info[ :src_exts ][ src_addr ]
1241
+ src_ext = @tun_info[ :src_exts ][ src_id ]
1243
1242
  return if src_ext.nil? || src_ext[ :dst_port ]
1244
1243
 
1245
1244
  src = src_ext[ :src ]
1246
1245
  return if src.closed?
1247
1246
 
1248
- @tun_info[ :last_recv_at ] = now
1249
-
1250
- # puts "debug1 got paired #{ Addrinfo.new( src_addr ).inspect } #{ dst_port }"
1247
+ # puts "debug1 got paired #{ src_id } #{ dst_port }"
1251
1248
 
1252
1249
  if dst_port == 0
1253
1250
  set_is_closing( src )
@@ -1255,7 +1252,7 @@ module Girl
1255
1252
  end
1256
1253
 
1257
1254
  src_ext[ :dst_port ] = dst_port
1258
- @tun_info[ :src_addrs ][ dst_port ] = src_addr
1255
+ @tun_info[ :src_ids ][ dst_port ] = src_id
1259
1256
 
1260
1257
  src_info = @src_infos[ src ]
1261
1258
 
@@ -1271,7 +1268,7 @@ module Girl
1271
1268
  # puts "debug1 add src rbuffs to tun wbuffs"
1272
1269
 
1273
1270
  datas.each do | _data |
1274
- add_tun_wbuff( src_addr, _data )
1271
+ add_tun_wbuff( src_id, _data )
1275
1272
  end
1276
1273
  end
1277
1274
  elsif src_info[ :proxy_proto ] == :socks5
@@ -1282,14 +1279,13 @@ module Girl
1282
1279
 
1283
1280
  dst_port, biggest_dst_pack_id, continue_src_pack_id = data[ 9, 18 ].unpack( 'nQ>Q>' )
1284
1281
 
1285
- src_addr = @tun_info[ :src_addrs ][ dst_port ]
1286
- return unless src_addr
1282
+ src_id = @tun_info[ :src_ids ][ dst_port ]
1283
+ return unless src_id
1287
1284
 
1288
- src_ext = @tun_info[ :src_exts ][ src_addr ]
1285
+ src_ext = @tun_info[ :src_exts ][ src_id ]
1289
1286
  return unless src_ext
1290
1287
 
1291
1288
  # puts "debug2 got dest status"
1292
- @tun_info[ :last_recv_at ] = now
1293
1289
 
1294
1290
  # 更新对面发到几
1295
1291
  if biggest_dst_pack_id > src_ext[ :biggest_dst_pack_id ]
@@ -1339,20 +1335,17 @@ module Girl
1339
1335
  when MISS
1340
1336
  return if from_addr != @tun_info[ :tund_addr ]
1341
1337
 
1342
- src_addr = data[ 9, 16 ]
1343
- pack_id_begin, pack_id_end = data[ 25, 16 ].unpack( 'Q>Q>' )
1338
+ src_id, pack_id_begin, pack_id_end = data[ 9, 24 ].unpack( 'Q>Q>Q>' )
1344
1339
 
1345
- src_ext = @tun_info[ :src_exts ][ src_addr ]
1340
+ src_ext = @tun_info[ :src_exts ][ src_id ]
1346
1341
  return unless src_ext
1347
1342
 
1348
- @tun_info[ :last_recv_at ] = now
1349
-
1350
1343
  ( pack_id_begin..pack_id_end ).each do | pack_id |
1351
1344
  send_at = src_ext[ :send_ats ][ pack_id ]
1352
1345
 
1353
1346
  if send_at
1354
1347
  break if now - send_at < STATUS_INTERVAL
1355
- @tun_info[ :resendings ] << [ src_addr, pack_id ]
1348
+ @tun_info[ :resendings ] << [ src_id, pack_id ]
1356
1349
  end
1357
1350
  end
1358
1351
 
@@ -1362,14 +1355,12 @@ module Girl
1362
1355
 
1363
1356
  dst_port, biggest_dst_pack_id, continue_src_pack_id = data[ 9, 18 ].unpack( 'nQ>Q>' )
1364
1357
 
1365
- src_addr = @tun_info[ :src_addrs ][ dst_port ]
1366
- return unless src_addr
1358
+ src_id = @tun_info[ :src_ids ][ dst_port ]
1359
+ return unless src_id
1367
1360
 
1368
- src_ext = @tun_info[ :src_exts ][ src_addr ]
1361
+ src_ext = @tun_info[ :src_exts ][ src_id ]
1369
1362
  return unless src_ext
1370
1363
 
1371
- @tun_info[ :last_recv_at ] = now
1372
-
1373
1364
  # puts "debug1 got fin1 #{ dst_port } biggest dst pack #{ biggest_dst_pack_id } completed src pack #{ continue_src_pack_id }"
1374
1365
  src_ext[ :is_dst_closed ] = true
1375
1366
  src_ext[ :biggest_dst_pack_id ] = biggest_dst_pack_id
@@ -1385,18 +1376,14 @@ module Girl
1385
1376
 
1386
1377
  dst_port = data[ 9, 2 ].unpack( 'n' ).first
1387
1378
 
1388
- src_addr = @tun_info[ :src_addrs ][ dst_port ]
1389
- return unless src_addr
1390
-
1391
- @tun_info[ :last_recv_at ] = now
1379
+ src_id = @tun_info[ :src_ids ][ dst_port ]
1380
+ return unless src_id
1392
1381
 
1393
1382
  # puts "debug1 1-2. recv fin2 -> del src ext"
1394
- del_src_ext( src_addr )
1383
+ del_src_ext( src_id )
1395
1384
  when TUND_FIN
1396
1385
  return if from_addr != @tun_info[ :tund_addr ]
1397
1386
 
1398
- @tun_info[ :last_recv_at ] = now
1399
-
1400
1387
  puts "p#{ Process.pid } #{ Time.new } recv tund fin"
1401
1388
  set_is_closing( tun )
1402
1389
  end
@@ -1408,15 +1395,13 @@ module Girl
1408
1395
 
1409
1396
  dst_port = data[ 8, 2 ].unpack( 'n' ).first
1410
1397
 
1411
- src_addr = @tun_info[ :src_addrs ][ dst_port ]
1412
- return unless src_addr
1398
+ src_id = @tun_info[ :src_ids ][ dst_port ]
1399
+ return unless src_id
1413
1400
 
1414
- src_ext = @tun_info[ :src_exts ][ src_addr ]
1401
+ src_ext = @tun_info[ :src_exts ][ src_id ]
1415
1402
  return if src_ext.nil? || src_ext[ :src ].closed?
1416
1403
  return if ( pack_id <= src_ext[ :continue_dst_pack_id ] ) || src_ext[ :pieces ].include?( pack_id )
1417
1404
 
1418
- @tun_info[ :last_recv_at ] = now
1419
-
1420
1405
  data = data[ 10..-1 ]
1421
1406
  # puts "debug2 got pack #{ pack_id }"
1422
1407
 
@@ -183,7 +183,7 @@ module Girl
183
183
  ##
184
184
  # resolve domain
185
185
  #
186
- def resolve_domain( tund, src_addr, destination_domain_port )
186
+ def resolve_domain( tund, src_id, destination_domain_port )
187
187
  resolv_cache = @resolv_caches[ destination_domain_port ]
188
188
 
189
189
  if resolv_cache
@@ -191,7 +191,7 @@ module Girl
191
191
 
192
192
  if Time.new - created_at < RESOLV_CACHE_EXPIRE
193
193
  # puts "debug1 #{ destination_domain_port } hit resolv cache #{ Addrinfo.new( destination_addr ).inspect }"
194
- deal_with_destination_addr( tund, src_addr, destination_addr )
194
+ deal_with_destination_addr( tund, src_id, destination_addr )
195
195
  return
196
196
  end
197
197
 
@@ -215,7 +215,7 @@ module Girl
215
215
  @resolv_caches[ destination_domain_port ] = [ destination_addr, Time.new ]
216
216
 
217
217
  unless tund.closed?
218
- if deal_with_destination_addr( tund, src_addr, destination_addr )
218
+ if deal_with_destination_addr( tund, src_id, destination_addr )
219
219
  next_tick
220
220
  end
221
221
  end
@@ -227,7 +227,7 @@ module Girl
227
227
  ##
228
228
  # deal with destination addr
229
229
  #
230
- def deal_with_destination_addr( tund, src_addr, destination_addr )
230
+ def deal_with_destination_addr( tund, src_id, destination_addr )
231
231
  dst = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
232
232
  dst.setsockopt( Socket::SOL_TCP, Socket::TCP_NODELAY, 1 )
233
233
 
@@ -239,7 +239,7 @@ module Girl
239
239
  return false
240
240
  end
241
241
 
242
- local_port = dst.local_address.ip_unpack.last
242
+ local_port = dst.local_address.ip_port
243
243
 
244
244
  @dst_infos[ dst ] = {
245
245
  local_port: local_port, # 本地端口
@@ -255,10 +255,10 @@ module Girl
255
255
  add_read( dst, :dst )
256
256
 
257
257
  tund_info = @tund_infos[ tund ]
258
- tund_info[ :dst_local_ports ][ src_addr ] = local_port
258
+ tund_info[ :dst_local_ports ][ src_id ] = local_port
259
259
  tund_info[ :dst_exts ][ local_port ] = {
260
260
  dst: dst, # dst
261
- src_addr: src_addr, # 近端src地址
261
+ src_id: src_id, # 近端src id
262
262
  wmems: {}, # 写后 pack_id => data
263
263
  send_ats: {}, # 上一次发出时间 pack_id => send_at
264
264
  biggest_pack_id: 0, # 发到几
@@ -270,7 +270,7 @@ module Girl
270
270
  last_continue_at: Time.new # 创建,或者上一次收到连续流量,或者发出新包的时间
271
271
  }
272
272
 
273
- data = [ [ 0, PAIRED ].pack( 'Q>C' ), src_addr, [ local_port ].pack( 'n' ) ].join
273
+ data = [ 0, PAIRED, src_id, local_port ].pack( 'Q>CQ>n' )
274
274
  # puts "debug1 add ctlmsg paired #{ data.inspect }"
275
275
  add_tund_ctlmsg( tund, data )
276
276
 
@@ -344,7 +344,7 @@ module Girl
344
344
  dst_info = @dst_infos[ dst ]
345
345
  dst_info[ :wbuff ] << data
346
346
 
347
- if dst_info[ :wbuff ].bytesize >= PROXY_CHUNK_SIZE
347
+ if dst_info[ :wbuff ].bytesize >= CHUNK_SIZE
348
348
  spring = dst_info[ :chunks ].size > 0 ? ( dst_info[ :spring ] + 1 ) : 0
349
349
  filename = "#{ Process.pid }-#{ dst_info[ :local_port ] }.#{ spring }"
350
350
  chunk_path = File.join( @dst_chunk_dir, filename )
@@ -480,7 +480,7 @@ module Girl
480
480
  dst_ext = tund_info[ :dst_exts ].delete( dst_local_port )
481
481
 
482
482
  if dst_ext
483
- tund_info[ :dst_local_ports ].delete( dst_ext[ :src_addr ] )
483
+ tund_info[ :dst_local_ports ].delete( dst_ext[ :src_id ] )
484
484
  end
485
485
  end
486
486
 
@@ -724,7 +724,7 @@ module Girl
724
724
 
725
725
  tund = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
726
726
  tund.bind( Socket.sockaddr_in( 0, '0.0.0.0' ) )
727
- port = tund.local_address.ip_unpack.last
727
+ port = tund.local_address.ip_port
728
728
 
729
729
  @tunneling_tunds[ from_addr ] = tund
730
730
  @tunds[ port ] = tund
@@ -738,7 +738,7 @@ module Girl
738
738
  tun_addr: from_addr, # tun地址
739
739
  is_tunneled: false, # 是否已和tun打通
740
740
  dst_exts: {}, # dst额外信息 dst_addr => {}
741
- dst_local_ports: {}, # src_addr => dst_local_port
741
+ dst_local_ports: {}, # src_id => dst_local_port
742
742
  paused: false, # 是否暂停写
743
743
  resendings: [], # 重传队列 [ dst_addr, pack_id ]
744
744
  created_at: Time.new, # 创建时间
@@ -758,7 +758,7 @@ module Girl
758
758
  #
759
759
  def read_dst( dst )
760
760
  begin
761
- data = dst.read_nonblock( PROXY_PACK_SIZE )
761
+ data = dst.read_nonblock( PACK_SIZE )
762
762
  rescue IO::WaitReadable, Errno::EINTR
763
763
  return
764
764
  rescue Exception => e
@@ -808,9 +808,9 @@ module Girl
808
808
 
809
809
  case ctl_num
810
810
  when A_NEW_SOURCE
811
- src_addr = data[ 9, 16 ]
812
- dst_local_port = tund_info[ :dst_local_ports ][ src_addr ]
813
- # puts "debug1 got a new source #{ Addrinfo.new( src_addr ).inspect }"
811
+ src_id = data[ 9, 8 ].unpack( 'Q>' ).first
812
+ dst_local_port = tund_info[ :dst_local_ports ][ src_id ]
813
+ # puts "debug1 got a new source #{ src_id }"
814
814
 
815
815
  if dst_local_port
816
816
  dst_ext = tund_info[ :dst_exts ][ dst_local_port ]
@@ -821,22 +821,21 @@ module Girl
821
821
  end
822
822
 
823
823
  # puts "debug1 readd ctlmsg paired #{ dst_local_port }"
824
- data2 = [ [ 0, PAIRED ].pack( 'Q>C' ), src_addr, [ dst_local_port ].pack( 'n' ) ].join
824
+ data2 = [ 0, PAIRED, src_id, dst_local_port ].pack( 'Q>CQ>n' )
825
825
  add_tund_ctlmsg( tund, data2 )
826
826
  return
827
827
  end
828
828
 
829
829
  tund_info[ :last_recv_at ] = now
830
830
 
831
- data = data[ 25..-1 ]
831
+ data = data[ 17..-1 ]
832
832
  # puts "debug1 #{ data }"
833
833
  destination_domain_port = @custom.decode( data )
834
- resolve_domain( tund, src_addr, destination_domain_port )
834
+ resolve_domain( tund, src_id, destination_domain_port )
835
835
  when SOURCE_STATUS
836
- src_addr = data[ 9, 16 ]
837
- biggest_src_pack_id, continue_dst_pack_id = data[ 25, 16 ].unpack( 'Q>Q>' )
836
+ src_id, biggest_src_pack_id, continue_dst_pack_id = data[ 9, 24 ].unpack( 'Q>Q>Q>' )
838
837
 
839
- dst_local_port = tund_info[ :dst_local_ports ][ src_addr ]
838
+ dst_local_port = tund_info[ :dst_local_ports ][ src_id ]
840
839
  return unless dst_local_port
841
840
 
842
841
  dst_ext = tund_info[ :dst_exts ][ dst_local_port ]
@@ -885,7 +884,7 @@ module Girl
885
884
  break
886
885
  end
887
886
 
888
- data2 = [ [ 0, MISS ].pack( 'Q>C' ), src_addr, [ pack_id_begin, pack_id_end ].pack( 'Q>Q>' ) ].join
887
+ data2 = [ 0, MISS, src_id, pack_id_begin, pack_id_end ].pack( 'Q>CQ>Q>Q>' )
889
888
  add_tund_ctlmsg( tund, data2 )
890
889
  pack_count += ( pack_id_end - pack_id_begin + 1 )
891
890
  end
@@ -909,10 +908,9 @@ module Girl
909
908
 
910
909
  add_write( tund )
911
910
  when FIN1
912
- src_addr = data[ 9, 16 ]
913
- biggest_src_pack_id, continue_dst_pack_id = data[ 25, 16 ].unpack( 'Q>Q>' )
911
+ src_id, biggest_src_pack_id, continue_dst_pack_id = data[ 9, 24 ].unpack( 'Q>Q>Q>' )
914
912
 
915
- dst_local_port = tund_info[ :dst_local_ports ][ src_addr ]
913
+ dst_local_port = tund_info[ :dst_local_ports ][ src_id ]
916
914
  return unless dst_local_port
917
915
 
918
916
  dst_ext = tund_info[ :dst_exts ][ dst_local_port ]
@@ -920,7 +918,7 @@ module Girl
920
918
 
921
919
  tund_info[ :last_recv_at ] = now
922
920
 
923
- # puts "debug1 got fin1 #{ Addrinfo.new( src_addr ).inspect } biggest src pack #{ biggest_src_pack_id } completed dst pack #{ continue_dst_pack_id }"
921
+ # puts "debug1 got fin1 #{ src_id } biggest src pack #{ biggest_src_pack_id } completed dst pack #{ continue_dst_pack_id }"
924
922
  dst_ext[ :is_src_closed ] = true
925
923
  dst_ext[ :biggest_src_pack_id ] = biggest_src_pack_id
926
924
  release_wmems( dst_ext, continue_dst_pack_id )
@@ -931,9 +929,9 @@ module Girl
931
929
  set_is_closing( dst_ext[ :dst ] )
932
930
  end
933
931
  when FIN2
934
- src_addr = data[ 9, 16 ]
932
+ src_id = data[ 9, 8 ].unpack( 'Q>' ).first
935
933
 
936
- dst_local_port = tund_info[ :dst_local_ports ][ src_addr ]
934
+ dst_local_port = tund_info[ :dst_local_ports ][ src_id ]
937
935
  return unless dst_local_port
938
936
 
939
937
  tund_info[ :last_recv_at ] = now
@@ -949,9 +947,9 @@ module Girl
949
947
  return
950
948
  end
951
949
 
952
- src_addr = data[ 8, 16 ]
950
+ src_id = data[ 8, 8 ].unpack( 'Q>' ).first
953
951
 
954
- dst_local_port = tund_info[ :dst_local_ports ][ src_addr ]
952
+ dst_local_port = tund_info[ :dst_local_ports ][ src_id ]
955
953
  return unless dst_local_port
956
954
 
957
955
  dst_ext = tund_info[ :dst_exts ][ dst_local_port ]
@@ -960,7 +958,7 @@ module Girl
960
958
 
961
959
  tund_info[ :last_recv_at ] = now
962
960
 
963
- data = data[ 24..-1 ]
961
+ data = data[ 16..-1 ]
964
962
  # puts "debug2 got pack #{ pack_id }"
965
963
 
966
964
  if pack_id <= CONFUSE_UNTIL
@@ -1,3 +1,3 @@
1
1
  module Girl
2
- VERSION = '0.64.1'.freeze
2
+ VERSION = '0.65.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.64.1
4
+ version: 0.65.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-06-22 00:00:00.000000000 Z
11
+ date: 2020-06-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: while internet is evil, here's a girl.
14
14
  email: