girl 9.1.3 → 9.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/girl/head.rb +3 -4
- data/lib/girl/proxy.rb +6 -34
- data/lib/girl/proxy_worker.rb +330 -501
- data/lib/girl/proxyd.rb +7 -31
- data/lib/girl/proxyd_worker.rb +501 -270
- data/lib/girl/version.rb +1 -1
- metadata +1 -1
data/lib/girl/proxyd_worker.rb
CHANGED
|
@@ -4,6 +4,7 @@ module Girl
|
|
|
4
4
|
|
|
5
5
|
def initialize(
|
|
6
6
|
proxyd_port,
|
|
7
|
+
bigd_port,
|
|
7
8
|
memd_port,
|
|
8
9
|
nameservers,
|
|
9
10
|
reset_traff_day,
|
|
@@ -18,44 +19,37 @@ module Girl
|
|
|
18
19
|
h_p1_close,
|
|
19
20
|
h_p2_close,
|
|
20
21
|
h_p2_traffic,
|
|
21
|
-
h_p1_overflow,
|
|
22
|
-
h_p1_underhalf,
|
|
23
|
-
h_p2_overflow,
|
|
24
|
-
h_p2_underhalf,
|
|
25
22
|
h_query,
|
|
26
23
|
h_response,
|
|
27
24
|
h_src_close,
|
|
28
25
|
h_traffic,
|
|
29
|
-
h_src_overflow,
|
|
30
|
-
h_src_underhalf,
|
|
31
|
-
h_dst_overflow,
|
|
32
|
-
h_dst_underhalf,
|
|
33
26
|
expire_connecting,
|
|
34
27
|
expire_long_after,
|
|
35
|
-
expire_proxy_after,
|
|
36
28
|
expire_resolv_cache,
|
|
37
29
|
expire_short_after,
|
|
38
30
|
is_debug,
|
|
39
|
-
is_server_fastopen
|
|
31
|
+
is_server_fastopen)
|
|
40
32
|
|
|
41
33
|
@nameserver_addrs = nameservers.map{|n| Socket.sockaddr_in(53, n)}
|
|
42
34
|
@reset_traff_day = reset_traff_day
|
|
43
|
-
@update_roles = [:dns, :dst, :mem, :p2, :proxy, :rsv] # 参与淘汰的角色
|
|
35
|
+
@update_roles = [:big, :dns, :dst, :mem, :p2, :proxy, :rsv] # 参与淘汰的角色
|
|
44
36
|
@updates_limit = 1011 - ims.size # 淘汰池上限,1015(mac) - info, infod, memd, proxyd, p2ds(=ims)
|
|
45
37
|
@eliminate_count = 0 # 淘汰次数
|
|
46
38
|
@reads = [] # 读池
|
|
47
39
|
@writes = [] # 写池
|
|
48
|
-
@roles = {} # sock => :dns / :dst / :infod / :mem / :memd / :p2 / :p2d / :proxy / :proxyd / :rsv
|
|
40
|
+
@roles = {} # sock => :big / :bigd / :dns / :dst / :infod / :mem / :memd / :p2 / :p2d / :proxy / :proxyd / :rsv
|
|
49
41
|
@updates = {} # sock => updated_at
|
|
50
|
-
@proxy_infos = {} # proxy => {:addrinfo :im :
|
|
51
|
-
@
|
|
42
|
+
@proxy_infos = {} # proxy => {:addrinfo :im :rbuff :wbuff}
|
|
43
|
+
@big_infos = {} # big => {:addrinfo :im :overflowing :rbuff :wbuff}
|
|
44
|
+
@im_infos = {} # im => {:addrinfo :big :big_connect_at :in :out :p2d :p2d_host :p2d_port :proxy :proxy_connect_at}
|
|
45
|
+
@src_infos = {} # src_id => {:created_at, :dst :im :rbuff}
|
|
52
46
|
@mem_infos = {} # mem => {:wbuff}
|
|
53
|
-
@dst_infos = {} # dst => {:closing :connected :domain :im :ip :overflowing :port :
|
|
54
|
-
@dns_infos = {} # dns => {:domain :im :port :
|
|
55
|
-
@rsv_infos = {} # rsv => {:domain :im :near_id
|
|
56
|
-
@resolv_caches = {} # domain => [ip
|
|
47
|
+
@dst_infos = {} # dst => {:closing :connected :domain :im :in :ip :is_big :overflowing :port :rbuffs :src_id :wbuff}
|
|
48
|
+
@dns_infos = {} # dns => {:domain :im :port :src_id}
|
|
49
|
+
@rsv_infos = {} # rsv => {:domain :im :near_id}
|
|
50
|
+
@resolv_caches = {} # domain => [:ip :created_at]
|
|
57
51
|
@p2d_infos = {} # p2d => {:im}
|
|
58
|
-
@p2_infos = {} # p2 => {:addrinfo :closing :im :overflowing :p2_id :wbuff}
|
|
52
|
+
@p2_infos = {} # p2 => {:addrinfo :closing :im :in :is_big :overflowing :p2_id :wbuff}
|
|
59
53
|
|
|
60
54
|
@head_len = head_len
|
|
61
55
|
@h_a_new_source = h_a_new_source
|
|
@@ -65,21 +59,12 @@ module Girl
|
|
|
65
59
|
@h_p1_close = h_p1_close
|
|
66
60
|
@h_p2_close = h_p2_close
|
|
67
61
|
@h_p2_traffic = h_p2_traffic
|
|
68
|
-
@h_p1_overflow = h_p1_overflow
|
|
69
|
-
@h_p1_underhalf = h_p1_underhalf
|
|
70
|
-
@h_p2_overflow = h_p2_overflow
|
|
71
|
-
@h_p2_underhalf = h_p2_underhalf
|
|
72
62
|
@h_query = h_query
|
|
73
63
|
@h_response = h_response
|
|
74
64
|
@h_src_close = h_src_close
|
|
75
65
|
@h_traffic = h_traffic
|
|
76
|
-
@h_src_overflow = h_src_overflow
|
|
77
|
-
@h_src_underhalf = h_src_underhalf
|
|
78
|
-
@h_dst_overflow = h_dst_overflow
|
|
79
|
-
@h_dst_underhalf = h_dst_underhalf
|
|
80
66
|
@expire_connecting = expire_connecting
|
|
81
67
|
@expire_long_after = expire_long_after
|
|
82
|
-
@expire_proxy_after = expire_proxy_after
|
|
83
68
|
@expire_resolv_cache = expire_resolv_cache
|
|
84
69
|
@expire_short_after = expire_short_after
|
|
85
70
|
@is_debug = is_debug
|
|
@@ -87,6 +72,7 @@ module Girl
|
|
|
87
72
|
|
|
88
73
|
init_im_infos(ims, p2d_host, p2d_port)
|
|
89
74
|
new_a_proxyd(proxyd_port)
|
|
75
|
+
new_a_bigd(bigd_port)
|
|
90
76
|
new_a_infod(proxyd_port)
|
|
91
77
|
new_a_memd(memd_port)
|
|
92
78
|
end
|
|
@@ -103,6 +89,10 @@ module Girl
|
|
|
103
89
|
role = @roles[sock]
|
|
104
90
|
|
|
105
91
|
case role
|
|
92
|
+
when :big
|
|
93
|
+
read_big(sock)
|
|
94
|
+
when :bigd
|
|
95
|
+
read_bigd(sock)
|
|
106
96
|
when :dns
|
|
107
97
|
read_dns(sock)
|
|
108
98
|
when :dst
|
|
@@ -132,6 +122,8 @@ module Girl
|
|
|
132
122
|
role = @roles[sock]
|
|
133
123
|
|
|
134
124
|
case role
|
|
125
|
+
when :big
|
|
126
|
+
write_big(sock)
|
|
135
127
|
when :dst
|
|
136
128
|
write_dst(sock)
|
|
137
129
|
when :mem
|
|
@@ -156,27 +148,64 @@ module Girl
|
|
|
156
148
|
|
|
157
149
|
private
|
|
158
150
|
|
|
151
|
+
def add_big_wbuff(big, data)
|
|
152
|
+
return if big.nil? || big.closed? || data.nil? || data.empty?
|
|
153
|
+
big_info = @big_infos[big]
|
|
154
|
+
big_info[:wbuff] << data
|
|
155
|
+
bytesize = big_info[:wbuff].bytesize
|
|
156
|
+
im = big_info[:im]
|
|
157
|
+
|
|
158
|
+
if bytesize >= CLOSE_ABOVE
|
|
159
|
+
puts "close overflow big #{im}"
|
|
160
|
+
close_big(big)
|
|
161
|
+
return
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
if !big_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
165
|
+
puts "big overflow #{im}"
|
|
166
|
+
big_info[:overflowing] = true
|
|
167
|
+
|
|
168
|
+
@dst_infos.select{|_, info| (info[:im] == im) && info[:is_big]}.each do |dst, info|
|
|
169
|
+
puts "pause dst #{info[:domain]}"
|
|
170
|
+
@reads.delete(dst)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
@p2_infos.select{|_, info| (info[:im] == im) && info[:is_big]}.each do |p2, info|
|
|
174
|
+
puts "pause p2 #{info[:p2_id]}"
|
|
175
|
+
@reads.delete(p2)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
add_write(big)
|
|
180
|
+
end
|
|
181
|
+
|
|
159
182
|
def add_dst_wbuff(dst, data)
|
|
160
183
|
return if dst.nil? || dst.closed? || data.nil? || data.empty?
|
|
161
184
|
dst_info = @dst_infos[dst]
|
|
162
185
|
dst_info[:wbuff] << data
|
|
163
186
|
bytesize = dst_info[:wbuff].bytesize
|
|
164
187
|
im = dst_info[:im]
|
|
165
|
-
src_id = dst_info[:src_id]
|
|
166
188
|
domain = dst_info[:domain]
|
|
167
189
|
|
|
168
190
|
if bytesize >= CLOSE_ABOVE
|
|
169
|
-
puts "close overflow dst #{
|
|
191
|
+
puts "close overflow dst #{im} #{domain}"
|
|
170
192
|
close_dst(dst)
|
|
171
193
|
return
|
|
172
194
|
end
|
|
173
195
|
|
|
174
196
|
if !dst_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
175
|
-
|
|
176
|
-
puts "add h_dst_overflow #{im} #{src_id} #{domain}"
|
|
177
|
-
msg = "#{@h_dst_overflow}#{[src_id].pack('Q>')}"
|
|
178
|
-
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
197
|
+
puts "dst overflow #{im} #{domain}"
|
|
179
198
|
dst_info[:overflowing] = true
|
|
199
|
+
im_info = @im_infos[im]
|
|
200
|
+
|
|
201
|
+
if im_info
|
|
202
|
+
big = im_info[:big]
|
|
203
|
+
|
|
204
|
+
if big
|
|
205
|
+
puts 'pause big'
|
|
206
|
+
@reads.delete(big)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
180
209
|
end
|
|
181
210
|
|
|
182
211
|
add_write(dst)
|
|
@@ -192,30 +221,30 @@ module Girl
|
|
|
192
221
|
def add_p2_wbuff(p2, data)
|
|
193
222
|
return if p2.nil? || p2.closed? || data.nil? || data.empty?
|
|
194
223
|
p2_info = @p2_infos[p2]
|
|
195
|
-
im = p2_info[:im]
|
|
196
|
-
im_info = @im_infos[im]
|
|
197
|
-
|
|
198
|
-
unless im_info
|
|
199
|
-
close_p2(p2)
|
|
200
|
-
return
|
|
201
|
-
end
|
|
202
|
-
|
|
203
224
|
p2_info[:wbuff] << data
|
|
204
225
|
bytesize = p2_info[:wbuff].bytesize
|
|
226
|
+
im = p2_info[:im]
|
|
205
227
|
p2_id = p2_info[:p2_id]
|
|
206
228
|
|
|
207
229
|
if bytesize >= CLOSE_ABOVE
|
|
208
|
-
puts "close overflow p2 #{p2_id}"
|
|
230
|
+
puts "close overflow p2 #{im} #{p2_id}"
|
|
209
231
|
close_p2(p2)
|
|
210
232
|
return
|
|
211
233
|
end
|
|
212
234
|
|
|
213
235
|
if !p2_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
214
|
-
|
|
215
|
-
puts "add h_p2_overflow #{im} #{p2_id}"
|
|
216
|
-
msg = "#{@h_p2_overflow}#{[p2_id].pack('Q>')}"
|
|
217
|
-
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
236
|
+
puts "p2 overflow #{im} #{p2_id}"
|
|
218
237
|
p2_info[:overflowing] = true
|
|
238
|
+
im_info = @im_infos[im]
|
|
239
|
+
|
|
240
|
+
if im_info
|
|
241
|
+
big = im_info[:big]
|
|
242
|
+
|
|
243
|
+
if big
|
|
244
|
+
puts 'pause big'
|
|
245
|
+
@reads.delete(big)
|
|
246
|
+
end
|
|
247
|
+
end
|
|
219
248
|
end
|
|
220
249
|
|
|
221
250
|
add_write(p2)
|
|
@@ -226,9 +255,10 @@ module Girl
|
|
|
226
255
|
proxy_info = @proxy_infos[proxy]
|
|
227
256
|
proxy_info[:wbuff] << data
|
|
228
257
|
bytesize = proxy_info[:wbuff].bytesize
|
|
258
|
+
im = proxy_info[:im]
|
|
229
259
|
|
|
230
260
|
if bytesize >= CLOSE_ABOVE
|
|
231
|
-
puts "close overflow proxy #{
|
|
261
|
+
puts "close overflow proxy #{im}"
|
|
232
262
|
close_proxy(proxy)
|
|
233
263
|
return
|
|
234
264
|
end
|
|
@@ -251,6 +281,27 @@ module Girl
|
|
|
251
281
|
end
|
|
252
282
|
end
|
|
253
283
|
|
|
284
|
+
def add_src_rbuff(src_id, data)
|
|
285
|
+
src_info = @src_infos[src_id]
|
|
286
|
+
|
|
287
|
+
if src_info
|
|
288
|
+
im = src_info[:im]
|
|
289
|
+
dst = src_info[:dst]
|
|
290
|
+
|
|
291
|
+
if dst
|
|
292
|
+
add_dst_wbuff(dst, data)
|
|
293
|
+
else
|
|
294
|
+
puts "add src rbuff #{im} #{data.bytesize}" if @is_debug
|
|
295
|
+
src_info[:rbuff] << data
|
|
296
|
+
|
|
297
|
+
if src_info[:rbuff].bytesize >= WBUFF_LIMIT
|
|
298
|
+
puts "src rbuff full"
|
|
299
|
+
@src_infos.delete(src_id)
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
254
305
|
def add_write(sock)
|
|
255
306
|
return if sock.nil? || sock.closed? || @writes.include?(sock)
|
|
256
307
|
@writes << sock
|
|
@@ -258,6 +309,15 @@ module Girl
|
|
|
258
309
|
set_update(sock) if @update_roles.include?(role)
|
|
259
310
|
end
|
|
260
311
|
|
|
312
|
+
def check_expire_bigs
|
|
313
|
+
now = Time.new
|
|
314
|
+
|
|
315
|
+
@big_infos.select{|big, _| now.to_i - @updates[big].to_i >= @expire_long_after}.each do |big, info|
|
|
316
|
+
puts "expire big #{info[:im]}"
|
|
317
|
+
close_big(big)
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
261
321
|
def check_expire_dnses
|
|
262
322
|
now = Time.new
|
|
263
323
|
|
|
@@ -312,23 +372,40 @@ module Girl
|
|
|
312
372
|
end
|
|
313
373
|
end
|
|
314
374
|
|
|
315
|
-
def check_expire_srcs
|
|
316
|
-
return if proxy.nil? || proxy.closed?
|
|
317
|
-
proxy_info = @proxy_infos[proxy]
|
|
318
|
-
im = proxy_info[:im]
|
|
375
|
+
def check_expire_srcs
|
|
319
376
|
now = Time.new
|
|
320
377
|
|
|
321
|
-
|
|
322
|
-
puts "expire src info #{im} #{src_id}" if @is_debug
|
|
323
|
-
|
|
378
|
+
@src_infos.select{|_, info| info[:dst].nil? && (now.to_i - info[:created_at].to_i >= @expire_short_after)}.each do |src_id, info|
|
|
379
|
+
puts "expire src info #{info[:im]} #{src_id}" if @is_debug
|
|
380
|
+
@src_infos.delete(src_id)
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def close_big(big)
|
|
385
|
+
return nil if big.nil? || big.closed?
|
|
386
|
+
close_sock(big)
|
|
387
|
+
big_info = @big_infos.delete(big)
|
|
388
|
+
|
|
389
|
+
if big_info
|
|
390
|
+
addrinfo = big_info[:addrinfo].ip_unpack.inspect
|
|
391
|
+
im = big_info[:im]
|
|
392
|
+
puts "close big #{addrinfo} #{im}" if @is_debug
|
|
324
393
|
end
|
|
394
|
+
|
|
395
|
+
big_info
|
|
325
396
|
end
|
|
326
397
|
|
|
327
398
|
def close_dns(dns)
|
|
328
399
|
return nil if dns.nil? || dns.closed?
|
|
329
400
|
close_sock(dns)
|
|
330
401
|
dns_info = @dns_infos.delete(dns)
|
|
331
|
-
|
|
402
|
+
|
|
403
|
+
if dns_info
|
|
404
|
+
im = dns_info[:im]
|
|
405
|
+
domain = dns_info[:domain]
|
|
406
|
+
puts "close dns #{im} #{domain}" if @is_debug
|
|
407
|
+
end
|
|
408
|
+
|
|
332
409
|
dns_info
|
|
333
410
|
end
|
|
334
411
|
|
|
@@ -336,19 +413,20 @@ module Girl
|
|
|
336
413
|
return nil if dst.nil? || dst.closed?
|
|
337
414
|
close_sock(dst)
|
|
338
415
|
dst_info = @dst_infos.delete(dst)
|
|
339
|
-
im = dst_info[:im]
|
|
340
|
-
src_id = dst_info[:src_id]
|
|
341
|
-
domain = dst_info[:domain]
|
|
342
|
-
puts "close dst #{im} #{src_id} #{domain}" if @is_debug
|
|
343
|
-
proxy = dst_info[:proxy]
|
|
344
416
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
417
|
+
if dst_info
|
|
418
|
+
im = dst_info[:im]
|
|
419
|
+
src_id = dst_info[:src_id]
|
|
420
|
+
domain = dst_info[:domain]
|
|
421
|
+
@src_infos.delete(src_id)
|
|
422
|
+
puts "close dst #{im} #{domain}" if @is_debug
|
|
423
|
+
im_info = @im_infos[im]
|
|
424
|
+
|
|
425
|
+
if im_info
|
|
426
|
+
puts "add h_dst_close #{im} #{domain} #{src_id}" if @is_debug
|
|
427
|
+
msg = "#{@h_dst_close}#{[src_id].pack('Q>')}"
|
|
428
|
+
add_proxy_wbuff(im_info[:proxy], pack_a_chunk(msg))
|
|
429
|
+
end
|
|
352
430
|
end
|
|
353
431
|
|
|
354
432
|
dst_info
|
|
@@ -364,17 +442,15 @@ module Girl
|
|
|
364
442
|
return nil if p2.nil? || p2.closed?
|
|
365
443
|
close_sock(p2)
|
|
366
444
|
p2_info = @p2_infos.delete(p2)
|
|
367
|
-
im = p2_info[:im]
|
|
368
|
-
p2_id = p2_info[:p2_id]
|
|
369
|
-
puts "close p2 #{im} #{p2_id}"
|
|
370
|
-
im_info = @im_infos[im]
|
|
371
445
|
|
|
372
|
-
if
|
|
373
|
-
|
|
446
|
+
if p2_info
|
|
447
|
+
im = p2_info[:im]
|
|
448
|
+
p2_id = p2_info[:p2_id]
|
|
449
|
+
puts "close p2 #{im} #{p2_id}"
|
|
450
|
+
im_info = @im_infos[im]
|
|
374
451
|
|
|
375
|
-
if
|
|
376
|
-
|
|
377
|
-
proxy_info[:paused_p2s].delete(p2)
|
|
452
|
+
if im_info
|
|
453
|
+
proxy = im_info[:proxy]
|
|
378
454
|
puts "add h_p2_close #{im} #{p2_id}"
|
|
379
455
|
msg = "#{@h_p2_close}#{[p2_id].pack('Q>')}"
|
|
380
456
|
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
@@ -388,10 +464,15 @@ module Girl
|
|
|
388
464
|
return nil if proxy.nil? || proxy.closed?
|
|
389
465
|
close_sock(proxy)
|
|
390
466
|
proxy_info = @proxy_infos.delete(proxy)
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
467
|
+
|
|
468
|
+
if proxy_info
|
|
469
|
+
addrinfo = proxy_info[:addrinfo].ip_unpack.inspect
|
|
470
|
+
im = proxy_info[:im]
|
|
471
|
+
puts "close proxy #{addrinfo} #{im}" if @is_debug
|
|
472
|
+
@dst_infos.select{|_, info| info[:im] == im}.each{|dst, _| close_dst(dst)}
|
|
473
|
+
@p2_infos.select{|_, info| info[:im] == im}.each{|p2, _| close_p2(p2)}
|
|
474
|
+
end
|
|
475
|
+
|
|
395
476
|
proxy_info
|
|
396
477
|
end
|
|
397
478
|
|
|
@@ -399,7 +480,13 @@ module Girl
|
|
|
399
480
|
return nil if rsv.nil? || rsv.closed?
|
|
400
481
|
close_sock(rsv)
|
|
401
482
|
rsv_info = @rsv_infos.delete(rsv)
|
|
402
|
-
|
|
483
|
+
|
|
484
|
+
if rsv_info
|
|
485
|
+
im = rsv_info[:im]
|
|
486
|
+
domain = rsv_info[:domain]
|
|
487
|
+
puts "close rsv #{im} #{domain}" if @is_debug
|
|
488
|
+
end
|
|
489
|
+
|
|
403
490
|
rsv_info
|
|
404
491
|
end
|
|
405
492
|
|
|
@@ -412,6 +499,30 @@ module Girl
|
|
|
412
499
|
@roles.delete(sock)
|
|
413
500
|
end
|
|
414
501
|
|
|
502
|
+
def deal_big_msg(data, big)
|
|
503
|
+
return if data.nil? || data.empty? || big.nil? || big.closed?
|
|
504
|
+
big_info = @big_infos[big]
|
|
505
|
+
im = big_info[:im]
|
|
506
|
+
return unless im
|
|
507
|
+
h = data[0]
|
|
508
|
+
|
|
509
|
+
case h
|
|
510
|
+
when @h_p2_traffic
|
|
511
|
+
return if data.bytesize < 9
|
|
512
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
513
|
+
data = data[9..-1]
|
|
514
|
+
# puts "big got h_p2_traffic #{im} #{p2_id} #{data.bytesize}" if @is_debug
|
|
515
|
+
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
516
|
+
add_p2_wbuff(p2, data)
|
|
517
|
+
when @h_traffic
|
|
518
|
+
return if data.bytesize < 9
|
|
519
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
520
|
+
data = data[9..-1]
|
|
521
|
+
# puts "big got h_traffic #{im} #{src_id} #{data.bytesize}" if @is_debug
|
|
522
|
+
add_src_rbuff(src_id, data)
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
|
|
415
526
|
def deal_msg(data, proxy)
|
|
416
527
|
return if data.nil? || data.empty? || proxy.nil? || proxy.closed?
|
|
417
528
|
proxy_info = @proxy_infos[proxy]
|
|
@@ -422,19 +533,17 @@ module Girl
|
|
|
422
533
|
case h
|
|
423
534
|
when @h_a_new_source
|
|
424
535
|
return if data.bytesize < 9
|
|
425
|
-
check_expire_srcs
|
|
536
|
+
check_expire_srcs
|
|
426
537
|
src_id = data[1, 8].unpack('Q>').first
|
|
427
538
|
domain_port = data[9..-1]
|
|
428
539
|
puts "got h_a_new_source #{im} #{src_id} #{domain_port.inspect}" if @is_debug
|
|
429
|
-
|
|
430
|
-
src_info = {
|
|
540
|
+
@src_infos[src_id] = {
|
|
431
541
|
created_at: Time.new,
|
|
432
542
|
dst: nil,
|
|
543
|
+
im: im,
|
|
433
544
|
rbuff: ''
|
|
434
545
|
}
|
|
435
|
-
|
|
436
|
-
proxy_info[:src_infos][src_id] = src_info
|
|
437
|
-
resolve_domain_port(domain_port, src_id, proxy, im)
|
|
546
|
+
resolve_domain_port(domain_port, src_id)
|
|
438
547
|
when @h_p1_close
|
|
439
548
|
return if data.bytesize < 9
|
|
440
549
|
p2_id = data[1, 8].unpack('Q>').first
|
|
@@ -445,22 +554,8 @@ module Girl
|
|
|
445
554
|
return if data.bytesize < 9
|
|
446
555
|
p2_id = data[1, 8].unpack('Q>').first
|
|
447
556
|
data = data[9..-1]
|
|
448
|
-
# puts "got h_p2_traffic #{im} #{p2_id} #{data.bytesize}" if @is_debug
|
|
449
557
|
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
450
558
|
add_p2_wbuff(p2, data)
|
|
451
|
-
when @h_p1_overflow
|
|
452
|
-
return if data.bytesize < 9
|
|
453
|
-
p2_id = data[1, 8].unpack('Q>').first
|
|
454
|
-
puts "got h_p1_overflow pause p2 #{im} #{p2_id}"
|
|
455
|
-
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
456
|
-
@reads.delete(p2)
|
|
457
|
-
proxy_info[:paused_p2s].delete(p2)
|
|
458
|
-
when @h_p1_underhalf
|
|
459
|
-
return if data.bytesize < 9
|
|
460
|
-
p2_id = data[1, 8].unpack('Q>').first
|
|
461
|
-
puts "got h_p1_underhalf #{im} #{p2_id}"
|
|
462
|
-
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
463
|
-
add_read(p2)
|
|
464
559
|
when @h_query
|
|
465
560
|
return if data.bytesize < 10
|
|
466
561
|
near_id, type = data[1, 9].unpack('Q>C')
|
|
@@ -473,49 +568,13 @@ module Girl
|
|
|
473
568
|
return if data.bytesize < 9
|
|
474
569
|
src_id = data[1, 8].unpack('Q>').first
|
|
475
570
|
puts "got h_src_close #{im} #{src_id}" if @is_debug
|
|
476
|
-
src_info =
|
|
571
|
+
src_info = @src_infos.delete(src_id)
|
|
477
572
|
set_dst_closing(src_info[:dst]) if src_info
|
|
478
573
|
when @h_traffic
|
|
479
574
|
return if data.bytesize < 9
|
|
480
575
|
src_id = data[1, 8].unpack('Q>').first
|
|
481
576
|
data = data[9..-1]
|
|
482
|
-
|
|
483
|
-
src_info = proxy_info[:src_infos][src_id]
|
|
484
|
-
|
|
485
|
-
if src_info
|
|
486
|
-
dst = src_info[:dst]
|
|
487
|
-
|
|
488
|
-
if dst
|
|
489
|
-
add_dst_wbuff(dst, data)
|
|
490
|
-
else
|
|
491
|
-
puts "add src rbuff #{im} #{data.bytesize}" if @is_debug
|
|
492
|
-
src_info[:rbuff] << data
|
|
493
|
-
|
|
494
|
-
if src_info[:rbuff].bytesize >= WBUFF_LIMIT
|
|
495
|
-
puts "src rbuff full"
|
|
496
|
-
close_proxy(proxy)
|
|
497
|
-
end
|
|
498
|
-
end
|
|
499
|
-
end
|
|
500
|
-
when @h_src_overflow
|
|
501
|
-
return if data.bytesize < 9
|
|
502
|
-
src_id = data[1, 8].unpack('Q>').first
|
|
503
|
-
puts "got h_src_overflow pause dst #{im} #{src_id}"
|
|
504
|
-
src_info = proxy_info[:src_infos][src_id]
|
|
505
|
-
|
|
506
|
-
if src_info
|
|
507
|
-
dst = src_info[:dst]
|
|
508
|
-
@reads.delete(dst)
|
|
509
|
-
# 远端收取目的地比传给近端快,近端收取远端又比传给客户端快(2g wifi),流量即在远端proxy堆积,又在近端src堆积
|
|
510
|
-
# 这种情况只等待近端src降半,等到降半消息才恢复读dst,忽略proxy降半
|
|
511
|
-
proxy_info[:paused_dsts].delete(dst)
|
|
512
|
-
end
|
|
513
|
-
when @h_src_underhalf
|
|
514
|
-
return if data.bytesize < 9
|
|
515
|
-
src_id = data[1, 8].unpack('Q>').first
|
|
516
|
-
puts "got h_src_underhalf #{im} #{src_id}"
|
|
517
|
-
src_info = proxy_info[:src_infos][src_id]
|
|
518
|
-
add_read(src_info[:dst]) if src_info
|
|
577
|
+
add_src_rbuff(src_id, data)
|
|
519
578
|
end
|
|
520
579
|
end
|
|
521
580
|
|
|
@@ -553,6 +612,7 @@ module Girl
|
|
|
553
612
|
ims.sort.each_with_index do |im, i|
|
|
554
613
|
@im_infos[im] = {
|
|
555
614
|
addrinfo: nil,
|
|
615
|
+
big: nil,
|
|
556
616
|
in: 0,
|
|
557
617
|
out: 0,
|
|
558
618
|
p2d: nil,
|
|
@@ -589,13 +649,22 @@ module Girl
|
|
|
589
649
|
end
|
|
590
650
|
end
|
|
591
651
|
|
|
592
|
-
def
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
652
|
+
def new_a_bigd(bigd_port)
|
|
653
|
+
bigd_host = '0.0.0.0'
|
|
654
|
+
bigd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
655
|
+
bigd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
656
|
+
bigd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
657
|
+
bigd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, BACKLOG) if @is_server_fastopen
|
|
658
|
+
bigd.bind(Socket.sockaddr_in(bigd_port, bigd_host))
|
|
659
|
+
bigd.listen(BACKLOG)
|
|
660
|
+
puts "bigd listen on #{bigd_host} #{bigd_port}"
|
|
661
|
+
add_read(bigd, :bigd)
|
|
662
|
+
end
|
|
598
663
|
|
|
664
|
+
def new_a_dst(domain, ip, port, src_id)
|
|
665
|
+
src_info = @src_infos[src_id]
|
|
666
|
+
return unless src_info
|
|
667
|
+
im = src_info[:im]
|
|
599
668
|
check_expire_dsts
|
|
600
669
|
|
|
601
670
|
begin
|
|
@@ -622,10 +691,11 @@ module Girl
|
|
|
622
691
|
connected: false,
|
|
623
692
|
domain: domain,
|
|
624
693
|
im: im,
|
|
694
|
+
in: 0,
|
|
625
695
|
ip: ip,
|
|
696
|
+
is_big: false, # 收来的流量是否是大流量
|
|
626
697
|
overflowing: false,
|
|
627
698
|
port: port,
|
|
628
|
-
proxy: proxy,
|
|
629
699
|
rbuffs: [],
|
|
630
700
|
src_id: src_id,
|
|
631
701
|
wbuff: src_info[:rbuff].dup
|
|
@@ -682,18 +752,15 @@ module Girl
|
|
|
682
752
|
return
|
|
683
753
|
end
|
|
684
754
|
|
|
685
|
-
|
|
755
|
+
@rsv_infos[rsv] = {
|
|
686
756
|
domain: domain,
|
|
687
757
|
im: im,
|
|
688
|
-
near_id: near_id
|
|
689
|
-
proxy: proxy
|
|
758
|
+
near_id: near_id
|
|
690
759
|
}
|
|
691
|
-
|
|
692
|
-
@rsv_infos[rsv] = rsv_info
|
|
693
760
|
add_read(rsv, :rsv)
|
|
694
761
|
end
|
|
695
762
|
|
|
696
|
-
def new_a_p2d(p2d_host, p2d_port, im
|
|
763
|
+
def new_a_p2d(p2d_host, p2d_port, im)
|
|
697
764
|
begin
|
|
698
765
|
p2d = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
699
766
|
p2d.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
@@ -757,6 +824,100 @@ module Girl
|
|
|
757
824
|
chunks
|
|
758
825
|
end
|
|
759
826
|
|
|
827
|
+
def read_big(big)
|
|
828
|
+
begin
|
|
829
|
+
data = big.read_nonblock(READ_SIZE)
|
|
830
|
+
rescue Errno::ENOTCONN => e
|
|
831
|
+
return
|
|
832
|
+
rescue Exception => e
|
|
833
|
+
puts "read big #{e.class}" if @is_debug
|
|
834
|
+
close_big(big)
|
|
835
|
+
return
|
|
836
|
+
end
|
|
837
|
+
|
|
838
|
+
set_update(big)
|
|
839
|
+
big_info = @big_infos[big]
|
|
840
|
+
im = big_info[:im]
|
|
841
|
+
data = "#{big_info[:rbuff]}#{data}"
|
|
842
|
+
|
|
843
|
+
unless im
|
|
844
|
+
if data.bytesize < @head_len + 1
|
|
845
|
+
big_info[:rbuff] = data
|
|
846
|
+
return
|
|
847
|
+
end
|
|
848
|
+
|
|
849
|
+
len = data[@head_len].unpack('C').first
|
|
850
|
+
|
|
851
|
+
if len == 0
|
|
852
|
+
puts "im zero len?"
|
|
853
|
+
return
|
|
854
|
+
end
|
|
855
|
+
|
|
856
|
+
if data.bytesize < @head_len + 1 + len
|
|
857
|
+
big_info[:rbuff] = data
|
|
858
|
+
return
|
|
859
|
+
end
|
|
860
|
+
|
|
861
|
+
im = data[@head_len + 1, len]
|
|
862
|
+
|
|
863
|
+
if @im_infos.any?
|
|
864
|
+
im_info = @im_infos[im]
|
|
865
|
+
|
|
866
|
+
unless im_info
|
|
867
|
+
puts "unknown im #{im.inspect}"
|
|
868
|
+
return
|
|
869
|
+
end
|
|
870
|
+
|
|
871
|
+
if im_info[:big] && !im_info[:big].closed?
|
|
872
|
+
puts "big already alive #{im.inspect}"
|
|
873
|
+
return
|
|
874
|
+
end
|
|
875
|
+
end
|
|
876
|
+
|
|
877
|
+
puts "big got im #{im}"
|
|
878
|
+
big_info[:im] = im
|
|
879
|
+
|
|
880
|
+
if im_info
|
|
881
|
+
im_info[:big] = big
|
|
882
|
+
im_info[:big_connect_at] = Time.new
|
|
883
|
+
end
|
|
884
|
+
|
|
885
|
+
add_big_wbuff(big, pack_a_chunk(@h_heartbeat))
|
|
886
|
+
data = data[(@head_len + 1 + len)..-1]
|
|
887
|
+
return if data.empty?
|
|
888
|
+
end
|
|
889
|
+
|
|
890
|
+
im_info = @im_infos[im]
|
|
891
|
+
im_info[:in] += data.bytesize if im_info
|
|
892
|
+
msgs, part = decode_to_msgs(data)
|
|
893
|
+
msgs.each{|msg| deal_big_msg(msg, big)}
|
|
894
|
+
big_info[:rbuff] = part
|
|
895
|
+
end
|
|
896
|
+
|
|
897
|
+
def read_bigd(bigd)
|
|
898
|
+
check_expire_bigs
|
|
899
|
+
|
|
900
|
+
begin
|
|
901
|
+
big, addrinfo = bigd.accept_nonblock
|
|
902
|
+
rescue Exception => e
|
|
903
|
+
puts "accept a big #{e.class}"
|
|
904
|
+
return
|
|
905
|
+
end
|
|
906
|
+
|
|
907
|
+
puts "accept a big #{addrinfo.ip_unpack.inspect}"
|
|
908
|
+
|
|
909
|
+
big_info = {
|
|
910
|
+
addrinfo: addrinfo,
|
|
911
|
+
im: nil,
|
|
912
|
+
overflowing: false,
|
|
913
|
+
rbuff: '',
|
|
914
|
+
wbuff: ''
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
@big_infos[big] = big_info
|
|
918
|
+
add_read(big, :big)
|
|
919
|
+
end
|
|
920
|
+
|
|
760
921
|
def read_dns(dns)
|
|
761
922
|
begin
|
|
762
923
|
data, addrinfo, rflags, *controls = dns.recvmsg
|
|
@@ -782,11 +943,10 @@ module Girl
|
|
|
782
943
|
if ip
|
|
783
944
|
port = dns_info[:port]
|
|
784
945
|
src_id = dns_info[:src_id]
|
|
785
|
-
proxy = dns_info[:proxy]
|
|
786
946
|
im = dns_info[:im]
|
|
787
947
|
puts "got ip #{im} #{domain} #{ip}" if @is_debug
|
|
788
|
-
new_a_dst(domain, ip, port, src_id
|
|
789
|
-
@resolv_caches[domain] = [ip, Time.new
|
|
948
|
+
new_a_dst(domain, ip, port, src_id)
|
|
949
|
+
@resolv_caches[domain] = [ip, Time.new]
|
|
790
950
|
else
|
|
791
951
|
puts "no ip in answer #{domain}" if @is_debug
|
|
792
952
|
end
|
|
@@ -800,34 +960,38 @@ module Girl
|
|
|
800
960
|
rescue Errno::ENOTCONN => e
|
|
801
961
|
return
|
|
802
962
|
rescue Exception => e
|
|
963
|
+
puts "read dst #{e.class}" if @is_debug
|
|
803
964
|
close_dst(dst)
|
|
804
965
|
return
|
|
805
966
|
end
|
|
806
967
|
|
|
807
968
|
set_update(dst)
|
|
808
969
|
dst_info = @dst_infos[dst]
|
|
809
|
-
|
|
970
|
+
dst_info[:in] += data.bytesize
|
|
971
|
+
im = dst_info[:im]
|
|
972
|
+
|
|
973
|
+
if !dst_info[:is_big] && (dst_info[:in] >= READ_SIZE)
|
|
974
|
+
puts "set dst is big #{im} #{dst_info[:domain]}"
|
|
975
|
+
dst_info[:is_big] = true
|
|
976
|
+
end
|
|
810
977
|
|
|
811
|
-
|
|
978
|
+
im_info = @im_infos[im]
|
|
979
|
+
|
|
980
|
+
unless im_info
|
|
812
981
|
close_dst(dst)
|
|
813
982
|
return
|
|
814
983
|
end
|
|
815
984
|
|
|
816
|
-
|
|
817
|
-
im_info = @im_infos[im]
|
|
818
|
-
im_info[:in] += data.bytesize if im_info
|
|
985
|
+
im_info[:in] += data.bytesize
|
|
819
986
|
src_id = dst_info[:src_id]
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
unless proxy.closed?
|
|
823
|
-
proxy_info = @proxy_infos[proxy]
|
|
824
|
-
bytesize = proxy_info[:wbuff].bytesize
|
|
987
|
+
data = pack_traffic(src_id, data)
|
|
825
988
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
989
|
+
if dst_info[:is_big]
|
|
990
|
+
big = im_info[:big]
|
|
991
|
+
add_big_wbuff(big, data)
|
|
992
|
+
else
|
|
993
|
+
proxy = im_info[:proxy]
|
|
994
|
+
add_proxy_wbuff(proxy, data)
|
|
831
995
|
end
|
|
832
996
|
end
|
|
833
997
|
|
|
@@ -853,6 +1017,7 @@ module Girl
|
|
|
853
1017
|
case message_type
|
|
854
1018
|
when 'heartbeat'
|
|
855
1019
|
@proxy_infos.select{|_, info| info[:im]}.each{|proxy, _| add_proxy_wbuff(proxy, pack_a_chunk(@h_heartbeat))}
|
|
1020
|
+
@big_infos.select{|_, info| info[:im]}.each{|big, _| add_big_wbuff(big, pack_a_chunk(@h_heartbeat))}
|
|
856
1021
|
when 'reset-traffic'
|
|
857
1022
|
puts "reset traffic"
|
|
858
1023
|
@im_infos.each{|_, info| info[:in] = info[:out] = 0}
|
|
@@ -875,24 +1040,27 @@ module Girl
|
|
|
875
1040
|
@im_infos.select{|_, info| info[:addrinfo]}.sort.each do |im, info|
|
|
876
1041
|
im_arr << {
|
|
877
1042
|
im: im,
|
|
1043
|
+
p2d_port: info[:p2d_port],
|
|
1044
|
+
p2d_host: info[:p2d_host],
|
|
878
1045
|
addrinfo: info[:addrinfo].ip_unpack,
|
|
879
1046
|
in: info[:in],
|
|
880
1047
|
out: info[:out],
|
|
881
|
-
|
|
882
|
-
|
|
1048
|
+
proxy_connect_at: info[:proxy_connect_at] ? info[:proxy_connect_at].strftime('%F %T') : '--',
|
|
1049
|
+
big_connect_at: info[:big_connect_at] ? info[:big_connect_at].strftime('%F %T') : '--'
|
|
883
1050
|
}
|
|
884
1051
|
end
|
|
885
1052
|
|
|
886
1053
|
msg = {
|
|
887
|
-
resolv_caches: @resolv_caches.sort,
|
|
888
1054
|
sizes: {
|
|
889
1055
|
reads: @reads.size,
|
|
890
1056
|
writes: @writes.size,
|
|
891
1057
|
roles: @roles.size,
|
|
892
1058
|
updates: @updates.size,
|
|
893
|
-
proxy_infos: @proxy_infos.size,
|
|
894
1059
|
im_infos: @im_infos.size,
|
|
1060
|
+
proxy_infos: @proxy_infos.size,
|
|
1061
|
+
big_infos: @big_infos.size,
|
|
895
1062
|
mem_infos: @mem_infos.size,
|
|
1063
|
+
src_infos: @src_infos.size,
|
|
896
1064
|
dst_infos: @dst_infos.size,
|
|
897
1065
|
dns_infos: @dns_infos.size,
|
|
898
1066
|
rsv_infos: @rsv_infos.size,
|
|
@@ -932,14 +1100,22 @@ module Girl
|
|
|
932
1100
|
rescue Errno::ENOTCONN => e
|
|
933
1101
|
return
|
|
934
1102
|
rescue Exception => e
|
|
1103
|
+
puts "read p2 #{e.class}" if @is_debug
|
|
935
1104
|
close_p2(p2)
|
|
936
1105
|
return
|
|
937
1106
|
end
|
|
938
1107
|
|
|
939
1108
|
set_update(p2)
|
|
940
1109
|
p2_info = @p2_infos[p2]
|
|
1110
|
+
p2_info[:in] += data.bytesize
|
|
941
1111
|
im = p2_info[:im]
|
|
942
|
-
|
|
1112
|
+
p2_id = p2_info[:p2_id]
|
|
1113
|
+
|
|
1114
|
+
if !p2_info[:is_big] && (p2_info[:in] >= READ_SIZE)
|
|
1115
|
+
puts "set p2 is big #{im} #{p2_id}"
|
|
1116
|
+
p2_info[:is_big] = true
|
|
1117
|
+
end
|
|
1118
|
+
|
|
943
1119
|
im_info = @im_infos[im]
|
|
944
1120
|
|
|
945
1121
|
unless im_info
|
|
@@ -947,19 +1123,14 @@ module Girl
|
|
|
947
1123
|
return
|
|
948
1124
|
end
|
|
949
1125
|
|
|
950
|
-
|
|
951
|
-
p2_id = p2_info[:p2_id]
|
|
952
|
-
add_proxy_wbuff(proxy, pack_p2_traffic(p2_id, data))
|
|
953
|
-
|
|
954
|
-
unless proxy.closed?
|
|
955
|
-
proxy_info = @proxy_infos[proxy]
|
|
956
|
-
bytesize = proxy_info[:wbuff].bytesize
|
|
1126
|
+
data = pack_p2_traffic(p2_id, data)
|
|
957
1127
|
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
1128
|
+
if p2_info[:is_big]
|
|
1129
|
+
big = im_info[:big]
|
|
1130
|
+
add_big_wbuff(big, data)
|
|
1131
|
+
else
|
|
1132
|
+
proxy = im_info[:proxy]
|
|
1133
|
+
add_proxy_wbuff(proxy, data)
|
|
963
1134
|
end
|
|
964
1135
|
end
|
|
965
1136
|
|
|
@@ -976,17 +1147,16 @@ module Girl
|
|
|
976
1147
|
p2d_info = @p2d_infos[p2d]
|
|
977
1148
|
im = p2d_info[:im]
|
|
978
1149
|
p2_id = rand((2 ** 64) - 2) + 1
|
|
979
|
-
|
|
980
|
-
p2_info = {
|
|
1150
|
+
@p2_infos[p2] = {
|
|
981
1151
|
addrinfo: addrinfo,
|
|
982
1152
|
closing: false,
|
|
983
1153
|
im: im,
|
|
1154
|
+
in: 0,
|
|
1155
|
+
is_big: false, # 收来的流量是否是大流量
|
|
984
1156
|
overflowing: false,
|
|
985
1157
|
p2_id: p2_id,
|
|
986
1158
|
wbuff: ''
|
|
987
1159
|
}
|
|
988
|
-
|
|
989
|
-
@p2_infos[p2] = p2_info
|
|
990
1160
|
add_read(p2, :p2)
|
|
991
1161
|
im_info = @im_infos[im]
|
|
992
1162
|
return unless im_info
|
|
@@ -1009,11 +1179,16 @@ module Girl
|
|
|
1009
1179
|
|
|
1010
1180
|
if data.bytesize <= 65526
|
|
1011
1181
|
rsv_info = @rsv_infos[rsv]
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1182
|
+
im = rsv_info[:im]
|
|
1183
|
+
im_info = @im_infos[im]
|
|
1184
|
+
|
|
1185
|
+
if im_info
|
|
1186
|
+
proxy = im_info[:proxy]
|
|
1187
|
+
near_id = rsv_info[:near_id]
|
|
1188
|
+
puts "add h_response #{im} #{near_id} #{rsv_info[:domain]} #{data.bytesize}" if @is_debug
|
|
1189
|
+
msg = "#{@h_response}#{[near_id].pack('Q>')}#{data}"
|
|
1190
|
+
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
1191
|
+
end
|
|
1017
1192
|
else
|
|
1018
1193
|
puts "response too big? #{data.bytesize}"
|
|
1019
1194
|
end
|
|
@@ -1027,6 +1202,7 @@ module Girl
|
|
|
1027
1202
|
rescue Errno::ENOTCONN => e
|
|
1028
1203
|
return
|
|
1029
1204
|
rescue Exception => e
|
|
1205
|
+
puts "read proxy #{e.class}" if @is_debug
|
|
1030
1206
|
close_proxy(proxy)
|
|
1031
1207
|
return
|
|
1032
1208
|
end
|
|
@@ -1056,19 +1232,28 @@ module Girl
|
|
|
1056
1232
|
|
|
1057
1233
|
im = data[@head_len + 1, len]
|
|
1058
1234
|
|
|
1059
|
-
if @im_infos.any?
|
|
1060
|
-
|
|
1061
|
-
|
|
1235
|
+
if @im_infos.any?
|
|
1236
|
+
im_info = @im_infos[im]
|
|
1237
|
+
|
|
1238
|
+
unless im_info
|
|
1239
|
+
puts "unknown im #{im.inspect}"
|
|
1240
|
+
return
|
|
1241
|
+
end
|
|
1242
|
+
|
|
1243
|
+
if im_info[:proxy] && !im_info[:proxy].closed?
|
|
1244
|
+
puts "proxy already alive #{im.inspect}"
|
|
1245
|
+
return
|
|
1246
|
+
end
|
|
1062
1247
|
end
|
|
1063
1248
|
|
|
1064
|
-
puts "got im #{im}"
|
|
1249
|
+
puts "proxy got im #{im}"
|
|
1065
1250
|
proxy_info[:im] = im
|
|
1066
|
-
|
|
1067
|
-
|
|
1251
|
+
|
|
1068
1252
|
if im_info
|
|
1069
1253
|
im_info[:proxy] = proxy
|
|
1254
|
+
im_info[:proxy_connect_at] = Time.new
|
|
1070
1255
|
im_info[:addrinfo] = proxy_info[:addrinfo]
|
|
1071
|
-
im_info[:p2d] = new_a_p2d(im_info[:p2d_host], im_info[:p2d_port], im
|
|
1256
|
+
im_info[:p2d] = new_a_p2d(im_info[:p2d_host], im_info[:p2d_port], im) unless im_info[:p2d]
|
|
1072
1257
|
end
|
|
1073
1258
|
|
|
1074
1259
|
add_proxy_wbuff(proxy, pack_a_chunk(@h_heartbeat))
|
|
@@ -1095,24 +1280,22 @@ module Girl
|
|
|
1095
1280
|
|
|
1096
1281
|
puts "accept a proxy #{addrinfo.ip_unpack.inspect}"
|
|
1097
1282
|
|
|
1098
|
-
|
|
1283
|
+
@proxy_infos[proxy] = {
|
|
1099
1284
|
addrinfo: addrinfo,
|
|
1100
1285
|
im: nil,
|
|
1101
|
-
paused_dsts: [],
|
|
1102
|
-
paused_p2s: [],
|
|
1103
1286
|
rbuff: '',
|
|
1104
|
-
src_infos: {}, # src_id => {:created_at :dst :rbuff}
|
|
1105
1287
|
wbuff: ''
|
|
1106
1288
|
}
|
|
1107
|
-
|
|
1108
|
-
@proxy_infos[proxy] = proxy_info
|
|
1109
1289
|
add_read(proxy, :proxy)
|
|
1110
1290
|
end
|
|
1111
1291
|
|
|
1112
|
-
def resolve_domain_port(domain_port, src_id
|
|
1292
|
+
def resolve_domain_port(domain_port, src_id)
|
|
1113
1293
|
return if domain_port.nil? || domain_port.empty?
|
|
1114
1294
|
colon_idx = domain_port.rindex(':')
|
|
1115
1295
|
return unless colon_idx
|
|
1296
|
+
src_info = @src_infos[src_id]
|
|
1297
|
+
return unless src_info
|
|
1298
|
+
im = src_info[:im]
|
|
1116
1299
|
|
|
1117
1300
|
domain = domain_port[0...colon_idx]
|
|
1118
1301
|
port = domain_port[(colon_idx + 1)..-1].to_i
|
|
@@ -1125,17 +1308,15 @@ module Girl
|
|
|
1125
1308
|
|
|
1126
1309
|
if domain =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$/
|
|
1127
1310
|
# ipv4
|
|
1128
|
-
new_a_dst(domain, domain, port, src_id
|
|
1311
|
+
new_a_dst(domain, domain, port, src_id)
|
|
1129
1312
|
return
|
|
1130
1313
|
end
|
|
1131
1314
|
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
if resolv_cache
|
|
1135
|
-
ip, created_at, im = resolv_cache
|
|
1315
|
+
ip, created_at = @resolv_caches[domain]
|
|
1136
1316
|
|
|
1317
|
+
if ip
|
|
1137
1318
|
if Time.new - created_at < @expire_resolv_cache
|
|
1138
|
-
new_a_dst(domain, ip, port, src_id
|
|
1319
|
+
new_a_dst(domain, ip, port, src_id)
|
|
1139
1320
|
return
|
|
1140
1321
|
end
|
|
1141
1322
|
|
|
@@ -1160,15 +1341,12 @@ module Girl
|
|
|
1160
1341
|
return
|
|
1161
1342
|
end
|
|
1162
1343
|
|
|
1163
|
-
|
|
1344
|
+
@dns_infos[dns] = {
|
|
1164
1345
|
domain: domain,
|
|
1165
1346
|
im: im,
|
|
1166
1347
|
port: port,
|
|
1167
|
-
proxy: proxy,
|
|
1168
1348
|
src_id: src_id
|
|
1169
1349
|
}
|
|
1170
|
-
|
|
1171
|
-
@dns_infos[dns] = dns_info
|
|
1172
1350
|
add_read(dns, :dns)
|
|
1173
1351
|
end
|
|
1174
1352
|
|
|
@@ -1208,6 +1386,8 @@ module Girl
|
|
|
1208
1386
|
|
|
1209
1387
|
@updates.keys.each do |_sock|
|
|
1210
1388
|
case @roles[_sock]
|
|
1389
|
+
when :big
|
|
1390
|
+
close_big(_sock)
|
|
1211
1391
|
when :dns
|
|
1212
1392
|
close_dns(_sock)
|
|
1213
1393
|
when :dst
|
|
@@ -1229,13 +1409,71 @@ module Girl
|
|
|
1229
1409
|
end
|
|
1230
1410
|
end
|
|
1231
1411
|
|
|
1232
|
-
def
|
|
1233
|
-
|
|
1234
|
-
|
|
1412
|
+
def write_big(big)
|
|
1413
|
+
big_info = @big_infos[big]
|
|
1414
|
+
|
|
1415
|
+
unless big_info
|
|
1416
|
+
puts "big info not found delete big"
|
|
1417
|
+
@writes.delete(big)
|
|
1418
|
+
return
|
|
1419
|
+
end
|
|
1420
|
+
|
|
1421
|
+
im = big_info[:im]
|
|
1422
|
+
im_info = @im_infos[im]
|
|
1423
|
+
|
|
1424
|
+
if im_info.nil? || im_info[:proxy].nil?
|
|
1425
|
+
puts "proxy not found close big"
|
|
1426
|
+
close_big(big)
|
|
1427
|
+
return
|
|
1428
|
+
end
|
|
1429
|
+
|
|
1430
|
+
return if @writes.include?(im_info[:proxy])
|
|
1431
|
+
data = big_info[:wbuff]
|
|
1432
|
+
|
|
1433
|
+
if data.empty?
|
|
1434
|
+
@writes.delete(big)
|
|
1235
1435
|
return
|
|
1236
1436
|
end
|
|
1237
1437
|
|
|
1438
|
+
begin
|
|
1439
|
+
written = big.write_nonblock(data)
|
|
1440
|
+
rescue Errno::EINPROGRESS
|
|
1441
|
+
return
|
|
1442
|
+
rescue Exception => e
|
|
1443
|
+
close_big(big)
|
|
1444
|
+
return
|
|
1445
|
+
end
|
|
1446
|
+
|
|
1447
|
+
set_update(big)
|
|
1448
|
+
im_info[:out] += written
|
|
1449
|
+
data = data[written..-1]
|
|
1450
|
+
big_info[:wbuff] = data
|
|
1451
|
+
|
|
1452
|
+
if big_info[:wbuff].empty? && big_info[:overflowing]
|
|
1453
|
+
puts "big empty #{big_info[:im]}"
|
|
1454
|
+
big_info[:overflowing] = false
|
|
1455
|
+
|
|
1456
|
+
@dst_infos.select{|_, info| (info[:im] == im) && info[:is_big]}.each do |dst, info|
|
|
1457
|
+
puts "resume dst #{info[:domain]}"
|
|
1458
|
+
add_read(dst)
|
|
1459
|
+
end
|
|
1460
|
+
|
|
1461
|
+
@p2_infos.select{|_, info| (info[:im] == im) && info[:is_big]}.each do |p2, info|
|
|
1462
|
+
puts "resume p2 #{info[:p2_id]}"
|
|
1463
|
+
add_read(p2)
|
|
1464
|
+
end
|
|
1465
|
+
end
|
|
1466
|
+
end
|
|
1467
|
+
|
|
1468
|
+
def write_dst(dst)
|
|
1238
1469
|
dst_info = @dst_infos[dst]
|
|
1470
|
+
|
|
1471
|
+
unless dst_info
|
|
1472
|
+
puts "dst info not found delete dst"
|
|
1473
|
+
@writes.delete(dst)
|
|
1474
|
+
return
|
|
1475
|
+
end
|
|
1476
|
+
|
|
1239
1477
|
dst_info[:connected] = true
|
|
1240
1478
|
data = dst_info[:wbuff]
|
|
1241
1479
|
|
|
@@ -1261,28 +1499,36 @@ module Girl
|
|
|
1261
1499
|
set_update(dst)
|
|
1262
1500
|
im = dst_info[:im]
|
|
1263
1501
|
im_info = @im_infos[im]
|
|
1264
|
-
|
|
1502
|
+
|
|
1503
|
+
if im_info
|
|
1504
|
+
im_info[:out] += written
|
|
1505
|
+
big = im_info[:big]
|
|
1506
|
+
end
|
|
1507
|
+
|
|
1265
1508
|
data = data[written..-1]
|
|
1266
1509
|
dst_info[:wbuff] = data
|
|
1267
|
-
|
|
1510
|
+
domain = dst_info[:domain]
|
|
1268
1511
|
|
|
1269
|
-
if dst_info[:
|
|
1270
|
-
|
|
1271
|
-
src_id = dst_info[:src_id]
|
|
1272
|
-
puts "add h_dst_underhalf #{im} #{src_id} #{dst_info[:domain]}"
|
|
1273
|
-
msg = "#{@h_dst_underhalf}#{[src_id].pack('Q>')}"
|
|
1274
|
-
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
1512
|
+
if dst_info[:wbuff].empty? && dst_info[:overflowing]
|
|
1513
|
+
puts "dst empty #{im} #{domain}"
|
|
1275
1514
|
dst_info[:overflowing] = false
|
|
1515
|
+
|
|
1516
|
+
if big
|
|
1517
|
+
puts "resume big"
|
|
1518
|
+
add_read(big)
|
|
1519
|
+
end
|
|
1276
1520
|
end
|
|
1277
1521
|
end
|
|
1278
1522
|
|
|
1279
1523
|
def write_mem(mem)
|
|
1280
|
-
|
|
1281
|
-
|
|
1524
|
+
mem_info = @mem_infos[mem]
|
|
1525
|
+
|
|
1526
|
+
unless mem_info
|
|
1527
|
+
puts "mem info not found delete mem"
|
|
1528
|
+
@writes.delete(mem)
|
|
1282
1529
|
return
|
|
1283
1530
|
end
|
|
1284
|
-
|
|
1285
|
-
mem_info = @mem_infos[mem]
|
|
1531
|
+
|
|
1286
1532
|
data = mem_info[:wbuff]
|
|
1287
1533
|
|
|
1288
1534
|
if data.empty?
|
|
@@ -1306,17 +1552,11 @@ module Girl
|
|
|
1306
1552
|
end
|
|
1307
1553
|
|
|
1308
1554
|
def write_p2(p2)
|
|
1309
|
-
if p2.closed?
|
|
1310
|
-
puts "write closed p2?"
|
|
1311
|
-
return
|
|
1312
|
-
end
|
|
1313
|
-
|
|
1314
1555
|
p2_info = @p2_infos[p2]
|
|
1315
|
-
im = p2_info[:im]
|
|
1316
|
-
im_info = @im_infos[im]
|
|
1317
1556
|
|
|
1318
|
-
unless
|
|
1319
|
-
|
|
1557
|
+
unless p2_info
|
|
1558
|
+
puts "p2 info not found delete p2"
|
|
1559
|
+
@writes.delete(p2)
|
|
1320
1560
|
return
|
|
1321
1561
|
end
|
|
1322
1562
|
|
|
@@ -1342,27 +1582,33 @@ module Girl
|
|
|
1342
1582
|
end
|
|
1343
1583
|
|
|
1344
1584
|
set_update(p2)
|
|
1585
|
+
im = p2_info[:im]
|
|
1586
|
+
im_info = @im_infos[im]
|
|
1587
|
+
big = im_info[:big] if im_info
|
|
1345
1588
|
data = data[written..-1]
|
|
1346
1589
|
p2_info[:wbuff] = data
|
|
1347
|
-
|
|
1590
|
+
p2_id = p2_info[:p2_id]
|
|
1348
1591
|
|
|
1349
|
-
if p2_info[:overflowing] &&
|
|
1350
|
-
|
|
1351
|
-
p2_id = p2_info[:p2_id]
|
|
1352
|
-
puts "add h_p2_underhalf #{im} #{p2_id}"
|
|
1353
|
-
msg = "#{@h_p2_underhalf}#{[p2_id].pack('Q>')}"
|
|
1354
|
-
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
1592
|
+
if p2_info[:overflowing] && p2_info[:wbuff].empty?
|
|
1593
|
+
puts "p2 empty #{im} #{p2_id}"
|
|
1355
1594
|
p2_info[:overflowing] = false
|
|
1595
|
+
|
|
1596
|
+
if big
|
|
1597
|
+
puts "resume big"
|
|
1598
|
+
add_read(big)
|
|
1599
|
+
end
|
|
1356
1600
|
end
|
|
1357
1601
|
end
|
|
1358
1602
|
|
|
1359
1603
|
def write_proxy(proxy)
|
|
1360
|
-
|
|
1361
|
-
|
|
1604
|
+
proxy_info = @proxy_infos[proxy]
|
|
1605
|
+
|
|
1606
|
+
unless proxy_info
|
|
1607
|
+
puts "proxy info not found delete proxy"
|
|
1608
|
+
@writes.delete(proxy)
|
|
1362
1609
|
return
|
|
1363
1610
|
end
|
|
1364
1611
|
|
|
1365
|
-
proxy_info = @proxy_infos[proxy]
|
|
1366
1612
|
data = proxy_info[:wbuff]
|
|
1367
1613
|
|
|
1368
1614
|
if data.empty?
|
|
@@ -1385,21 +1631,6 @@ module Girl
|
|
|
1385
1631
|
im_info[:out] += written if im_info
|
|
1386
1632
|
data = data[written..-1]
|
|
1387
1633
|
proxy_info[:wbuff] = data
|
|
1388
|
-
bytesize = proxy_info[:wbuff].bytesize
|
|
1389
|
-
|
|
1390
|
-
if bytesize < RESUME_BELOW
|
|
1391
|
-
if proxy_info[:paused_dsts].any?
|
|
1392
|
-
puts "proxy underhalf resume dsts #{im} #{proxy_info[:paused_dsts].size}"
|
|
1393
|
-
proxy_info[:paused_dsts].each{|dst| add_read(dst)}
|
|
1394
|
-
proxy_info[:paused_dsts].clear
|
|
1395
|
-
end
|
|
1396
|
-
|
|
1397
|
-
if proxy_info[:paused_p2s].any?
|
|
1398
|
-
puts "proxy underhalf resume p2s #{im} #{proxy_info[:paused_p2s].size}"
|
|
1399
|
-
proxy_info[:paused_p2s].each{|p2| add_read(p2)}
|
|
1400
|
-
proxy_info[:paused_p2s].clear
|
|
1401
|
-
end
|
|
1402
|
-
end
|
|
1403
1634
|
end
|
|
1404
1635
|
|
|
1405
1636
|
end
|