girl 9.1.3 → 9.1.5
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 +7 -8
- data/lib/girl/proxy.rb +6 -34
- data/lib/girl/proxy_worker.rb +348 -506
- data/lib/girl/proxyd.rb +7 -31
- data/lib/girl/proxyd_worker.rb +531 -272
- 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,45 +19,36 @@ 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
|
|
40
|
-
|
|
31
|
+
is_server_fastopen)
|
|
41
32
|
@nameserver_addrs = nameservers.map{|n| Socket.sockaddr_in(53, n)}
|
|
42
33
|
@reset_traff_day = reset_traff_day
|
|
43
|
-
@update_roles = [:dns, :dst, :mem, :p2, :proxy, :rsv] # 参与淘汰的角色
|
|
44
|
-
@updates_limit =
|
|
34
|
+
@update_roles = [:big, :dns, :dst, :mem, :p2, :proxy, :rsv] # 参与淘汰的角色
|
|
35
|
+
@updates_limit = 1010 - ims.size # 淘汰池上限,1015(mac) - [bigd info infod memd proxyd] - p2ds
|
|
45
36
|
@eliminate_count = 0 # 淘汰次数
|
|
46
37
|
@reads = [] # 读池
|
|
47
38
|
@writes = [] # 写池
|
|
48
|
-
@roles = {} # sock => :dns / :dst / :infod / :mem / :memd / :p2 / :p2d / :proxy / :proxyd / :rsv
|
|
39
|
+
@roles = {} # sock => :big / :bigd / :dns / :dst / :infod / :mem / :memd / :p2 / :p2d / :proxy / :proxyd / :rsv
|
|
49
40
|
@updates = {} # sock => updated_at
|
|
50
|
-
@proxy_infos = {} # proxy => {:addrinfo :im :
|
|
51
|
-
@
|
|
41
|
+
@proxy_infos = {} # proxy => {:addrinfo :im :rbuff :wbuff}
|
|
42
|
+
@big_infos = {} # big => {:addrinfo :im :overflowing :rbuff :wbuff}
|
|
43
|
+
@im_infos = {} # im => {:addrinfo :big :big_connect_at :in :out :p2d :p2d_host :p2d_port :proxy :proxy_connect_at}
|
|
44
|
+
@src_infos = {} # src_id => {:created_at :dst :im :rbuff}
|
|
52
45
|
@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
|
|
46
|
+
@dst_infos = {} # dst => {:closing :connected :domain :im :in :ip :is_big :overflowing :port :rbuffs :src_id :wbuff}
|
|
47
|
+
@dns_infos = {} # dns => {:domain :im :port :src_id}
|
|
48
|
+
@rsv_infos = {} # rsv => {:domain :im :near_id}
|
|
49
|
+
@resolv_caches = {} # domain => [ip created_at]
|
|
57
50
|
@p2d_infos = {} # p2d => {:im}
|
|
58
|
-
@p2_infos = {} # p2 => {:addrinfo :closing :im :overflowing :p2_id :wbuff}
|
|
59
|
-
|
|
51
|
+
@p2_infos = {} # p2 => {:addrinfo :closing :im :in :is_big :overflowing :p2_id :wbuff}
|
|
60
52
|
@head_len = head_len
|
|
61
53
|
@h_a_new_source = h_a_new_source
|
|
62
54
|
@h_a_new_p2 = h_a_new_p2
|
|
@@ -65,28 +57,19 @@ module Girl
|
|
|
65
57
|
@h_p1_close = h_p1_close
|
|
66
58
|
@h_p2_close = h_p2_close
|
|
67
59
|
@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
60
|
@h_query = h_query
|
|
73
61
|
@h_response = h_response
|
|
74
62
|
@h_src_close = h_src_close
|
|
75
63
|
@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
64
|
@expire_connecting = expire_connecting
|
|
81
65
|
@expire_long_after = expire_long_after
|
|
82
|
-
@expire_proxy_after = expire_proxy_after
|
|
83
66
|
@expire_resolv_cache = expire_resolv_cache
|
|
84
67
|
@expire_short_after = expire_short_after
|
|
85
68
|
@is_debug = is_debug
|
|
86
69
|
@is_server_fastopen = is_server_fastopen
|
|
87
|
-
|
|
88
70
|
init_im_infos(ims, p2d_host, p2d_port)
|
|
89
71
|
new_a_proxyd(proxyd_port)
|
|
72
|
+
new_a_bigd(bigd_port)
|
|
90
73
|
new_a_infod(proxyd_port)
|
|
91
74
|
new_a_memd(memd_port)
|
|
92
75
|
end
|
|
@@ -103,6 +86,10 @@ module Girl
|
|
|
103
86
|
role = @roles[sock]
|
|
104
87
|
|
|
105
88
|
case role
|
|
89
|
+
when :big
|
|
90
|
+
read_big(sock)
|
|
91
|
+
when :bigd
|
|
92
|
+
read_bigd(sock)
|
|
106
93
|
when :dns
|
|
107
94
|
read_dns(sock)
|
|
108
95
|
when :dst
|
|
@@ -132,6 +119,8 @@ module Girl
|
|
|
132
119
|
role = @roles[sock]
|
|
133
120
|
|
|
134
121
|
case role
|
|
122
|
+
when :big
|
|
123
|
+
write_big(sock)
|
|
135
124
|
when :dst
|
|
136
125
|
write_dst(sock)
|
|
137
126
|
when :mem
|
|
@@ -156,27 +145,55 @@ module Girl
|
|
|
156
145
|
|
|
157
146
|
private
|
|
158
147
|
|
|
148
|
+
def add_big_wbuff(big, data)
|
|
149
|
+
return if big.nil? || big.closed? || data.nil? || data.empty?
|
|
150
|
+
big_info = @big_infos[big]
|
|
151
|
+
big_info[:wbuff] << data
|
|
152
|
+
bytesize = big_info[:wbuff].bytesize
|
|
153
|
+
im = big_info[:im]
|
|
154
|
+
|
|
155
|
+
if bytesize >= CLOSE_ABOVE
|
|
156
|
+
puts "close overflow big #{im}"
|
|
157
|
+
close_big(big)
|
|
158
|
+
return
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
if !big_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
162
|
+
puts "big overflow #{im}"
|
|
163
|
+
big_info[:overflowing] = true
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
add_write(big)
|
|
167
|
+
big_info[:overflowing]
|
|
168
|
+
end
|
|
169
|
+
|
|
159
170
|
def add_dst_wbuff(dst, data)
|
|
160
171
|
return if dst.nil? || dst.closed? || data.nil? || data.empty?
|
|
161
172
|
dst_info = @dst_infos[dst]
|
|
162
173
|
dst_info[:wbuff] << data
|
|
163
174
|
bytesize = dst_info[:wbuff].bytesize
|
|
164
175
|
im = dst_info[:im]
|
|
165
|
-
src_id = dst_info[:src_id]
|
|
166
176
|
domain = dst_info[:domain]
|
|
167
177
|
|
|
168
178
|
if bytesize >= CLOSE_ABOVE
|
|
169
|
-
puts "close overflow dst #{
|
|
179
|
+
puts "close overflow dst #{im} #{domain}"
|
|
170
180
|
close_dst(dst)
|
|
171
181
|
return
|
|
172
182
|
end
|
|
173
183
|
|
|
174
184
|
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))
|
|
185
|
+
puts "dst overflow #{im} #{domain}"
|
|
179
186
|
dst_info[:overflowing] = true
|
|
187
|
+
im_info = @im_infos[im]
|
|
188
|
+
|
|
189
|
+
if im_info
|
|
190
|
+
big = im_info[:big]
|
|
191
|
+
|
|
192
|
+
if big
|
|
193
|
+
puts 'pause big'
|
|
194
|
+
@reads.delete(big)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
180
197
|
end
|
|
181
198
|
|
|
182
199
|
add_write(dst)
|
|
@@ -192,30 +209,30 @@ module Girl
|
|
|
192
209
|
def add_p2_wbuff(p2, data)
|
|
193
210
|
return if p2.nil? || p2.closed? || data.nil? || data.empty?
|
|
194
211
|
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
212
|
p2_info[:wbuff] << data
|
|
204
213
|
bytesize = p2_info[:wbuff].bytesize
|
|
214
|
+
im = p2_info[:im]
|
|
205
215
|
p2_id = p2_info[:p2_id]
|
|
206
216
|
|
|
207
217
|
if bytesize >= CLOSE_ABOVE
|
|
208
|
-
puts "close overflow p2 #{p2_id}"
|
|
218
|
+
puts "close overflow p2 #{im} #{p2_id}"
|
|
209
219
|
close_p2(p2)
|
|
210
220
|
return
|
|
211
221
|
end
|
|
212
222
|
|
|
213
223
|
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))
|
|
224
|
+
puts "p2 overflow #{im} #{p2_id}"
|
|
218
225
|
p2_info[:overflowing] = true
|
|
226
|
+
im_info = @im_infos[im]
|
|
227
|
+
|
|
228
|
+
if im_info
|
|
229
|
+
big = im_info[:big]
|
|
230
|
+
|
|
231
|
+
if big
|
|
232
|
+
puts 'pause big'
|
|
233
|
+
@reads.delete(big)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
219
236
|
end
|
|
220
237
|
|
|
221
238
|
add_write(p2)
|
|
@@ -226,9 +243,10 @@ module Girl
|
|
|
226
243
|
proxy_info = @proxy_infos[proxy]
|
|
227
244
|
proxy_info[:wbuff] << data
|
|
228
245
|
bytesize = proxy_info[:wbuff].bytesize
|
|
246
|
+
im = proxy_info[:im]
|
|
229
247
|
|
|
230
248
|
if bytesize >= CLOSE_ABOVE
|
|
231
|
-
puts "close overflow proxy #{
|
|
249
|
+
puts "close overflow proxy #{im}"
|
|
232
250
|
close_proxy(proxy)
|
|
233
251
|
return
|
|
234
252
|
end
|
|
@@ -251,6 +269,27 @@ module Girl
|
|
|
251
269
|
end
|
|
252
270
|
end
|
|
253
271
|
|
|
272
|
+
def add_src_rbuff(src_id, data)
|
|
273
|
+
src_info = @src_infos[src_id]
|
|
274
|
+
|
|
275
|
+
if src_info
|
|
276
|
+
im = src_info[:im]
|
|
277
|
+
dst = src_info[:dst]
|
|
278
|
+
|
|
279
|
+
if dst
|
|
280
|
+
add_dst_wbuff(dst, data)
|
|
281
|
+
else
|
|
282
|
+
puts "add src rbuff #{im} #{data.bytesize}" if @is_debug
|
|
283
|
+
src_info[:rbuff] << data
|
|
284
|
+
|
|
285
|
+
if src_info[:rbuff].bytesize >= WBUFF_LIMIT
|
|
286
|
+
puts "src rbuff full"
|
|
287
|
+
@src_infos.delete(src_id)
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
254
293
|
def add_write(sock)
|
|
255
294
|
return if sock.nil? || sock.closed? || @writes.include?(sock)
|
|
256
295
|
@writes << sock
|
|
@@ -258,6 +297,15 @@ module Girl
|
|
|
258
297
|
set_update(sock) if @update_roles.include?(role)
|
|
259
298
|
end
|
|
260
299
|
|
|
300
|
+
def check_expire_bigs
|
|
301
|
+
now = Time.new
|
|
302
|
+
|
|
303
|
+
@big_infos.select{|big, _| now.to_i - @updates[big].to_i >= @expire_long_after}.each do |big, info|
|
|
304
|
+
puts "expire big #{info[:im]}"
|
|
305
|
+
close_big(big)
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
261
309
|
def check_expire_dnses
|
|
262
310
|
now = Time.new
|
|
263
311
|
|
|
@@ -312,23 +360,40 @@ module Girl
|
|
|
312
360
|
end
|
|
313
361
|
end
|
|
314
362
|
|
|
315
|
-
def check_expire_srcs
|
|
316
|
-
return if proxy.nil? || proxy.closed?
|
|
317
|
-
proxy_info = @proxy_infos[proxy]
|
|
318
|
-
im = proxy_info[:im]
|
|
363
|
+
def check_expire_srcs
|
|
319
364
|
now = Time.new
|
|
320
365
|
|
|
321
|
-
|
|
322
|
-
puts "expire src info #{im} #{src_id}" if @is_debug
|
|
323
|
-
|
|
366
|
+
@src_infos.select{|_, info| info[:dst].nil? && (now.to_i - info[:created_at].to_i >= @expire_short_after)}.each do |src_id, info|
|
|
367
|
+
puts "expire src info #{info[:im]} #{src_id}" if @is_debug
|
|
368
|
+
@src_infos.delete(src_id)
|
|
324
369
|
end
|
|
325
370
|
end
|
|
326
371
|
|
|
372
|
+
def close_big(big)
|
|
373
|
+
return nil if big.nil? || big.closed?
|
|
374
|
+
close_sock(big)
|
|
375
|
+
big_info = @big_infos.delete(big)
|
|
376
|
+
|
|
377
|
+
if big_info
|
|
378
|
+
addrinfo = big_info[:addrinfo].ip_unpack.inspect
|
|
379
|
+
im = big_info[:im]
|
|
380
|
+
puts "close big #{addrinfo} #{im}" if @is_debug
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
big_info
|
|
384
|
+
end
|
|
385
|
+
|
|
327
386
|
def close_dns(dns)
|
|
328
387
|
return nil if dns.nil? || dns.closed?
|
|
329
388
|
close_sock(dns)
|
|
330
389
|
dns_info = @dns_infos.delete(dns)
|
|
331
|
-
|
|
390
|
+
|
|
391
|
+
if dns_info
|
|
392
|
+
im = dns_info[:im]
|
|
393
|
+
domain = dns_info[:domain]
|
|
394
|
+
puts "close dns #{im} #{domain}" if @is_debug
|
|
395
|
+
end
|
|
396
|
+
|
|
332
397
|
dns_info
|
|
333
398
|
end
|
|
334
399
|
|
|
@@ -336,19 +401,20 @@ module Girl
|
|
|
336
401
|
return nil if dst.nil? || dst.closed?
|
|
337
402
|
close_sock(dst)
|
|
338
403
|
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
404
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
405
|
+
if dst_info
|
|
406
|
+
im = dst_info[:im]
|
|
407
|
+
src_id = dst_info[:src_id]
|
|
408
|
+
domain = dst_info[:domain]
|
|
409
|
+
@src_infos.delete(src_id)
|
|
410
|
+
puts "close dst #{im} #{domain}" if @is_debug
|
|
411
|
+
im_info = @im_infos[im]
|
|
412
|
+
|
|
413
|
+
if im_info
|
|
414
|
+
puts "add h_dst_close #{im} #{domain} #{src_id}" if @is_debug
|
|
415
|
+
msg = "#{@h_dst_close}#{[src_id].pack('Q>')}"
|
|
416
|
+
add_proxy_wbuff(im_info[:proxy], pack_a_chunk(msg))
|
|
417
|
+
end
|
|
352
418
|
end
|
|
353
419
|
|
|
354
420
|
dst_info
|
|
@@ -364,20 +430,17 @@ module Girl
|
|
|
364
430
|
return nil if p2.nil? || p2.closed?
|
|
365
431
|
close_sock(p2)
|
|
366
432
|
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
433
|
|
|
372
|
-
if
|
|
373
|
-
|
|
434
|
+
if p2_info
|
|
435
|
+
im = p2_info[:im]
|
|
436
|
+
p2_id = p2_info[:p2_id]
|
|
437
|
+
puts "close p2 #{im} #{p2_id}"
|
|
438
|
+
im_info = @im_infos[im]
|
|
374
439
|
|
|
375
|
-
if
|
|
376
|
-
proxy_info = @proxy_infos[proxy]
|
|
377
|
-
proxy_info[:paused_p2s].delete(p2)
|
|
440
|
+
if im_info
|
|
378
441
|
puts "add h_p2_close #{im} #{p2_id}"
|
|
379
442
|
msg = "#{@h_p2_close}#{[p2_id].pack('Q>')}"
|
|
380
|
-
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
443
|
+
add_proxy_wbuff(im_info[:proxy], pack_a_chunk(msg))
|
|
381
444
|
end
|
|
382
445
|
end
|
|
383
446
|
|
|
@@ -388,10 +451,15 @@ module Girl
|
|
|
388
451
|
return nil if proxy.nil? || proxy.closed?
|
|
389
452
|
close_sock(proxy)
|
|
390
453
|
proxy_info = @proxy_infos.delete(proxy)
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
454
|
+
|
|
455
|
+
if proxy_info
|
|
456
|
+
addrinfo = proxy_info[:addrinfo].ip_unpack.inspect
|
|
457
|
+
im = proxy_info[:im]
|
|
458
|
+
puts "close proxy #{addrinfo} #{im}" if @is_debug
|
|
459
|
+
@dst_infos.select{|_, info| info[:im] == im}.each{|dst, _| close_dst(dst)}
|
|
460
|
+
@p2_infos.select{|_, info| info[:im] == im}.each{|p2, _| close_p2(p2)}
|
|
461
|
+
end
|
|
462
|
+
|
|
395
463
|
proxy_info
|
|
396
464
|
end
|
|
397
465
|
|
|
@@ -399,7 +467,13 @@ module Girl
|
|
|
399
467
|
return nil if rsv.nil? || rsv.closed?
|
|
400
468
|
close_sock(rsv)
|
|
401
469
|
rsv_info = @rsv_infos.delete(rsv)
|
|
402
|
-
|
|
470
|
+
|
|
471
|
+
if rsv_info
|
|
472
|
+
im = rsv_info[:im]
|
|
473
|
+
domain = rsv_info[:domain]
|
|
474
|
+
puts "close rsv #{im} #{domain}" if @is_debug
|
|
475
|
+
end
|
|
476
|
+
|
|
403
477
|
rsv_info
|
|
404
478
|
end
|
|
405
479
|
|
|
@@ -412,6 +486,30 @@ module Girl
|
|
|
412
486
|
@roles.delete(sock)
|
|
413
487
|
end
|
|
414
488
|
|
|
489
|
+
def deal_big_msg(data, big)
|
|
490
|
+
return if data.nil? || data.empty? || big.nil? || big.closed?
|
|
491
|
+
big_info = @big_infos[big]
|
|
492
|
+
im = big_info[:im]
|
|
493
|
+
return unless im
|
|
494
|
+
h = data[0]
|
|
495
|
+
|
|
496
|
+
case h
|
|
497
|
+
when @h_p2_traffic
|
|
498
|
+
return if data.bytesize < 9
|
|
499
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
500
|
+
data = data[9..-1]
|
|
501
|
+
# puts "big got h_p2_traffic #{im} #{p2_id} #{data.bytesize}" if @is_debug
|
|
502
|
+
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
503
|
+
add_p2_wbuff(p2, data)
|
|
504
|
+
when @h_traffic
|
|
505
|
+
return if data.bytesize < 9
|
|
506
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
507
|
+
data = data[9..-1]
|
|
508
|
+
# puts "big got h_traffic #{im} #{src_id} #{data.bytesize}" if @is_debug
|
|
509
|
+
add_src_rbuff(src_id, data)
|
|
510
|
+
end
|
|
511
|
+
end
|
|
512
|
+
|
|
415
513
|
def deal_msg(data, proxy)
|
|
416
514
|
return if data.nil? || data.empty? || proxy.nil? || proxy.closed?
|
|
417
515
|
proxy_info = @proxy_infos[proxy]
|
|
@@ -422,19 +520,17 @@ module Girl
|
|
|
422
520
|
case h
|
|
423
521
|
when @h_a_new_source
|
|
424
522
|
return if data.bytesize < 9
|
|
425
|
-
check_expire_srcs
|
|
523
|
+
check_expire_srcs
|
|
426
524
|
src_id = data[1, 8].unpack('Q>').first
|
|
427
525
|
domain_port = data[9..-1]
|
|
428
526
|
puts "got h_a_new_source #{im} #{src_id} #{domain_port.inspect}" if @is_debug
|
|
429
|
-
|
|
430
|
-
src_info = {
|
|
527
|
+
@src_infos[src_id] = {
|
|
431
528
|
created_at: Time.new,
|
|
432
529
|
dst: nil,
|
|
530
|
+
im: im,
|
|
433
531
|
rbuff: ''
|
|
434
532
|
}
|
|
435
|
-
|
|
436
|
-
proxy_info[:src_infos][src_id] = src_info
|
|
437
|
-
resolve_domain_port(domain_port, src_id, proxy, im)
|
|
533
|
+
resolve_domain_port(domain_port, src_id)
|
|
438
534
|
when @h_p1_close
|
|
439
535
|
return if data.bytesize < 9
|
|
440
536
|
p2_id = data[1, 8].unpack('Q>').first
|
|
@@ -445,22 +541,8 @@ module Girl
|
|
|
445
541
|
return if data.bytesize < 9
|
|
446
542
|
p2_id = data[1, 8].unpack('Q>').first
|
|
447
543
|
data = data[9..-1]
|
|
448
|
-
# puts "got h_p2_traffic #{im} #{p2_id} #{data.bytesize}" if @is_debug
|
|
449
544
|
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
450
545
|
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
546
|
when @h_query
|
|
465
547
|
return if data.bytesize < 10
|
|
466
548
|
near_id, type = data[1, 9].unpack('Q>C')
|
|
@@ -473,49 +555,13 @@ module Girl
|
|
|
473
555
|
return if data.bytesize < 9
|
|
474
556
|
src_id = data[1, 8].unpack('Q>').first
|
|
475
557
|
puts "got h_src_close #{im} #{src_id}" if @is_debug
|
|
476
|
-
src_info =
|
|
558
|
+
src_info = @src_infos.delete(src_id)
|
|
477
559
|
set_dst_closing(src_info[:dst]) if src_info
|
|
478
560
|
when @h_traffic
|
|
479
561
|
return if data.bytesize < 9
|
|
480
562
|
src_id = data[1, 8].unpack('Q>').first
|
|
481
563
|
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
|
|
564
|
+
add_src_rbuff(src_id, data)
|
|
519
565
|
end
|
|
520
566
|
end
|
|
521
567
|
|
|
@@ -553,6 +599,7 @@ module Girl
|
|
|
553
599
|
ims.sort.each_with_index do |im, i|
|
|
554
600
|
@im_infos[im] = {
|
|
555
601
|
addrinfo: nil,
|
|
602
|
+
big: nil,
|
|
556
603
|
in: 0,
|
|
557
604
|
out: 0,
|
|
558
605
|
p2d: nil,
|
|
@@ -589,13 +636,22 @@ module Girl
|
|
|
589
636
|
end
|
|
590
637
|
end
|
|
591
638
|
|
|
592
|
-
def
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
639
|
+
def new_a_bigd(bigd_port)
|
|
640
|
+
bigd_host = '0.0.0.0'
|
|
641
|
+
bigd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
642
|
+
bigd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
643
|
+
bigd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
644
|
+
bigd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, BACKLOG) if @is_server_fastopen
|
|
645
|
+
bigd.bind(Socket.sockaddr_in(bigd_port, bigd_host))
|
|
646
|
+
bigd.listen(BACKLOG)
|
|
647
|
+
puts "bigd listen on #{bigd_host} #{bigd_port}"
|
|
648
|
+
add_read(bigd, :bigd)
|
|
649
|
+
end
|
|
598
650
|
|
|
651
|
+
def new_a_dst(domain, ip, port, src_id)
|
|
652
|
+
src_info = @src_infos[src_id]
|
|
653
|
+
return unless src_info
|
|
654
|
+
im = src_info[:im]
|
|
599
655
|
check_expire_dsts
|
|
600
656
|
|
|
601
657
|
begin
|
|
@@ -622,10 +678,11 @@ module Girl
|
|
|
622
678
|
connected: false,
|
|
623
679
|
domain: domain,
|
|
624
680
|
im: im,
|
|
681
|
+
in: 0,
|
|
625
682
|
ip: ip,
|
|
683
|
+
is_big: false, # 是否收流量大户
|
|
626
684
|
overflowing: false,
|
|
627
685
|
port: port,
|
|
628
|
-
proxy: proxy,
|
|
629
686
|
rbuffs: [],
|
|
630
687
|
src_id: src_id,
|
|
631
688
|
wbuff: src_info[:rbuff].dup
|
|
@@ -682,18 +739,15 @@ module Girl
|
|
|
682
739
|
return
|
|
683
740
|
end
|
|
684
741
|
|
|
685
|
-
|
|
742
|
+
@rsv_infos[rsv] = {
|
|
686
743
|
domain: domain,
|
|
687
744
|
im: im,
|
|
688
|
-
near_id: near_id
|
|
689
|
-
proxy: proxy
|
|
745
|
+
near_id: near_id
|
|
690
746
|
}
|
|
691
|
-
|
|
692
|
-
@rsv_infos[rsv] = rsv_info
|
|
693
747
|
add_read(rsv, :rsv)
|
|
694
748
|
end
|
|
695
749
|
|
|
696
|
-
def new_a_p2d(p2d_host, p2d_port, im
|
|
750
|
+
def new_a_p2d(p2d_host, p2d_port, im)
|
|
697
751
|
begin
|
|
698
752
|
p2d = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
699
753
|
p2d.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
@@ -757,6 +811,100 @@ module Girl
|
|
|
757
811
|
chunks
|
|
758
812
|
end
|
|
759
813
|
|
|
814
|
+
def read_big(big)
|
|
815
|
+
begin
|
|
816
|
+
data = big.read_nonblock(READ_SIZE)
|
|
817
|
+
rescue Errno::ENOTCONN => e
|
|
818
|
+
return
|
|
819
|
+
rescue Exception => e
|
|
820
|
+
puts "read big #{e.class}" if @is_debug
|
|
821
|
+
close_big(big)
|
|
822
|
+
return
|
|
823
|
+
end
|
|
824
|
+
|
|
825
|
+
set_update(big)
|
|
826
|
+
big_info = @big_infos[big]
|
|
827
|
+
im = big_info[:im]
|
|
828
|
+
data = "#{big_info[:rbuff]}#{data}"
|
|
829
|
+
|
|
830
|
+
unless im
|
|
831
|
+
if data.bytesize < @head_len + 1
|
|
832
|
+
big_info[:rbuff] = data
|
|
833
|
+
return
|
|
834
|
+
end
|
|
835
|
+
|
|
836
|
+
len = data[@head_len].unpack('C').first
|
|
837
|
+
|
|
838
|
+
if len == 0
|
|
839
|
+
puts "im zero len?"
|
|
840
|
+
return
|
|
841
|
+
end
|
|
842
|
+
|
|
843
|
+
if data.bytesize < @head_len + 1 + len
|
|
844
|
+
big_info[:rbuff] = data
|
|
845
|
+
return
|
|
846
|
+
end
|
|
847
|
+
|
|
848
|
+
im = data[@head_len + 1, len]
|
|
849
|
+
|
|
850
|
+
if @im_infos.any?
|
|
851
|
+
im_info = @im_infos[im]
|
|
852
|
+
|
|
853
|
+
unless im_info
|
|
854
|
+
puts "unknown im #{im.inspect}"
|
|
855
|
+
return
|
|
856
|
+
end
|
|
857
|
+
|
|
858
|
+
if im_info[:big] && !im_info[:big].closed?
|
|
859
|
+
puts "big already alive #{im.inspect}"
|
|
860
|
+
return
|
|
861
|
+
end
|
|
862
|
+
end
|
|
863
|
+
|
|
864
|
+
puts "big got im #{im}"
|
|
865
|
+
big_info[:im] = im
|
|
866
|
+
|
|
867
|
+
if im_info
|
|
868
|
+
im_info[:big] = big
|
|
869
|
+
im_info[:big_connect_at] = Time.new
|
|
870
|
+
end
|
|
871
|
+
|
|
872
|
+
add_big_wbuff(big, pack_a_chunk(@h_heartbeat))
|
|
873
|
+
data = data[(@head_len + 1 + len)..-1]
|
|
874
|
+
return if data.empty?
|
|
875
|
+
end
|
|
876
|
+
|
|
877
|
+
im_info = @im_infos[im]
|
|
878
|
+
im_info[:in] += data.bytesize if im_info
|
|
879
|
+
msgs, part = decode_to_msgs(data)
|
|
880
|
+
msgs.each{|msg| deal_big_msg(msg, big)}
|
|
881
|
+
big_info[:rbuff] = part
|
|
882
|
+
end
|
|
883
|
+
|
|
884
|
+
def read_bigd(bigd)
|
|
885
|
+
check_expire_bigs
|
|
886
|
+
|
|
887
|
+
begin
|
|
888
|
+
big, addrinfo = bigd.accept_nonblock
|
|
889
|
+
rescue Exception => e
|
|
890
|
+
puts "accept a big #{e.class}"
|
|
891
|
+
return
|
|
892
|
+
end
|
|
893
|
+
|
|
894
|
+
puts "accept a big #{addrinfo.ip_unpack.inspect}"
|
|
895
|
+
|
|
896
|
+
big_info = {
|
|
897
|
+
addrinfo: addrinfo,
|
|
898
|
+
im: nil,
|
|
899
|
+
overflowing: false,
|
|
900
|
+
rbuff: '',
|
|
901
|
+
wbuff: ''
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
@big_infos[big] = big_info
|
|
905
|
+
add_read(big, :big)
|
|
906
|
+
end
|
|
907
|
+
|
|
760
908
|
def read_dns(dns)
|
|
761
909
|
begin
|
|
762
910
|
data, addrinfo, rflags, *controls = dns.recvmsg
|
|
@@ -782,11 +930,10 @@ module Girl
|
|
|
782
930
|
if ip
|
|
783
931
|
port = dns_info[:port]
|
|
784
932
|
src_id = dns_info[:src_id]
|
|
785
|
-
proxy = dns_info[:proxy]
|
|
786
933
|
im = dns_info[:im]
|
|
787
934
|
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
|
|
935
|
+
new_a_dst(domain, ip, port, src_id)
|
|
936
|
+
@resolv_caches[domain] = [ip, Time.new]
|
|
790
937
|
else
|
|
791
938
|
puts "no ip in answer #{domain}" if @is_debug
|
|
792
939
|
end
|
|
@@ -800,34 +947,56 @@ module Girl
|
|
|
800
947
|
rescue Errno::ENOTCONN => e
|
|
801
948
|
return
|
|
802
949
|
rescue Exception => e
|
|
950
|
+
puts "read dst #{e.class}" if @is_debug
|
|
803
951
|
close_dst(dst)
|
|
804
952
|
return
|
|
805
953
|
end
|
|
806
954
|
|
|
807
955
|
set_update(dst)
|
|
808
956
|
dst_info = @dst_infos[dst]
|
|
809
|
-
|
|
957
|
+
dst_info[:in] += data.bytesize
|
|
958
|
+
im = dst_info[:im]
|
|
959
|
+
src_id = dst_info[:src_id]
|
|
960
|
+
domain = dst_info[:domain]
|
|
810
961
|
|
|
811
|
-
if
|
|
962
|
+
if !dst_info[:is_big] && (dst_info[:in] >= READ_SIZE)
|
|
963
|
+
puts "set dst is big #{im} #{src_id} #{domain}"
|
|
964
|
+
dst_info[:is_big] = true
|
|
965
|
+
end
|
|
966
|
+
|
|
967
|
+
im_info = @im_infos[im]
|
|
968
|
+
|
|
969
|
+
unless im_info
|
|
812
970
|
close_dst(dst)
|
|
813
971
|
return
|
|
814
972
|
end
|
|
815
973
|
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
974
|
+
im_info[:in] += data.bytesize
|
|
975
|
+
data = pack_traffic(src_id, data)
|
|
976
|
+
|
|
977
|
+
if dst_info[:is_big]
|
|
978
|
+
big = im_info[:big]
|
|
821
979
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
980
|
+
if big.nil? || big.closed?
|
|
981
|
+
close_dst(dst)
|
|
982
|
+
return
|
|
983
|
+
end
|
|
825
984
|
|
|
826
|
-
|
|
827
|
-
|
|
985
|
+
overflowing = add_big_wbuff(big, data)
|
|
986
|
+
|
|
987
|
+
if overflowing
|
|
988
|
+
puts "big overflowing pause dst #{src_id} #{domain}"
|
|
828
989
|
@reads.delete(dst)
|
|
829
|
-
proxy_info[:paused_dsts] << dst
|
|
830
990
|
end
|
|
991
|
+
else
|
|
992
|
+
proxy = im_info[:proxy]
|
|
993
|
+
|
|
994
|
+
if proxy.nil? || proxy.closed?
|
|
995
|
+
close_dst(dst)
|
|
996
|
+
return
|
|
997
|
+
end
|
|
998
|
+
|
|
999
|
+
add_proxy_wbuff(proxy, data)
|
|
831
1000
|
end
|
|
832
1001
|
end
|
|
833
1002
|
|
|
@@ -853,6 +1022,7 @@ module Girl
|
|
|
853
1022
|
case message_type
|
|
854
1023
|
when 'heartbeat'
|
|
855
1024
|
@proxy_infos.select{|_, info| info[:im]}.each{|proxy, _| add_proxy_wbuff(proxy, pack_a_chunk(@h_heartbeat))}
|
|
1025
|
+
@big_infos.select{|_, info| info[:im]}.each{|big, _| add_big_wbuff(big, pack_a_chunk(@h_heartbeat))}
|
|
856
1026
|
when 'reset-traffic'
|
|
857
1027
|
puts "reset traffic"
|
|
858
1028
|
@im_infos.each{|_, info| info[:in] = info[:out] = 0}
|
|
@@ -875,24 +1045,27 @@ module Girl
|
|
|
875
1045
|
@im_infos.select{|_, info| info[:addrinfo]}.sort.each do |im, info|
|
|
876
1046
|
im_arr << {
|
|
877
1047
|
im: im,
|
|
1048
|
+
p2d_port: info[:p2d_port],
|
|
1049
|
+
p2d_host: info[:p2d_host],
|
|
878
1050
|
addrinfo: info[:addrinfo].ip_unpack,
|
|
879
1051
|
in: info[:in],
|
|
880
1052
|
out: info[:out],
|
|
881
|
-
|
|
882
|
-
|
|
1053
|
+
proxy_connect_at: info[:proxy_connect_at] ? info[:proxy_connect_at].strftime('%F %T') : '--',
|
|
1054
|
+
big_connect_at: info[:big_connect_at] ? info[:big_connect_at].strftime('%F %T') : '--'
|
|
883
1055
|
}
|
|
884
1056
|
end
|
|
885
1057
|
|
|
886
1058
|
msg = {
|
|
887
|
-
resolv_caches: @resolv_caches.sort,
|
|
888
1059
|
sizes: {
|
|
889
1060
|
reads: @reads.size,
|
|
890
1061
|
writes: @writes.size,
|
|
891
1062
|
roles: @roles.size,
|
|
892
1063
|
updates: @updates.size,
|
|
893
|
-
proxy_infos: @proxy_infos.size,
|
|
894
1064
|
im_infos: @im_infos.size,
|
|
1065
|
+
proxy_infos: @proxy_infos.size,
|
|
1066
|
+
big_infos: @big_infos.size,
|
|
895
1067
|
mem_infos: @mem_infos.size,
|
|
1068
|
+
src_infos: @src_infos.size,
|
|
896
1069
|
dst_infos: @dst_infos.size,
|
|
897
1070
|
dns_infos: @dns_infos.size,
|
|
898
1071
|
rsv_infos: @rsv_infos.size,
|
|
@@ -932,14 +1105,22 @@ module Girl
|
|
|
932
1105
|
rescue Errno::ENOTCONN => e
|
|
933
1106
|
return
|
|
934
1107
|
rescue Exception => e
|
|
1108
|
+
puts "read p2 #{e.class}" if @is_debug
|
|
935
1109
|
close_p2(p2)
|
|
936
1110
|
return
|
|
937
1111
|
end
|
|
938
1112
|
|
|
939
1113
|
set_update(p2)
|
|
940
1114
|
p2_info = @p2_infos[p2]
|
|
1115
|
+
p2_info[:in] += data.bytesize
|
|
941
1116
|
im = p2_info[:im]
|
|
942
|
-
|
|
1117
|
+
p2_id = p2_info[:p2_id]
|
|
1118
|
+
|
|
1119
|
+
if !p2_info[:is_big] && (p2_info[:in] >= READ_SIZE)
|
|
1120
|
+
puts "set p2 is big #{im} #{p2_id}"
|
|
1121
|
+
p2_info[:is_big] = true
|
|
1122
|
+
end
|
|
1123
|
+
|
|
943
1124
|
im_info = @im_infos[im]
|
|
944
1125
|
|
|
945
1126
|
unless im_info
|
|
@@ -947,19 +1128,31 @@ module Girl
|
|
|
947
1128
|
return
|
|
948
1129
|
end
|
|
949
1130
|
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
1131
|
+
data = pack_p2_traffic(p2_id, data)
|
|
1132
|
+
|
|
1133
|
+
if p2_info[:is_big]
|
|
1134
|
+
big = im_info[:big]
|
|
953
1135
|
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
1136
|
+
if big.nil? || big.closed?
|
|
1137
|
+
close_p2(p2)
|
|
1138
|
+
return
|
|
1139
|
+
end
|
|
957
1140
|
|
|
958
|
-
|
|
959
|
-
|
|
1141
|
+
overflowing = add_big_wbuff(big, data)
|
|
1142
|
+
|
|
1143
|
+
if overflowing
|
|
1144
|
+
puts "big overflowing pause p2 #{p2_id}"
|
|
960
1145
|
@reads.delete(p2)
|
|
961
|
-
proxy_info[:paused_p2s] << p2
|
|
962
1146
|
end
|
|
1147
|
+
else
|
|
1148
|
+
proxy = im_info[:proxy]
|
|
1149
|
+
|
|
1150
|
+
if proxy.nil? || proxy.closed?
|
|
1151
|
+
close_p2(p2)
|
|
1152
|
+
return
|
|
1153
|
+
end
|
|
1154
|
+
|
|
1155
|
+
add_proxy_wbuff(proxy, data)
|
|
963
1156
|
end
|
|
964
1157
|
end
|
|
965
1158
|
|
|
@@ -976,24 +1169,22 @@ module Girl
|
|
|
976
1169
|
p2d_info = @p2d_infos[p2d]
|
|
977
1170
|
im = p2d_info[:im]
|
|
978
1171
|
p2_id = rand((2 ** 64) - 2) + 1
|
|
979
|
-
|
|
980
|
-
p2_info = {
|
|
1172
|
+
@p2_infos[p2] = {
|
|
981
1173
|
addrinfo: addrinfo,
|
|
982
1174
|
closing: false,
|
|
983
1175
|
im: im,
|
|
1176
|
+
in: 0,
|
|
1177
|
+
is_big: false, # 是否收流量大户
|
|
984
1178
|
overflowing: false,
|
|
985
1179
|
p2_id: p2_id,
|
|
986
1180
|
wbuff: ''
|
|
987
1181
|
}
|
|
988
|
-
|
|
989
|
-
@p2_infos[p2] = p2_info
|
|
990
1182
|
add_read(p2, :p2)
|
|
991
1183
|
im_info = @im_infos[im]
|
|
992
1184
|
return unless im_info
|
|
993
|
-
proxy = im_info[:proxy]
|
|
994
1185
|
puts "add h_a_new_p2 #{im} #{p2_id}"
|
|
995
1186
|
msg = "#{@h_a_new_p2}#{[p2_id].pack('Q>')}"
|
|
996
|
-
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
1187
|
+
add_proxy_wbuff(im_info[:proxy], pack_a_chunk(msg))
|
|
997
1188
|
end
|
|
998
1189
|
|
|
999
1190
|
def read_rsv(rsv)
|
|
@@ -1009,11 +1200,22 @@ module Girl
|
|
|
1009
1200
|
|
|
1010
1201
|
if data.bytesize <= 65526
|
|
1011
1202
|
rsv_info = @rsv_infos[rsv]
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1203
|
+
im = rsv_info[:im]
|
|
1204
|
+
im_info = @im_infos[im]
|
|
1205
|
+
|
|
1206
|
+
if im_info
|
|
1207
|
+
proxy = im_info[:proxy]
|
|
1208
|
+
|
|
1209
|
+
if proxy.nil? || proxy.closed?
|
|
1210
|
+
close_rsv(rsv)
|
|
1211
|
+
return
|
|
1212
|
+
end
|
|
1213
|
+
|
|
1214
|
+
near_id = rsv_info[:near_id]
|
|
1215
|
+
puts "add h_response #{im} #{near_id} #{rsv_info[:domain]} #{data.bytesize}" if @is_debug
|
|
1216
|
+
msg = "#{@h_response}#{[near_id].pack('Q>')}#{data}"
|
|
1217
|
+
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
1218
|
+
end
|
|
1017
1219
|
else
|
|
1018
1220
|
puts "response too big? #{data.bytesize}"
|
|
1019
1221
|
end
|
|
@@ -1027,6 +1229,7 @@ module Girl
|
|
|
1027
1229
|
rescue Errno::ENOTCONN => e
|
|
1028
1230
|
return
|
|
1029
1231
|
rescue Exception => e
|
|
1232
|
+
puts "read proxy #{e.class}" if @is_debug
|
|
1030
1233
|
close_proxy(proxy)
|
|
1031
1234
|
return
|
|
1032
1235
|
end
|
|
@@ -1056,19 +1259,28 @@ module Girl
|
|
|
1056
1259
|
|
|
1057
1260
|
im = data[@head_len + 1, len]
|
|
1058
1261
|
|
|
1059
|
-
if @im_infos.any?
|
|
1060
|
-
|
|
1061
|
-
|
|
1262
|
+
if @im_infos.any?
|
|
1263
|
+
im_info = @im_infos[im]
|
|
1264
|
+
|
|
1265
|
+
unless im_info
|
|
1266
|
+
puts "unknown im #{im.inspect}"
|
|
1267
|
+
return
|
|
1268
|
+
end
|
|
1269
|
+
|
|
1270
|
+
if im_info[:proxy] && !im_info[:proxy].closed?
|
|
1271
|
+
puts "proxy already alive #{im.inspect}"
|
|
1272
|
+
return
|
|
1273
|
+
end
|
|
1062
1274
|
end
|
|
1063
1275
|
|
|
1064
|
-
puts "got im #{im}"
|
|
1276
|
+
puts "proxy got im #{im}"
|
|
1065
1277
|
proxy_info[:im] = im
|
|
1066
|
-
|
|
1067
|
-
|
|
1278
|
+
|
|
1068
1279
|
if im_info
|
|
1069
1280
|
im_info[:proxy] = proxy
|
|
1281
|
+
im_info[:proxy_connect_at] = Time.new
|
|
1070
1282
|
im_info[:addrinfo] = proxy_info[:addrinfo]
|
|
1071
|
-
im_info[:p2d] = new_a_p2d(im_info[:p2d_host], im_info[:p2d_port], im
|
|
1283
|
+
im_info[:p2d] = new_a_p2d(im_info[:p2d_host], im_info[:p2d_port], im) unless im_info[:p2d]
|
|
1072
1284
|
end
|
|
1073
1285
|
|
|
1074
1286
|
add_proxy_wbuff(proxy, pack_a_chunk(@h_heartbeat))
|
|
@@ -1095,24 +1307,22 @@ module Girl
|
|
|
1095
1307
|
|
|
1096
1308
|
puts "accept a proxy #{addrinfo.ip_unpack.inspect}"
|
|
1097
1309
|
|
|
1098
|
-
|
|
1310
|
+
@proxy_infos[proxy] = {
|
|
1099
1311
|
addrinfo: addrinfo,
|
|
1100
1312
|
im: nil,
|
|
1101
|
-
paused_dsts: [],
|
|
1102
|
-
paused_p2s: [],
|
|
1103
1313
|
rbuff: '',
|
|
1104
|
-
src_infos: {}, # src_id => {:created_at :dst :rbuff}
|
|
1105
1314
|
wbuff: ''
|
|
1106
1315
|
}
|
|
1107
|
-
|
|
1108
|
-
@proxy_infos[proxy] = proxy_info
|
|
1109
1316
|
add_read(proxy, :proxy)
|
|
1110
1317
|
end
|
|
1111
1318
|
|
|
1112
|
-
def resolve_domain_port(domain_port, src_id
|
|
1319
|
+
def resolve_domain_port(domain_port, src_id)
|
|
1113
1320
|
return if domain_port.nil? || domain_port.empty?
|
|
1114
1321
|
colon_idx = domain_port.rindex(':')
|
|
1115
1322
|
return unless colon_idx
|
|
1323
|
+
src_info = @src_infos[src_id]
|
|
1324
|
+
return unless src_info
|
|
1325
|
+
im = src_info[:im]
|
|
1116
1326
|
|
|
1117
1327
|
domain = domain_port[0...colon_idx]
|
|
1118
1328
|
port = domain_port[(colon_idx + 1)..-1].to_i
|
|
@@ -1125,17 +1335,15 @@ module Girl
|
|
|
1125
1335
|
|
|
1126
1336
|
if domain =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$/
|
|
1127
1337
|
# ipv4
|
|
1128
|
-
new_a_dst(domain, domain, port, src_id
|
|
1338
|
+
new_a_dst(domain, domain, port, src_id)
|
|
1129
1339
|
return
|
|
1130
1340
|
end
|
|
1131
1341
|
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
if resolv_cache
|
|
1135
|
-
ip, created_at, im = resolv_cache
|
|
1342
|
+
ip, created_at = @resolv_caches[domain]
|
|
1136
1343
|
|
|
1344
|
+
if ip
|
|
1137
1345
|
if Time.new - created_at < @expire_resolv_cache
|
|
1138
|
-
new_a_dst(domain, ip, port, src_id
|
|
1346
|
+
new_a_dst(domain, ip, port, src_id)
|
|
1139
1347
|
return
|
|
1140
1348
|
end
|
|
1141
1349
|
|
|
@@ -1160,15 +1368,12 @@ module Girl
|
|
|
1160
1368
|
return
|
|
1161
1369
|
end
|
|
1162
1370
|
|
|
1163
|
-
|
|
1371
|
+
@dns_infos[dns] = {
|
|
1164
1372
|
domain: domain,
|
|
1165
1373
|
im: im,
|
|
1166
1374
|
port: port,
|
|
1167
|
-
proxy: proxy,
|
|
1168
1375
|
src_id: src_id
|
|
1169
1376
|
}
|
|
1170
|
-
|
|
1171
|
-
@dns_infos[dns] = dns_info
|
|
1172
1377
|
add_read(dns, :dns)
|
|
1173
1378
|
end
|
|
1174
1379
|
|
|
@@ -1208,6 +1413,8 @@ module Girl
|
|
|
1208
1413
|
|
|
1209
1414
|
@updates.keys.each do |_sock|
|
|
1210
1415
|
case @roles[_sock]
|
|
1416
|
+
when :big
|
|
1417
|
+
close_big(_sock)
|
|
1211
1418
|
when :dns
|
|
1212
1419
|
close_dns(_sock)
|
|
1213
1420
|
when :dst
|
|
@@ -1229,13 +1436,71 @@ module Girl
|
|
|
1229
1436
|
end
|
|
1230
1437
|
end
|
|
1231
1438
|
|
|
1232
|
-
def
|
|
1233
|
-
|
|
1234
|
-
|
|
1439
|
+
def write_big(big)
|
|
1440
|
+
big_info = @big_infos[big]
|
|
1441
|
+
|
|
1442
|
+
unless big_info
|
|
1443
|
+
puts "big info not found delete big"
|
|
1444
|
+
@writes.delete(big)
|
|
1445
|
+
return
|
|
1446
|
+
end
|
|
1447
|
+
|
|
1448
|
+
im = big_info[:im]
|
|
1449
|
+
im_info = @im_infos[im]
|
|
1450
|
+
|
|
1451
|
+
if im_info.nil? || im_info[:proxy].nil?
|
|
1452
|
+
puts "proxy not found close big"
|
|
1453
|
+
close_big(big)
|
|
1454
|
+
return
|
|
1455
|
+
end
|
|
1456
|
+
|
|
1457
|
+
return if @writes.include?(im_info[:proxy])
|
|
1458
|
+
data = big_info[:wbuff]
|
|
1459
|
+
|
|
1460
|
+
if data.empty?
|
|
1461
|
+
@writes.delete(big)
|
|
1235
1462
|
return
|
|
1236
1463
|
end
|
|
1237
1464
|
|
|
1465
|
+
begin
|
|
1466
|
+
written = big.write_nonblock(data)
|
|
1467
|
+
rescue Errno::EINPROGRESS
|
|
1468
|
+
return
|
|
1469
|
+
rescue Exception => e
|
|
1470
|
+
close_big(big)
|
|
1471
|
+
return
|
|
1472
|
+
end
|
|
1473
|
+
|
|
1474
|
+
set_update(big)
|
|
1475
|
+
im_info[:out] += written
|
|
1476
|
+
data = data[written..-1]
|
|
1477
|
+
big_info[:wbuff] = data
|
|
1478
|
+
|
|
1479
|
+
if big_info[:wbuff].empty? && big_info[:overflowing]
|
|
1480
|
+
puts "big empty #{big_info[:im]}"
|
|
1481
|
+
big_info[:overflowing] = false
|
|
1482
|
+
|
|
1483
|
+
@dst_infos.select{|_, info| (info[:im] == im) && info[:is_big]}.each do |dst, info|
|
|
1484
|
+
puts "resume dst #{info[:src_id]} #{info[:domain]}"
|
|
1485
|
+
add_read(dst)
|
|
1486
|
+
end
|
|
1487
|
+
|
|
1488
|
+
@p2_infos.select{|_, info| (info[:im] == im) && info[:is_big]}.each do |p2, info|
|
|
1489
|
+
puts "resume p2 #{info[:p2_id]}"
|
|
1490
|
+
add_read(p2)
|
|
1491
|
+
end
|
|
1492
|
+
end
|
|
1493
|
+
end
|
|
1494
|
+
|
|
1495
|
+
def write_dst(dst)
|
|
1238
1496
|
dst_info = @dst_infos[dst]
|
|
1497
|
+
|
|
1498
|
+
unless dst_info
|
|
1499
|
+
puts "dst info not found delete dst"
|
|
1500
|
+
@writes.delete(dst)
|
|
1501
|
+
return
|
|
1502
|
+
end
|
|
1503
|
+
|
|
1239
1504
|
dst_info[:connected] = true
|
|
1240
1505
|
data = dst_info[:wbuff]
|
|
1241
1506
|
|
|
@@ -1261,28 +1526,37 @@ module Girl
|
|
|
1261
1526
|
set_update(dst)
|
|
1262
1527
|
im = dst_info[:im]
|
|
1263
1528
|
im_info = @im_infos[im]
|
|
1264
|
-
|
|
1529
|
+
|
|
1530
|
+
if im_info
|
|
1531
|
+
im_info[:out] += written
|
|
1532
|
+
big = im_info[:big]
|
|
1533
|
+
end
|
|
1534
|
+
|
|
1265
1535
|
data = data[written..-1]
|
|
1266
1536
|
dst_info[:wbuff] = data
|
|
1267
|
-
|
|
1537
|
+
src_id = dst_info[:src_id]
|
|
1538
|
+
domain = dst_info[:domain]
|
|
1268
1539
|
|
|
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))
|
|
1540
|
+
if dst_info[:wbuff].empty? && dst_info[:overflowing]
|
|
1541
|
+
puts "dst empty #{im} #{src_id} #{domain}"
|
|
1275
1542
|
dst_info[:overflowing] = false
|
|
1543
|
+
|
|
1544
|
+
if big
|
|
1545
|
+
puts "resume big"
|
|
1546
|
+
add_read(big)
|
|
1547
|
+
end
|
|
1276
1548
|
end
|
|
1277
1549
|
end
|
|
1278
1550
|
|
|
1279
1551
|
def write_mem(mem)
|
|
1280
|
-
|
|
1281
|
-
|
|
1552
|
+
mem_info = @mem_infos[mem]
|
|
1553
|
+
|
|
1554
|
+
unless mem_info
|
|
1555
|
+
puts "mem info not found delete mem"
|
|
1556
|
+
@writes.delete(mem)
|
|
1282
1557
|
return
|
|
1283
1558
|
end
|
|
1284
|
-
|
|
1285
|
-
mem_info = @mem_infos[mem]
|
|
1559
|
+
|
|
1286
1560
|
data = mem_info[:wbuff]
|
|
1287
1561
|
|
|
1288
1562
|
if data.empty?
|
|
@@ -1306,17 +1580,11 @@ module Girl
|
|
|
1306
1580
|
end
|
|
1307
1581
|
|
|
1308
1582
|
def write_p2(p2)
|
|
1309
|
-
if p2.closed?
|
|
1310
|
-
puts "write closed p2?"
|
|
1311
|
-
return
|
|
1312
|
-
end
|
|
1313
|
-
|
|
1314
1583
|
p2_info = @p2_infos[p2]
|
|
1315
|
-
im = p2_info[:im]
|
|
1316
|
-
im_info = @im_infos[im]
|
|
1317
1584
|
|
|
1318
|
-
unless
|
|
1319
|
-
|
|
1585
|
+
unless p2_info
|
|
1586
|
+
puts "p2 info not found delete p2"
|
|
1587
|
+
@writes.delete(p2)
|
|
1320
1588
|
return
|
|
1321
1589
|
end
|
|
1322
1590
|
|
|
@@ -1342,27 +1610,33 @@ module Girl
|
|
|
1342
1610
|
end
|
|
1343
1611
|
|
|
1344
1612
|
set_update(p2)
|
|
1613
|
+
im = p2_info[:im]
|
|
1614
|
+
im_info = @im_infos[im]
|
|
1615
|
+
big = im_info[:big] if im_info
|
|
1345
1616
|
data = data[written..-1]
|
|
1346
1617
|
p2_info[:wbuff] = data
|
|
1347
|
-
|
|
1618
|
+
p2_id = p2_info[:p2_id]
|
|
1348
1619
|
|
|
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))
|
|
1620
|
+
if p2_info[:overflowing] && p2_info[:wbuff].empty?
|
|
1621
|
+
puts "p2 empty #{im} #{p2_id}"
|
|
1355
1622
|
p2_info[:overflowing] = false
|
|
1623
|
+
|
|
1624
|
+
if big
|
|
1625
|
+
puts "resume big"
|
|
1626
|
+
add_read(big)
|
|
1627
|
+
end
|
|
1356
1628
|
end
|
|
1357
1629
|
end
|
|
1358
1630
|
|
|
1359
1631
|
def write_proxy(proxy)
|
|
1360
|
-
|
|
1361
|
-
|
|
1632
|
+
proxy_info = @proxy_infos[proxy]
|
|
1633
|
+
|
|
1634
|
+
unless proxy_info
|
|
1635
|
+
puts "proxy info not found delete proxy"
|
|
1636
|
+
@writes.delete(proxy)
|
|
1362
1637
|
return
|
|
1363
1638
|
end
|
|
1364
1639
|
|
|
1365
|
-
proxy_info = @proxy_infos[proxy]
|
|
1366
1640
|
data = proxy_info[:wbuff]
|
|
1367
1641
|
|
|
1368
1642
|
if data.empty?
|
|
@@ -1385,21 +1659,6 @@ module Girl
|
|
|
1385
1659
|
im_info[:out] += written if im_info
|
|
1386
1660
|
data = data[written..-1]
|
|
1387
1661
|
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
1662
|
end
|
|
1404
1663
|
|
|
1405
1664
|
end
|