girl 0.93.0 → 0.94.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 +4 -4
- data/lib/girl/head.rb +3 -0
- data/lib/girl/proxy.rb +24 -7
- data/lib/girl/proxy_worker.rb +76 -46
- data/lib/girl/proxyd.rb +11 -1
- data/lib/girl/proxyd_worker.rb +132 -29
- data/lib/girl/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4fbf6e091f1c0c19865e86a349ed046284ca1f807a171cd8f91b19066de3e28
|
4
|
+
data.tar.gz: cb9077f3870ba5cfc2ed17bf66f9c97767d94d882836acf757d5b6addcc4860a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bcb135b88eb5fd4d06f7b7cbeb063553a380efa8c3359c0420b69c2b5481a873ea5632333c8dd56899112d86fb1a11697fa8befc734a168cb2d03ef9eb5b2b2
|
7
|
+
data.tar.gz: '07935ef257d9301b18a9e1b4ed943ac0499ca4b502434b27aebebcfb8fcbe3f7a576fc9e5e7a16e9ed37ab9cf75eb490b1802dcad7024dd33f7db1a32ae11d37'
|
data/lib/girl/head.rb
CHANGED
@@ -5,6 +5,8 @@ module Girl
|
|
5
5
|
SEND_HELLO_COUNT = 10 # hello/a new source最多发几次
|
6
6
|
SEND_HELLO_INTERVAL = 0.5 # 发送hello/a new source间隔
|
7
7
|
EXPIRE_AFTER = 300 # 多久没有新流量,过期
|
8
|
+
RESET_TRAFF_DAY = 1 # 流量计数重置日,0为不重置
|
9
|
+
CHECK_TRAFF_INTERVAL = 86400 # 检查今天是否是流量计数重置日间隔
|
8
10
|
CHECK_EXPIRE_INTERVAL = 30 # 检查过期间隔
|
9
11
|
CHECK_RESUME_INTERVAL = 1 # 检查恢复读间隔
|
10
12
|
RESOLV_CACHE_EXPIRE = 300 # dns查询结果缓存多久过期
|
@@ -27,6 +29,7 @@ module Girl
|
|
27
29
|
CONTINUE = 17
|
28
30
|
IS_RESEND_READY = 18
|
29
31
|
RESEND_READY = 19
|
32
|
+
TRAFF_INFOS = 101
|
30
33
|
HTTP_OK = "HTTP/1.1 200 OK\r\n\r\n"
|
31
34
|
# https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
32
35
|
RESERVED_ROUTE = <<EOF
|
data/lib/girl/proxy.rb
CHANGED
@@ -20,21 +20,34 @@ proxyd-tun:
|
|
20
20
|
|
21
21
|
Q>: 0 ctlmsg -> C: 1 tund port -> n: tund port -> n: tcpd port
|
22
22
|
|
23
|
+
local-infod:
|
24
|
+
|
25
|
+
C: 101 traff infos
|
26
|
+
|
27
|
+
infod-local:
|
28
|
+
|
29
|
+
C: 101 traff infos -> [ C: im len -> im -> Q>: traff in -> Q>: traff out ]
|
30
|
+
|
23
31
|
tun-tund:
|
24
32
|
|
25
33
|
Q>: 0 ctlmsg -> C: 2 heartbeat
|
26
34
|
3 a new source -> Q>: src id -> encoded destination address
|
27
35
|
4 paired -> Q>: src id -> n: dst id
|
28
|
-
5 dest status
|
29
|
-
6 source status
|
30
|
-
7 miss
|
31
|
-
8 fin1
|
32
|
-
9 confirm fin1
|
33
|
-
10 fin2
|
34
|
-
11 confirm fin2
|
36
|
+
5 dest status NOT USE
|
37
|
+
6 source status NOT USE
|
38
|
+
7 miss NOT USE
|
39
|
+
8 fin1 NOT USE
|
40
|
+
9 confirm fin1 NOT USE
|
41
|
+
10 fin2 NOT USE
|
42
|
+
11 confirm fin2 NOT USE
|
35
43
|
12 tund fin
|
36
44
|
13 tun fin
|
37
45
|
14 tun ip changed
|
46
|
+
15 single miss NOT USE
|
47
|
+
16 range miss NOT USE
|
48
|
+
17 continue NOT USE
|
49
|
+
18 is resend ready NOT USE
|
50
|
+
19 resend ready NOT USE
|
38
51
|
=end
|
39
52
|
|
40
53
|
module Girl
|
@@ -109,6 +122,10 @@ module Girl
|
|
109
122
|
puts "im #{ im }"
|
110
123
|
puts "worker count #{ worker_count }"
|
111
124
|
|
125
|
+
Girl::Custom.constants.each do | name |
|
126
|
+
puts "#{ name } #{ Girl::Custom.const_get( name ).inspect }"
|
127
|
+
end
|
128
|
+
|
112
129
|
len = CONSTS.map{ | name | name.size }.max
|
113
130
|
|
114
131
|
CONSTS.each do | name |
|
data/lib/girl/proxy_worker.rb
CHANGED
@@ -188,7 +188,10 @@ module Girl
|
|
188
188
|
def close_dst( dst )
|
189
189
|
# puts "debug1 close dst"
|
190
190
|
close_sock( dst )
|
191
|
-
@dst_infos.delete( dst )
|
191
|
+
dst_info = @dst_infos.delete( dst )
|
192
|
+
src = dst_info[ :src ]
|
193
|
+
close_read_src( src )
|
194
|
+
set_src_closing_write( src )
|
192
195
|
end
|
193
196
|
|
194
197
|
##
|
@@ -258,7 +261,32 @@ module Girl
|
|
258
261
|
def close_src( src )
|
259
262
|
# puts "debug1 close src"
|
260
263
|
close_sock( src )
|
261
|
-
del_src_info( src )
|
264
|
+
src_info = del_src_info( src )
|
265
|
+
dst = src_info[ :dst ]
|
266
|
+
|
267
|
+
if dst then
|
268
|
+
close_read_dst( dst )
|
269
|
+
set_dst_closing_write( dst )
|
270
|
+
else
|
271
|
+
stream = src_info[ :stream ]
|
272
|
+
|
273
|
+
if stream then
|
274
|
+
close_read_stream( stream )
|
275
|
+
set_stream_closing_write( stream )
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
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 )
|
262
290
|
end
|
263
291
|
|
264
292
|
##
|
@@ -287,6 +315,7 @@ module Girl
|
|
287
315
|
src_info = @src_infos[ src ]
|
288
316
|
end
|
289
317
|
|
318
|
+
src_info[ :closed_write ] = true
|
290
319
|
src_info
|
291
320
|
end
|
292
321
|
|
@@ -307,6 +336,7 @@ module Girl
|
|
307
336
|
dst_info = @dst_infos[ dst ]
|
308
337
|
end
|
309
338
|
|
339
|
+
dst_info[ :closed_write ] = true
|
310
340
|
dst_info
|
311
341
|
end
|
312
342
|
|
@@ -327,6 +357,7 @@ module Girl
|
|
327
357
|
stream_info = @stream_infos[ stream ]
|
328
358
|
end
|
329
359
|
|
360
|
+
stream_info[ :closed_write ] = true
|
330
361
|
stream_info
|
331
362
|
end
|
332
363
|
|
@@ -531,7 +562,8 @@ module Girl
|
|
531
562
|
wbuff: '', # 写前,从src读到的流量
|
532
563
|
paused: false, # 是否已暂停读
|
533
564
|
closing: false, # 准备关闭
|
534
|
-
closing_write: false
|
565
|
+
closing_write: false, # 准备关闭写
|
566
|
+
closed_write: false # 已关闭写
|
535
567
|
}
|
536
568
|
|
537
569
|
@dst_infos[ dst ] = dst_info
|
@@ -590,10 +622,13 @@ module Girl
|
|
590
622
|
|
591
623
|
domain = src_info[ :destination_domain ]
|
592
624
|
@stream_infos[ stream ] = {
|
593
|
-
src: src,
|
594
|
-
domain: domain,
|
595
|
-
wbuff: data,
|
596
|
-
paused: false
|
625
|
+
src: src, # 对应src
|
626
|
+
domain: domain, # 目的地
|
627
|
+
wbuff: data, # 写前,写往远端streamd
|
628
|
+
paused: false, # 是否已暂停读
|
629
|
+
closing: false, # 准备关闭
|
630
|
+
closing_write: false, # 准备关闭写
|
631
|
+
closed_write: false # 已关闭写
|
597
632
|
}
|
598
633
|
|
599
634
|
src_info[ :dst_id ] = dst_id
|
@@ -750,9 +785,14 @@ module Girl
|
|
750
785
|
def set_dst_closing( dst )
|
751
786
|
return if dst.closed?
|
752
787
|
dst_info = @dst_infos[ dst ]
|
753
|
-
|
754
|
-
|
755
|
-
|
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
|
756
796
|
end
|
757
797
|
|
758
798
|
##
|
@@ -760,7 +800,10 @@ module Girl
|
|
760
800
|
#
|
761
801
|
def set_dst_closing_write( dst )
|
762
802
|
return if dst.closed?
|
803
|
+
|
763
804
|
dst_info = @dst_infos[ dst ]
|
805
|
+
return if dst_info[ :closed_write ]
|
806
|
+
|
764
807
|
dst_info[ :closing_write ] = true
|
765
808
|
add_write( dst )
|
766
809
|
end
|
@@ -770,10 +813,16 @@ module Girl
|
|
770
813
|
#
|
771
814
|
def set_src_closing( src )
|
772
815
|
return if src.closed?
|
773
|
-
|
816
|
+
|
774
817
|
src_info = @src_infos[ src ]
|
775
|
-
|
776
|
-
|
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
|
777
826
|
end
|
778
827
|
|
779
828
|
##
|
@@ -781,7 +830,10 @@ module Girl
|
|
781
830
|
#
|
782
831
|
def set_src_closing_write( src )
|
783
832
|
return if src.closed?
|
833
|
+
|
784
834
|
src_info = @src_infos[ src ]
|
835
|
+
return if src_info[ :closed_write ]
|
836
|
+
|
785
837
|
src_info[ :closing_write ] = true
|
786
838
|
add_write( src )
|
787
839
|
end
|
@@ -806,23 +858,15 @@ module Girl
|
|
806
858
|
end
|
807
859
|
end
|
808
860
|
|
809
|
-
##
|
810
|
-
# set stream closing
|
811
|
-
#
|
812
|
-
def set_stream_closing( stream )
|
813
|
-
return if stream.closed?
|
814
|
-
stream_info = @stream_infos[ stream ]
|
815
|
-
stream_info[ :closing ] = true
|
816
|
-
@reads.delete( stream )
|
817
|
-
add_write( stream )
|
818
|
-
end
|
819
|
-
|
820
861
|
##
|
821
862
|
# set stream closing write
|
822
863
|
#
|
823
864
|
def set_stream_closing_write( stream )
|
824
865
|
return if stream.closed?
|
866
|
+
|
825
867
|
stream_info = @stream_infos[ stream ]
|
868
|
+
return if stream_info[ :closed_write ]
|
869
|
+
|
826
870
|
stream_info[ :closing_write ] = true
|
827
871
|
add_write( stream )
|
828
872
|
end
|
@@ -894,7 +938,8 @@ module Girl
|
|
894
938
|
last_sent_at: nil, # 上一次发出流量(由dst发出,或者由stream发出)的时间
|
895
939
|
paused: false, # 是否已暂停读
|
896
940
|
closing: false, # 准备关闭
|
897
|
-
closing_write: false
|
941
|
+
closing_write: false, # 准备关闭写
|
942
|
+
closed_write: false # 已关闭写
|
898
943
|
}
|
899
944
|
|
900
945
|
add_read( src, :src )
|
@@ -961,6 +1006,8 @@ module Girl
|
|
961
1006
|
# read src
|
962
1007
|
#
|
963
1008
|
def read_src( src )
|
1009
|
+
return if src.closed?
|
1010
|
+
|
964
1011
|
begin
|
965
1012
|
data = src.read_nonblock( READ_SIZE )
|
966
1013
|
rescue IO::WaitReadable, Errno::EINTR
|
@@ -1164,6 +1211,8 @@ module Girl
|
|
1164
1211
|
# read dst
|
1165
1212
|
#
|
1166
1213
|
def read_dst( dst )
|
1214
|
+
return if dst.closed?
|
1215
|
+
|
1167
1216
|
begin
|
1168
1217
|
data = dst.read_nonblock( READ_SIZE )
|
1169
1218
|
rescue IO::WaitReadable, Errno::EINTR
|
@@ -1186,6 +1235,8 @@ module Girl
|
|
1186
1235
|
# read stream
|
1187
1236
|
#
|
1188
1237
|
def read_stream( stream )
|
1238
|
+
return if stream.closed?
|
1239
|
+
|
1189
1240
|
begin
|
1190
1241
|
data = stream.read_nonblock( READ_SIZE )
|
1191
1242
|
rescue IO::WaitReadable, Errno::EINTR
|
@@ -1250,19 +1301,6 @@ module Girl
|
|
1250
1301
|
# 处理关闭
|
1251
1302
|
if src_info[ :closing ] then
|
1252
1303
|
close_src( src )
|
1253
|
-
|
1254
|
-
if dst then
|
1255
|
-
close_read_dst( dst )
|
1256
|
-
set_dst_closing_write( dst )
|
1257
|
-
else
|
1258
|
-
stream = src_info[ :stream ]
|
1259
|
-
|
1260
|
-
if stream then
|
1261
|
-
close_read_stream( stream )
|
1262
|
-
set_stream_closing_write( stream )
|
1263
|
-
end
|
1264
|
-
end
|
1265
|
-
|
1266
1304
|
return
|
1267
1305
|
end
|
1268
1306
|
|
@@ -1316,12 +1354,6 @@ module Girl
|
|
1316
1354
|
# 处理关闭
|
1317
1355
|
if dst_info[ :closing ] then
|
1318
1356
|
close_dst( dst )
|
1319
|
-
|
1320
|
-
if src then
|
1321
|
-
close_read_src( src )
|
1322
|
-
set_src_closing_write( src )
|
1323
|
-
end
|
1324
|
-
|
1325
1357
|
return
|
1326
1358
|
end
|
1327
1359
|
|
@@ -1371,8 +1403,6 @@ module Girl
|
|
1371
1403
|
# 处理关闭
|
1372
1404
|
if stream_info[ :closing ] then
|
1373
1405
|
close_stream( stream )
|
1374
|
-
close_read_src( src )
|
1375
|
-
set_src_closing_write( src )
|
1376
1406
|
return
|
1377
1407
|
end
|
1378
1408
|
|
data/lib/girl/proxyd.rb
CHANGED
@@ -17,6 +17,7 @@ module Girl
|
|
17
17
|
raise "not found config file #{ config_path }" unless File.exist?( config_path )
|
18
18
|
conf = JSON.parse( IO.binread( config_path ), symbolize_names: true )
|
19
19
|
proxyd_port = conf[ :proxyd_port ]
|
20
|
+
infod_port = conf[ :infod_port ]
|
20
21
|
worker_count = conf[ :worker_count ]
|
21
22
|
end
|
22
23
|
|
@@ -24,6 +25,10 @@ module Girl
|
|
24
25
|
proxyd_port = 6060
|
25
26
|
end
|
26
27
|
|
28
|
+
unless infod_port then
|
29
|
+
infod_port = 6070
|
30
|
+
end
|
31
|
+
|
27
32
|
nprocessors = Etc.nprocessors
|
28
33
|
|
29
34
|
if worker_count.nil? || worker_count <= 0 || worker_count > nprocessors then
|
@@ -33,8 +38,13 @@ module Girl
|
|
33
38
|
title = "girl proxyd #{ Girl::VERSION }"
|
34
39
|
puts title
|
35
40
|
puts "proxyd port #{ proxyd_port }"
|
41
|
+
puts "infod port #{ infod_port }"
|
36
42
|
puts "worker count #{ worker_count }"
|
37
43
|
|
44
|
+
Girl::Custom.constants.each do | name |
|
45
|
+
puts "#{ name } #{ Girl::Custom.const_get( name ).inspect }"
|
46
|
+
end
|
47
|
+
|
38
48
|
len = CONSTS.map{ | name | name.size }.max
|
39
49
|
|
40
50
|
CONSTS.each do | name |
|
@@ -47,7 +57,7 @@ module Girl
|
|
47
57
|
worker_count.times do | i |
|
48
58
|
workers << fork do
|
49
59
|
$0 = 'girl proxyd worker'
|
50
|
-
worker = Girl::ProxydWorker.new( proxyd_port )
|
60
|
+
worker = Girl::ProxydWorker.new( proxyd_port, infod_port )
|
51
61
|
|
52
62
|
Signal.trap( :TERM ) do
|
53
63
|
puts "w#{ i } exit"
|
data/lib/girl/proxyd_worker.rb
CHANGED
@@ -4,23 +4,26 @@ module Girl
|
|
4
4
|
##
|
5
5
|
# initialize
|
6
6
|
#
|
7
|
-
def initialize( proxyd_port )
|
7
|
+
def initialize( proxyd_port, infod_port )
|
8
8
|
@custom = Girl::ProxydCustom.new
|
9
9
|
@mutex = Mutex.new
|
10
10
|
@reads = []
|
11
11
|
@writes = []
|
12
|
-
@roles = {} # sock => :dotr / :proxyd / :dst / :tund / :tcpd / :streamd
|
12
|
+
@roles = {} # sock => :dotr / :proxyd / :infod / :dst / :tund / :tcpd / :streamd
|
13
13
|
@tund_infos = {} # tund => {}
|
14
14
|
@tcpd_infos = {} # tcpd => {}
|
15
15
|
@dst_infos = {} # dst => {}
|
16
16
|
@streamd_infos = {} # streamd => {}
|
17
17
|
@tunneling_tunds = {} # tunneling_addr => tund
|
18
18
|
@resolv_caches = {} # domain => [ ip, created_at ]
|
19
|
+
@traff_ins = {} # im => 0
|
20
|
+
@traff_outs = {} # im => 0
|
19
21
|
|
20
22
|
dotr, dotw = IO.pipe
|
21
23
|
@dotw = dotw
|
22
24
|
add_read( dotr, :dotr )
|
23
25
|
new_a_proxyd( proxyd_port )
|
26
|
+
new_a_infod( infod_port )
|
24
27
|
end
|
25
28
|
|
26
29
|
##
|
@@ -30,6 +33,7 @@ module Girl
|
|
30
33
|
puts "p#{ Process.pid } #{ Time.new } looping"
|
31
34
|
loop_check_expire
|
32
35
|
loop_check_resume
|
36
|
+
loop_check_traff
|
33
37
|
|
34
38
|
loop do
|
35
39
|
rs, ws = IO.select( @reads, @writes )
|
@@ -42,6 +46,8 @@ module Girl
|
|
42
46
|
read_dotr( sock )
|
43
47
|
when :proxyd then
|
44
48
|
read_proxyd( sock )
|
49
|
+
when :infod then
|
50
|
+
read_infod( sock )
|
45
51
|
when :tund then
|
46
52
|
read_tund( sock )
|
47
53
|
when :tcpd then
|
@@ -137,7 +143,13 @@ module Girl
|
|
137
143
|
def close_dst( dst )
|
138
144
|
# puts "debug1 close dst"
|
139
145
|
close_sock( dst )
|
140
|
-
del_dst_info( dst )
|
146
|
+
dst_info = del_dst_info( dst )
|
147
|
+
streamd = dst_info[ :streamd ]
|
148
|
+
|
149
|
+
if streamd then
|
150
|
+
close_read_streamd( streamd )
|
151
|
+
set_streamd_closing_write( streamd )
|
152
|
+
end
|
141
153
|
end
|
142
154
|
|
143
155
|
##
|
@@ -197,7 +209,13 @@ module Girl
|
|
197
209
|
def close_streamd( streamd )
|
198
210
|
# puts "debug1 close streamd"
|
199
211
|
close_sock( streamd )
|
200
|
-
@streamd_infos.delete( streamd )
|
212
|
+
streamd_info = @streamd_infos.delete( streamd )
|
213
|
+
dst = streamd_info[ :dst ]
|
214
|
+
|
215
|
+
if dst then
|
216
|
+
close_read_dst( dst )
|
217
|
+
set_dst_closing_write( dst )
|
218
|
+
end
|
201
219
|
end
|
202
220
|
|
203
221
|
##
|
@@ -231,6 +249,7 @@ module Girl
|
|
231
249
|
dst_info = @dst_infos[ dst ]
|
232
250
|
end
|
233
251
|
|
252
|
+
dst_info[ :closed_write ] = true
|
234
253
|
dst_info
|
235
254
|
end
|
236
255
|
|
@@ -251,6 +270,7 @@ module Girl
|
|
251
270
|
streamd_info = @streamd_infos[ streamd ]
|
252
271
|
end
|
253
272
|
|
273
|
+
streamd_info[ :closed_write ] = true
|
254
274
|
streamd_info
|
255
275
|
end
|
256
276
|
|
@@ -270,10 +290,12 @@ module Girl
|
|
270
290
|
end
|
271
291
|
|
272
292
|
dst_id = dst.local_address.ip_port
|
293
|
+
tund_info = @tund_infos[ tund ]
|
273
294
|
|
274
295
|
@dst_infos[ dst ] = {
|
275
296
|
id: dst_id, # id
|
276
297
|
tund: tund, # 对应tund
|
298
|
+
im: tund_info[ :im ], # 标识
|
277
299
|
domain_port: domain_port, # 目的地和端口
|
278
300
|
rbuff: '', # 对应的streamd没准备好,暂存读到的流量
|
279
301
|
streamd: nil, # 对应的streamd
|
@@ -284,12 +306,12 @@ module Girl
|
|
284
306
|
last_sent_at: nil, # 上一次发出流量(由streamd发出)的时间
|
285
307
|
paused: false, # 是否已暂停读
|
286
308
|
closing: false, # 准备关闭
|
287
|
-
closing_write: false
|
309
|
+
closing_write: false, # 准备关闭写
|
310
|
+
closed_write: false # 已关闭写
|
288
311
|
}
|
289
312
|
|
290
313
|
add_read( dst, :dst )
|
291
314
|
|
292
|
-
tund_info = @tund_infos[ tund ]
|
293
315
|
tund_info[ :dst_ids ][ src_id ] = dst_id
|
294
316
|
tund_info[ :dsts ][ dst_id ] = dst
|
295
317
|
|
@@ -395,6 +417,27 @@ module Girl
|
|
395
417
|
end
|
396
418
|
end
|
397
419
|
|
420
|
+
##
|
421
|
+
# loop check traff
|
422
|
+
#
|
423
|
+
def loop_check_traff
|
424
|
+
if RESET_TRAFF_DAY > 0 then
|
425
|
+
Thread.new do
|
426
|
+
loop do
|
427
|
+
sleep CHECK_TRAFF_INTERVAL
|
428
|
+
|
429
|
+
@mutex.synchronize do
|
430
|
+
if Time.new.day == RESET_TRAFF_DAY then
|
431
|
+
puts "p#{ Process.pid } #{ Time.new } reset traffs"
|
432
|
+
@traff_ins.transform_values!{ | _ | 0 }
|
433
|
+
@traff_outs.transform_values!{ | _ | 0 }
|
434
|
+
end
|
435
|
+
end
|
436
|
+
end
|
437
|
+
end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
398
441
|
##
|
399
442
|
# new a proxyd
|
400
443
|
#
|
@@ -412,6 +455,18 @@ module Girl
|
|
412
455
|
add_read( proxyd, :proxyd )
|
413
456
|
end
|
414
457
|
|
458
|
+
##
|
459
|
+
# new a infod
|
460
|
+
#
|
461
|
+
def new_a_infod( infod_port )
|
462
|
+
infod = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
|
463
|
+
infod.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1 )
|
464
|
+
infod.bind( Socket.sockaddr_in( infod_port, '127.0.0.1' ) )
|
465
|
+
|
466
|
+
puts "p#{ Process.pid } #{ Time.new } infod bind on #{ infod_port }"
|
467
|
+
add_read( infod, :infod )
|
468
|
+
end
|
469
|
+
|
415
470
|
##
|
416
471
|
# next tick
|
417
472
|
#
|
@@ -488,10 +543,16 @@ module Girl
|
|
488
543
|
#
|
489
544
|
def set_dst_closing( dst )
|
490
545
|
return if dst.closed?
|
546
|
+
|
491
547
|
dst_info = @dst_infos[ dst ]
|
492
|
-
|
493
|
-
|
494
|
-
|
548
|
+
|
549
|
+
if dst_info[ :closed_write ] then
|
550
|
+
close_read_dst( dst )
|
551
|
+
else
|
552
|
+
dst_info[ :closing ] = true
|
553
|
+
@reads.delete( dst )
|
554
|
+
add_write( dst )
|
555
|
+
end
|
495
556
|
end
|
496
557
|
|
497
558
|
##
|
@@ -499,7 +560,10 @@ module Girl
|
|
499
560
|
#
|
500
561
|
def set_dst_closing_write( dst )
|
501
562
|
return if dst.closed?
|
563
|
+
|
502
564
|
dst_info = @dst_infos[ dst ]
|
565
|
+
return if dst_info[ :closed_write ]
|
566
|
+
|
503
567
|
dst_info[ :closing_write ] = true
|
504
568
|
add_write( dst )
|
505
569
|
end
|
@@ -510,9 +574,14 @@ module Girl
|
|
510
574
|
def set_streamd_closing( streamd )
|
511
575
|
return if streamd.closed?
|
512
576
|
streamd_info = @streamd_infos[ streamd ]
|
513
|
-
|
514
|
-
|
515
|
-
|
577
|
+
|
578
|
+
if streamd_info[ :closed_write ] then
|
579
|
+
close_read_streamd( streamd )
|
580
|
+
else
|
581
|
+
streamd_info[ :closing ] = true
|
582
|
+
@reads.delete( streamd )
|
583
|
+
add_write( streamd )
|
584
|
+
end
|
516
585
|
end
|
517
586
|
|
518
587
|
##
|
@@ -520,7 +589,10 @@ module Girl
|
|
520
589
|
#
|
521
590
|
def set_streamd_closing_write( streamd )
|
522
591
|
return if streamd.closed?
|
592
|
+
|
523
593
|
streamd_info = @streamd_infos[ streamd ]
|
594
|
+
return if streamd_info[ :closed_write ]
|
595
|
+
|
524
596
|
streamd_info[ :closing_write ] = true
|
525
597
|
add_write( streamd )
|
526
598
|
end
|
@@ -565,6 +637,13 @@ module Girl
|
|
565
637
|
return
|
566
638
|
end
|
567
639
|
|
640
|
+
im = data
|
641
|
+
|
642
|
+
unless @traff_ins.include?( im ) then
|
643
|
+
@traff_ins[ im ] = 0
|
644
|
+
@traff_outs[ im ] = 0
|
645
|
+
end
|
646
|
+
|
568
647
|
tund = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
|
569
648
|
tund.bind( Socket.sockaddr_in( 0, '0.0.0.0' ) )
|
570
649
|
tund_port = tund.local_address.ip_port
|
@@ -578,6 +657,7 @@ module Girl
|
|
578
657
|
add_read( tcpd, :tcpd )
|
579
658
|
|
580
659
|
tund_info = {
|
660
|
+
im: im, # 标识
|
581
661
|
port: tund_port, # 端口
|
582
662
|
tcpd: tcpd, # 对应的tcpd
|
583
663
|
tcpd_port: tcpd_port, # tcpd端口
|
@@ -601,6 +681,28 @@ module Girl
|
|
601
681
|
add_proxyd_ctlmsg_tund_port( tund_info )
|
602
682
|
end
|
603
683
|
|
684
|
+
##
|
685
|
+
# read infod
|
686
|
+
#
|
687
|
+
def read_infod( infod )
|
688
|
+
data, addrinfo, rflags, *controls = infod.recvmsg
|
689
|
+
ctl_num = data[ 0 ].unpack( 'C' ).first
|
690
|
+
# puts "debug1 infod recv #{ ctl_num } #{ addrinfo.inspect }"
|
691
|
+
|
692
|
+
case ctl_num
|
693
|
+
when TRAFF_INFOS then
|
694
|
+
data2 = [ TRAFF_INFOS ].pack( 'C' )
|
695
|
+
|
696
|
+
@traff_ins.keys.sort.each do | im |
|
697
|
+
traff_in = @traff_ins[ im ]
|
698
|
+
traff_out = @traff_outs[ im ]
|
699
|
+
data2 << [ [ im.bytesize ].pack( 'C' ), im, [ traff_in, traff_out ].pack( 'Q>Q>' ) ].join
|
700
|
+
end
|
701
|
+
|
702
|
+
send_data( infod, data2, addrinfo )
|
703
|
+
end
|
704
|
+
end
|
705
|
+
|
604
706
|
##
|
605
707
|
# read tund
|
606
708
|
#
|
@@ -672,13 +774,18 @@ module Girl
|
|
672
774
|
# puts "debug1 accept a streamd"
|
673
775
|
tcpd_info = @tcpd_infos[ tcpd ]
|
674
776
|
tund = tcpd_info[ :tund ]
|
777
|
+
tund_info = @tund_infos[ tund ]
|
675
778
|
|
676
779
|
@streamd_infos[ streamd ] = {
|
677
|
-
tund: tund,
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
780
|
+
tund: tund, # 对应tund
|
781
|
+
im: tund_info[ :im ], # 标识
|
782
|
+
dst: nil, # 对应dst
|
783
|
+
domain_port: nil, # dst的目的地和端口
|
784
|
+
wbuff: '', # 写前,写往近端stream
|
785
|
+
paused: false, # 是否已暂停读
|
786
|
+
closing: false, # 准备关闭
|
787
|
+
closing_write: false, # 准备关闭写
|
788
|
+
closed_write: false # 已关闭写
|
682
789
|
}
|
683
790
|
|
684
791
|
add_read( streamd, :streamd )
|
@@ -688,6 +795,8 @@ module Girl
|
|
688
795
|
# read dst
|
689
796
|
#
|
690
797
|
def read_dst( dst )
|
798
|
+
return if dst.closed?
|
799
|
+
|
691
800
|
begin
|
692
801
|
data = dst.read_nonblock( READ_SIZE )
|
693
802
|
rescue IO::WaitReadable, Errno::EINTR
|
@@ -702,6 +811,7 @@ module Girl
|
|
702
811
|
end
|
703
812
|
|
704
813
|
dst_info = @dst_infos[ dst ]
|
814
|
+
@traff_ins[ dst_info[ :im ] ] += data.bytesize
|
705
815
|
streamd = dst_info[ :streamd ]
|
706
816
|
|
707
817
|
if streamd then
|
@@ -732,6 +842,8 @@ module Girl
|
|
732
842
|
# read streamd
|
733
843
|
#
|
734
844
|
def read_streamd( streamd )
|
845
|
+
return if streamd.closed?
|
846
|
+
|
735
847
|
begin
|
736
848
|
data = streamd.read_nonblock( READ_SIZE )
|
737
849
|
rescue IO::WaitReadable, Errno::EINTR
|
@@ -746,6 +858,7 @@ module Girl
|
|
746
858
|
end
|
747
859
|
|
748
860
|
streamd_info = @streamd_infos[ streamd ]
|
861
|
+
@traff_ins[ streamd_info[ :im ] ] += data.bytesize
|
749
862
|
dst = streamd_info[ :dst ]
|
750
863
|
|
751
864
|
unless dst then
|
@@ -866,12 +979,6 @@ module Girl
|
|
866
979
|
# 处理关闭
|
867
980
|
if dst_info[ :closing ] then
|
868
981
|
close_dst( dst )
|
869
|
-
|
870
|
-
if streamd then
|
871
|
-
close_read_streamd( streamd )
|
872
|
-
set_streamd_closing_write( streamd )
|
873
|
-
end
|
874
|
-
|
875
982
|
return
|
876
983
|
end
|
877
984
|
|
@@ -904,6 +1011,7 @@ module Girl
|
|
904
1011
|
# puts "debug2 written dst #{ written }"
|
905
1012
|
data = data[ written..-1 ]
|
906
1013
|
dst_info[ :wbuff ] = data
|
1014
|
+
@traff_outs[ dst_info[ :im ] ] += written
|
907
1015
|
end
|
908
1016
|
|
909
1017
|
##
|
@@ -917,12 +1025,6 @@ module Girl
|
|
917
1025
|
# 处理关闭
|
918
1026
|
if streamd_info[ :closing ] then
|
919
1027
|
close_streamd( streamd )
|
920
|
-
|
921
|
-
if dst then
|
922
|
-
close_read_dst( dst )
|
923
|
-
set_dst_closing_write( dst )
|
924
|
-
end
|
925
|
-
|
926
1028
|
return
|
927
1029
|
end
|
928
1030
|
|
@@ -955,6 +1057,7 @@ module Girl
|
|
955
1057
|
# puts "debug2 written streamd #{ written }"
|
956
1058
|
data = data[ written..-1 ]
|
957
1059
|
streamd_info[ :wbuff ] = data
|
1060
|
+
@traff_outs[ streamd_info[ :im ] ] += written
|
958
1061
|
|
959
1062
|
if dst && !dst.closed? then
|
960
1063
|
dst_info = @dst_infos[ dst ]
|
data/lib/girl/version.rb
CHANGED
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.
|
4
|
+
version: 0.94.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-10-
|
11
|
+
date: 2020-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: escape evil.
|
14
14
|
email:
|