girl 9.1.2 → 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/dns.rb +52 -55
- data/lib/girl/head.rb +3 -4
- data/lib/girl/proxy.rb +61 -98
- data/lib/girl/proxy_worker.rb +1012 -1193
- data/lib/girl/proxyd.rb +52 -79
- data/lib/girl/proxyd_worker.rb +937 -709
- data/lib/girl/version.rb +1 -1
- metadata +3 -6
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
|
-
@nameserver_addrs = nameservers.map{
|
|
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 => {
|
|
51
|
-
@
|
|
52
|
-
@
|
|
53
|
-
@
|
|
54
|
-
@
|
|
55
|
-
@
|
|
56
|
-
@
|
|
57
|
-
@
|
|
58
|
-
@
|
|
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}
|
|
46
|
+
@mem_infos = {} # mem => {:wbuff}
|
|
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]
|
|
51
|
+
@p2d_infos = {} # p2d => {:im}
|
|
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,30 +59,22 @@ 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
|
|
86
71
|
@is_server_fastopen = is_server_fastopen
|
|
87
72
|
|
|
88
|
-
init_im_infos(
|
|
89
|
-
new_a_proxyd(
|
|
90
|
-
|
|
91
|
-
|
|
73
|
+
init_im_infos(ims, p2d_host, p2d_port)
|
|
74
|
+
new_a_proxyd(proxyd_port)
|
|
75
|
+
new_a_bigd(bigd_port)
|
|
76
|
+
new_a_infod(proxyd_port)
|
|
77
|
+
new_a_memd(memd_port)
|
|
92
78
|
end
|
|
93
79
|
|
|
94
80
|
def looping
|
|
@@ -97,51 +83,57 @@ module Girl
|
|
|
97
83
|
loop_check_traff
|
|
98
84
|
|
|
99
85
|
loop do
|
|
100
|
-
rs, ws = IO.select(
|
|
86
|
+
rs, ws = IO.select(@reads, @writes)
|
|
101
87
|
|
|
102
|
-
rs.each do |
|
|
103
|
-
role = @roles[
|
|
88
|
+
rs.each do |sock|
|
|
89
|
+
role = @roles[sock]
|
|
104
90
|
|
|
105
91
|
case role
|
|
106
|
-
when :
|
|
107
|
-
|
|
108
|
-
when :
|
|
109
|
-
|
|
110
|
-
when :
|
|
111
|
-
|
|
112
|
-
when :
|
|
113
|
-
|
|
114
|
-
when :
|
|
115
|
-
|
|
116
|
-
when :
|
|
117
|
-
|
|
118
|
-
when :
|
|
119
|
-
|
|
120
|
-
when :
|
|
121
|
-
|
|
122
|
-
when :
|
|
123
|
-
|
|
124
|
-
when :
|
|
125
|
-
|
|
92
|
+
when :big
|
|
93
|
+
read_big(sock)
|
|
94
|
+
when :bigd
|
|
95
|
+
read_bigd(sock)
|
|
96
|
+
when :dns
|
|
97
|
+
read_dns(sock)
|
|
98
|
+
when :dst
|
|
99
|
+
read_dst(sock)
|
|
100
|
+
when :infod
|
|
101
|
+
read_infod(sock)
|
|
102
|
+
when :mem
|
|
103
|
+
read_mem(sock)
|
|
104
|
+
when :memd
|
|
105
|
+
read_memd(sock)
|
|
106
|
+
when :p2
|
|
107
|
+
read_p2(sock)
|
|
108
|
+
when :p2d
|
|
109
|
+
read_p2d(sock)
|
|
110
|
+
when :rsv
|
|
111
|
+
read_rsv(sock)
|
|
112
|
+
when :proxy
|
|
113
|
+
read_proxy(sock)
|
|
114
|
+
when :proxyd
|
|
115
|
+
read_proxyd(sock)
|
|
126
116
|
else
|
|
127
|
-
close_sock(
|
|
117
|
+
close_sock(sock)
|
|
128
118
|
end
|
|
129
119
|
end
|
|
130
120
|
|
|
131
|
-
ws.each do |
|
|
132
|
-
role = @roles[
|
|
121
|
+
ws.each do |sock|
|
|
122
|
+
role = @roles[sock]
|
|
133
123
|
|
|
134
124
|
case role
|
|
135
|
-
when :
|
|
136
|
-
|
|
137
|
-
when :
|
|
138
|
-
|
|
139
|
-
when :
|
|
140
|
-
|
|
141
|
-
when :
|
|
142
|
-
|
|
125
|
+
when :big
|
|
126
|
+
write_big(sock)
|
|
127
|
+
when :dst
|
|
128
|
+
write_dst(sock)
|
|
129
|
+
when :mem
|
|
130
|
+
write_mem(sock)
|
|
131
|
+
when :p2
|
|
132
|
+
write_p2(sock)
|
|
133
|
+
when :proxy
|
|
134
|
+
write_proxy(sock)
|
|
143
135
|
else
|
|
144
|
-
close_sock(
|
|
136
|
+
close_sock(sock)
|
|
145
137
|
end
|
|
146
138
|
end
|
|
147
139
|
end
|
|
@@ -156,406 +148,471 @@ module Girl
|
|
|
156
148
|
|
|
157
149
|
private
|
|
158
150
|
|
|
159
|
-
def
|
|
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
|
+
|
|
182
|
+
def add_dst_wbuff(dst, data)
|
|
160
183
|
return if dst.nil? || dst.closed? || data.nil? || data.empty?
|
|
161
|
-
dst_info = @dst_infos[
|
|
162
|
-
dst_info[
|
|
163
|
-
bytesize = dst_info[
|
|
164
|
-
im = dst_info[
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
close_dst( dst )
|
|
184
|
+
dst_info = @dst_infos[dst]
|
|
185
|
+
dst_info[:wbuff] << data
|
|
186
|
+
bytesize = dst_info[:wbuff].bytesize
|
|
187
|
+
im = dst_info[:im]
|
|
188
|
+
domain = dst_info[:domain]
|
|
189
|
+
|
|
190
|
+
if bytesize >= CLOSE_ABOVE
|
|
191
|
+
puts "close overflow dst #{im} #{domain}"
|
|
192
|
+
close_dst(dst)
|
|
171
193
|
return
|
|
172
194
|
end
|
|
173
195
|
|
|
174
|
-
if !dst_info[
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
196
|
+
if !dst_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
197
|
+
puts "dst overflow #{im} #{domain}"
|
|
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
|
-
add_write(
|
|
211
|
+
add_write(dst)
|
|
183
212
|
end
|
|
184
213
|
|
|
185
|
-
def add_mem_wbuff(
|
|
214
|
+
def add_mem_wbuff(mem, data)
|
|
186
215
|
return if mem.nil? || mem.closed? || data.nil? || data.empty?
|
|
187
|
-
mem_info = @mem_infos[
|
|
188
|
-
mem_info[
|
|
189
|
-
add_write(
|
|
216
|
+
mem_info = @mem_infos[mem]
|
|
217
|
+
mem_info[:wbuff] << data
|
|
218
|
+
add_write(mem)
|
|
190
219
|
end
|
|
191
220
|
|
|
192
|
-
def add_p2_wbuff(
|
|
221
|
+
def add_p2_wbuff(p2, data)
|
|
193
222
|
return if p2.nil? || p2.closed? || data.nil? || data.empty?
|
|
194
|
-
p2_info = @p2_infos[
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
223
|
+
p2_info = @p2_infos[p2]
|
|
224
|
+
p2_info[:wbuff] << data
|
|
225
|
+
bytesize = p2_info[:wbuff].bytesize
|
|
226
|
+
im = p2_info[:im]
|
|
227
|
+
p2_id = p2_info[:p2_id]
|
|
228
|
+
|
|
229
|
+
if bytesize >= CLOSE_ABOVE
|
|
230
|
+
puts "close overflow p2 #{im} #{p2_id}"
|
|
231
|
+
close_p2(p2)
|
|
200
232
|
return
|
|
201
233
|
end
|
|
202
234
|
|
|
203
|
-
p2_info[
|
|
204
|
-
|
|
205
|
-
|
|
235
|
+
if !p2_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
236
|
+
puts "p2 overflow #{im} #{p2_id}"
|
|
237
|
+
p2_info[:overflowing] = true
|
|
238
|
+
im_info = @im_infos[im]
|
|
206
239
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
close_p2( p2 )
|
|
210
|
-
return
|
|
211
|
-
end
|
|
240
|
+
if im_info
|
|
241
|
+
big = im_info[:big]
|
|
212
242
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
p2_info[ :overflowing ] = true
|
|
243
|
+
if big
|
|
244
|
+
puts 'pause big'
|
|
245
|
+
@reads.delete(big)
|
|
246
|
+
end
|
|
247
|
+
end
|
|
219
248
|
end
|
|
220
249
|
|
|
221
|
-
add_write(
|
|
250
|
+
add_write(p2)
|
|
222
251
|
end
|
|
223
252
|
|
|
224
|
-
def add_proxy_wbuff(
|
|
253
|
+
def add_proxy_wbuff(proxy, data)
|
|
225
254
|
return if proxy.nil? || proxy.closed? || data.nil? || data.empty?
|
|
226
|
-
proxy_info = @proxy_infos[
|
|
227
|
-
proxy_info[
|
|
228
|
-
bytesize = proxy_info[
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
255
|
+
proxy_info = @proxy_infos[proxy]
|
|
256
|
+
proxy_info[:wbuff] << data
|
|
257
|
+
bytesize = proxy_info[:wbuff].bytesize
|
|
258
|
+
im = proxy_info[:im]
|
|
259
|
+
|
|
260
|
+
if bytesize >= CLOSE_ABOVE
|
|
261
|
+
puts "close overflow proxy #{im}"
|
|
262
|
+
close_proxy(proxy)
|
|
233
263
|
return
|
|
234
264
|
end
|
|
235
265
|
|
|
236
|
-
add_write(
|
|
266
|
+
add_write(proxy)
|
|
237
267
|
end
|
|
238
268
|
|
|
239
|
-
def add_read(
|
|
240
|
-
return if sock.nil? || sock.closed? || @reads.include?(
|
|
269
|
+
def add_read(sock, role = nil)
|
|
270
|
+
return if sock.nil? || sock.closed? || @reads.include?(sock)
|
|
241
271
|
@reads << sock
|
|
242
272
|
|
|
243
|
-
if role
|
|
244
|
-
@roles[
|
|
273
|
+
if role
|
|
274
|
+
@roles[sock] = role
|
|
245
275
|
else
|
|
246
|
-
role = @roles[
|
|
276
|
+
role = @roles[sock]
|
|
247
277
|
end
|
|
248
278
|
|
|
249
|
-
if @update_roles.include?(
|
|
250
|
-
set_update(
|
|
279
|
+
if @update_roles.include?(role)
|
|
280
|
+
set_update(sock)
|
|
251
281
|
end
|
|
252
282
|
end
|
|
253
283
|
|
|
254
|
-
def
|
|
255
|
-
|
|
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
|
+
|
|
305
|
+
def add_write(sock)
|
|
306
|
+
return if sock.nil? || sock.closed? || @writes.include?(sock)
|
|
256
307
|
@writes << sock
|
|
257
|
-
role = @roles[
|
|
308
|
+
role = @roles[sock]
|
|
309
|
+
set_update(sock) if @update_roles.include?(role)
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
def check_expire_bigs
|
|
313
|
+
now = Time.new
|
|
258
314
|
|
|
259
|
-
|
|
260
|
-
|
|
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)
|
|
261
318
|
end
|
|
262
319
|
end
|
|
263
320
|
|
|
264
321
|
def check_expire_dnses
|
|
265
322
|
now = Time.new
|
|
266
323
|
|
|
267
|
-
@dns_infos.select{
|
|
268
|
-
puts "expire dns #{
|
|
269
|
-
close_dns(
|
|
324
|
+
@dns_infos.select{|dns, _| now.to_i - @updates[dns].to_i >= @expire_short_after}.each do |dns, info|
|
|
325
|
+
puts "expire dns #{info[:im]} #{info[:domain]}" if @is_debug
|
|
326
|
+
close_dns(dns)
|
|
270
327
|
end
|
|
271
328
|
end
|
|
272
329
|
|
|
273
330
|
def check_expire_dsts
|
|
274
331
|
now = Time.new
|
|
275
332
|
|
|
276
|
-
@dst_infos.select{
|
|
277
|
-
puts "expire dst #{
|
|
278
|
-
close_dst(
|
|
333
|
+
@dst_infos.select{|dst, info| info[:connected] ? (now.to_i - @updates[dst].to_i >= @expire_long_after) : (now.to_i - @updates[dst].to_i >= @expire_connecting)}.each do |dst, info|
|
|
334
|
+
puts "expire dst #{info[:im]} #{info[:domain]}" if @is_debug
|
|
335
|
+
close_dst(dst)
|
|
279
336
|
end
|
|
280
337
|
end
|
|
281
338
|
|
|
282
339
|
def check_expire_mems
|
|
283
340
|
now = Time.new
|
|
284
341
|
|
|
285
|
-
@mem_infos.select{
|
|
342
|
+
@mem_infos.select{|mem, _| now.to_i - @updates[mem].to_i >= @expire_short_after}.each do |mem, _|
|
|
286
343
|
puts "expire mem" if @is_debug
|
|
287
|
-
close_mem(
|
|
344
|
+
close_mem(mem)
|
|
288
345
|
end
|
|
289
346
|
end
|
|
290
347
|
|
|
291
348
|
def check_expire_p2s
|
|
292
349
|
now = Time.new
|
|
293
350
|
|
|
294
|
-
@p2_infos.select{
|
|
295
|
-
puts "expire p2 #{
|
|
296
|
-
close_p2(
|
|
351
|
+
@p2_infos.select{|p2, _| now.to_i - @updates[p2].to_i >= @expire_long_after}.each do |p2, info|
|
|
352
|
+
puts "expire p2 #{info[:im]} #{info[:p2_id]}" if @is_debug
|
|
353
|
+
close_p2(p2)
|
|
297
354
|
end
|
|
298
355
|
end
|
|
299
356
|
|
|
300
357
|
def check_expire_proxies
|
|
301
358
|
now = Time.new
|
|
302
359
|
|
|
303
|
-
@proxy_infos.select{
|
|
304
|
-
puts "expire proxy #{
|
|
305
|
-
close_proxy(
|
|
360
|
+
@proxy_infos.select{|proxy, _| now.to_i - @updates[proxy].to_i >= @expire_long_after}.each do |proxy, info|
|
|
361
|
+
puts "expire proxy #{info[:im]}"
|
|
362
|
+
close_proxy(proxy)
|
|
306
363
|
end
|
|
307
364
|
end
|
|
308
365
|
|
|
309
366
|
def check_expire_rsvs
|
|
310
367
|
now = Time.new
|
|
311
368
|
|
|
312
|
-
@rsv_infos.select{
|
|
313
|
-
puts "expire rsv #{
|
|
314
|
-
close_rsv(
|
|
369
|
+
@rsv_infos.select{|rsv, _| now.to_i - @updates[rsv].to_i >= @expire_short_after}.each do |rsv, info|
|
|
370
|
+
puts "expire rsv #{info[:im]} #{info[:domain]}" if @is_debug
|
|
371
|
+
close_rsv(rsv)
|
|
315
372
|
end
|
|
316
373
|
end
|
|
317
374
|
|
|
318
|
-
def check_expire_srcs
|
|
319
|
-
return if proxy.nil? || proxy.closed?
|
|
320
|
-
proxy_info = @proxy_infos[ proxy ]
|
|
321
|
-
im = proxy_info[ :im ]
|
|
375
|
+
def check_expire_srcs
|
|
322
376
|
now = Time.new
|
|
323
377
|
|
|
324
|
-
|
|
325
|
-
puts "expire src info #{
|
|
326
|
-
|
|
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
|
|
327
393
|
end
|
|
394
|
+
|
|
395
|
+
big_info
|
|
328
396
|
end
|
|
329
397
|
|
|
330
|
-
def close_dns(
|
|
398
|
+
def close_dns(dns)
|
|
331
399
|
return nil if dns.nil? || dns.closed?
|
|
332
|
-
close_sock(
|
|
333
|
-
dns_info = @dns_infos.delete(
|
|
334
|
-
|
|
400
|
+
close_sock(dns)
|
|
401
|
+
dns_info = @dns_infos.delete(dns)
|
|
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
|
+
|
|
335
409
|
dns_info
|
|
336
410
|
end
|
|
337
411
|
|
|
338
|
-
def close_dst(
|
|
412
|
+
def close_dst(dst)
|
|
339
413
|
return nil if dst.nil? || dst.closed?
|
|
340
|
-
close_sock(
|
|
341
|
-
dst_info = @dst_infos.delete(
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
414
|
+
close_sock(dst)
|
|
415
|
+
dst_info = @dst_infos.delete(dst)
|
|
416
|
+
|
|
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
|
|
355
430
|
end
|
|
356
431
|
|
|
357
432
|
dst_info
|
|
358
433
|
end
|
|
359
434
|
|
|
360
|
-
def close_mem(
|
|
435
|
+
def close_mem(mem)
|
|
361
436
|
return nil if mem.nil? || mem.closed?
|
|
362
|
-
close_sock(
|
|
363
|
-
@mem_infos.delete(
|
|
437
|
+
close_sock(mem)
|
|
438
|
+
@mem_infos.delete(mem)
|
|
364
439
|
end
|
|
365
440
|
|
|
366
|
-
def close_p2(
|
|
441
|
+
def close_p2(p2)
|
|
367
442
|
return nil if p2.nil? || p2.closed?
|
|
368
|
-
close_sock(
|
|
369
|
-
p2_info = @p2_infos.delete(
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
msg = "#{ @h_p2_close }#{ [ p2_id ].pack( 'Q>' ) }"
|
|
383
|
-
add_proxy_wbuff( proxy, pack_a_chunk( msg ) )
|
|
443
|
+
close_sock(p2)
|
|
444
|
+
p2_info = @p2_infos.delete(p2)
|
|
445
|
+
|
|
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]
|
|
451
|
+
|
|
452
|
+
if im_info
|
|
453
|
+
proxy = im_info[:proxy]
|
|
454
|
+
puts "add h_p2_close #{im} #{p2_id}"
|
|
455
|
+
msg = "#{@h_p2_close}#{[p2_id].pack('Q>')}"
|
|
456
|
+
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
384
457
|
end
|
|
385
458
|
end
|
|
386
459
|
|
|
387
460
|
p2_info
|
|
388
461
|
end
|
|
389
462
|
|
|
390
|
-
def close_proxy(
|
|
463
|
+
def close_proxy(proxy)
|
|
391
464
|
return nil if proxy.nil? || proxy.closed?
|
|
392
|
-
close_sock(
|
|
393
|
-
proxy_info = @proxy_infos.delete(
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
465
|
+
close_sock(proxy)
|
|
466
|
+
proxy_info = @proxy_infos.delete(proxy)
|
|
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
|
+
|
|
398
476
|
proxy_info
|
|
399
477
|
end
|
|
400
478
|
|
|
401
|
-
def close_rsv(
|
|
479
|
+
def close_rsv(rsv)
|
|
402
480
|
return nil if rsv.nil? || rsv.closed?
|
|
403
|
-
close_sock(
|
|
404
|
-
rsv_info = @rsv_infos.delete(
|
|
405
|
-
|
|
481
|
+
close_sock(rsv)
|
|
482
|
+
rsv_info = @rsv_infos.delete(rsv)
|
|
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
|
+
|
|
406
490
|
rsv_info
|
|
407
491
|
end
|
|
408
492
|
|
|
409
|
-
def close_sock(
|
|
493
|
+
def close_sock(sock)
|
|
410
494
|
return if sock.nil? || sock.closed?
|
|
411
495
|
sock.close
|
|
412
|
-
@reads.delete(
|
|
413
|
-
@writes.delete(
|
|
414
|
-
@updates.delete(
|
|
415
|
-
@roles.delete(
|
|
496
|
+
@reads.delete(sock)
|
|
497
|
+
@writes.delete(sock)
|
|
498
|
+
@updates.delete(sock)
|
|
499
|
+
@roles.delete(sock)
|
|
416
500
|
end
|
|
417
501
|
|
|
418
|
-
def
|
|
419
|
-
return if data.nil? || data.empty? ||
|
|
420
|
-
|
|
421
|
-
im =
|
|
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]
|
|
422
506
|
return unless im
|
|
423
|
-
h = data[
|
|
507
|
+
h = data[0]
|
|
424
508
|
|
|
425
509
|
case h
|
|
426
|
-
when @
|
|
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
|
|
427
518
|
return if data.bytesize < 9
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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
|
+
|
|
526
|
+
def deal_msg(data, proxy)
|
|
527
|
+
return if data.nil? || data.empty? || proxy.nil? || proxy.closed?
|
|
528
|
+
proxy_info = @proxy_infos[proxy]
|
|
529
|
+
im = proxy_info[:im]
|
|
530
|
+
return unless im
|
|
531
|
+
h = data[0]
|
|
432
532
|
|
|
433
|
-
|
|
533
|
+
case h
|
|
534
|
+
when @h_a_new_source
|
|
535
|
+
return if data.bytesize < 9
|
|
536
|
+
check_expire_srcs
|
|
537
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
538
|
+
domain_port = data[9..-1]
|
|
539
|
+
puts "got h_a_new_source #{im} #{src_id} #{domain_port.inspect}" if @is_debug
|
|
540
|
+
@src_infos[src_id] = {
|
|
434
541
|
created_at: Time.new,
|
|
435
542
|
dst: nil,
|
|
543
|
+
im: im,
|
|
436
544
|
rbuff: ''
|
|
437
545
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
resolve_domain_port( domain_port, src_id, proxy, im )
|
|
441
|
-
when @h_p1_close then
|
|
442
|
-
return if data.bytesize < 9
|
|
443
|
-
p2_id = data[ 1, 8 ].unpack( 'Q>' ).first
|
|
444
|
-
puts "got h_p1_close #{ im } #{ p2_id }"
|
|
445
|
-
p2, _ = @p2_infos.find{ | _, info | ( info[ :im ] == im ) && ( info[ :p2_id ] == p2_id ) }
|
|
446
|
-
set_p2_closing( p2 )
|
|
447
|
-
when @h_p2_traffic then
|
|
448
|
-
return if data.bytesize < 9
|
|
449
|
-
p2_id = data[ 1, 8 ].unpack( 'Q>' ).first
|
|
450
|
-
data = data[ 9..-1 ]
|
|
451
|
-
# puts "got h_p2_traffic #{ im } #{ p2_id } #{ data.bytesize }" if @is_debug
|
|
452
|
-
p2, _ = @p2_infos.find{ | _, info | ( info[ :im ] == im ) && ( info[ :p2_id ] == p2_id ) }
|
|
453
|
-
add_p2_wbuff( p2, data )
|
|
454
|
-
when @h_p1_overflow then
|
|
546
|
+
resolve_domain_port(domain_port, src_id)
|
|
547
|
+
when @h_p1_close
|
|
455
548
|
return if data.bytesize < 9
|
|
456
|
-
p2_id = data[
|
|
457
|
-
puts "got
|
|
458
|
-
p2, _ = @p2_infos.find{
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
when @h_p1_underhalf then
|
|
549
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
550
|
+
puts "got h_p1_close #{im} #{p2_id}"
|
|
551
|
+
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
552
|
+
set_p2_closing(p2)
|
|
553
|
+
when @h_p2_traffic
|
|
462
554
|
return if data.bytesize < 9
|
|
463
|
-
p2_id = data[
|
|
464
|
-
|
|
465
|
-
p2, _ = @p2_infos.find{
|
|
466
|
-
|
|
467
|
-
when @h_query
|
|
555
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
556
|
+
data = data[9..-1]
|
|
557
|
+
p2, _ = @p2_infos.find{|_, info| (info[:im] == im) && (info[:p2_id] == p2_id)}
|
|
558
|
+
add_p2_wbuff(p2, data)
|
|
559
|
+
when @h_query
|
|
468
560
|
return if data.bytesize < 10
|
|
469
|
-
near_id, type = data[
|
|
470
|
-
return unless [
|
|
471
|
-
domain = data[
|
|
561
|
+
near_id, type = data[1, 9].unpack('Q>C')
|
|
562
|
+
return unless [1, 28].include?(type)
|
|
563
|
+
domain = data[10..-1]
|
|
472
564
|
return if domain.nil? || domain.empty?
|
|
473
|
-
puts "got h_query #{
|
|
474
|
-
new_a_rsv(
|
|
475
|
-
when @h_src_close
|
|
565
|
+
puts "got h_query #{im} #{near_id} #{type} #{domain.inspect}" if @is_debug
|
|
566
|
+
new_a_rsv(domain, near_id, type, proxy, im)
|
|
567
|
+
when @h_src_close
|
|
476
568
|
return if data.bytesize < 9
|
|
477
|
-
src_id = data[
|
|
478
|
-
puts "got h_src_close #{
|
|
479
|
-
src_info =
|
|
480
|
-
set_dst_closing(
|
|
481
|
-
when @h_traffic
|
|
569
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
570
|
+
puts "got h_src_close #{im} #{src_id}" if @is_debug
|
|
571
|
+
src_info = @src_infos.delete(src_id)
|
|
572
|
+
set_dst_closing(src_info[:dst]) if src_info
|
|
573
|
+
when @h_traffic
|
|
482
574
|
return if data.bytesize < 9
|
|
483
|
-
src_id = data[
|
|
484
|
-
data = data[
|
|
485
|
-
|
|
486
|
-
src_info = proxy_info[ :src_infos ][ src_id ]
|
|
487
|
-
|
|
488
|
-
if src_info then
|
|
489
|
-
dst = src_info[ :dst ]
|
|
490
|
-
|
|
491
|
-
if dst then
|
|
492
|
-
add_dst_wbuff( dst, data )
|
|
493
|
-
else
|
|
494
|
-
puts "add src rbuff #{ im } #{ data.bytesize }" if @is_debug
|
|
495
|
-
src_info[ :rbuff ] << data
|
|
496
|
-
|
|
497
|
-
if src_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
|
|
498
|
-
puts "src rbuff full"
|
|
499
|
-
close_proxy( proxy )
|
|
500
|
-
end
|
|
501
|
-
end
|
|
502
|
-
end
|
|
503
|
-
when @h_src_overflow then
|
|
504
|
-
return if data.bytesize < 9
|
|
505
|
-
src_id = data[ 1, 8 ].unpack( 'Q>' ).first
|
|
506
|
-
puts "got h_src_overflow pause dst #{ im } #{ src_id }"
|
|
507
|
-
src_info = proxy_info[ :src_infos ][ src_id ]
|
|
508
|
-
|
|
509
|
-
if src_info then
|
|
510
|
-
dst = src_info[ :dst ]
|
|
511
|
-
@reads.delete( dst )
|
|
512
|
-
# 远端收取目的地比传给近端快,近端收取远端又比传给客户端快(2g wifi),流量即在远端proxy堆积,又在近端src堆积
|
|
513
|
-
# 这种情况只等待近端src降半,等到降半消息才恢复读dst,忽略proxy降半
|
|
514
|
-
proxy_info[ :paused_dsts ].delete( dst )
|
|
515
|
-
end
|
|
516
|
-
when @h_src_underhalf then
|
|
517
|
-
return if data.bytesize < 9
|
|
518
|
-
src_id = data[ 1, 8 ].unpack( 'Q>' ).first
|
|
519
|
-
puts "got h_src_underhalf #{ im } #{ src_id }"
|
|
520
|
-
src_info = proxy_info[ :src_infos ][ src_id ]
|
|
521
|
-
add_read( src_info[ :dst ] ) if src_info
|
|
575
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
576
|
+
data = data[9..-1]
|
|
577
|
+
add_src_rbuff(src_id, data)
|
|
522
578
|
end
|
|
523
579
|
end
|
|
524
580
|
|
|
525
|
-
def decode_to_msgs(
|
|
581
|
+
def decode_to_msgs(data)
|
|
526
582
|
msgs = []
|
|
527
583
|
part = ''
|
|
528
584
|
|
|
529
585
|
loop do
|
|
530
|
-
if data.bytesize <= 2
|
|
586
|
+
if data.bytesize <= 2
|
|
531
587
|
part = data
|
|
532
588
|
break
|
|
533
589
|
end
|
|
534
590
|
|
|
535
|
-
len = data[
|
|
591
|
+
len = data[0, 2].unpack('n').first
|
|
536
592
|
|
|
537
|
-
if len == 0
|
|
593
|
+
if len == 0
|
|
538
594
|
puts "msg zero len?"
|
|
539
595
|
break
|
|
540
596
|
end
|
|
541
597
|
|
|
542
|
-
if data.bytesize < (
|
|
598
|
+
if data.bytesize < (2 + len)
|
|
543
599
|
part = data
|
|
544
600
|
break
|
|
545
601
|
end
|
|
546
602
|
|
|
547
|
-
msgs << data[
|
|
548
|
-
data = data[
|
|
603
|
+
msgs << data[2, len]
|
|
604
|
+
data = data[(2 + len)..-1]
|
|
549
605
|
break if data.empty?
|
|
550
606
|
end
|
|
551
607
|
|
|
552
|
-
[
|
|
608
|
+
[msgs, part]
|
|
553
609
|
end
|
|
554
610
|
|
|
555
|
-
def init_im_infos(
|
|
556
|
-
ims.sort.each_with_index do |
|
|
557
|
-
@im_infos[
|
|
611
|
+
def init_im_infos(ims, p2d_host, p2d_port)
|
|
612
|
+
ims.sort.each_with_index do |im, i|
|
|
613
|
+
@im_infos[im] = {
|
|
558
614
|
addrinfo: nil,
|
|
615
|
+
big: nil,
|
|
559
616
|
in: 0,
|
|
560
617
|
out: 0,
|
|
561
618
|
p2d: nil,
|
|
@@ -567,15 +624,15 @@ module Girl
|
|
|
567
624
|
end
|
|
568
625
|
|
|
569
626
|
def loop_check_traff
|
|
570
|
-
if @reset_traff_day > 0
|
|
627
|
+
if @reset_traff_day > 0
|
|
571
628
|
Thread.new do
|
|
572
629
|
loop do
|
|
573
630
|
sleep CHECK_TRAFF_INTERVAL
|
|
574
631
|
now = Time.new
|
|
575
632
|
|
|
576
|
-
if (
|
|
577
|
-
msg = {
|
|
578
|
-
send_data(
|
|
633
|
+
if (now.day == @reset_traff_day) && (now.hour == 0)
|
|
634
|
+
msg = {message_type: 'reset-traffic'}
|
|
635
|
+
send_data(@info, JSON.generate(msg), @infod_addr)
|
|
579
636
|
end
|
|
580
637
|
end
|
|
581
638
|
end
|
|
@@ -586,36 +643,45 @@ module Girl
|
|
|
586
643
|
Thread.new do
|
|
587
644
|
loop do
|
|
588
645
|
sleep HEARTBEAT_INTERVAL
|
|
589
|
-
msg = {
|
|
590
|
-
send_data(
|
|
646
|
+
msg = {message_type: 'heartbeat'}
|
|
647
|
+
send_data(@info, JSON.generate(msg), @infod_addr)
|
|
591
648
|
end
|
|
592
649
|
end
|
|
593
650
|
end
|
|
594
651
|
|
|
595
|
-
def
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
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
|
|
601
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]
|
|
602
668
|
check_expire_dsts
|
|
603
669
|
|
|
604
670
|
begin
|
|
605
|
-
dst = Socket.new(
|
|
671
|
+
dst = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
606
672
|
rescue Exception => e
|
|
607
|
-
puts "new a dst #{
|
|
673
|
+
puts "new a dst #{e.class} #{im} #{domain}:#{port}"
|
|
608
674
|
return
|
|
609
675
|
end
|
|
610
676
|
|
|
611
|
-
dst.setsockopt(
|
|
677
|
+
dst.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
612
678
|
|
|
613
679
|
begin
|
|
614
|
-
destination_addr = Socket.sockaddr_in(
|
|
615
|
-
dst.connect_nonblock(
|
|
680
|
+
destination_addr = Socket.sockaddr_in(port, ip)
|
|
681
|
+
dst.connect_nonblock(destination_addr)
|
|
616
682
|
rescue IO::WaitWritable
|
|
617
683
|
rescue Exception => e
|
|
618
|
-
puts "connect destination #{
|
|
684
|
+
puts "connect destination #{e.class} #{im} #{domain}:#{port}"
|
|
619
685
|
dst.close
|
|
620
686
|
return
|
|
621
687
|
end
|
|
@@ -625,277 +691,376 @@ module Girl
|
|
|
625
691
|
connected: false,
|
|
626
692
|
domain: domain,
|
|
627
693
|
im: im,
|
|
694
|
+
in: 0,
|
|
628
695
|
ip: ip,
|
|
696
|
+
is_big: false, # 收来的流量是否是大流量
|
|
629
697
|
overflowing: false,
|
|
630
698
|
port: port,
|
|
631
|
-
proxy: proxy,
|
|
632
699
|
rbuffs: [],
|
|
633
700
|
src_id: src_id,
|
|
634
|
-
wbuff: src_info[
|
|
701
|
+
wbuff: src_info[:rbuff].dup
|
|
635
702
|
}
|
|
636
703
|
|
|
637
|
-
@dst_infos[
|
|
638
|
-
add_read(
|
|
639
|
-
add_write(
|
|
640
|
-
src_info[
|
|
704
|
+
@dst_infos[dst] = dst_info
|
|
705
|
+
add_read(dst, :dst)
|
|
706
|
+
add_write(dst)
|
|
707
|
+
src_info[:dst] = dst
|
|
641
708
|
end
|
|
642
709
|
|
|
643
|
-
def new_a_infod(
|
|
710
|
+
def new_a_infod(infod_port)
|
|
644
711
|
infod_host = '127.0.0.1'
|
|
645
|
-
infod_addr = Socket.sockaddr_in(
|
|
646
|
-
infod = Socket.new(
|
|
647
|
-
infod.setsockopt(
|
|
648
|
-
infod.bind(
|
|
649
|
-
puts "infod bind on #{
|
|
650
|
-
add_read(
|
|
651
|
-
info = Socket.new(
|
|
712
|
+
infod_addr = Socket.sockaddr_in(infod_port, infod_host)
|
|
713
|
+
infod = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
714
|
+
infod.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
715
|
+
infod.bind(infod_addr)
|
|
716
|
+
puts "infod bind on #{infod_host} #{infod_port}"
|
|
717
|
+
add_read(infod, :infod)
|
|
718
|
+
info = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
652
719
|
@infod_addr = infod_addr
|
|
653
720
|
@infod = infod
|
|
654
721
|
@info = info
|
|
655
722
|
end
|
|
656
723
|
|
|
657
|
-
def new_a_memd(
|
|
724
|
+
def new_a_memd(memd_port)
|
|
658
725
|
memd_host = '127.0.0.1'
|
|
659
|
-
memd = Socket.new(
|
|
660
|
-
memd.setsockopt(
|
|
661
|
-
memd.setsockopt(
|
|
662
|
-
memd.setsockopt(
|
|
663
|
-
memd.bind(
|
|
664
|
-
memd.listen(
|
|
665
|
-
puts "memd listen on #{
|
|
666
|
-
add_read(
|
|
726
|
+
memd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
727
|
+
memd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
728
|
+
memd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
729
|
+
memd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, 5) if @is_server_fastopen
|
|
730
|
+
memd.bind(Socket.sockaddr_in(memd_port, memd_host))
|
|
731
|
+
memd.listen(5)
|
|
732
|
+
puts "memd listen on #{memd_host} #{memd_port}"
|
|
733
|
+
add_read(memd, :memd)
|
|
667
734
|
end
|
|
668
735
|
|
|
669
|
-
def new_a_rsv(
|
|
736
|
+
def new_a_rsv(domain, near_id, type, proxy, im)
|
|
670
737
|
check_expire_rsvs
|
|
671
|
-
rsv = Socket.new(
|
|
738
|
+
rsv = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
672
739
|
|
|
673
740
|
begin
|
|
674
|
-
data = pack_a_query(
|
|
741
|
+
data = pack_a_query(domain, type)
|
|
675
742
|
rescue Exception => e
|
|
676
|
-
puts "rsv pack a query #{
|
|
743
|
+
puts "rsv pack a query #{e.class} #{e.message} #{domain}" if @is_debug
|
|
677
744
|
return
|
|
678
745
|
end
|
|
679
746
|
|
|
680
747
|
begin
|
|
681
|
-
@nameserver_addrs.each{
|
|
748
|
+
@nameserver_addrs.each{|addr| rsv.sendmsg(data, 0, addr)}
|
|
682
749
|
rescue Exception => e
|
|
683
|
-
puts "rsv send data #{
|
|
750
|
+
puts "rsv send data #{e.class}"
|
|
684
751
|
rsv.close
|
|
685
752
|
return
|
|
686
753
|
end
|
|
687
754
|
|
|
688
|
-
|
|
755
|
+
@rsv_infos[rsv] = {
|
|
689
756
|
domain: domain,
|
|
690
757
|
im: im,
|
|
691
|
-
near_id: near_id
|
|
692
|
-
proxy: proxy
|
|
758
|
+
near_id: near_id
|
|
693
759
|
}
|
|
694
|
-
|
|
695
|
-
@rsv_infos[ rsv ] = rsv_info
|
|
696
|
-
add_read( rsv, :rsv )
|
|
760
|
+
add_read(rsv, :rsv)
|
|
697
761
|
end
|
|
698
762
|
|
|
699
|
-
def new_a_p2d(
|
|
763
|
+
def new_a_p2d(p2d_host, p2d_port, im)
|
|
700
764
|
begin
|
|
701
|
-
p2d = Socket.new(
|
|
702
|
-
p2d.setsockopt(
|
|
703
|
-
p2d.setsockopt(
|
|
704
|
-
p2d.setsockopt(
|
|
705
|
-
p2d.bind(
|
|
706
|
-
p2d.listen(
|
|
707
|
-
puts "p2d listen on #{
|
|
708
|
-
@p2d_infos[
|
|
709
|
-
add_read(
|
|
765
|
+
p2d = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
766
|
+
p2d.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
767
|
+
p2d.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
768
|
+
p2d.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, 5) if @is_server_fastopen
|
|
769
|
+
p2d.bind(Socket.sockaddr_in(p2d_port, p2d_host))
|
|
770
|
+
p2d.listen(5)
|
|
771
|
+
puts "p2d listen on #{p2d_host} #{p2d_port} #{im}"
|
|
772
|
+
@p2d_infos[p2d] = {im: im}
|
|
773
|
+
add_read(p2d, :p2d)
|
|
710
774
|
rescue Exception => e
|
|
711
|
-
puts "new a p2d #{
|
|
775
|
+
puts "new a p2d #{e.class}"
|
|
712
776
|
end
|
|
713
777
|
|
|
714
778
|
p2d
|
|
715
779
|
end
|
|
716
780
|
|
|
717
|
-
def new_a_proxyd(
|
|
781
|
+
def new_a_proxyd(proxyd_port)
|
|
718
782
|
proxyd_host = '0.0.0.0'
|
|
719
|
-
proxyd = Socket.new(
|
|
720
|
-
proxyd.setsockopt(
|
|
721
|
-
proxyd.setsockopt(
|
|
722
|
-
proxyd.setsockopt(
|
|
723
|
-
proxyd.bind(
|
|
724
|
-
proxyd.listen(
|
|
725
|
-
puts "proxyd listen on #{
|
|
726
|
-
add_read(
|
|
783
|
+
proxyd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
784
|
+
proxyd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
785
|
+
proxyd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
786
|
+
proxyd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, BACKLOG) if @is_server_fastopen
|
|
787
|
+
proxyd.bind(Socket.sockaddr_in(proxyd_port, proxyd_host))
|
|
788
|
+
proxyd.listen(BACKLOG)
|
|
789
|
+
puts "proxyd listen on #{proxyd_host} #{proxyd_port}"
|
|
790
|
+
add_read(proxyd, :proxyd)
|
|
727
791
|
end
|
|
728
792
|
|
|
729
|
-
def pack_a_chunk(
|
|
730
|
-
"#{
|
|
793
|
+
def pack_a_chunk(msg)
|
|
794
|
+
"#{[msg.bytesize].pack('n')}#{msg}"
|
|
731
795
|
end
|
|
732
796
|
|
|
733
|
-
def pack_p2_traffic(
|
|
797
|
+
def pack_p2_traffic(p2_id, data)
|
|
734
798
|
chunks = ''
|
|
735
799
|
|
|
736
800
|
loop do
|
|
737
|
-
part = data[
|
|
738
|
-
# puts "add h_p2_traffic #{
|
|
739
|
-
msg = "#{
|
|
740
|
-
chunks << pack_a_chunk(
|
|
741
|
-
data = data[
|
|
801
|
+
part = data[0, 65526]
|
|
802
|
+
# puts "add h_p2_traffic #{p2_id} #{part.bytesize}" if @is_debug
|
|
803
|
+
msg = "#{@h_p2_traffic}#{[p2_id].pack('Q>')}#{part}"
|
|
804
|
+
chunks << pack_a_chunk(msg)
|
|
805
|
+
data = data[part.bytesize..-1]
|
|
742
806
|
break if data.empty?
|
|
743
807
|
end
|
|
744
808
|
|
|
745
809
|
chunks
|
|
746
810
|
end
|
|
747
811
|
|
|
748
|
-
def pack_traffic(
|
|
812
|
+
def pack_traffic(src_id, data)
|
|
749
813
|
chunks = ''
|
|
750
814
|
|
|
751
815
|
loop do
|
|
752
|
-
part = data[
|
|
753
|
-
# puts "add h_traffic #{
|
|
754
|
-
msg = "#{
|
|
755
|
-
chunks << pack_a_chunk(
|
|
756
|
-
data = data[
|
|
816
|
+
part = data[0, 65526]
|
|
817
|
+
# puts "add h_traffic #{src_id} #{part.bytesize}" if @is_debug
|
|
818
|
+
msg = "#{@h_traffic}#{[src_id].pack('Q>')}#{part}"
|
|
819
|
+
chunks << pack_a_chunk(msg)
|
|
820
|
+
data = data[part.bytesize..-1]
|
|
757
821
|
break if data.empty?
|
|
758
822
|
end
|
|
759
823
|
|
|
760
824
|
chunks
|
|
761
825
|
end
|
|
762
826
|
|
|
763
|
-
def
|
|
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
|
+
|
|
921
|
+
def read_dns(dns)
|
|
764
922
|
begin
|
|
765
923
|
data, addrinfo, rflags, *controls = dns.recvmsg
|
|
766
924
|
rescue Exception => e
|
|
767
|
-
puts "dns recvmsg #{
|
|
768
|
-
close_dns(
|
|
925
|
+
puts "dns recvmsg #{e.class}"
|
|
926
|
+
close_dns(dns)
|
|
769
927
|
return
|
|
770
928
|
end
|
|
771
929
|
|
|
772
930
|
return if data.empty?
|
|
773
931
|
|
|
774
932
|
begin
|
|
775
|
-
ip = seek_ip(
|
|
933
|
+
ip = seek_ip(data)
|
|
776
934
|
rescue Exception => e
|
|
777
|
-
puts "seek ip #{
|
|
778
|
-
close_dns(
|
|
935
|
+
puts "seek ip #{e.class} #{e.message}"
|
|
936
|
+
close_dns(dns)
|
|
779
937
|
return
|
|
780
938
|
end
|
|
781
939
|
|
|
782
|
-
dns_info = @dns_infos[
|
|
783
|
-
domain = dns_info[
|
|
940
|
+
dns_info = @dns_infos[dns]
|
|
941
|
+
domain = dns_info[:domain]
|
|
784
942
|
|
|
785
|
-
if ip
|
|
786
|
-
port = dns_info[
|
|
787
|
-
src_id = dns_info[
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
@resolv_caches[ domain ] = [ ip, Time.new, im ]
|
|
943
|
+
if ip
|
|
944
|
+
port = dns_info[:port]
|
|
945
|
+
src_id = dns_info[:src_id]
|
|
946
|
+
im = dns_info[:im]
|
|
947
|
+
puts "got ip #{im} #{domain} #{ip}" if @is_debug
|
|
948
|
+
new_a_dst(domain, ip, port, src_id)
|
|
949
|
+
@resolv_caches[domain] = [ip, Time.new]
|
|
793
950
|
else
|
|
794
|
-
puts "no ip in answer #{
|
|
951
|
+
puts "no ip in answer #{domain}" if @is_debug
|
|
795
952
|
end
|
|
796
953
|
|
|
797
|
-
close_dns(
|
|
954
|
+
close_dns(dns)
|
|
798
955
|
end
|
|
799
956
|
|
|
800
|
-
def read_dst(
|
|
957
|
+
def read_dst(dst)
|
|
801
958
|
begin
|
|
802
|
-
data = dst.read_nonblock(
|
|
959
|
+
data = dst.read_nonblock(READ_SIZE)
|
|
803
960
|
rescue Errno::ENOTCONN => e
|
|
804
961
|
return
|
|
805
962
|
rescue Exception => e
|
|
806
|
-
|
|
963
|
+
puts "read dst #{e.class}" if @is_debug
|
|
964
|
+
close_dst(dst)
|
|
807
965
|
return
|
|
808
966
|
end
|
|
809
967
|
|
|
810
|
-
set_update(
|
|
811
|
-
dst_info = @dst_infos[
|
|
812
|
-
|
|
968
|
+
set_update(dst)
|
|
969
|
+
dst_info = @dst_infos[dst]
|
|
970
|
+
dst_info[:in] += data.bytesize
|
|
971
|
+
im = dst_info[:im]
|
|
813
972
|
|
|
814
|
-
if
|
|
815
|
-
|
|
816
|
-
|
|
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
|
|
817
976
|
end
|
|
818
977
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
978
|
+
im_info = @im_infos[im]
|
|
979
|
+
|
|
980
|
+
unless im_info
|
|
981
|
+
close_dst(dst)
|
|
982
|
+
return
|
|
983
|
+
end
|
|
824
984
|
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
985
|
+
im_info[:in] += data.bytesize
|
|
986
|
+
src_id = dst_info[:src_id]
|
|
987
|
+
data = pack_traffic(src_id, data)
|
|
828
988
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
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)
|
|
834
995
|
end
|
|
835
996
|
end
|
|
836
997
|
|
|
837
|
-
def read_infod(
|
|
998
|
+
def read_infod(infod)
|
|
838
999
|
begin
|
|
839
1000
|
data, addrinfo, rflags, *controls = infod.recvmsg
|
|
840
1001
|
rescue Exception => e
|
|
841
|
-
puts "infod recvmsg #{
|
|
1002
|
+
puts "infod recvmsg #{e.class}"
|
|
842
1003
|
return
|
|
843
1004
|
end
|
|
844
1005
|
|
|
845
1006
|
return if data.empty?
|
|
846
1007
|
|
|
847
1008
|
begin
|
|
848
|
-
msg = JSON.parse(
|
|
1009
|
+
msg = JSON.parse(data, symbolize_names: true)
|
|
849
1010
|
rescue JSON::ParserError, EncodingError => e
|
|
850
|
-
puts "read infod #{
|
|
1011
|
+
puts "read infod #{e.class}"
|
|
851
1012
|
return
|
|
852
1013
|
end
|
|
853
1014
|
|
|
854
|
-
message_type = msg[
|
|
1015
|
+
message_type = msg[:message_type]
|
|
855
1016
|
|
|
856
1017
|
case message_type
|
|
857
|
-
when 'heartbeat'
|
|
858
|
-
@proxy_infos.select{
|
|
859
|
-
|
|
1018
|
+
when 'heartbeat'
|
|
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))}
|
|
1021
|
+
when 'reset-traffic'
|
|
860
1022
|
puts "reset traffic"
|
|
861
|
-
@im_infos.each{
|
|
1023
|
+
@im_infos.each{|_, info| info[:in] = info[:out] = 0}
|
|
862
1024
|
end
|
|
863
1025
|
end
|
|
864
1026
|
|
|
865
|
-
def read_mem(
|
|
1027
|
+
def read_mem(mem)
|
|
866
1028
|
begin
|
|
867
|
-
mem.read_nonblock(
|
|
1029
|
+
mem.read_nonblock(READ_SIZE)
|
|
868
1030
|
rescue Errno::ENOTCONN => e
|
|
869
1031
|
return
|
|
870
1032
|
rescue Exception => e
|
|
871
|
-
close_mem(
|
|
1033
|
+
close_mem(mem)
|
|
872
1034
|
return
|
|
873
1035
|
end
|
|
874
1036
|
|
|
875
|
-
set_update(
|
|
1037
|
+
set_update(mem)
|
|
876
1038
|
im_arr = []
|
|
877
1039
|
|
|
878
|
-
@im_infos.select{
|
|
1040
|
+
@im_infos.select{|_, info| info[:addrinfo]}.sort.each do |im, info|
|
|
879
1041
|
im_arr << {
|
|
880
1042
|
im: im,
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
1043
|
+
p2d_port: info[:p2d_port],
|
|
1044
|
+
p2d_host: info[:p2d_host],
|
|
1045
|
+
addrinfo: info[:addrinfo].ip_unpack,
|
|
1046
|
+
in: info[:in],
|
|
1047
|
+
out: info[:out],
|
|
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') : '--'
|
|
886
1050
|
}
|
|
887
1051
|
end
|
|
888
1052
|
|
|
889
1053
|
msg = {
|
|
890
|
-
resolv_caches: @resolv_caches.sort,
|
|
891
1054
|
sizes: {
|
|
892
1055
|
reads: @reads.size,
|
|
893
1056
|
writes: @writes.size,
|
|
894
1057
|
roles: @roles.size,
|
|
895
1058
|
updates: @updates.size,
|
|
896
|
-
proxy_infos: @proxy_infos.size,
|
|
897
1059
|
im_infos: @im_infos.size,
|
|
1060
|
+
proxy_infos: @proxy_infos.size,
|
|
1061
|
+
big_infos: @big_infos.size,
|
|
898
1062
|
mem_infos: @mem_infos.size,
|
|
1063
|
+
src_infos: @src_infos.size,
|
|
899
1064
|
dst_infos: @dst_infos.size,
|
|
900
1065
|
dns_infos: @dns_infos.size,
|
|
901
1066
|
rsv_infos: @rsv_infos.size,
|
|
@@ -908,16 +1073,16 @@ module Girl
|
|
|
908
1073
|
im_arr: im_arr
|
|
909
1074
|
}
|
|
910
1075
|
|
|
911
|
-
add_mem_wbuff(
|
|
1076
|
+
add_mem_wbuff(mem, JSON.generate(msg))
|
|
912
1077
|
end
|
|
913
1078
|
|
|
914
|
-
def read_memd(
|
|
1079
|
+
def read_memd(memd)
|
|
915
1080
|
check_expire_mems
|
|
916
1081
|
|
|
917
1082
|
begin
|
|
918
1083
|
mem, addrinfo = memd.accept_nonblock
|
|
919
1084
|
rescue Exception => e
|
|
920
|
-
puts "memd accept #{
|
|
1085
|
+
puts "memd accept #{e.class}"
|
|
921
1086
|
return
|
|
922
1087
|
end
|
|
923
1088
|
|
|
@@ -925,306 +1090,318 @@ module Girl
|
|
|
925
1090
|
wbuff: ''
|
|
926
1091
|
}
|
|
927
1092
|
|
|
928
|
-
@mem_infos[
|
|
929
|
-
add_read(
|
|
1093
|
+
@mem_infos[mem] = mem_info
|
|
1094
|
+
add_read(mem, :mem)
|
|
930
1095
|
end
|
|
931
1096
|
|
|
932
|
-
def read_p2(
|
|
1097
|
+
def read_p2(p2)
|
|
933
1098
|
begin
|
|
934
|
-
data = p2.read_nonblock(
|
|
1099
|
+
data = p2.read_nonblock(READ_SIZE)
|
|
935
1100
|
rescue Errno::ENOTCONN => e
|
|
936
1101
|
return
|
|
937
1102
|
rescue Exception => e
|
|
938
|
-
|
|
1103
|
+
puts "read p2 #{e.class}" if @is_debug
|
|
1104
|
+
close_p2(p2)
|
|
939
1105
|
return
|
|
940
1106
|
end
|
|
941
1107
|
|
|
942
|
-
set_update(
|
|
943
|
-
p2_info = @p2_infos[
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1108
|
+
set_update(p2)
|
|
1109
|
+
p2_info = @p2_infos[p2]
|
|
1110
|
+
p2_info[:in] += data.bytesize
|
|
1111
|
+
im = p2_info[:im]
|
|
1112
|
+
p2_id = p2_info[:p2_id]
|
|
947
1113
|
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
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
|
|
951
1117
|
end
|
|
952
1118
|
|
|
953
|
-
|
|
954
|
-
p2_id = p2_info[ :p2_id ]
|
|
955
|
-
add_proxy_wbuff( proxy, pack_p2_traffic( p2_id, data ) )
|
|
1119
|
+
im_info = @im_infos[im]
|
|
956
1120
|
|
|
957
|
-
unless
|
|
958
|
-
|
|
959
|
-
|
|
1121
|
+
unless im_info
|
|
1122
|
+
close_p2(p2)
|
|
1123
|
+
return
|
|
1124
|
+
end
|
|
960
1125
|
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
1126
|
+
data = pack_p2_traffic(p2_id, data)
|
|
1127
|
+
|
|
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)
|
|
966
1134
|
end
|
|
967
1135
|
end
|
|
968
1136
|
|
|
969
|
-
def read_p2d(
|
|
1137
|
+
def read_p2d(p2d)
|
|
970
1138
|
check_expire_p2s
|
|
971
1139
|
|
|
972
1140
|
begin
|
|
973
1141
|
p2, addrinfo = p2d.accept_nonblock
|
|
974
1142
|
rescue Exception => e
|
|
975
|
-
puts "p2d accept #{
|
|
1143
|
+
puts "p2d accept #{e.class}"
|
|
976
1144
|
return
|
|
977
1145
|
end
|
|
978
1146
|
|
|
979
|
-
p2d_info = @p2d_infos[
|
|
980
|
-
im = p2d_info[
|
|
981
|
-
p2_id = rand(
|
|
982
|
-
|
|
983
|
-
p2_info = {
|
|
1147
|
+
p2d_info = @p2d_infos[p2d]
|
|
1148
|
+
im = p2d_info[:im]
|
|
1149
|
+
p2_id = rand((2 ** 64) - 2) + 1
|
|
1150
|
+
@p2_infos[p2] = {
|
|
984
1151
|
addrinfo: addrinfo,
|
|
985
1152
|
closing: false,
|
|
986
1153
|
im: im,
|
|
1154
|
+
in: 0,
|
|
1155
|
+
is_big: false, # 收来的流量是否是大流量
|
|
987
1156
|
overflowing: false,
|
|
988
1157
|
p2_id: p2_id,
|
|
989
1158
|
wbuff: ''
|
|
990
1159
|
}
|
|
991
|
-
|
|
992
|
-
@
|
|
993
|
-
add_read( p2, :p2 )
|
|
994
|
-
im_info = @im_infos[ im ]
|
|
1160
|
+
add_read(p2, :p2)
|
|
1161
|
+
im_info = @im_infos[im]
|
|
995
1162
|
return unless im_info
|
|
996
|
-
proxy = im_info[
|
|
997
|
-
puts "add h_a_new_p2 #{
|
|
998
|
-
msg = "#{
|
|
999
|
-
add_proxy_wbuff(
|
|
1163
|
+
proxy = im_info[:proxy]
|
|
1164
|
+
puts "add h_a_new_p2 #{im} #{p2_id}"
|
|
1165
|
+
msg = "#{@h_a_new_p2}#{[p2_id].pack('Q>')}"
|
|
1166
|
+
add_proxy_wbuff(proxy, pack_a_chunk(msg))
|
|
1000
1167
|
end
|
|
1001
1168
|
|
|
1002
|
-
def read_rsv(
|
|
1169
|
+
def read_rsv(rsv)
|
|
1003
1170
|
begin
|
|
1004
1171
|
data, addrinfo, rflags, *controls = rsv.recvmsg
|
|
1005
1172
|
rescue Exception => e
|
|
1006
|
-
puts "rsv recvmsg #{
|
|
1007
|
-
close_rsv(
|
|
1173
|
+
puts "rsv recvmsg #{e.class}"
|
|
1174
|
+
close_rsv(rsv)
|
|
1008
1175
|
return
|
|
1009
1176
|
end
|
|
1010
1177
|
|
|
1011
1178
|
return if data.empty?
|
|
1012
1179
|
|
|
1013
|
-
if data.bytesize <= 65526
|
|
1014
|
-
rsv_info = @rsv_infos[
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1180
|
+
if data.bytesize <= 65526
|
|
1181
|
+
rsv_info = @rsv_infos[rsv]
|
|
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
|
|
1020
1192
|
else
|
|
1021
|
-
puts "response too big? #{
|
|
1193
|
+
puts "response too big? #{data.bytesize}"
|
|
1022
1194
|
end
|
|
1023
1195
|
|
|
1024
|
-
close_rsv(
|
|
1196
|
+
close_rsv(rsv)
|
|
1025
1197
|
end
|
|
1026
1198
|
|
|
1027
|
-
def read_proxy(
|
|
1199
|
+
def read_proxy(proxy)
|
|
1028
1200
|
begin
|
|
1029
|
-
data = proxy.read_nonblock(
|
|
1201
|
+
data = proxy.read_nonblock(READ_SIZE)
|
|
1030
1202
|
rescue Errno::ENOTCONN => e
|
|
1031
1203
|
return
|
|
1032
1204
|
rescue Exception => e
|
|
1033
|
-
|
|
1205
|
+
puts "read proxy #{e.class}" if @is_debug
|
|
1206
|
+
close_proxy(proxy)
|
|
1034
1207
|
return
|
|
1035
1208
|
end
|
|
1036
1209
|
|
|
1037
|
-
set_update(
|
|
1038
|
-
proxy_info = @proxy_infos[
|
|
1039
|
-
im = proxy_info[
|
|
1040
|
-
data = "#{
|
|
1210
|
+
set_update(proxy)
|
|
1211
|
+
proxy_info = @proxy_infos[proxy]
|
|
1212
|
+
im = proxy_info[:im]
|
|
1213
|
+
data = "#{proxy_info[:rbuff]}#{data}"
|
|
1041
1214
|
|
|
1042
|
-
unless im
|
|
1043
|
-
if data.bytesize < @head_len + 1
|
|
1044
|
-
proxy_info[
|
|
1215
|
+
unless im
|
|
1216
|
+
if data.bytesize < @head_len + 1
|
|
1217
|
+
proxy_info[:rbuff] = data
|
|
1045
1218
|
return
|
|
1046
1219
|
end
|
|
1047
1220
|
|
|
1048
|
-
len = data[
|
|
1221
|
+
len = data[@head_len].unpack('C').first
|
|
1049
1222
|
|
|
1050
|
-
if len == 0
|
|
1223
|
+
if len == 0
|
|
1051
1224
|
puts "im zero len?"
|
|
1052
1225
|
return
|
|
1053
1226
|
end
|
|
1054
1227
|
|
|
1055
|
-
if data.bytesize < @head_len + 1 + len
|
|
1056
|
-
proxy_info[
|
|
1228
|
+
if data.bytesize < @head_len + 1 + len
|
|
1229
|
+
proxy_info[:rbuff] = data
|
|
1057
1230
|
return
|
|
1058
1231
|
end
|
|
1059
1232
|
|
|
1060
|
-
im = data[
|
|
1233
|
+
im = data[@head_len + 1, len]
|
|
1061
1234
|
|
|
1062
|
-
if @im_infos.any?
|
|
1063
|
-
|
|
1064
|
-
return
|
|
1065
|
-
end
|
|
1235
|
+
if @im_infos.any?
|
|
1236
|
+
im_info = @im_infos[im]
|
|
1066
1237
|
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1238
|
+
unless im_info
|
|
1239
|
+
puts "unknown im #{im.inspect}"
|
|
1240
|
+
return
|
|
1241
|
+
end
|
|
1070
1242
|
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1243
|
+
if im_info[:proxy] && !im_info[:proxy].closed?
|
|
1244
|
+
puts "proxy already alive #{im.inspect}"
|
|
1245
|
+
return
|
|
1246
|
+
end
|
|
1075
1247
|
end
|
|
1076
1248
|
|
|
1077
|
-
|
|
1078
|
-
|
|
1249
|
+
puts "proxy got im #{im}"
|
|
1250
|
+
proxy_info[:im] = im
|
|
1251
|
+
|
|
1252
|
+
if im_info
|
|
1253
|
+
im_info[:proxy] = proxy
|
|
1254
|
+
im_info[:proxy_connect_at] = Time.new
|
|
1255
|
+
im_info[:addrinfo] = proxy_info[:addrinfo]
|
|
1256
|
+
im_info[:p2d] = new_a_p2d(im_info[:p2d_host], im_info[:p2d_port], im) unless im_info[:p2d]
|
|
1257
|
+
end
|
|
1258
|
+
|
|
1259
|
+
add_proxy_wbuff(proxy, pack_a_chunk(@h_heartbeat))
|
|
1260
|
+
data = data[(@head_len + 1 + len)..-1]
|
|
1079
1261
|
return if data.empty?
|
|
1080
1262
|
end
|
|
1081
1263
|
|
|
1082
|
-
im_info = @im_infos[
|
|
1083
|
-
im_info[
|
|
1084
|
-
msgs, part = decode_to_msgs(
|
|
1085
|
-
msgs.each{
|
|
1086
|
-
proxy_info[
|
|
1264
|
+
im_info = @im_infos[im]
|
|
1265
|
+
im_info[:in] += data.bytesize if im_info
|
|
1266
|
+
msgs, part = decode_to_msgs(data)
|
|
1267
|
+
msgs.each{|msg| deal_msg(msg, proxy)}
|
|
1268
|
+
proxy_info[:rbuff] = part
|
|
1087
1269
|
end
|
|
1088
1270
|
|
|
1089
|
-
def read_proxyd(
|
|
1271
|
+
def read_proxyd(proxyd)
|
|
1090
1272
|
check_expire_proxies
|
|
1091
1273
|
|
|
1092
1274
|
begin
|
|
1093
1275
|
proxy, addrinfo = proxyd.accept_nonblock
|
|
1094
1276
|
rescue Exception => e
|
|
1095
|
-
puts "accept a proxy #{
|
|
1277
|
+
puts "accept a proxy #{e.class}"
|
|
1096
1278
|
return
|
|
1097
1279
|
end
|
|
1098
1280
|
|
|
1099
|
-
puts "accept a proxy #{
|
|
1281
|
+
puts "accept a proxy #{addrinfo.ip_unpack.inspect}"
|
|
1100
1282
|
|
|
1101
|
-
|
|
1283
|
+
@proxy_infos[proxy] = {
|
|
1102
1284
|
addrinfo: addrinfo,
|
|
1103
1285
|
im: nil,
|
|
1104
|
-
paused_dsts: [],
|
|
1105
|
-
paused_p2s: [],
|
|
1106
1286
|
rbuff: '',
|
|
1107
|
-
src_infos: {}, # src_id => { :created_at :dst :rbuff }
|
|
1108
1287
|
wbuff: ''
|
|
1109
1288
|
}
|
|
1110
|
-
|
|
1111
|
-
@proxy_infos[ proxy ] = proxy_info
|
|
1112
|
-
add_read( proxy, :proxy )
|
|
1289
|
+
add_read(proxy, :proxy)
|
|
1113
1290
|
end
|
|
1114
1291
|
|
|
1115
|
-
def resolve_domain_port(
|
|
1292
|
+
def resolve_domain_port(domain_port, src_id)
|
|
1116
1293
|
return if domain_port.nil? || domain_port.empty?
|
|
1117
|
-
colon_idx = domain_port.rindex(
|
|
1294
|
+
colon_idx = domain_port.rindex(':')
|
|
1118
1295
|
return unless colon_idx
|
|
1296
|
+
src_info = @src_infos[src_id]
|
|
1297
|
+
return unless src_info
|
|
1298
|
+
im = src_info[:im]
|
|
1119
1299
|
|
|
1120
|
-
domain = domain_port[
|
|
1121
|
-
port = domain_port[
|
|
1300
|
+
domain = domain_port[0...colon_idx]
|
|
1301
|
+
port = domain_port[(colon_idx + 1)..-1].to_i
|
|
1122
1302
|
|
|
1123
|
-
if (
|
|
1303
|
+
if (domain !~ /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/) || (domain =~ /^((0\.\d{1,3}\.\d{1,3}\.\d{1,3})|(10\.\d{1,3}\.\d{1,3}\.\d{1,3})|(127\.\d{1,3}\.\d{1,3}\.\d{1,3})|(169\.254\.\d{1,3}\.\d{1,3})|(172\.((1[6-9])|(2\d)|(3[01]))\.\d{1,3}\.\d{1,3})|(192\.168\.\d{1,3}\.\d{1,3})|(255\.255\.255\.255)|(localhost))$/)
|
|
1124
1304
|
# 忽略非法域名,内网地址
|
|
1125
|
-
puts "ignore #{
|
|
1305
|
+
puts "ignore #{domain}"
|
|
1126
1306
|
return
|
|
1127
1307
|
end
|
|
1128
1308
|
|
|
1129
|
-
if domain =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$/
|
|
1309
|
+
if domain =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$/
|
|
1130
1310
|
# ipv4
|
|
1131
|
-
new_a_dst(
|
|
1311
|
+
new_a_dst(domain, domain, port, src_id)
|
|
1132
1312
|
return
|
|
1133
1313
|
end
|
|
1134
1314
|
|
|
1135
|
-
|
|
1315
|
+
ip, created_at = @resolv_caches[domain]
|
|
1136
1316
|
|
|
1137
|
-
if
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
if Time.new - created_at < @expire_resolv_cache then
|
|
1141
|
-
new_a_dst( domain, ip, port, src_id, proxy )
|
|
1317
|
+
if ip
|
|
1318
|
+
if Time.new - created_at < @expire_resolv_cache
|
|
1319
|
+
new_a_dst(domain, ip, port, src_id)
|
|
1142
1320
|
return
|
|
1143
1321
|
end
|
|
1144
1322
|
|
|
1145
|
-
@resolv_caches.delete(
|
|
1323
|
+
@resolv_caches.delete(domain)
|
|
1146
1324
|
end
|
|
1147
1325
|
|
|
1148
1326
|
begin
|
|
1149
|
-
data = pack_a_query(
|
|
1327
|
+
data = pack_a_query(domain)
|
|
1150
1328
|
rescue Exception => e
|
|
1151
|
-
puts "dns pack a query #{
|
|
1329
|
+
puts "dns pack a query #{e.class} #{e.message} #{domain}" if @is_debug
|
|
1152
1330
|
return
|
|
1153
1331
|
end
|
|
1154
1332
|
|
|
1155
1333
|
check_expire_dnses
|
|
1156
|
-
dns = Socket.new(
|
|
1334
|
+
dns = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
1157
1335
|
|
|
1158
1336
|
begin
|
|
1159
|
-
@nameserver_addrs.each{
|
|
1337
|
+
@nameserver_addrs.each{|addr| dns.sendmsg(data, 0, addr)}
|
|
1160
1338
|
rescue Exception => e
|
|
1161
|
-
puts "dns send data #{
|
|
1339
|
+
puts "dns send data #{e.class} #{domain}"
|
|
1162
1340
|
dns.close
|
|
1163
1341
|
return
|
|
1164
1342
|
end
|
|
1165
1343
|
|
|
1166
|
-
|
|
1344
|
+
@dns_infos[dns] = {
|
|
1167
1345
|
domain: domain,
|
|
1168
1346
|
im: im,
|
|
1169
1347
|
port: port,
|
|
1170
|
-
proxy: proxy,
|
|
1171
1348
|
src_id: src_id
|
|
1172
1349
|
}
|
|
1173
|
-
|
|
1174
|
-
@dns_infos[ dns ] = dns_info
|
|
1175
|
-
add_read( dns, :dns )
|
|
1350
|
+
add_read(dns, :dns)
|
|
1176
1351
|
end
|
|
1177
1352
|
|
|
1178
|
-
def send_data(
|
|
1353
|
+
def send_data(sock, data, target_addr)
|
|
1179
1354
|
begin
|
|
1180
|
-
sock.sendmsg(
|
|
1355
|
+
sock.sendmsg(data, 0, target_addr)
|
|
1181
1356
|
rescue Exception => e
|
|
1182
|
-
puts "sendmsg #{
|
|
1357
|
+
puts "sendmsg #{e.class}"
|
|
1183
1358
|
end
|
|
1184
1359
|
end
|
|
1185
1360
|
|
|
1186
|
-
def set_dst_closing(
|
|
1361
|
+
def set_dst_closing(dst)
|
|
1187
1362
|
return if dst.nil? || dst.closed?
|
|
1188
|
-
dst_info = @dst_infos[
|
|
1189
|
-
return if dst_info.nil? || dst_info[
|
|
1190
|
-
dst_info[
|
|
1191
|
-
add_write(
|
|
1363
|
+
dst_info = @dst_infos[dst]
|
|
1364
|
+
return if dst_info.nil? || dst_info[:closing]
|
|
1365
|
+
dst_info[:closing] = true
|
|
1366
|
+
add_write(dst)
|
|
1192
1367
|
end
|
|
1193
1368
|
|
|
1194
|
-
def set_p2_closing(
|
|
1369
|
+
def set_p2_closing(p2)
|
|
1195
1370
|
return if p2.nil? || p2.closed?
|
|
1196
|
-
p2_info = @p2_infos[
|
|
1197
|
-
return if p2_info.nil? || p2_info[
|
|
1198
|
-
p2_info[
|
|
1199
|
-
add_write(
|
|
1371
|
+
p2_info = @p2_infos[p2]
|
|
1372
|
+
return if p2_info.nil? || p2_info[:closing]
|
|
1373
|
+
p2_info[:closing] = true
|
|
1374
|
+
add_write(p2)
|
|
1200
1375
|
end
|
|
1201
1376
|
|
|
1202
|
-
def set_update(
|
|
1203
|
-
@updates[
|
|
1377
|
+
def set_update(sock)
|
|
1378
|
+
@updates[sock] = Time.new
|
|
1204
1379
|
|
|
1205
|
-
if @updates_limit - @updates.size <= 20
|
|
1206
|
-
puts "updates #{
|
|
1380
|
+
if @updates_limit - @updates.size <= 20
|
|
1381
|
+
puts "updates #{@updates.size}"
|
|
1207
1382
|
end
|
|
1208
1383
|
|
|
1209
|
-
if @updates.size >= @updates_limit
|
|
1384
|
+
if @updates.size >= @updates_limit
|
|
1210
1385
|
puts "eliminate updates"
|
|
1211
1386
|
|
|
1212
|
-
@updates.keys.each do |
|
|
1213
|
-
case @roles[
|
|
1387
|
+
@updates.keys.each do |_sock|
|
|
1388
|
+
case @roles[_sock]
|
|
1389
|
+
when :big
|
|
1390
|
+
close_big(_sock)
|
|
1214
1391
|
when :dns
|
|
1215
|
-
close_dns(
|
|
1392
|
+
close_dns(_sock)
|
|
1216
1393
|
when :dst
|
|
1217
|
-
close_dst(
|
|
1394
|
+
close_dst(_sock)
|
|
1218
1395
|
when :mem
|
|
1219
|
-
close_mem(
|
|
1396
|
+
close_mem(_sock)
|
|
1220
1397
|
when :p2
|
|
1221
|
-
close_p2(
|
|
1398
|
+
close_p2(_sock)
|
|
1222
1399
|
when :proxy
|
|
1223
|
-
close_proxy(
|
|
1400
|
+
close_proxy(_sock)
|
|
1224
1401
|
when :rsv
|
|
1225
|
-
close_rsv(
|
|
1402
|
+
close_rsv(_sock)
|
|
1226
1403
|
else
|
|
1227
|
-
close_sock(
|
|
1404
|
+
close_sock(_sock)
|
|
1228
1405
|
end
|
|
1229
1406
|
end
|
|
1230
1407
|
|
|
@@ -1232,177 +1409,228 @@ module Girl
|
|
|
1232
1409
|
end
|
|
1233
1410
|
end
|
|
1234
1411
|
|
|
1235
|
-
def
|
|
1236
|
-
|
|
1237
|
-
|
|
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)
|
|
1238
1418
|
return
|
|
1239
1419
|
end
|
|
1240
1420
|
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
data = dst_info[ :wbuff ]
|
|
1421
|
+
im = big_info[:im]
|
|
1422
|
+
im_info = @im_infos[im]
|
|
1244
1423
|
|
|
1245
|
-
if
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
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]
|
|
1251
1432
|
|
|
1433
|
+
if data.empty?
|
|
1434
|
+
@writes.delete(big)
|
|
1252
1435
|
return
|
|
1253
1436
|
end
|
|
1254
1437
|
|
|
1255
1438
|
begin
|
|
1256
|
-
written =
|
|
1439
|
+
written = big.write_nonblock(data)
|
|
1257
1440
|
rescue Errno::EINPROGRESS
|
|
1258
1441
|
return
|
|
1259
1442
|
rescue Exception => e
|
|
1260
|
-
|
|
1443
|
+
close_big(big)
|
|
1261
1444
|
return
|
|
1262
1445
|
end
|
|
1263
1446
|
|
|
1264
|
-
set_update(
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
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
|
|
1271
1455
|
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
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
|
|
1279
1465
|
end
|
|
1280
1466
|
end
|
|
1281
1467
|
|
|
1282
|
-
def
|
|
1283
|
-
|
|
1284
|
-
|
|
1468
|
+
def write_dst(dst)
|
|
1469
|
+
dst_info = @dst_infos[dst]
|
|
1470
|
+
|
|
1471
|
+
unless dst_info
|
|
1472
|
+
puts "dst info not found delete dst"
|
|
1473
|
+
@writes.delete(dst)
|
|
1285
1474
|
return
|
|
1286
1475
|
end
|
|
1287
1476
|
|
|
1288
|
-
|
|
1289
|
-
data =
|
|
1477
|
+
dst_info[:connected] = true
|
|
1478
|
+
data = dst_info[:wbuff]
|
|
1479
|
+
|
|
1480
|
+
if data.empty?
|
|
1481
|
+
if dst_info[:closing]
|
|
1482
|
+
close_dst(dst)
|
|
1483
|
+
else
|
|
1484
|
+
@writes.delete(dst)
|
|
1485
|
+
end
|
|
1290
1486
|
|
|
1291
|
-
if data.empty? then
|
|
1292
|
-
@writes.delete( mem )
|
|
1293
|
-
close_mem( mem )
|
|
1294
1487
|
return
|
|
1295
1488
|
end
|
|
1296
1489
|
|
|
1297
1490
|
begin
|
|
1298
|
-
written =
|
|
1491
|
+
written = dst.write_nonblock(data)
|
|
1299
1492
|
rescue Errno::EINPROGRESS
|
|
1300
1493
|
return
|
|
1301
1494
|
rescue Exception => e
|
|
1302
|
-
|
|
1495
|
+
close_dst(dst)
|
|
1303
1496
|
return
|
|
1304
1497
|
end
|
|
1305
1498
|
|
|
1306
|
-
set_update(
|
|
1307
|
-
|
|
1308
|
-
|
|
1499
|
+
set_update(dst)
|
|
1500
|
+
im = dst_info[:im]
|
|
1501
|
+
im_info = @im_infos[im]
|
|
1502
|
+
|
|
1503
|
+
if im_info
|
|
1504
|
+
im_info[:out] += written
|
|
1505
|
+
big = im_info[:big]
|
|
1506
|
+
end
|
|
1507
|
+
|
|
1508
|
+
data = data[written..-1]
|
|
1509
|
+
dst_info[:wbuff] = data
|
|
1510
|
+
domain = dst_info[:domain]
|
|
1511
|
+
|
|
1512
|
+
if dst_info[:wbuff].empty? && dst_info[:overflowing]
|
|
1513
|
+
puts "dst empty #{im} #{domain}"
|
|
1514
|
+
dst_info[:overflowing] = false
|
|
1515
|
+
|
|
1516
|
+
if big
|
|
1517
|
+
puts "resume big"
|
|
1518
|
+
add_read(big)
|
|
1519
|
+
end
|
|
1520
|
+
end
|
|
1309
1521
|
end
|
|
1310
1522
|
|
|
1311
|
-
def
|
|
1312
|
-
|
|
1313
|
-
|
|
1523
|
+
def write_mem(mem)
|
|
1524
|
+
mem_info = @mem_infos[mem]
|
|
1525
|
+
|
|
1526
|
+
unless mem_info
|
|
1527
|
+
puts "mem info not found delete mem"
|
|
1528
|
+
@writes.delete(mem)
|
|
1314
1529
|
return
|
|
1315
1530
|
end
|
|
1531
|
+
|
|
1532
|
+
data = mem_info[:wbuff]
|
|
1316
1533
|
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1534
|
+
if data.empty?
|
|
1535
|
+
@writes.delete(mem)
|
|
1536
|
+
close_mem(mem)
|
|
1537
|
+
return
|
|
1538
|
+
end
|
|
1320
1539
|
|
|
1321
|
-
|
|
1322
|
-
|
|
1540
|
+
begin
|
|
1541
|
+
written = mem.write_nonblock(data)
|
|
1542
|
+
rescue Errno::EINPROGRESS
|
|
1543
|
+
return
|
|
1544
|
+
rescue Exception => e
|
|
1545
|
+
close_mem(mem)
|
|
1323
1546
|
return
|
|
1324
1547
|
end
|
|
1325
1548
|
|
|
1326
|
-
|
|
1549
|
+
set_update(mem)
|
|
1550
|
+
data = data[written..-1]
|
|
1551
|
+
mem_info[:wbuff] = data
|
|
1552
|
+
end
|
|
1553
|
+
|
|
1554
|
+
def write_p2(p2)
|
|
1555
|
+
p2_info = @p2_infos[p2]
|
|
1327
1556
|
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1557
|
+
unless p2_info
|
|
1558
|
+
puts "p2 info not found delete p2"
|
|
1559
|
+
@writes.delete(p2)
|
|
1560
|
+
return
|
|
1561
|
+
end
|
|
1562
|
+
|
|
1563
|
+
data = p2_info[:wbuff]
|
|
1564
|
+
|
|
1565
|
+
if data.empty?
|
|
1566
|
+
if p2_info[:closing]
|
|
1567
|
+
close_p2(p2)
|
|
1331
1568
|
else
|
|
1332
|
-
@writes.delete(
|
|
1569
|
+
@writes.delete(p2)
|
|
1333
1570
|
end
|
|
1334
1571
|
|
|
1335
1572
|
return
|
|
1336
1573
|
end
|
|
1337
1574
|
|
|
1338
1575
|
begin
|
|
1339
|
-
written = p2.write_nonblock(
|
|
1576
|
+
written = p2.write_nonblock(data)
|
|
1340
1577
|
rescue Errno::EINPROGRESS
|
|
1341
1578
|
return
|
|
1342
1579
|
rescue Exception => e
|
|
1343
|
-
close_p2(
|
|
1580
|
+
close_p2(p2)
|
|
1344
1581
|
return
|
|
1345
1582
|
end
|
|
1346
1583
|
|
|
1347
|
-
set_update(
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1584
|
+
set_update(p2)
|
|
1585
|
+
im = p2_info[:im]
|
|
1586
|
+
im_info = @im_infos[im]
|
|
1587
|
+
big = im_info[:big] if im_info
|
|
1588
|
+
data = data[written..-1]
|
|
1589
|
+
p2_info[:wbuff] = data
|
|
1590
|
+
p2_id = p2_info[:p2_id]
|
|
1591
|
+
|
|
1592
|
+
if p2_info[:overflowing] && p2_info[:wbuff].empty?
|
|
1593
|
+
puts "p2 empty #{im} #{p2_id}"
|
|
1594
|
+
p2_info[:overflowing] = false
|
|
1351
1595
|
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
msg = "#{ @h_p2_underhalf }#{ [ p2_id ].pack( 'Q>' ) }"
|
|
1357
|
-
add_proxy_wbuff( proxy, pack_a_chunk( msg ) )
|
|
1358
|
-
p2_info[ :overflowing ] = false
|
|
1596
|
+
if big
|
|
1597
|
+
puts "resume big"
|
|
1598
|
+
add_read(big)
|
|
1599
|
+
end
|
|
1359
1600
|
end
|
|
1360
1601
|
end
|
|
1361
1602
|
|
|
1362
|
-
def write_proxy(
|
|
1363
|
-
|
|
1364
|
-
|
|
1603
|
+
def write_proxy(proxy)
|
|
1604
|
+
proxy_info = @proxy_infos[proxy]
|
|
1605
|
+
|
|
1606
|
+
unless proxy_info
|
|
1607
|
+
puts "proxy info not found delete proxy"
|
|
1608
|
+
@writes.delete(proxy)
|
|
1365
1609
|
return
|
|
1366
1610
|
end
|
|
1367
1611
|
|
|
1368
|
-
|
|
1369
|
-
data = proxy_info[ :wbuff ]
|
|
1612
|
+
data = proxy_info[:wbuff]
|
|
1370
1613
|
|
|
1371
|
-
if data.empty?
|
|
1372
|
-
@writes.delete(
|
|
1614
|
+
if data.empty?
|
|
1615
|
+
@writes.delete(proxy)
|
|
1373
1616
|
return
|
|
1374
1617
|
end
|
|
1375
1618
|
|
|
1376
1619
|
begin
|
|
1377
|
-
written = proxy.write_nonblock(
|
|
1620
|
+
written = proxy.write_nonblock(data)
|
|
1378
1621
|
rescue Errno::EINPROGRESS
|
|
1379
1622
|
return
|
|
1380
1623
|
rescue Exception => e
|
|
1381
|
-
close_proxy(
|
|
1624
|
+
close_proxy(proxy)
|
|
1382
1625
|
return
|
|
1383
1626
|
end
|
|
1384
1627
|
|
|
1385
|
-
set_update(
|
|
1386
|
-
im = proxy_info[
|
|
1387
|
-
im_info = @im_infos[
|
|
1388
|
-
im_info[
|
|
1389
|
-
data = data[
|
|
1390
|
-
proxy_info[
|
|
1391
|
-
bytesize = proxy_info[ :wbuff ].bytesize
|
|
1392
|
-
|
|
1393
|
-
if bytesize < RESUME_BELOW then
|
|
1394
|
-
if proxy_info[ :paused_dsts ].any? then
|
|
1395
|
-
puts "proxy underhalf resume dsts #{ im } #{ proxy_info[ :paused_dsts ].size }"
|
|
1396
|
-
proxy_info[ :paused_dsts ].each{ | dst | add_read( dst ) }
|
|
1397
|
-
proxy_info[ :paused_dsts ].clear
|
|
1398
|
-
end
|
|
1399
|
-
|
|
1400
|
-
if proxy_info[ :paused_p2s ].any? then
|
|
1401
|
-
puts "proxy underhalf resume p2s #{ im } #{ proxy_info[ :paused_p2s ].size }"
|
|
1402
|
-
proxy_info[ :paused_p2s ].each{ | p2 | add_read( p2 ) }
|
|
1403
|
-
proxy_info[ :paused_p2s ].clear
|
|
1404
|
-
end
|
|
1405
|
-
end
|
|
1628
|
+
set_update(proxy)
|
|
1629
|
+
im = proxy_info[:im]
|
|
1630
|
+
im_info = @im_infos[im]
|
|
1631
|
+
im_info[:out] += written if im_info
|
|
1632
|
+
data = data[written..-1]
|
|
1633
|
+
proxy_info[:wbuff] = data
|
|
1406
1634
|
end
|
|
1407
1635
|
|
|
1408
1636
|
end
|