girl 9.1.2 → 9.1.3
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 +2 -2
- data/lib/girl/proxy.rb +66 -75
- data/lib/girl/proxy_worker.rb +968 -978
- data/lib/girl/proxyd.rb +55 -58
- data/lib/girl/proxyd_worker.rb +626 -629
- data/lib/girl/version.rb +1 -1
- metadata +3 -6
data/lib/girl/proxy_worker.rb
CHANGED
|
@@ -48,34 +48,34 @@ module Girl
|
|
|
48
48
|
is_server_fastopen )
|
|
49
49
|
|
|
50
50
|
@proxyd_host = proxyd_host
|
|
51
|
-
@proxyd_addr = Socket.sockaddr_in(
|
|
52
|
-
@nameserver_addrs = nameservers.map{
|
|
51
|
+
@proxyd_addr = Socket.sockaddr_in(proxyd_port, proxyd_host)
|
|
52
|
+
@nameserver_addrs = nameservers.map{|n| Socket.sockaddr_in(53, n)}
|
|
53
53
|
@im = im
|
|
54
54
|
@directs = directs
|
|
55
55
|
@remotes = remotes
|
|
56
|
-
@local_ips = Socket.ip_address_list.select{
|
|
57
|
-
@update_roles = [
|
|
56
|
+
@local_ips = Socket.ip_address_list.select{|info| info.ipv4?}.map{|info| info.ip_address}
|
|
57
|
+
@update_roles = [:dns, :dst, :mem, :p1, :src, :rsv] # 参与淘汰的角色
|
|
58
58
|
@updates_limit = 1007 # 淘汰池上限,1015(mac) - info, infod, memd, proxy, redir, relayd, rsvd, tspd
|
|
59
59
|
@eliminate_count = 0 # 淘汰次数
|
|
60
60
|
@reads = [] # 读池
|
|
61
61
|
@writes = [] # 写池
|
|
62
62
|
@roles = {} # sock => :dns / :dst / :girl / :infod / :mem / :memd / :p1 / :proxy / :redir / :relay / :relayd / :rsv / :rsvd / :src / :tspd
|
|
63
63
|
@updates = {} # sock => updated_at
|
|
64
|
-
@proxy_infos = {} # proxy => {
|
|
65
|
-
@mem_infos = {} # mem => {
|
|
66
|
-
@relay_infos = {} # relay => {
|
|
67
|
-
@girl_infos = {} # girl => {
|
|
68
|
-
@src_infos = {} # src => {
|
|
69
|
-
@dst_infos = {} # dst => {
|
|
70
|
-
@dns_infos = {} # dns => {
|
|
71
|
-
@rsv_infos = {} # rsv => {
|
|
72
|
-
@near_infos = {} # near_id => {
|
|
73
|
-
@resolv_caches = {} # domain => [
|
|
64
|
+
@proxy_infos = {} # proxy => {:is_syn :paused_p1s :paused_srcs :rbuff :recv_at :wbuff}
|
|
65
|
+
@mem_infos = {} # mem => {:wbuff}
|
|
66
|
+
@relay_infos = {} # relay => {:addrinfo :closing :girl :overflowing :wbuff}
|
|
67
|
+
@girl_infos = {} # girl => {:closing :connected :is_syn :overflowing :relay :wbuff}
|
|
68
|
+
@src_infos = {} # src => {:addrinfo :closing :destination_domain :destination_port :dst :is_connect :overflowing :proxy_proto :proxy_type :rbuff :src_id :wbuff}
|
|
69
|
+
@dst_infos = {} # dst => {:closing :connected :domain :ip :overflowing :port :src :wbuff}
|
|
70
|
+
@dns_infos = {} # dns => {:domain :src}
|
|
71
|
+
@rsv_infos = {} # rsv => {:addrinfo :domain :type}
|
|
72
|
+
@near_infos = {} # near_id => {:addrinfo :created_at :domain :id :type}
|
|
73
|
+
@resolv_caches = {} # domain => [ip, created_at]
|
|
74
74
|
@is_direct_caches = {} # ip => true / false
|
|
75
|
-
@response_caches = {} # domain => [
|
|
76
|
-
@response6_caches = {} # domain => [
|
|
77
|
-
@p1_infos = {} # p1 => {
|
|
78
|
-
@appd_addr = Socket.sockaddr_in(
|
|
75
|
+
@response_caches = {} # domain => [response, created_at, ip, is_remote]
|
|
76
|
+
@response6_caches = {} # domain => [response, created_at, ip, is_remote]
|
|
77
|
+
@p1_infos = {} # p1 => {:closing :connected :overflowing :p2_id :wbuff}
|
|
78
|
+
@appd_addr = Socket.sockaddr_in(appd_port, appd_host)
|
|
79
79
|
|
|
80
80
|
@head_len = head_len
|
|
81
81
|
@h_a_new_source = h_a_new_source
|
|
@@ -106,12 +106,12 @@ module Girl
|
|
|
106
106
|
@is_client_fastopen = is_client_fastopen
|
|
107
107
|
@is_server_fastopen = is_server_fastopen
|
|
108
108
|
|
|
109
|
-
new_a_redir(
|
|
110
|
-
new_a_infod(
|
|
111
|
-
new_a_memd(
|
|
112
|
-
new_a_relayd(
|
|
113
|
-
new_a_rsvd(
|
|
114
|
-
new_a_tspd(
|
|
109
|
+
new_a_redir(redir_host, redir_port)
|
|
110
|
+
new_a_infod(redir_port)
|
|
111
|
+
new_a_memd(memd_port)
|
|
112
|
+
new_a_relayd(relayd_host, relayd_port)
|
|
113
|
+
new_a_rsvd(tspd_host, tspd_port)
|
|
114
|
+
new_a_tspd(tspd_host, tspd_port)
|
|
115
115
|
new_a_proxy
|
|
116
116
|
end
|
|
117
117
|
|
|
@@ -120,67 +120,67 @@ module Girl
|
|
|
120
120
|
loop_heartbeat
|
|
121
121
|
|
|
122
122
|
loop do
|
|
123
|
-
rs, ws = IO.select(
|
|
123
|
+
rs, ws = IO.select(@reads, @writes)
|
|
124
124
|
|
|
125
|
-
rs.each do |
|
|
126
|
-
role = @roles[
|
|
125
|
+
rs.each do |sock|
|
|
126
|
+
role = @roles[sock]
|
|
127
127
|
|
|
128
128
|
case role
|
|
129
|
-
when :dns
|
|
130
|
-
read_dns(
|
|
131
|
-
when :dst
|
|
132
|
-
read_dst(
|
|
133
|
-
when :girl
|
|
134
|
-
read_girl(
|
|
135
|
-
when :infod
|
|
136
|
-
read_infod(
|
|
137
|
-
when :mem
|
|
138
|
-
read_mem(
|
|
139
|
-
when :memd
|
|
140
|
-
read_memd(
|
|
141
|
-
when :p1
|
|
142
|
-
read_p1(
|
|
143
|
-
when :proxy
|
|
144
|
-
read_proxy(
|
|
145
|
-
when :redir
|
|
146
|
-
read_redir(
|
|
147
|
-
when :relay
|
|
148
|
-
read_relay(
|
|
149
|
-
when :relayd
|
|
150
|
-
read_relayd(
|
|
151
|
-
when :rsv
|
|
152
|
-
read_rsv(
|
|
153
|
-
when :rsvd
|
|
154
|
-
read_rsvd(
|
|
155
|
-
when :src
|
|
156
|
-
read_src(
|
|
157
|
-
when :tspd
|
|
158
|
-
read_tspd(
|
|
129
|
+
when :dns
|
|
130
|
+
read_dns(sock)
|
|
131
|
+
when :dst
|
|
132
|
+
read_dst(sock)
|
|
133
|
+
when :girl
|
|
134
|
+
read_girl(sock)
|
|
135
|
+
when :infod
|
|
136
|
+
read_infod(sock)
|
|
137
|
+
when :mem
|
|
138
|
+
read_mem(sock)
|
|
139
|
+
when :memd
|
|
140
|
+
read_memd(sock)
|
|
141
|
+
when :p1
|
|
142
|
+
read_p1(sock)
|
|
143
|
+
when :proxy
|
|
144
|
+
read_proxy(sock)
|
|
145
|
+
when :redir
|
|
146
|
+
read_redir(sock)
|
|
147
|
+
when :relay
|
|
148
|
+
read_relay(sock)
|
|
149
|
+
when :relayd
|
|
150
|
+
read_relayd(sock)
|
|
151
|
+
when :rsv
|
|
152
|
+
read_rsv(sock)
|
|
153
|
+
when :rsvd
|
|
154
|
+
read_rsvd(sock)
|
|
155
|
+
when :src
|
|
156
|
+
read_src(sock)
|
|
157
|
+
when :tspd
|
|
158
|
+
read_tspd(sock)
|
|
159
159
|
else
|
|
160
|
-
close_sock(
|
|
160
|
+
close_sock(sock)
|
|
161
161
|
end
|
|
162
162
|
end
|
|
163
163
|
|
|
164
|
-
ws.each do |
|
|
165
|
-
role = @roles[
|
|
164
|
+
ws.each do |sock|
|
|
165
|
+
role = @roles[sock]
|
|
166
166
|
|
|
167
167
|
case role
|
|
168
|
-
when :dst
|
|
169
|
-
write_dst(
|
|
170
|
-
when :girl
|
|
171
|
-
write_girl(
|
|
172
|
-
when :mem
|
|
173
|
-
write_mem(
|
|
174
|
-
when :p1
|
|
175
|
-
write_p1(
|
|
176
|
-
when :proxy
|
|
177
|
-
write_proxy(
|
|
178
|
-
when :relay
|
|
179
|
-
write_relay(
|
|
180
|
-
when :src
|
|
181
|
-
write_src(
|
|
168
|
+
when :dst
|
|
169
|
+
write_dst(sock)
|
|
170
|
+
when :girl
|
|
171
|
+
write_girl(sock)
|
|
172
|
+
when :mem
|
|
173
|
+
write_mem(sock)
|
|
174
|
+
when :p1
|
|
175
|
+
write_p1(sock)
|
|
176
|
+
when :proxy
|
|
177
|
+
write_proxy(sock)
|
|
178
|
+
when :relay
|
|
179
|
+
write_relay(sock)
|
|
180
|
+
when :src
|
|
181
|
+
write_src(sock)
|
|
182
182
|
else
|
|
183
|
-
close_sock(
|
|
183
|
+
close_sock(sock)
|
|
184
184
|
end
|
|
185
185
|
end
|
|
186
186
|
end
|
|
@@ -195,583 +195,578 @@ module Girl
|
|
|
195
195
|
|
|
196
196
|
private
|
|
197
197
|
|
|
198
|
-
def add_dst_wbuff(
|
|
198
|
+
def add_dst_wbuff(dst, data)
|
|
199
199
|
return if dst.nil? || dst.closed? || data.nil? || data.empty?
|
|
200
|
-
dst_info = @dst_infos[
|
|
201
|
-
dst_info[
|
|
202
|
-
bytesize = dst_info[
|
|
200
|
+
dst_info = @dst_infos[dst]
|
|
201
|
+
dst_info[:wbuff] << data
|
|
202
|
+
bytesize = dst_info[:wbuff].bytesize
|
|
203
203
|
|
|
204
|
-
if bytesize >= CLOSE_ABOVE
|
|
205
|
-
puts "close overflow dst #{
|
|
206
|
-
close_dst(
|
|
204
|
+
if bytesize >= CLOSE_ABOVE
|
|
205
|
+
puts "close overflow dst #{dst_info[:domain]}"
|
|
206
|
+
close_dst(dst)
|
|
207
207
|
return
|
|
208
208
|
end
|
|
209
209
|
|
|
210
|
-
if !dst_info[
|
|
211
|
-
puts "dst overflow pause src #{
|
|
212
|
-
@reads.delete(
|
|
213
|
-
dst_info[
|
|
210
|
+
if !dst_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
211
|
+
puts "dst overflow pause src #{dst_info[:domain]}"
|
|
212
|
+
@reads.delete(dst_info[:src])
|
|
213
|
+
dst_info[:overflowing] = true
|
|
214
214
|
end
|
|
215
215
|
|
|
216
|
-
add_write(
|
|
216
|
+
add_write(dst)
|
|
217
217
|
end
|
|
218
218
|
|
|
219
|
-
def add_girl_wbuff(
|
|
219
|
+
def add_girl_wbuff(girl, data)
|
|
220
220
|
return if girl.nil? || girl.closed? || data.nil? || data.empty?
|
|
221
|
-
girl_info = @girl_infos[
|
|
222
|
-
girl_info[
|
|
223
|
-
bytesize = girl_info[
|
|
221
|
+
girl_info = @girl_infos[girl]
|
|
222
|
+
girl_info[:wbuff] << data
|
|
223
|
+
bytesize = girl_info[:wbuff].bytesize
|
|
224
224
|
|
|
225
|
-
if bytesize >= CLOSE_ABOVE
|
|
225
|
+
if bytesize >= CLOSE_ABOVE
|
|
226
226
|
puts "close overflow girl"
|
|
227
|
-
close_girl(
|
|
227
|
+
close_girl(girl)
|
|
228
228
|
return
|
|
229
229
|
end
|
|
230
230
|
|
|
231
|
-
if !girl_info[
|
|
231
|
+
if !girl_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
232
232
|
puts "girl overflow pause relay"
|
|
233
|
-
@reads.delete(
|
|
234
|
-
girl_info[
|
|
233
|
+
@reads.delete(girl_info[:relay])
|
|
234
|
+
girl_info[:overflowing] = true
|
|
235
235
|
end
|
|
236
236
|
|
|
237
|
-
add_write(
|
|
237
|
+
add_write(girl)
|
|
238
238
|
end
|
|
239
239
|
|
|
240
|
-
def add_mem_wbuff(
|
|
240
|
+
def add_mem_wbuff(mem, data)
|
|
241
241
|
return if mem.nil? || mem.closed? || data.nil? || data.empty?
|
|
242
|
-
mem_info = @mem_infos[
|
|
243
|
-
mem_info[
|
|
244
|
-
add_write(
|
|
242
|
+
mem_info = @mem_infos[mem]
|
|
243
|
+
mem_info[:wbuff] << data
|
|
244
|
+
add_write(mem)
|
|
245
245
|
end
|
|
246
246
|
|
|
247
|
-
def add_p1_wbuff(
|
|
247
|
+
def add_p1_wbuff(p1, data)
|
|
248
248
|
return if p1.nil? || p1.closed? || data.nil? || data.empty?
|
|
249
|
-
p1_info = @p1_infos[
|
|
250
|
-
p1_info[
|
|
251
|
-
bytesize = p1_info[
|
|
252
|
-
p2_id = p1_info[
|
|
249
|
+
p1_info = @p1_infos[p1]
|
|
250
|
+
p1_info[:wbuff] << data
|
|
251
|
+
bytesize = p1_info[:wbuff].bytesize
|
|
252
|
+
p2_id = p1_info[:p2_id]
|
|
253
253
|
|
|
254
|
-
if bytesize >= CLOSE_ABOVE
|
|
255
|
-
puts "close overflow p1 #{
|
|
256
|
-
close_p1(
|
|
254
|
+
if bytesize >= CLOSE_ABOVE
|
|
255
|
+
puts "close overflow p1 #{p2_id}"
|
|
256
|
+
close_p1(p1)
|
|
257
257
|
return
|
|
258
258
|
end
|
|
259
259
|
|
|
260
|
-
if !p1_info[
|
|
261
|
-
puts "add h_p1_overflow #{
|
|
262
|
-
msg = "#{
|
|
263
|
-
add_proxy_wbuff(
|
|
264
|
-
p1_info[
|
|
260
|
+
if !p1_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
261
|
+
puts "add h_p1_overflow #{p2_id}"
|
|
262
|
+
msg = "#{@h_p1_overflow}#{[p2_id].pack('Q>')}"
|
|
263
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
264
|
+
p1_info[:overflowing] = true
|
|
265
265
|
end
|
|
266
266
|
|
|
267
|
-
add_write(
|
|
267
|
+
add_write(p1)
|
|
268
268
|
end
|
|
269
269
|
|
|
270
|
-
def add_proxy_wbuff(
|
|
270
|
+
def add_proxy_wbuff(data)
|
|
271
271
|
return if @proxy.closed? || data.nil? || data.empty?
|
|
272
|
-
proxy_info = @proxy_infos[
|
|
273
|
-
proxy_info[
|
|
274
|
-
bytesize = proxy_info[
|
|
272
|
+
proxy_info = @proxy_infos[@proxy]
|
|
273
|
+
proxy_info[:wbuff] << data
|
|
274
|
+
bytesize = proxy_info[:wbuff].bytesize
|
|
275
275
|
|
|
276
|
-
if bytesize >= CLOSE_ABOVE
|
|
276
|
+
if bytesize >= CLOSE_ABOVE
|
|
277
277
|
puts "close overflow proxy"
|
|
278
|
-
close_proxy(
|
|
278
|
+
close_proxy(@proxy)
|
|
279
279
|
return
|
|
280
280
|
end
|
|
281
281
|
|
|
282
|
-
add_write(
|
|
282
|
+
add_write(@proxy)
|
|
283
283
|
end
|
|
284
284
|
|
|
285
|
-
def add_read(
|
|
286
|
-
return if sock.nil? || sock.closed? || @reads.include?(
|
|
285
|
+
def add_read(sock, role = nil)
|
|
286
|
+
return if sock.nil? || sock.closed? || @reads.include?(sock)
|
|
287
287
|
@reads << sock
|
|
288
288
|
|
|
289
|
-
if role
|
|
290
|
-
@roles[
|
|
289
|
+
if role
|
|
290
|
+
@roles[sock] = role
|
|
291
291
|
else
|
|
292
|
-
role = @roles[
|
|
292
|
+
role = @roles[sock]
|
|
293
293
|
end
|
|
294
294
|
|
|
295
|
-
if @update_roles.include?(
|
|
296
|
-
set_update( sock )
|
|
297
|
-
end
|
|
295
|
+
set_update(sock) if @update_roles.include?(role)
|
|
298
296
|
end
|
|
299
297
|
|
|
300
|
-
def add_relay_wbuff(
|
|
298
|
+
def add_relay_wbuff(relay, data)
|
|
301
299
|
return if relay.nil? || relay.closed? || data.nil? || data.empty?
|
|
302
|
-
relay_info = @relay_infos[
|
|
303
|
-
relay_info[
|
|
304
|
-
bytesize = relay_info[
|
|
300
|
+
relay_info = @relay_infos[relay]
|
|
301
|
+
relay_info[:wbuff] << data
|
|
302
|
+
bytesize = relay_info[:wbuff].bytesize
|
|
305
303
|
|
|
306
|
-
if bytesize >= CLOSE_ABOVE
|
|
307
|
-
puts "close overflow relay #{
|
|
308
|
-
close_relay(
|
|
304
|
+
if bytesize >= CLOSE_ABOVE
|
|
305
|
+
puts "close overflow relay #{relay_info[:addrinfo].ip_unpack.inspect}"
|
|
306
|
+
close_relay(relay)
|
|
309
307
|
return
|
|
310
308
|
end
|
|
311
309
|
|
|
312
|
-
if !relay_info[
|
|
313
|
-
puts "relay overflow pause girl #{
|
|
314
|
-
@reads.delete(
|
|
315
|
-
relay_info[
|
|
310
|
+
if !relay_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
311
|
+
puts "relay overflow pause girl #{relay_info[:addrinfo].ip_unpack.inspect}"
|
|
312
|
+
@reads.delete(relay_info[:girl])
|
|
313
|
+
relay_info[:overflowing] = true
|
|
316
314
|
end
|
|
317
315
|
|
|
318
|
-
add_write(
|
|
316
|
+
add_write(relay)
|
|
319
317
|
end
|
|
320
318
|
|
|
321
|
-
def add_socks5_conn_reply(
|
|
319
|
+
def add_socks5_conn_reply(src)
|
|
322
320
|
# +----+-----+-------+------+----------+----------+
|
|
323
321
|
# |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
|
|
324
322
|
# +----+-----+-------+------+----------+----------+
|
|
325
323
|
# | 1 | 1 | X'00' | 1 | Variable | 2 |
|
|
326
324
|
# +----+-----+-------+------+----------+----------+
|
|
327
325
|
redir_ip, redir_port = @redir_local_address.ip_unpack
|
|
328
|
-
data = [
|
|
329
|
-
add_src_wbuff(
|
|
326
|
+
data = [[5, 0, 0, 1].pack('C4'), IPAddr.new(redir_ip).hton, [redir_port].pack('n')].join
|
|
327
|
+
add_src_wbuff(src, data)
|
|
330
328
|
end
|
|
331
329
|
|
|
332
|
-
def add_src_rbuff(
|
|
330
|
+
def add_src_rbuff(src, data)
|
|
333
331
|
return if src.nil? || src.closed? || data.nil? || data.empty?
|
|
334
|
-
src_info = @src_infos[
|
|
335
|
-
puts "add src rbuff #{
|
|
336
|
-
src_info[
|
|
332
|
+
src_info = @src_infos[src]
|
|
333
|
+
puts "add src rbuff #{data.bytesize}" if @is_debug
|
|
334
|
+
src_info[:rbuff] << data
|
|
337
335
|
|
|
338
|
-
if src_info[
|
|
336
|
+
if src_info[:rbuff].bytesize >= WBUFF_LIMIT
|
|
339
337
|
puts "src rbuff full"
|
|
340
|
-
close_src(
|
|
338
|
+
close_src(src)
|
|
341
339
|
end
|
|
342
340
|
end
|
|
343
341
|
|
|
344
|
-
def add_src_wbuff(
|
|
342
|
+
def add_src_wbuff(src, data)
|
|
345
343
|
return if src.nil? || src.closed? || data.nil? || data.empty?
|
|
346
|
-
src_info = @src_infos[
|
|
347
|
-
src_info[
|
|
348
|
-
bytesize = src_info[
|
|
349
|
-
src_id = src_info[
|
|
350
|
-
domain = src_info[
|
|
351
|
-
|
|
352
|
-
if bytesize >= CLOSE_ABOVE
|
|
353
|
-
puts "close overflow src #{
|
|
354
|
-
close_src(
|
|
355
|
-
return
|
|
356
|
-
end
|
|
357
|
-
|
|
358
|
-
if !src_info[
|
|
359
|
-
if src_info[
|
|
360
|
-
puts "src overflow pause dst #{
|
|
361
|
-
@reads.delete(
|
|
362
|
-
elsif src_info[
|
|
363
|
-
puts "add h_src_overflow #{
|
|
364
|
-
msg = "#{
|
|
365
|
-
add_proxy_wbuff(
|
|
344
|
+
src_info = @src_infos[src]
|
|
345
|
+
src_info[:wbuff] << data
|
|
346
|
+
bytesize = src_info[:wbuff].bytesize
|
|
347
|
+
src_id = src_info[:src_id]
|
|
348
|
+
domain = src_info[:destination_domain]
|
|
349
|
+
|
|
350
|
+
if bytesize >= CLOSE_ABOVE
|
|
351
|
+
puts "close overflow src #{src_id} #{domain}"
|
|
352
|
+
close_src(src)
|
|
353
|
+
return
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
if !src_info[:overflowing] && (bytesize >= WBUFF_LIMIT)
|
|
357
|
+
if src_info[:proxy_type] == :direct
|
|
358
|
+
puts "src overflow pause dst #{src_id} #{domain}"
|
|
359
|
+
@reads.delete(src_info[:dst])
|
|
360
|
+
elsif src_info[:proxy_type] == :remote
|
|
361
|
+
puts "add h_src_overflow #{src_id} #{domain}"
|
|
362
|
+
msg = "#{@h_src_overflow}#{[src_id].pack('Q>')}"
|
|
363
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
366
364
|
end
|
|
367
365
|
|
|
368
|
-
src_info[
|
|
366
|
+
src_info[:overflowing] = true
|
|
369
367
|
end
|
|
370
368
|
|
|
371
|
-
add_write(
|
|
369
|
+
add_write(src)
|
|
372
370
|
end
|
|
373
371
|
|
|
374
|
-
def add_write(
|
|
375
|
-
return if sock.nil? || sock.closed? || @writes.include?(
|
|
372
|
+
def add_write(sock)
|
|
373
|
+
return if sock.nil? || sock.closed? || @writes.include?(sock)
|
|
376
374
|
@writes << sock
|
|
377
|
-
role = @roles[
|
|
378
|
-
|
|
379
|
-
if @update_roles.include?( role ) then
|
|
380
|
-
set_update( sock )
|
|
381
|
-
end
|
|
375
|
+
role = @roles[sock]
|
|
376
|
+
set_update(sock) if @update_roles.include?(role)
|
|
382
377
|
end
|
|
383
378
|
|
|
384
379
|
def check_expire_dnses
|
|
385
380
|
now = Time.new
|
|
386
381
|
|
|
387
|
-
@dns_infos.select{
|
|
388
|
-
puts "expire dns #{
|
|
389
|
-
close_dns(
|
|
382
|
+
@dns_infos.select{|dns, _| now.to_i - @updates[dns].to_i >= @expire_short_after}.each do |dns, info|
|
|
383
|
+
puts "expire dns #{info[:domain]}" if @is_debug
|
|
384
|
+
close_dns(dns)
|
|
390
385
|
end
|
|
391
386
|
end
|
|
392
387
|
|
|
393
388
|
def check_expire_dsts
|
|
394
389
|
now = Time.new
|
|
395
390
|
|
|
396
|
-
@dst_infos.select{
|
|
397
|
-
puts "expire dst #{
|
|
398
|
-
close_dst(
|
|
391
|
+
@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|
|
|
392
|
+
puts "expire dst #{info[:domain]}" if @is_debug
|
|
393
|
+
close_dst(dst)
|
|
399
394
|
end
|
|
400
395
|
end
|
|
401
396
|
|
|
402
397
|
def check_expire_girls
|
|
403
398
|
now = Time.new
|
|
404
399
|
|
|
405
|
-
@girl_infos.select{
|
|
400
|
+
@girl_infos.select{|girl, info| info[:connected] ? (now.to_i - @updates[girl].to_i >= @expire_long_after) : (now.to_i - @updates[girl].to_i >= @expire_connecting)}.each do |girl, _|
|
|
406
401
|
puts "expire girl" if @is_debug
|
|
407
|
-
close_girl(
|
|
402
|
+
close_girl(girl)
|
|
408
403
|
end
|
|
409
404
|
end
|
|
410
405
|
|
|
411
406
|
def check_expire_mems
|
|
412
407
|
now = Time.new
|
|
413
408
|
|
|
414
|
-
@mem_infos.select{
|
|
409
|
+
@mem_infos.select{|mem, _| now.to_i - @updates[mem].to_i >= @expire_short_after}.each do |mem, _|
|
|
415
410
|
puts "expire mem" if @is_debug
|
|
416
|
-
close_mem(
|
|
411
|
+
close_mem(mem)
|
|
417
412
|
end
|
|
418
413
|
end
|
|
419
414
|
|
|
420
415
|
def check_expire_nears
|
|
421
416
|
now = Time.new
|
|
422
417
|
|
|
423
|
-
@near_infos.select{
|
|
424
|
-
puts "expire near #{
|
|
425
|
-
@near_infos.delete(
|
|
418
|
+
@near_infos.select{|_, info| now.to_i - info[:created_at].to_i >= @expire_short_after}.each do |near_id, info|
|
|
419
|
+
puts "expire near #{info[:domain]}" if @is_debug
|
|
420
|
+
@near_infos.delete(near_id)
|
|
426
421
|
end
|
|
427
422
|
end
|
|
428
423
|
|
|
429
424
|
def check_expire_p1s
|
|
430
425
|
now = Time.new
|
|
431
426
|
|
|
432
|
-
@p1_infos.select{
|
|
433
|
-
puts "expire p1 #{
|
|
434
|
-
close_p1(
|
|
427
|
+
@p1_infos.select{|p1, info| info[:connected] ? (now.to_i - @updates[p1].to_i >= @expire_long_after) : (now.to_i - @updates[p1].to_i >= @expire_connecting)}.each do |p1, info|
|
|
428
|
+
puts "expire p1 #{info[:p2_id]}" if @is_debug
|
|
429
|
+
close_p1(p1)
|
|
435
430
|
end
|
|
436
431
|
end
|
|
437
432
|
|
|
438
433
|
def check_expire_relays
|
|
439
434
|
now = Time.new
|
|
440
435
|
|
|
441
|
-
@relay_infos.select{
|
|
442
|
-
puts "expire relay #{
|
|
443
|
-
close_relay(
|
|
436
|
+
@relay_infos.select{|relay, _| now.to_i - @updates[relay].to_i >= @expire_long_after}.each do |relay, info|
|
|
437
|
+
puts "expire relay #{info[:addrinfo].ip_unpack.inspect}" if @is_debug
|
|
438
|
+
close_relay(relay)
|
|
444
439
|
end
|
|
445
440
|
end
|
|
446
441
|
|
|
447
442
|
def check_expire_rsvs
|
|
448
443
|
now = Time.new
|
|
449
444
|
|
|
450
|
-
@rsv_infos.select{
|
|
451
|
-
puts "expire rsv #{
|
|
452
|
-
close_rsv(
|
|
445
|
+
@rsv_infos.select{|rsv, _| now.to_i - @updates[rsv].to_i >= @expire_short_after}.each do |rsv, info|
|
|
446
|
+
puts "expire rsv #{info[:domain]}" if @is_debug
|
|
447
|
+
close_rsv(rsv)
|
|
453
448
|
end
|
|
454
449
|
end
|
|
455
450
|
|
|
456
451
|
def check_expire_srcs
|
|
457
452
|
now = Time.new
|
|
458
453
|
|
|
459
|
-
@src_infos.select{
|
|
460
|
-
puts "expire src #{
|
|
461
|
-
close_src(
|
|
454
|
+
@src_infos.select{|src, _| now.to_i - @updates[src].to_i >= @expire_long_after}.each do |src, info|
|
|
455
|
+
puts "expire src #{info[:destination_domain]}" if @is_debug
|
|
456
|
+
close_src(src)
|
|
462
457
|
end
|
|
463
458
|
end
|
|
464
459
|
|
|
465
|
-
def close_dns(
|
|
460
|
+
def close_dns(dns)
|
|
466
461
|
return nil if dns.nil? || dns.closed?
|
|
467
|
-
close_sock(
|
|
468
|
-
dns_info = @dns_infos.delete(
|
|
469
|
-
puts "close dns #{
|
|
462
|
+
close_sock(dns)
|
|
463
|
+
dns_info = @dns_infos.delete(dns)
|
|
464
|
+
puts "close dns #{dns_info[:domain]}" if @is_debug
|
|
470
465
|
dns_info
|
|
471
466
|
end
|
|
472
467
|
|
|
473
|
-
def close_dst(
|
|
468
|
+
def close_dst(dst)
|
|
474
469
|
return nil if dst.nil? || dst.closed?
|
|
475
|
-
close_sock(
|
|
476
|
-
dst_info = @dst_infos.delete(
|
|
477
|
-
puts "close dst #{
|
|
478
|
-
set_src_closing(
|
|
470
|
+
close_sock(dst)
|
|
471
|
+
dst_info = @dst_infos.delete(dst)
|
|
472
|
+
puts "close dst #{dst_info[:domain]}" if @is_debug
|
|
473
|
+
set_src_closing(dst_info[:src]) if dst_info
|
|
479
474
|
dst_info
|
|
480
475
|
end
|
|
481
476
|
|
|
482
|
-
def close_girl(
|
|
477
|
+
def close_girl(girl)
|
|
483
478
|
return nil if girl.nil? || girl.closed?
|
|
484
|
-
close_sock(
|
|
485
|
-
girl_info = @girl_infos.delete(
|
|
479
|
+
close_sock(girl)
|
|
480
|
+
girl_info = @girl_infos.delete(girl)
|
|
486
481
|
puts "close girl" if @is_debug
|
|
487
|
-
set_relay_closing(
|
|
482
|
+
set_relay_closing(girl_info[:relay]) if girl_info
|
|
488
483
|
girl_info
|
|
489
484
|
end
|
|
490
485
|
|
|
491
|
-
def close_mem(
|
|
486
|
+
def close_mem(mem)
|
|
492
487
|
return nil if mem.nil? || mem.closed?
|
|
493
|
-
close_sock(
|
|
494
|
-
@mem_infos.delete(
|
|
488
|
+
close_sock(mem)
|
|
489
|
+
@mem_infos.delete(mem)
|
|
495
490
|
end
|
|
496
491
|
|
|
497
|
-
def close_p1(
|
|
492
|
+
def close_p1(p1)
|
|
498
493
|
return nil if p1.nil? || p1.closed?
|
|
499
|
-
close_sock(
|
|
500
|
-
p1_info = @p1_infos.delete(
|
|
494
|
+
close_sock(p1)
|
|
495
|
+
p1_info = @p1_infos.delete(p1)
|
|
501
496
|
|
|
502
|
-
unless @proxy.closed?
|
|
503
|
-
proxy_info = @proxy_infos[
|
|
504
|
-
proxy_info[
|
|
505
|
-
p2_id = p1_info[
|
|
506
|
-
puts "add h_p1_close #{
|
|
507
|
-
msg = "#{
|
|
508
|
-
add_proxy_wbuff(
|
|
497
|
+
unless @proxy.closed?
|
|
498
|
+
proxy_info = @proxy_infos[@proxy]
|
|
499
|
+
proxy_info[:paused_p1s].delete(p1)
|
|
500
|
+
p2_id = p1_info[:p2_id]
|
|
501
|
+
puts "add h_p1_close #{p2_id}"
|
|
502
|
+
msg = "#{@h_p1_close}#{[p2_id].pack('Q>')}"
|
|
503
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
509
504
|
end
|
|
510
505
|
|
|
511
506
|
p1_info
|
|
512
507
|
end
|
|
513
508
|
|
|
514
|
-
def close_proxy(
|
|
509
|
+
def close_proxy(proxy)
|
|
515
510
|
return if proxy.nil? || proxy.closed?
|
|
516
|
-
close_sock(
|
|
517
|
-
proxy_info = @proxy_infos.delete(
|
|
511
|
+
close_sock(proxy)
|
|
512
|
+
proxy_info = @proxy_infos.delete(proxy)
|
|
518
513
|
puts "close proxy"
|
|
519
|
-
@src_infos.each{
|
|
520
|
-
@p1_infos.each{
|
|
514
|
+
@src_infos.each{|src, _| close_src(src)}
|
|
515
|
+
@p1_infos.each{|p1, _| close_p1(p1)}
|
|
521
516
|
proxy_info
|
|
522
517
|
end
|
|
523
518
|
|
|
524
|
-
def close_relay(
|
|
519
|
+
def close_relay(relay)
|
|
525
520
|
return nil if relay.nil? || relay.closed?
|
|
526
|
-
close_sock(
|
|
527
|
-
relay_info = @relay_infos.delete(
|
|
521
|
+
close_sock(relay)
|
|
522
|
+
relay_info = @relay_infos.delete(relay)
|
|
528
523
|
puts "close relay" if @is_debug
|
|
529
|
-
set_girl_closing(
|
|
524
|
+
set_girl_closing(relay_info[:girl]) if relay_info
|
|
530
525
|
relay_info
|
|
531
526
|
end
|
|
532
527
|
|
|
533
|
-
def close_rsv(
|
|
528
|
+
def close_rsv(rsv)
|
|
534
529
|
return nil if rsv.nil? || rsv.closed?
|
|
535
|
-
close_sock(
|
|
536
|
-
rsv_info = @rsv_infos.delete(
|
|
537
|
-
puts "close rsv #{
|
|
530
|
+
close_sock(rsv)
|
|
531
|
+
rsv_info = @rsv_infos.delete(rsv)
|
|
532
|
+
puts "close rsv #{rsv_info[:domain]}" if @is_debug
|
|
538
533
|
rsv_info
|
|
539
534
|
end
|
|
540
535
|
|
|
541
|
-
def close_sock(
|
|
536
|
+
def close_sock(sock)
|
|
542
537
|
return if sock.nil? || sock.closed?
|
|
543
538
|
sock.close
|
|
544
|
-
@reads.delete(
|
|
545
|
-
@writes.delete(
|
|
546
|
-
@updates.delete(
|
|
547
|
-
@roles.delete(
|
|
539
|
+
@reads.delete(sock)
|
|
540
|
+
@writes.delete(sock)
|
|
541
|
+
@updates.delete(sock)
|
|
542
|
+
@roles.delete(sock)
|
|
548
543
|
end
|
|
549
544
|
|
|
550
|
-
def close_src(
|
|
545
|
+
def close_src(src)
|
|
551
546
|
return nil if src.nil? || src.closed?
|
|
552
|
-
close_sock(
|
|
553
|
-
src_info = @src_infos.delete(
|
|
554
|
-
src_id = src_info[
|
|
555
|
-
domain = src_info[
|
|
556
|
-
puts "close src #{
|
|
557
|
-
|
|
558
|
-
if src_info[
|
|
559
|
-
set_dst_closing(
|
|
560
|
-
elsif (
|
|
561
|
-
proxy_info = @proxy_infos[
|
|
562
|
-
proxy_info[
|
|
563
|
-
puts "add h_src_close #{
|
|
564
|
-
msg = "#{
|
|
565
|
-
add_proxy_wbuff(
|
|
547
|
+
close_sock(src)
|
|
548
|
+
src_info = @src_infos.delete(src)
|
|
549
|
+
src_id = src_info[:src_id]
|
|
550
|
+
domain = src_info[:destination_domain]
|
|
551
|
+
puts "close src #{domain}" if @is_debug
|
|
552
|
+
|
|
553
|
+
if src_info[:proxy_type] == :direct
|
|
554
|
+
set_dst_closing(src_info[:dst])
|
|
555
|
+
elsif (src_info[:proxy_type] == :remote) && !@proxy.closed?
|
|
556
|
+
proxy_info = @proxy_infos[@proxy]
|
|
557
|
+
proxy_info[:paused_srcs].delete(src)
|
|
558
|
+
puts "add h_src_close #{src_id}" if @is_debug
|
|
559
|
+
msg = "#{@h_src_close}#{[src_id].pack('Q>')}"
|
|
560
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
566
561
|
end
|
|
567
562
|
|
|
568
563
|
src_info
|
|
569
564
|
end
|
|
570
565
|
|
|
571
|
-
def deal_msg(
|
|
566
|
+
def deal_msg(data)
|
|
572
567
|
return if data.nil? || data.empty? || @proxy.closed?
|
|
573
|
-
proxy_info = @proxy_infos[
|
|
568
|
+
proxy_info = @proxy_infos[@proxy]
|
|
574
569
|
now = Time.new
|
|
575
|
-
proxy_info[
|
|
576
|
-
h = data[
|
|
570
|
+
proxy_info[:recv_at] = now
|
|
571
|
+
h = data[0]
|
|
577
572
|
|
|
578
573
|
case h
|
|
579
|
-
when @h_a_new_p2
|
|
574
|
+
when @h_a_new_p2
|
|
580
575
|
return if data.bytesize < 9
|
|
581
|
-
p2_id = data[
|
|
582
|
-
puts "got h_a_new_p2 #{
|
|
583
|
-
new_a_p1(
|
|
584
|
-
when @h_dst_close
|
|
576
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
577
|
+
puts "got h_a_new_p2 #{p2_id}"
|
|
578
|
+
new_a_p1(p2_id)
|
|
579
|
+
when @h_dst_close
|
|
585
580
|
return if data.bytesize < 9
|
|
586
|
-
src_id = data[
|
|
587
|
-
puts "got h_dst_close #{
|
|
588
|
-
src, _ = @src_infos.find{
|
|
589
|
-
set_src_closing(
|
|
590
|
-
when @h_heartbeat
|
|
581
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
582
|
+
puts "got h_dst_close #{src_id}" if @is_debug
|
|
583
|
+
src, _ = @src_infos.find{|_, info| info[:src_id] == src_id}
|
|
584
|
+
set_src_closing(src)
|
|
585
|
+
when @h_heartbeat
|
|
591
586
|
puts "got h_heartbeat" if @is_debug
|
|
592
|
-
when @h_p2_close
|
|
587
|
+
when @h_p2_close
|
|
593
588
|
return if data.bytesize < 9
|
|
594
|
-
p2_id = data[
|
|
595
|
-
puts "got h_p2_close #{
|
|
596
|
-
p1, _ = @p1_infos.find{
|
|
597
|
-
set_p1_closing(
|
|
598
|
-
when @h_p2_traffic
|
|
589
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
590
|
+
puts "got h_p2_close #{p2_id}"
|
|
591
|
+
p1, _ = @p1_infos.find{|_, info| info[:p2_id] == p2_id}
|
|
592
|
+
set_p1_closing(p1)
|
|
593
|
+
when @h_p2_traffic
|
|
599
594
|
return if data.bytesize < 9
|
|
600
|
-
p2_id = data[
|
|
601
|
-
data = data[
|
|
602
|
-
# puts "got h_p2_traffic #{
|
|
603
|
-
p1, _ = @p1_infos.find{
|
|
604
|
-
add_p1_wbuff(
|
|
605
|
-
when @h_p2_overflow
|
|
595
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
596
|
+
data = data[9..-1]
|
|
597
|
+
# puts "got h_p2_traffic #{p2_id} #{data.bytesize}" if @is_debug
|
|
598
|
+
p1, _ = @p1_infos.find{|_, info| info[:p2_id] == p2_id}
|
|
599
|
+
add_p1_wbuff(p1, data)
|
|
600
|
+
when @h_p2_overflow
|
|
606
601
|
return if data.bytesize < 9
|
|
607
|
-
p2_id = data[
|
|
608
|
-
puts "got h_p2_overflow pause p1 #{
|
|
609
|
-
p1, _ = @p1_infos.find{
|
|
610
|
-
@reads.delete(
|
|
611
|
-
proxy_info[
|
|
612
|
-
when @h_p2_underhalf
|
|
602
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
603
|
+
puts "got h_p2_overflow pause p1 #{p2_id}"
|
|
604
|
+
p1, _ = @p1_infos.find{|_, info| info[:p2_id] == p2_id}
|
|
605
|
+
@reads.delete(p1)
|
|
606
|
+
proxy_info[:paused_p1s].delete(p1)
|
|
607
|
+
when @h_p2_underhalf
|
|
613
608
|
return if data.bytesize < 9
|
|
614
|
-
p2_id = data[
|
|
615
|
-
puts "got h_p2_underhalf #{
|
|
616
|
-
p1, _ = @p1_infos.find{
|
|
617
|
-
add_read(
|
|
618
|
-
when @h_response
|
|
609
|
+
p2_id = data[1, 8].unpack('Q>').first
|
|
610
|
+
puts "got h_p2_underhalf #{p2_id}"
|
|
611
|
+
p1, _ = @p1_infos.find{|_, info| info[:p2_id] == p2_id}
|
|
612
|
+
add_read(p1)
|
|
613
|
+
when @h_response
|
|
619
614
|
return if data.bytesize < 3
|
|
620
|
-
near_id = data[
|
|
621
|
-
data = data[
|
|
622
|
-
puts "got h_response #{
|
|
623
|
-
near_info = @near_infos.delete(
|
|
615
|
+
near_id = data[1, 8].unpack('Q>').first
|
|
616
|
+
data = data[9..-1]
|
|
617
|
+
puts "got h_response #{near_id} #{data.bytesize}" if @is_debug
|
|
618
|
+
near_info = @near_infos.delete(near_id)
|
|
624
619
|
|
|
625
|
-
if near_info
|
|
626
|
-
data[
|
|
627
|
-
addrinfo = near_info[
|
|
628
|
-
send_data(
|
|
620
|
+
if near_info
|
|
621
|
+
data[0, 2] = near_info[:id]
|
|
622
|
+
addrinfo = near_info[:addrinfo]
|
|
623
|
+
send_data(@rsvd, data, addrinfo)
|
|
629
624
|
|
|
630
625
|
begin
|
|
631
|
-
ip = seek_ip(
|
|
626
|
+
ip = seek_ip(data)
|
|
632
627
|
rescue Exception => e
|
|
633
|
-
puts "response seek ip #{
|
|
628
|
+
puts "response seek ip #{e.class} #{e.message}"
|
|
634
629
|
end
|
|
635
630
|
|
|
636
|
-
if ip
|
|
637
|
-
domain = near_info[
|
|
638
|
-
type = near_info[
|
|
631
|
+
if ip
|
|
632
|
+
domain = near_info[:domain]
|
|
633
|
+
type = near_info[:type]
|
|
639
634
|
|
|
640
|
-
if type == 1
|
|
641
|
-
@response_caches[
|
|
635
|
+
if type == 1
|
|
636
|
+
@response_caches[domain] = [data, now, ip, true]
|
|
642
637
|
else
|
|
643
|
-
@response6_caches[
|
|
638
|
+
@response6_caches[domain] = [data, now, ip, true]
|
|
644
639
|
end
|
|
645
640
|
end
|
|
646
641
|
end
|
|
647
|
-
when @h_traffic
|
|
642
|
+
when @h_traffic
|
|
648
643
|
return if data.bytesize < 9
|
|
649
|
-
src_id = data[
|
|
650
|
-
data = data[
|
|
651
|
-
# puts "got h_traffic #{
|
|
652
|
-
src, _ = @src_infos.find{
|
|
653
|
-
add_src_wbuff(
|
|
654
|
-
when @h_dst_overflow
|
|
644
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
645
|
+
data = data[9..-1]
|
|
646
|
+
# puts "got h_traffic #{src_id} #{data.bytesize}" if @is_debug
|
|
647
|
+
src, _ = @src_infos.find{|_, info| info[:src_id] == src_id}
|
|
648
|
+
add_src_wbuff(src, data)
|
|
649
|
+
when @h_dst_overflow
|
|
655
650
|
return if data.bytesize < 9
|
|
656
|
-
src_id = data[
|
|
657
|
-
puts "got h_dst_overflow pause src #{
|
|
658
|
-
src, _ = @src_infos.find{
|
|
659
|
-
@reads.delete(
|
|
660
|
-
proxy_info[
|
|
661
|
-
when @h_dst_underhalf
|
|
651
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
652
|
+
puts "got h_dst_overflow pause src #{src_id}"
|
|
653
|
+
src, _ = @src_infos.find{|_, info| info[:src_id] == src_id}
|
|
654
|
+
@reads.delete(src)
|
|
655
|
+
proxy_info[:paused_srcs].delete(src)
|
|
656
|
+
when @h_dst_underhalf
|
|
662
657
|
return if data.bytesize < 9
|
|
663
|
-
src_id = data[
|
|
664
|
-
puts "got h_dst_underhalf #{
|
|
665
|
-
src, _ = @src_infos.find{
|
|
666
|
-
add_read(
|
|
658
|
+
src_id = data[1, 8].unpack('Q>').first
|
|
659
|
+
puts "got h_dst_underhalf #{src_id}"
|
|
660
|
+
src, _ = @src_infos.find{|_, info| info[:src_id] == src_id}
|
|
661
|
+
add_read(src)
|
|
667
662
|
end
|
|
668
663
|
end
|
|
669
664
|
|
|
670
|
-
def decode_to_msgs(
|
|
665
|
+
def decode_to_msgs(data)
|
|
671
666
|
msgs = []
|
|
672
667
|
part = ''
|
|
673
668
|
|
|
674
669
|
loop do
|
|
675
|
-
if data.bytesize <= 2
|
|
670
|
+
if data.bytesize <= 2
|
|
676
671
|
part = data
|
|
677
672
|
break
|
|
678
673
|
end
|
|
679
674
|
|
|
680
|
-
len = data[
|
|
675
|
+
len = data[0, 2].unpack('n').first
|
|
681
676
|
|
|
682
|
-
if len == 0
|
|
677
|
+
if len == 0
|
|
683
678
|
puts "msg zero len?"
|
|
684
679
|
break
|
|
685
680
|
end
|
|
686
681
|
|
|
687
|
-
if data.bytesize < (
|
|
682
|
+
if data.bytesize < (2 + len)
|
|
688
683
|
part = data
|
|
689
684
|
break
|
|
690
685
|
end
|
|
691
686
|
|
|
692
|
-
msgs << data[
|
|
693
|
-
data = data[
|
|
687
|
+
msgs << data[2, len]
|
|
688
|
+
data = data[(2 + len)..-1]
|
|
694
689
|
break if data.empty?
|
|
695
690
|
end
|
|
696
691
|
|
|
697
|
-
[
|
|
692
|
+
[msgs, part]
|
|
698
693
|
end
|
|
699
694
|
|
|
700
695
|
def loop_heartbeat
|
|
701
696
|
Thread.new do
|
|
702
697
|
loop do
|
|
703
698
|
sleep HEARTBEAT_INTERVAL
|
|
704
|
-
msg = {
|
|
705
|
-
send_data(
|
|
699
|
+
msg = {message_type: 'heartbeat'}
|
|
700
|
+
send_data(@info, JSON.generate(msg), @infod_addr)
|
|
706
701
|
end
|
|
707
702
|
end
|
|
708
703
|
end
|
|
709
704
|
|
|
710
|
-
def make_tunnel(
|
|
705
|
+
def make_tunnel(ip, src)
|
|
711
706
|
return if src.nil? || src.closed?
|
|
712
|
-
src_info = @src_infos[
|
|
713
|
-
domain = src_info[
|
|
714
|
-
port = src_info[
|
|
707
|
+
src_info = @src_infos[src]
|
|
708
|
+
domain = src_info[:destination_domain]
|
|
709
|
+
port = src_info[:destination_port]
|
|
715
710
|
|
|
716
|
-
if @local_ips.include?(
|
|
717
|
-
puts "ignore #{
|
|
718
|
-
close_src(
|
|
711
|
+
if @local_ips.include?(ip) && [@redir_port, @relayd_port, @tspd_port].include?(port)
|
|
712
|
+
puts "ignore #{ip}:#{port}"
|
|
713
|
+
close_src(src)
|
|
719
714
|
return
|
|
720
715
|
end
|
|
721
716
|
|
|
722
|
-
if [
|
|
717
|
+
if [domain, ip].include?(@proxyd_host) && ![80, 443].include?(port)
|
|
723
718
|
# 访问远端非http端口,直连
|
|
724
|
-
puts "direct #{
|
|
725
|
-
new_a_dst(
|
|
719
|
+
puts "direct #{ip} #{port}"
|
|
720
|
+
new_a_dst(ip, src)
|
|
726
721
|
return
|
|
727
722
|
end
|
|
728
723
|
|
|
729
|
-
if @is_direct_caches.include?(
|
|
730
|
-
is_direct = @is_direct_caches[
|
|
724
|
+
if @is_direct_caches.include?(ip)
|
|
725
|
+
is_direct = @is_direct_caches[ip]
|
|
731
726
|
else
|
|
732
727
|
begin
|
|
733
|
-
is_direct = @directs.any?{
|
|
728
|
+
is_direct = @directs.any?{|direct| direct.include?(ip)}
|
|
734
729
|
rescue IPAddr::InvalidAddressError => e
|
|
735
|
-
puts "make tunnel #{
|
|
736
|
-
close_src(
|
|
730
|
+
puts "make tunnel #{e.class}"
|
|
731
|
+
close_src(src)
|
|
737
732
|
return
|
|
738
733
|
end
|
|
739
734
|
|
|
740
|
-
@is_direct_caches[
|
|
735
|
+
@is_direct_caches[ip] = is_direct
|
|
741
736
|
end
|
|
742
737
|
|
|
743
|
-
if is_direct
|
|
744
|
-
new_a_dst(
|
|
738
|
+
if is_direct
|
|
739
|
+
new_a_dst(ip, src)
|
|
745
740
|
else
|
|
746
|
-
set_remote(
|
|
741
|
+
set_remote(src)
|
|
747
742
|
end
|
|
748
743
|
end
|
|
749
744
|
|
|
750
|
-
def new_a_dst(
|
|
745
|
+
def new_a_dst(ip, src)
|
|
751
746
|
return if src.nil? || src.closed?
|
|
752
|
-
src_info = @src_infos[
|
|
753
|
-
domain = src_info[
|
|
754
|
-
port = src_info[
|
|
747
|
+
src_info = @src_infos[src]
|
|
748
|
+
domain = src_info[:destination_domain]
|
|
749
|
+
port = src_info[:destination_port]
|
|
755
750
|
check_expire_dsts
|
|
756
751
|
|
|
757
752
|
begin
|
|
758
|
-
destination_addr = Socket.sockaddr_in(
|
|
759
|
-
dst = Socket.new(
|
|
753
|
+
destination_addr = Socket.sockaddr_in(port, ip)
|
|
754
|
+
dst = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
760
755
|
rescue Exception => e
|
|
761
|
-
puts "new a dst #{
|
|
762
|
-
close_src(
|
|
756
|
+
puts "new a dst #{e.class} #{domain} #{ip}:#{port}"
|
|
757
|
+
close_src(src)
|
|
763
758
|
return
|
|
764
759
|
end
|
|
765
760
|
|
|
766
|
-
dst.setsockopt(
|
|
761
|
+
dst.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
767
762
|
|
|
768
763
|
begin
|
|
769
|
-
dst.connect_nonblock(
|
|
764
|
+
dst.connect_nonblock(destination_addr)
|
|
770
765
|
rescue IO::WaitWritable
|
|
771
766
|
rescue Exception => e
|
|
772
|
-
puts "dst connect destination #{
|
|
767
|
+
puts "dst connect destination #{e.class} #{domain} #{ip}:#{port}"
|
|
773
768
|
dst.close
|
|
774
|
-
close_src(
|
|
769
|
+
close_src(src)
|
|
775
770
|
return
|
|
776
771
|
end
|
|
777
772
|
|
|
@@ -786,51 +781,51 @@ module Girl
|
|
|
786
781
|
wbuff: ''
|
|
787
782
|
}
|
|
788
783
|
|
|
789
|
-
@dst_infos[
|
|
790
|
-
src_info[
|
|
791
|
-
src_info[
|
|
784
|
+
@dst_infos[dst] = dst_info
|
|
785
|
+
src_info[:proxy_type] = :direct
|
|
786
|
+
src_info[:dst] = dst
|
|
792
787
|
|
|
793
|
-
if src_info[
|
|
794
|
-
if src_info[
|
|
788
|
+
if src_info[:proxy_proto] == :http
|
|
789
|
+
if src_info[:is_connect]
|
|
795
790
|
puts "add HTTP_OK" if @is_debug
|
|
796
|
-
add_src_wbuff(
|
|
791
|
+
add_src_wbuff(src, HTTP_OK)
|
|
797
792
|
end
|
|
798
|
-
elsif src_info[
|
|
793
|
+
elsif src_info[:proxy_proto] == :socks5
|
|
799
794
|
puts "add_socks5_conn_reply" if @is_debug
|
|
800
|
-
add_socks5_conn_reply(
|
|
795
|
+
add_socks5_conn_reply(src)
|
|
801
796
|
end
|
|
802
797
|
|
|
803
|
-
add_read(
|
|
804
|
-
add_write(
|
|
805
|
-
data = src_info[
|
|
798
|
+
add_read(dst, :dst)
|
|
799
|
+
add_write(dst)
|
|
800
|
+
data = src_info[:rbuff].dup
|
|
806
801
|
|
|
807
|
-
unless data.empty?
|
|
808
|
-
puts "move src rbuff to dst #{
|
|
809
|
-
add_dst_wbuff(
|
|
802
|
+
unless data.empty?
|
|
803
|
+
puts "move src rbuff to dst #{domain} #{data.bytesize}" if @is_debug
|
|
804
|
+
add_dst_wbuff(dst, data)
|
|
810
805
|
end
|
|
811
806
|
end
|
|
812
807
|
|
|
813
|
-
def new_a_girl(
|
|
808
|
+
def new_a_girl(relay)
|
|
814
809
|
return if relay.nil? || relay.closed?
|
|
815
810
|
check_expire_girls
|
|
816
811
|
|
|
817
812
|
begin
|
|
818
|
-
girl = Socket.new(
|
|
813
|
+
girl = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
819
814
|
rescue Exception => e
|
|
820
|
-
puts "new a girl #{
|
|
821
|
-
close_girl(
|
|
815
|
+
puts "new a girl #{e.class}"
|
|
816
|
+
close_girl(girl)
|
|
822
817
|
return
|
|
823
818
|
end
|
|
824
819
|
|
|
825
|
-
girl.setsockopt(
|
|
820
|
+
girl.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
826
821
|
|
|
827
822
|
begin
|
|
828
|
-
girl.connect_nonblock(
|
|
823
|
+
girl.connect_nonblock(@proxyd_addr)
|
|
829
824
|
rescue IO::WaitWritable
|
|
830
825
|
rescue Exception => e
|
|
831
|
-
puts "girl connect proxyd #{
|
|
826
|
+
puts "girl connect proxyd #{e.class}"
|
|
832
827
|
girl.close
|
|
833
|
-
close_girl(
|
|
828
|
+
close_girl(girl)
|
|
834
829
|
return
|
|
835
830
|
end
|
|
836
831
|
|
|
@@ -843,55 +838,55 @@ module Girl
|
|
|
843
838
|
wbuff: ''
|
|
844
839
|
}
|
|
845
840
|
|
|
846
|
-
@girl_infos[
|
|
847
|
-
add_read(
|
|
848
|
-
add_write(
|
|
841
|
+
@girl_infos[girl] = girl_info
|
|
842
|
+
add_read(girl, :girl)
|
|
843
|
+
add_write(girl)
|
|
849
844
|
girl
|
|
850
845
|
end
|
|
851
846
|
|
|
852
|
-
def new_a_infod(
|
|
847
|
+
def new_a_infod(infod_port)
|
|
853
848
|
infod_host = '127.0.0.1'
|
|
854
|
-
infod_addr = Socket.sockaddr_in(
|
|
855
|
-
infod = Socket.new(
|
|
856
|
-
infod.setsockopt(
|
|
857
|
-
infod.bind(
|
|
858
|
-
puts "infod bind on #{
|
|
859
|
-
add_read(
|
|
860
|
-
info = Socket.new(
|
|
849
|
+
infod_addr = Socket.sockaddr_in(infod_port, infod_host)
|
|
850
|
+
infod = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
851
|
+
infod.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
852
|
+
infod.bind(infod_addr)
|
|
853
|
+
puts "infod bind on #{infod_host} #{infod_port}"
|
|
854
|
+
add_read(infod, :infod)
|
|
855
|
+
info = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
861
856
|
@infod_addr = infod_addr
|
|
862
857
|
@infod = infod
|
|
863
858
|
@info = info
|
|
864
859
|
end
|
|
865
860
|
|
|
866
|
-
def new_a_memd(
|
|
861
|
+
def new_a_memd(memd_port)
|
|
867
862
|
memd_host = '127.0.0.1'
|
|
868
|
-
memd = Socket.new(
|
|
869
|
-
memd.setsockopt(
|
|
870
|
-
memd.setsockopt(
|
|
871
|
-
memd.setsockopt(
|
|
872
|
-
memd.bind(
|
|
873
|
-
memd.listen(
|
|
874
|
-
puts "memd listen on #{
|
|
875
|
-
add_read(
|
|
863
|
+
memd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
864
|
+
memd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
865
|
+
memd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
866
|
+
memd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, 5) if @is_server_fastopen
|
|
867
|
+
memd.bind(Socket.sockaddr_in(memd_port, memd_host))
|
|
868
|
+
memd.listen(5)
|
|
869
|
+
puts "memd listen on #{memd_host} #{memd_port}"
|
|
870
|
+
add_read(memd, :memd)
|
|
876
871
|
end
|
|
877
872
|
|
|
878
|
-
def new_a_p1(
|
|
873
|
+
def new_a_p1(p2_id)
|
|
879
874
|
check_expire_p1s
|
|
880
875
|
|
|
881
876
|
begin
|
|
882
|
-
p1 = Socket.new(
|
|
877
|
+
p1 = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
883
878
|
rescue Exception => e
|
|
884
|
-
puts "new a p1 #{
|
|
879
|
+
puts "new a p1 #{e.class} #{p2_id}"
|
|
885
880
|
return
|
|
886
881
|
end
|
|
887
882
|
|
|
888
|
-
p1.setsockopt(
|
|
883
|
+
p1.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
889
884
|
|
|
890
885
|
begin
|
|
891
|
-
p1.connect_nonblock(
|
|
886
|
+
p1.connect_nonblock(@appd_addr)
|
|
892
887
|
rescue IO::WaitWritable
|
|
893
888
|
rescue Exception => e
|
|
894
|
-
puts "connect appd_addr #{
|
|
889
|
+
puts "connect appd_addr #{e.class} #{p2_id}"
|
|
895
890
|
p1.close
|
|
896
891
|
return
|
|
897
892
|
end
|
|
@@ -904,32 +899,32 @@ module Girl
|
|
|
904
899
|
wbuff: ''
|
|
905
900
|
}
|
|
906
901
|
|
|
907
|
-
@p1_infos[
|
|
908
|
-
add_read(
|
|
909
|
-
add_write(
|
|
902
|
+
@p1_infos[p1] = p1_info
|
|
903
|
+
add_read(p1, :p1)
|
|
904
|
+
add_write(p1)
|
|
910
905
|
end
|
|
911
906
|
|
|
912
907
|
def new_a_proxy
|
|
913
|
-
proxy = Socket.new(
|
|
914
|
-
proxy.setsockopt(
|
|
908
|
+
proxy = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
909
|
+
proxy.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
915
910
|
|
|
916
|
-
if @is_client_fastopen
|
|
917
|
-
proxy.setsockopt(
|
|
911
|
+
if @is_client_fastopen
|
|
912
|
+
proxy.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, 5)
|
|
918
913
|
else
|
|
919
914
|
begin
|
|
920
|
-
proxy.connect_nonblock(
|
|
915
|
+
proxy.connect_nonblock(@proxyd_addr)
|
|
921
916
|
rescue IO::WaitWritable
|
|
922
917
|
rescue Exception => e
|
|
923
|
-
puts "connect proxyd #{
|
|
918
|
+
puts "connect proxyd #{e.class}"
|
|
924
919
|
proxy.close
|
|
925
920
|
return
|
|
926
921
|
end
|
|
927
922
|
end
|
|
928
923
|
|
|
929
|
-
puts "im #{
|
|
924
|
+
puts "im #{@im}"
|
|
930
925
|
chars = []
|
|
931
|
-
@head_len.times{
|
|
932
|
-
head = "#{
|
|
926
|
+
@head_len.times{chars << rand(256)}
|
|
927
|
+
head = "#{chars.pack('C*')}#{[@im.bytesize].pack('C')}#{@im}"
|
|
933
928
|
|
|
934
929
|
proxy_info = {
|
|
935
930
|
is_syn: @is_client_fastopen,
|
|
@@ -941,47 +936,47 @@ module Girl
|
|
|
941
936
|
}
|
|
942
937
|
|
|
943
938
|
@proxy = proxy
|
|
944
|
-
@proxy_infos[
|
|
945
|
-
add_read(
|
|
946
|
-
add_write(
|
|
939
|
+
@proxy_infos[proxy] = proxy_info
|
|
940
|
+
add_read(proxy, :proxy)
|
|
941
|
+
add_write(proxy)
|
|
947
942
|
proxy_info
|
|
948
943
|
end
|
|
949
944
|
|
|
950
|
-
def new_a_redir(
|
|
951
|
-
redir = Socket.new(
|
|
952
|
-
redir.setsockopt(
|
|
953
|
-
redir.setsockopt(
|
|
954
|
-
redir.setsockopt(
|
|
955
|
-
redir.setsockopt(
|
|
956
|
-
redir.bind(
|
|
957
|
-
redir.listen(
|
|
958
|
-
puts "redir listen on #{
|
|
959
|
-
add_read(
|
|
945
|
+
def new_a_redir(redir_host, redir_port)
|
|
946
|
+
redir = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
947
|
+
redir.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
948
|
+
redir.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
|
|
949
|
+
redir.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
950
|
+
redir.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, BACKLOG) if @is_server_fastopen
|
|
951
|
+
redir.bind(Socket.sockaddr_in(redir_port, redir_host))
|
|
952
|
+
redir.listen(BACKLOG)
|
|
953
|
+
puts "redir listen on #{redir_host} #{redir_port}"
|
|
954
|
+
add_read(redir, :redir)
|
|
960
955
|
@redir_port = redir_port
|
|
961
956
|
@redir_local_address = redir.local_address
|
|
962
957
|
end
|
|
963
958
|
|
|
964
|
-
def new_a_relayd(
|
|
965
|
-
relayd = Socket.new(
|
|
966
|
-
relayd.setsockopt(
|
|
967
|
-
relayd.setsockopt(
|
|
968
|
-
relayd.setsockopt(
|
|
969
|
-
relayd.setsockopt(
|
|
970
|
-
relayd.bind(
|
|
971
|
-
relayd.listen(
|
|
972
|
-
puts "relayd listen on #{
|
|
973
|
-
add_read(
|
|
959
|
+
def new_a_relayd(relayd_host, relayd_port)
|
|
960
|
+
relayd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
961
|
+
relayd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
962
|
+
relayd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
|
|
963
|
+
relayd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
964
|
+
relayd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, BACKLOG) if @is_server_fastopen
|
|
965
|
+
relayd.bind(Socket.sockaddr_in(relayd_port, relayd_host))
|
|
966
|
+
relayd.listen(BACKLOG)
|
|
967
|
+
puts "relayd listen on #{relayd_host} #{relayd_port}"
|
|
968
|
+
add_read(relayd, :relayd)
|
|
974
969
|
@relayd_port = relayd_port
|
|
975
970
|
end
|
|
976
971
|
|
|
977
|
-
def new_a_rsv(
|
|
972
|
+
def new_a_rsv(data, addrinfo, domain, type)
|
|
978
973
|
check_expire_rsvs
|
|
979
|
-
rsv = Socket.new(
|
|
974
|
+
rsv = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
980
975
|
|
|
981
976
|
begin
|
|
982
|
-
@nameserver_addrs.each{
|
|
977
|
+
@nameserver_addrs.each{|addr| rsv.sendmsg(data, 0, addr)}
|
|
983
978
|
rescue Exception => e
|
|
984
|
-
puts "rsv send data #{
|
|
979
|
+
puts "rsv send data #{e.class}"
|
|
985
980
|
rsv.close
|
|
986
981
|
return
|
|
987
982
|
end
|
|
@@ -992,197 +987,197 @@ module Girl
|
|
|
992
987
|
type: type
|
|
993
988
|
}
|
|
994
989
|
|
|
995
|
-
@rsv_infos[
|
|
996
|
-
add_read(
|
|
990
|
+
@rsv_infos[rsv] = rsv_info
|
|
991
|
+
add_read(rsv, :rsv)
|
|
997
992
|
end
|
|
998
993
|
|
|
999
|
-
def new_a_rsvd(
|
|
1000
|
-
rsvd_addr = Socket.sockaddr_in(
|
|
1001
|
-
rsvd = Socket.new(
|
|
1002
|
-
rsvd.setsockopt(
|
|
1003
|
-
rsvd.bind(
|
|
1004
|
-
puts "rsvd bind on #{
|
|
1005
|
-
add_read(
|
|
994
|
+
def new_a_rsvd(rsvd_host, rsvd_port)
|
|
995
|
+
rsvd_addr = Socket.sockaddr_in(rsvd_port, rsvd_host)
|
|
996
|
+
rsvd = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
997
|
+
rsvd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
998
|
+
rsvd.bind(rsvd_addr)
|
|
999
|
+
puts "rsvd bind on #{rsvd_host} #{rsvd_port}"
|
|
1000
|
+
add_read(rsvd, :rsvd)
|
|
1006
1001
|
@rsvd = rsvd
|
|
1007
1002
|
end
|
|
1008
1003
|
|
|
1009
|
-
def new_a_tspd(
|
|
1010
|
-
tspd = Socket.new(
|
|
1011
|
-
tspd.setsockopt(
|
|
1012
|
-
tspd.setsockopt(
|
|
1013
|
-
tspd.setsockopt(
|
|
1014
|
-
tspd.setsockopt(
|
|
1015
|
-
tspd.bind(
|
|
1016
|
-
tspd.listen(
|
|
1017
|
-
puts "tspd listen on #{
|
|
1018
|
-
add_read(
|
|
1004
|
+
def new_a_tspd(tspd_host, tspd_port)
|
|
1005
|
+
tspd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
1006
|
+
tspd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
1007
|
+
tspd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
|
|
1008
|
+
tspd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, 1) if RUBY_PLATFORM.include?('linux')
|
|
1009
|
+
tspd.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_FASTOPEN, BACKLOG) if @is_server_fastopen
|
|
1010
|
+
tspd.bind(Socket.sockaddr_in(tspd_port, tspd_host))
|
|
1011
|
+
tspd.listen(BACKLOG)
|
|
1012
|
+
puts "tspd listen on #{tspd_host} #{tspd_port}"
|
|
1013
|
+
add_read(tspd, :tspd)
|
|
1019
1014
|
@tspd_port = tspd_port
|
|
1020
1015
|
end
|
|
1021
1016
|
|
|
1022
|
-
def pack_a_chunk(
|
|
1023
|
-
"#{
|
|
1017
|
+
def pack_a_chunk(msg)
|
|
1018
|
+
"#{[msg.bytesize].pack('n')}#{msg}"
|
|
1024
1019
|
end
|
|
1025
1020
|
|
|
1026
|
-
def pack_p2_traffic(
|
|
1021
|
+
def pack_p2_traffic(p2_id, data)
|
|
1027
1022
|
chunks = ''
|
|
1028
1023
|
|
|
1029
1024
|
loop do
|
|
1030
|
-
part = data[
|
|
1031
|
-
# puts "add h_p2_traffic #{
|
|
1032
|
-
msg = "#{
|
|
1033
|
-
chunks << pack_a_chunk(
|
|
1034
|
-
data = data[
|
|
1025
|
+
part = data[0, 65526]
|
|
1026
|
+
# puts "add h_p2_traffic #{p2_id} #{part.bytesize}" if @is_debug
|
|
1027
|
+
msg = "#{@h_p2_traffic}#{[p2_id].pack('Q>')}#{part}"
|
|
1028
|
+
chunks << pack_a_chunk(msg)
|
|
1029
|
+
data = data[part.bytesize..-1]
|
|
1035
1030
|
break if data.empty?
|
|
1036
1031
|
end
|
|
1037
1032
|
|
|
1038
1033
|
chunks
|
|
1039
1034
|
end
|
|
1040
1035
|
|
|
1041
|
-
def pack_traffic(
|
|
1036
|
+
def pack_traffic(src_id, data)
|
|
1042
1037
|
chunks = ''
|
|
1043
1038
|
|
|
1044
1039
|
loop do
|
|
1045
|
-
part = data[
|
|
1046
|
-
# puts "add h_traffic #{
|
|
1047
|
-
msg = "#{
|
|
1048
|
-
chunks << pack_a_chunk(
|
|
1049
|
-
data = data[
|
|
1040
|
+
part = data[0, 65526]
|
|
1041
|
+
# puts "add h_traffic #{src_id} #{part.bytesize}" if @is_debug
|
|
1042
|
+
msg = "#{@h_traffic}#{[src_id].pack('Q>')}#{part}"
|
|
1043
|
+
chunks << pack_a_chunk(msg)
|
|
1044
|
+
data = data[part.bytesize..-1]
|
|
1050
1045
|
break if data.empty?
|
|
1051
1046
|
end
|
|
1052
1047
|
|
|
1053
1048
|
chunks
|
|
1054
1049
|
end
|
|
1055
1050
|
|
|
1056
|
-
def read_dns(
|
|
1051
|
+
def read_dns(dns)
|
|
1057
1052
|
begin
|
|
1058
1053
|
data, addrinfo, rflags, *controls = dns.recvmsg
|
|
1059
1054
|
rescue Exception => e
|
|
1060
|
-
puts "dns recvmsg #{
|
|
1061
|
-
close_dns(
|
|
1055
|
+
puts "dns recvmsg #{e.class}"
|
|
1056
|
+
close_dns(dns)
|
|
1062
1057
|
return
|
|
1063
1058
|
end
|
|
1064
1059
|
|
|
1065
1060
|
return if data.empty?
|
|
1066
1061
|
|
|
1067
1062
|
begin
|
|
1068
|
-
ip = seek_ip(
|
|
1063
|
+
ip = seek_ip(data)
|
|
1069
1064
|
rescue Exception => e
|
|
1070
|
-
puts "dns seek ip #{
|
|
1071
|
-
close_dns(
|
|
1065
|
+
puts "dns seek ip #{e.class} #{e.message}"
|
|
1066
|
+
close_dns(dns)
|
|
1072
1067
|
return
|
|
1073
1068
|
end
|
|
1074
1069
|
|
|
1075
|
-
dns_info = @dns_infos[
|
|
1076
|
-
domain = dns_info[
|
|
1070
|
+
dns_info = @dns_infos[dns]
|
|
1071
|
+
domain = dns_info[:domain]
|
|
1077
1072
|
|
|
1078
|
-
if ip
|
|
1079
|
-
src = dns_info[
|
|
1080
|
-
make_tunnel(
|
|
1081
|
-
puts "set resolv cache #{
|
|
1082
|
-
@resolv_caches[
|
|
1073
|
+
if ip
|
|
1074
|
+
src = dns_info[:src]
|
|
1075
|
+
make_tunnel(ip, src)
|
|
1076
|
+
puts "set resolv cache #{domain} #{ip}" if @is_debug
|
|
1077
|
+
@resolv_caches[domain] = [ip, Time.new]
|
|
1083
1078
|
else
|
|
1084
|
-
puts "no ip in answer #{
|
|
1085
|
-
close_src(
|
|
1079
|
+
puts "no ip in answer #{domain}"
|
|
1080
|
+
close_src(src)
|
|
1086
1081
|
end
|
|
1087
1082
|
|
|
1088
|
-
close_dns(
|
|
1083
|
+
close_dns(dns)
|
|
1089
1084
|
end
|
|
1090
1085
|
|
|
1091
|
-
def read_dst(
|
|
1086
|
+
def read_dst(dst)
|
|
1092
1087
|
begin
|
|
1093
|
-
data = dst.read_nonblock(
|
|
1088
|
+
data = dst.read_nonblock(READ_SIZE)
|
|
1094
1089
|
rescue Errno::ENOTCONN => e
|
|
1095
1090
|
return
|
|
1096
1091
|
rescue Exception => e
|
|
1097
|
-
close_dst(
|
|
1092
|
+
close_dst(dst)
|
|
1098
1093
|
return
|
|
1099
1094
|
end
|
|
1100
1095
|
|
|
1101
|
-
set_update(
|
|
1102
|
-
dst_info = @dst_infos[
|
|
1103
|
-
# puts "read dst #{
|
|
1104
|
-
src = dst_info[
|
|
1105
|
-
add_src_wbuff(
|
|
1096
|
+
set_update(dst)
|
|
1097
|
+
dst_info = @dst_infos[dst]
|
|
1098
|
+
# puts "read dst #{dst_info[:domain]} #{data.bytesize}" if @is_debug
|
|
1099
|
+
src = dst_info[:src]
|
|
1100
|
+
add_src_wbuff(src, data)
|
|
1106
1101
|
end
|
|
1107
1102
|
|
|
1108
|
-
def read_girl(
|
|
1103
|
+
def read_girl(girl)
|
|
1109
1104
|
begin
|
|
1110
|
-
data = girl.read_nonblock(
|
|
1105
|
+
data = girl.read_nonblock(READ_SIZE)
|
|
1111
1106
|
rescue Errno::ENOTCONN => e
|
|
1112
1107
|
return
|
|
1113
1108
|
rescue Exception => e
|
|
1114
|
-
close_girl(
|
|
1109
|
+
close_girl(girl)
|
|
1115
1110
|
return
|
|
1116
1111
|
end
|
|
1117
1112
|
|
|
1118
|
-
set_update(
|
|
1119
|
-
girl_info = @girl_infos[
|
|
1120
|
-
relay = girl_info[
|
|
1121
|
-
add_relay_wbuff(
|
|
1113
|
+
set_update(girl)
|
|
1114
|
+
girl_info = @girl_infos[girl]
|
|
1115
|
+
relay = girl_info[:relay]
|
|
1116
|
+
add_relay_wbuff(relay, data)
|
|
1122
1117
|
end
|
|
1123
1118
|
|
|
1124
|
-
def read_infod(
|
|
1119
|
+
def read_infod(infod)
|
|
1125
1120
|
begin
|
|
1126
1121
|
data, addrinfo, rflags, *controls = infod.recvmsg
|
|
1127
1122
|
rescue Exception => e
|
|
1128
|
-
puts "infod recvmsg #{
|
|
1123
|
+
puts "infod recvmsg #{e.class}"
|
|
1129
1124
|
return
|
|
1130
1125
|
end
|
|
1131
1126
|
|
|
1132
1127
|
return if data.empty?
|
|
1133
1128
|
|
|
1134
1129
|
begin
|
|
1135
|
-
msg = JSON.parse(
|
|
1130
|
+
msg = JSON.parse(data, symbolize_names: true)
|
|
1136
1131
|
rescue JSON::ParserError, EncodingError => e
|
|
1137
|
-
puts "read infod #{
|
|
1132
|
+
puts "read infod #{e.class}"
|
|
1138
1133
|
return
|
|
1139
1134
|
end
|
|
1140
1135
|
|
|
1141
|
-
message_type = msg[
|
|
1136
|
+
message_type = msg[:message_type]
|
|
1142
1137
|
|
|
1143
1138
|
case message_type
|
|
1144
|
-
when 'heartbeat'
|
|
1145
|
-
if @proxy.closed?
|
|
1139
|
+
when 'heartbeat'
|
|
1140
|
+
if @proxy.closed?
|
|
1146
1141
|
new_a_proxy
|
|
1147
1142
|
else
|
|
1148
|
-
proxy_info = @proxy_infos[
|
|
1143
|
+
proxy_info = @proxy_infos[@proxy]
|
|
1149
1144
|
|
|
1150
|
-
if Time.new.to_i - proxy_info[
|
|
1151
|
-
close_proxy(
|
|
1145
|
+
if Time.new.to_i - proxy_info[:recv_at].to_i >= @expire_proxy_after
|
|
1146
|
+
close_proxy(@proxy)
|
|
1152
1147
|
new_a_proxy
|
|
1153
1148
|
else
|
|
1154
1149
|
puts "heartbeat" if @is_debug
|
|
1155
|
-
add_proxy_wbuff(
|
|
1150
|
+
add_proxy_wbuff(pack_a_chunk(@h_heartbeat))
|
|
1156
1151
|
end
|
|
1157
1152
|
end
|
|
1158
1153
|
end
|
|
1159
1154
|
end
|
|
1160
1155
|
|
|
1161
|
-
def read_mem(
|
|
1156
|
+
def read_mem(mem)
|
|
1162
1157
|
begin
|
|
1163
|
-
mem.read_nonblock(
|
|
1158
|
+
mem.read_nonblock(READ_SIZE)
|
|
1164
1159
|
rescue Errno::ENOTCONN => e
|
|
1165
1160
|
return
|
|
1166
1161
|
rescue Exception => e
|
|
1167
|
-
close_mem(
|
|
1162
|
+
close_mem(mem)
|
|
1168
1163
|
return
|
|
1169
1164
|
end
|
|
1170
1165
|
|
|
1171
|
-
set_update(
|
|
1166
|
+
set_update(mem)
|
|
1172
1167
|
src_arr = []
|
|
1173
1168
|
|
|
1174
|
-
@src_infos.each do |
|
|
1169
|
+
@src_infos.each do |_, info|
|
|
1175
1170
|
src_arr << {
|
|
1176
|
-
addrinfo: info[
|
|
1177
|
-
destination_domain: info[
|
|
1178
|
-
destination_port: info[
|
|
1171
|
+
addrinfo: info[:addrinfo].ip_unpack,
|
|
1172
|
+
destination_domain: info[:destination_domain],
|
|
1173
|
+
destination_port: info[:destination_port]
|
|
1179
1174
|
}
|
|
1180
1175
|
end
|
|
1181
1176
|
|
|
1182
1177
|
msg = {
|
|
1183
1178
|
resolv_caches: @resolv_caches.sort,
|
|
1184
|
-
response_caches: @response_caches.sort.map{
|
|
1185
|
-
response6_caches: @response6_caches.sort.map{
|
|
1179
|
+
response_caches: @response_caches.sort.map{|a| [a[0], a[1][2], a[1][3]]},
|
|
1180
|
+
response6_caches: @response6_caches.sort.map{|a| [a[0], a[1][2], a[1][3]]},
|
|
1186
1181
|
sizes: {
|
|
1187
1182
|
directs: @directs.size,
|
|
1188
1183
|
remotes: @remotes.size,
|
|
@@ -1210,16 +1205,16 @@ module Girl
|
|
|
1210
1205
|
src_arr: src_arr
|
|
1211
1206
|
}
|
|
1212
1207
|
|
|
1213
|
-
add_mem_wbuff(
|
|
1208
|
+
add_mem_wbuff(mem, JSON.generate(msg))
|
|
1214
1209
|
end
|
|
1215
1210
|
|
|
1216
|
-
def read_memd(
|
|
1211
|
+
def read_memd(memd)
|
|
1217
1212
|
check_expire_mems
|
|
1218
1213
|
|
|
1219
1214
|
begin
|
|
1220
1215
|
mem, addrinfo = memd.accept_nonblock
|
|
1221
1216
|
rescue Exception => e
|
|
1222
|
-
puts "memd accept #{
|
|
1217
|
+
puts "memd accept #{e.class}"
|
|
1223
1218
|
return
|
|
1224
1219
|
end
|
|
1225
1220
|
|
|
@@ -1227,75 +1222,75 @@ module Girl
|
|
|
1227
1222
|
wbuff: ''
|
|
1228
1223
|
}
|
|
1229
1224
|
|
|
1230
|
-
@mem_infos[
|
|
1231
|
-
add_read(
|
|
1225
|
+
@mem_infos[mem] = mem_info
|
|
1226
|
+
add_read(mem, :mem)
|
|
1232
1227
|
end
|
|
1233
1228
|
|
|
1234
|
-
def read_p1(
|
|
1229
|
+
def read_p1(p1)
|
|
1235
1230
|
begin
|
|
1236
|
-
data = p1.read_nonblock(
|
|
1231
|
+
data = p1.read_nonblock(READ_SIZE)
|
|
1237
1232
|
rescue Errno::ENOTCONN => e
|
|
1238
1233
|
return
|
|
1239
1234
|
rescue Exception => e
|
|
1240
|
-
close_p1(
|
|
1235
|
+
close_p1(p1)
|
|
1241
1236
|
return
|
|
1242
1237
|
end
|
|
1243
1238
|
|
|
1244
|
-
set_update(
|
|
1239
|
+
set_update(p1)
|
|
1245
1240
|
|
|
1246
|
-
if @proxy.closed?
|
|
1247
|
-
close_p1(
|
|
1241
|
+
if @proxy.closed?
|
|
1242
|
+
close_p1(p1)
|
|
1248
1243
|
return
|
|
1249
1244
|
end
|
|
1250
1245
|
|
|
1251
|
-
p1_info = @p1_infos[
|
|
1252
|
-
p2_id = p1_info[
|
|
1253
|
-
# puts "read p1 #{
|
|
1254
|
-
add_proxy_wbuff(
|
|
1246
|
+
p1_info = @p1_infos[p1]
|
|
1247
|
+
p2_id = p1_info[:p2_id]
|
|
1248
|
+
# puts "read p1 #{p2_id} #{data.bytesize}" if @is_debug
|
|
1249
|
+
add_proxy_wbuff(pack_p2_traffic(p2_id, data))
|
|
1255
1250
|
|
|
1256
|
-
unless @proxy.closed?
|
|
1257
|
-
proxy_info = @proxy_infos[
|
|
1258
|
-
bytesize = proxy_info[
|
|
1251
|
+
unless @proxy.closed?
|
|
1252
|
+
proxy_info = @proxy_infos[@proxy]
|
|
1253
|
+
bytesize = proxy_info[:wbuff].bytesize
|
|
1259
1254
|
|
|
1260
|
-
if (
|
|
1261
|
-
puts "proxy overflow pause p1 #{
|
|
1262
|
-
@reads.delete(
|
|
1263
|
-
proxy_info[
|
|
1255
|
+
if (bytesize >= WBUFF_LIMIT) && !proxy_info[:paused_p1s].include?(p1)
|
|
1256
|
+
puts "proxy overflow pause p1 #{p2_id}"
|
|
1257
|
+
@reads.delete(p1)
|
|
1258
|
+
proxy_info[:paused_p1s] << p1
|
|
1264
1259
|
end
|
|
1265
1260
|
end
|
|
1266
1261
|
end
|
|
1267
1262
|
|
|
1268
|
-
def read_proxy(
|
|
1263
|
+
def read_proxy(proxy)
|
|
1269
1264
|
begin
|
|
1270
|
-
data = proxy.read_nonblock(
|
|
1265
|
+
data = proxy.read_nonblock(READ_SIZE)
|
|
1271
1266
|
rescue Errno::ENOTCONN => e
|
|
1272
1267
|
return
|
|
1273
1268
|
rescue Exception => e
|
|
1274
|
-
close_proxy(
|
|
1269
|
+
close_proxy(proxy)
|
|
1275
1270
|
return
|
|
1276
1271
|
end
|
|
1277
1272
|
|
|
1278
|
-
set_update(
|
|
1279
|
-
proxy_info = @proxy_infos[
|
|
1280
|
-
data = "#{
|
|
1273
|
+
set_update(proxy)
|
|
1274
|
+
proxy_info = @proxy_infos[proxy]
|
|
1275
|
+
data = "#{proxy_info[:rbuff]}#{data}"
|
|
1281
1276
|
|
|
1282
|
-
msgs, part = decode_to_msgs(
|
|
1283
|
-
msgs.each{
|
|
1284
|
-
proxy_info[
|
|
1277
|
+
msgs, part = decode_to_msgs(data)
|
|
1278
|
+
msgs.each{|msg| deal_msg(msg)}
|
|
1279
|
+
proxy_info[:rbuff] = part
|
|
1285
1280
|
end
|
|
1286
1281
|
|
|
1287
|
-
def read_redir(
|
|
1282
|
+
def read_redir(redir)
|
|
1288
1283
|
check_expire_srcs
|
|
1289
1284
|
|
|
1290
1285
|
begin
|
|
1291
1286
|
src, addrinfo = redir.accept_nonblock
|
|
1292
1287
|
rescue IO::WaitReadable, Errno::EINTR => e
|
|
1293
|
-
puts "redir accept #{
|
|
1288
|
+
puts "redir accept #{e.class}"
|
|
1294
1289
|
return
|
|
1295
1290
|
end
|
|
1296
1291
|
|
|
1297
|
-
puts "redir accept a src #{
|
|
1298
|
-
src_id = rand(
|
|
1292
|
+
puts "redir accept a src #{addrinfo.ip_unpack.inspect}" if @is_debug
|
|
1293
|
+
src_id = rand((2 ** 64) - 2) + 1
|
|
1299
1294
|
|
|
1300
1295
|
src_info = {
|
|
1301
1296
|
addrinfo: addrinfo,
|
|
@@ -1312,37 +1307,37 @@ module Girl
|
|
|
1312
1307
|
wbuff: ''
|
|
1313
1308
|
}
|
|
1314
1309
|
|
|
1315
|
-
@src_infos[
|
|
1316
|
-
add_read(
|
|
1310
|
+
@src_infos[src] = src_info
|
|
1311
|
+
add_read(src, :src)
|
|
1317
1312
|
end
|
|
1318
1313
|
|
|
1319
|
-
def read_relay(
|
|
1314
|
+
def read_relay(relay)
|
|
1320
1315
|
begin
|
|
1321
|
-
data = relay.read_nonblock(
|
|
1316
|
+
data = relay.read_nonblock(READ_SIZE)
|
|
1322
1317
|
rescue Errno::ENOTCONN => e
|
|
1323
1318
|
return
|
|
1324
1319
|
rescue Exception => e
|
|
1325
|
-
close_relay(
|
|
1320
|
+
close_relay(relay)
|
|
1326
1321
|
return
|
|
1327
1322
|
end
|
|
1328
1323
|
|
|
1329
|
-
set_update(
|
|
1330
|
-
relay_info = @relay_infos[
|
|
1331
|
-
girl = relay_info[
|
|
1332
|
-
add_girl_wbuff(
|
|
1324
|
+
set_update(relay)
|
|
1325
|
+
relay_info = @relay_infos[relay]
|
|
1326
|
+
girl = relay_info[:girl]
|
|
1327
|
+
add_girl_wbuff(girl, data)
|
|
1333
1328
|
end
|
|
1334
1329
|
|
|
1335
|
-
def read_relayd(
|
|
1330
|
+
def read_relayd(relayd)
|
|
1336
1331
|
check_expire_relays
|
|
1337
1332
|
|
|
1338
1333
|
begin
|
|
1339
1334
|
relay, addrinfo = relayd.accept_nonblock
|
|
1340
1335
|
rescue IO::WaitReadable, Errno::EINTR => e
|
|
1341
|
-
puts "relayd accept #{
|
|
1336
|
+
puts "relayd accept #{e.class}"
|
|
1342
1337
|
return
|
|
1343
1338
|
end
|
|
1344
1339
|
|
|
1345
|
-
puts "relayd accept a relay #{
|
|
1340
|
+
puts "relayd accept a relay #{addrinfo.ip_unpack.inspect}"
|
|
1346
1341
|
|
|
1347
1342
|
relay_info = {
|
|
1348
1343
|
addrinfo: addrinfo,
|
|
@@ -1352,98 +1347,98 @@ module Girl
|
|
|
1352
1347
|
wbuff: ''
|
|
1353
1348
|
}
|
|
1354
1349
|
|
|
1355
|
-
@relay_infos[
|
|
1356
|
-
add_read(
|
|
1357
|
-
relay_info[
|
|
1350
|
+
@relay_infos[relay] = relay_info
|
|
1351
|
+
add_read(relay, :relay)
|
|
1352
|
+
relay_info[:girl] = new_a_girl(relay)
|
|
1358
1353
|
end
|
|
1359
1354
|
|
|
1360
|
-
def read_rsv(
|
|
1355
|
+
def read_rsv(rsv)
|
|
1361
1356
|
begin
|
|
1362
1357
|
data, addrinfo, rflags, *controls = rsv.recvmsg
|
|
1363
1358
|
rescue Exception => e
|
|
1364
|
-
puts "rsv recvmsg #{
|
|
1365
|
-
close_rsv(
|
|
1359
|
+
puts "rsv recvmsg #{e.class}"
|
|
1360
|
+
close_rsv(rsv)
|
|
1366
1361
|
return
|
|
1367
1362
|
end
|
|
1368
1363
|
|
|
1369
1364
|
return if data.empty?
|
|
1370
1365
|
|
|
1371
|
-
rsv_info = @rsv_infos[
|
|
1372
|
-
addrinfo = rsv_info[
|
|
1373
|
-
domain = rsv_info[
|
|
1374
|
-
type = rsv_info[
|
|
1375
|
-
send_data(
|
|
1366
|
+
rsv_info = @rsv_infos[rsv]
|
|
1367
|
+
addrinfo = rsv_info[:addrinfo]
|
|
1368
|
+
domain = rsv_info[:domain]
|
|
1369
|
+
type = rsv_info[:type]
|
|
1370
|
+
send_data(@rsvd, data, addrinfo)
|
|
1376
1371
|
|
|
1377
1372
|
begin
|
|
1378
|
-
ip = seek_ip(
|
|
1373
|
+
ip = seek_ip(data)
|
|
1379
1374
|
rescue Exception => e
|
|
1380
|
-
puts "rsv seek ip #{
|
|
1381
|
-
close_rsv(
|
|
1375
|
+
puts "rsv seek ip #{e.class} #{e.message}"
|
|
1376
|
+
close_rsv(rsv)
|
|
1382
1377
|
return
|
|
1383
1378
|
end
|
|
1384
1379
|
|
|
1385
|
-
if ip
|
|
1386
|
-
if type == 1
|
|
1387
|
-
puts "set response cache #{
|
|
1388
|
-
@response_caches[
|
|
1380
|
+
if ip
|
|
1381
|
+
if type == 1
|
|
1382
|
+
puts "set response cache #{domain} #{ip}" if @is_debug
|
|
1383
|
+
@response_caches[domain] = [data, Time.new, ip, false]
|
|
1389
1384
|
else
|
|
1390
|
-
puts "set response6 cache #{
|
|
1391
|
-
@response6_caches[
|
|
1385
|
+
puts "set response6 cache #{domain} #{ip}" if @is_debug
|
|
1386
|
+
@response6_caches[domain] = [data, Time.new, ip, false]
|
|
1392
1387
|
end
|
|
1393
1388
|
end
|
|
1394
1389
|
|
|
1395
|
-
close_rsv(
|
|
1390
|
+
close_rsv(rsv)
|
|
1396
1391
|
end
|
|
1397
1392
|
|
|
1398
|
-
def read_rsvd(
|
|
1393
|
+
def read_rsvd(rsvd)
|
|
1399
1394
|
begin
|
|
1400
1395
|
data, addrinfo, rflags, *controls = rsvd.recvmsg
|
|
1401
1396
|
rescue Exception => e
|
|
1402
|
-
puts "rsvd recvmsg #{
|
|
1397
|
+
puts "rsvd recvmsg #{e.class}"
|
|
1403
1398
|
return
|
|
1404
1399
|
end
|
|
1405
1400
|
|
|
1406
1401
|
return if data.empty?
|
|
1407
1402
|
|
|
1408
1403
|
begin
|
|
1409
|
-
id, domain, type = seek_question_dn(
|
|
1404
|
+
id, domain, type = seek_question_dn(data)
|
|
1410
1405
|
rescue Exception => e
|
|
1411
|
-
puts "seek question dn #{
|
|
1406
|
+
puts "seek question dn #{e.class} #{e.message}"
|
|
1412
1407
|
return
|
|
1413
1408
|
end
|
|
1414
1409
|
|
|
1415
|
-
return unless [
|
|
1410
|
+
return unless [1, 12, 28].include?(type)
|
|
1416
1411
|
|
|
1417
|
-
if type == 12
|
|
1418
|
-
new_a_rsv(
|
|
1412
|
+
if type == 12
|
|
1413
|
+
new_a_rsv(data, addrinfo, domain, type)
|
|
1419
1414
|
return
|
|
1420
1415
|
end
|
|
1421
1416
|
|
|
1422
|
-
if type == 1
|
|
1423
|
-
response_cache = @response_caches[
|
|
1417
|
+
if type == 1
|
|
1418
|
+
response_cache = @response_caches[domain]
|
|
1424
1419
|
else
|
|
1425
|
-
response_cache = @response6_caches[
|
|
1420
|
+
response_cache = @response6_caches[domain]
|
|
1426
1421
|
end
|
|
1427
1422
|
|
|
1428
|
-
if response_cache
|
|
1423
|
+
if response_cache
|
|
1429
1424
|
response, created_at = response_cache
|
|
1430
1425
|
|
|
1431
|
-
if Time.new - created_at < @expire_resolv_cache
|
|
1432
|
-
response[
|
|
1433
|
-
send_data(
|
|
1426
|
+
if Time.new - created_at < @expire_resolv_cache
|
|
1427
|
+
response[0, 2] = id
|
|
1428
|
+
send_data(@rsvd, response, addrinfo)
|
|
1434
1429
|
return
|
|
1435
1430
|
end
|
|
1436
1431
|
|
|
1437
|
-
if type == 1
|
|
1438
|
-
@response_caches.delete(
|
|
1432
|
+
if type == 1
|
|
1433
|
+
@response_caches.delete(domain)
|
|
1439
1434
|
else
|
|
1440
|
-
@response6_caches.delete(
|
|
1435
|
+
@response6_caches.delete(domain)
|
|
1441
1436
|
end
|
|
1442
1437
|
end
|
|
1443
1438
|
|
|
1444
|
-
if @remotes.any?{
|
|
1439
|
+
if @remotes.any?{|r| domain.include?(r)}
|
|
1445
1440
|
check_expire_nears
|
|
1446
|
-
near_id = rand(
|
|
1441
|
+
near_id = rand((2 ** 64) - 2) + 1
|
|
1447
1442
|
|
|
1448
1443
|
near_info = {
|
|
1449
1444
|
addrinfo: addrinfo,
|
|
@@ -1453,41 +1448,41 @@ module Girl
|
|
|
1453
1448
|
type: type
|
|
1454
1449
|
}
|
|
1455
1450
|
|
|
1456
|
-
@near_infos[
|
|
1457
|
-
puts "add h_query #{
|
|
1458
|
-
msg = "#{
|
|
1459
|
-
add_proxy_wbuff(
|
|
1451
|
+
@near_infos[near_id] = near_info
|
|
1452
|
+
puts "add h_query #{near_id} #{type} #{domain}" if @is_debug
|
|
1453
|
+
msg = "#{@h_query}#{[near_id, type].pack('Q>C')}#{domain}"
|
|
1454
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
1460
1455
|
return
|
|
1461
1456
|
end
|
|
1462
1457
|
|
|
1463
|
-
new_a_rsv(
|
|
1458
|
+
new_a_rsv(data, addrinfo, domain, type)
|
|
1464
1459
|
end
|
|
1465
1460
|
|
|
1466
|
-
def read_src(
|
|
1461
|
+
def read_src(src)
|
|
1467
1462
|
begin
|
|
1468
|
-
data = src.read_nonblock(
|
|
1463
|
+
data = src.read_nonblock(READ_SIZE)
|
|
1469
1464
|
rescue Errno::ENOTCONN => e
|
|
1470
1465
|
return
|
|
1471
1466
|
rescue Exception => e
|
|
1472
|
-
close_src(
|
|
1467
|
+
close_src(src)
|
|
1473
1468
|
return
|
|
1474
1469
|
end
|
|
1475
1470
|
|
|
1476
|
-
set_update(
|
|
1477
|
-
src_info = @src_infos[
|
|
1478
|
-
proxy_type = src_info[
|
|
1471
|
+
set_update(src)
|
|
1472
|
+
src_info = @src_infos[src]
|
|
1473
|
+
proxy_type = src_info[:proxy_type]
|
|
1479
1474
|
|
|
1480
1475
|
case proxy_type
|
|
1481
|
-
when :uncheck
|
|
1482
|
-
if data[
|
|
1483
|
-
domain_port = data.split(
|
|
1476
|
+
when :uncheck
|
|
1477
|
+
if data[0, 7] == 'CONNECT'
|
|
1478
|
+
domain_port = data.split("\r\n")[0].split(' ')[1]
|
|
1484
1479
|
|
|
1485
|
-
unless domain_port
|
|
1480
|
+
unless domain_port
|
|
1486
1481
|
puts "CONNECT miss domain"
|
|
1487
|
-
close_src(
|
|
1482
|
+
close_src(src)
|
|
1488
1483
|
return
|
|
1489
1484
|
end
|
|
1490
|
-
elsif data[
|
|
1485
|
+
elsif data[0].unpack('C').first == 5
|
|
1491
1486
|
# https://tools.ietf.org/html/rfc1928
|
|
1492
1487
|
#
|
|
1493
1488
|
# +----+----------+----------+
|
|
@@ -1495,12 +1490,12 @@ module Girl
|
|
|
1495
1490
|
# +----+----------+----------+
|
|
1496
1491
|
# | 1 | 1 | 1 to 255 |
|
|
1497
1492
|
# +----+----------+----------+
|
|
1498
|
-
nmethods = data[
|
|
1499
|
-
methods = data[
|
|
1493
|
+
nmethods = data[1].unpack('C').first
|
|
1494
|
+
methods = data[2, nmethods].unpack('C*')
|
|
1500
1495
|
|
|
1501
|
-
unless methods.include?(
|
|
1496
|
+
unless methods.include?(0)
|
|
1502
1497
|
puts "miss method 00"
|
|
1503
|
-
close_src(
|
|
1498
|
+
close_src(src)
|
|
1504
1499
|
return
|
|
1505
1500
|
end
|
|
1506
1501
|
|
|
@@ -1509,159 +1504,159 @@ module Girl
|
|
|
1509
1504
|
# +----+--------+
|
|
1510
1505
|
# | 1 | 1 |
|
|
1511
1506
|
# +----+--------+
|
|
1512
|
-
puts "read src version 5 nmethods #{
|
|
1513
|
-
data2 = [
|
|
1514
|
-
add_src_wbuff(
|
|
1507
|
+
puts "read src version 5 nmethods #{nmethods} methods #{methods.inspect}" if @is_debug
|
|
1508
|
+
data2 = [5, 0].pack('CC')
|
|
1509
|
+
add_src_wbuff(src, data2)
|
|
1515
1510
|
return if src.closed?
|
|
1516
|
-
src_info[
|
|
1517
|
-
src_info[
|
|
1511
|
+
src_info[:proxy_proto] = :socks5
|
|
1512
|
+
src_info[:proxy_type] = :negotiation
|
|
1518
1513
|
return
|
|
1519
1514
|
else
|
|
1520
|
-
host_line = data.split(
|
|
1515
|
+
host_line = data.split("\r\n").find{|_line| _line[0, 6] == 'Host: '}
|
|
1521
1516
|
|
|
1522
|
-
unless host_line
|
|
1523
|
-
close_src(
|
|
1517
|
+
unless host_line
|
|
1518
|
+
close_src(src)
|
|
1524
1519
|
return
|
|
1525
1520
|
end
|
|
1526
1521
|
|
|
1527
|
-
lines = data.split(
|
|
1522
|
+
lines = data.split("\r\n")
|
|
1528
1523
|
|
|
1529
|
-
unless lines.empty?
|
|
1530
|
-
method, url, proto = lines.first.split(
|
|
1524
|
+
unless lines.empty?
|
|
1525
|
+
method, url, proto = lines.first.split(' ')
|
|
1531
1526
|
|
|
1532
|
-
if proto && url && proto[
|
|
1533
|
-
domain_port = url.split(
|
|
1527
|
+
if proto && url && proto[0, 4] == 'HTTP' && url[0, 7] == 'http://'
|
|
1528
|
+
domain_port = url.split('/')[2]
|
|
1534
1529
|
end
|
|
1535
1530
|
end
|
|
1536
1531
|
|
|
1537
|
-
unless domain_port
|
|
1538
|
-
domain_port = host_line.split(
|
|
1532
|
+
unless domain_port
|
|
1533
|
+
domain_port = host_line.split(' ')[1]
|
|
1539
1534
|
|
|
1540
|
-
unless domain_port
|
|
1535
|
+
unless domain_port
|
|
1541
1536
|
puts "Host line miss domain"
|
|
1542
|
-
close_src(
|
|
1537
|
+
close_src(src)
|
|
1543
1538
|
return
|
|
1544
1539
|
end
|
|
1545
1540
|
end
|
|
1546
1541
|
|
|
1547
|
-
src_info[
|
|
1548
|
-
add_src_rbuff(
|
|
1542
|
+
src_info[:is_connect] = false
|
|
1543
|
+
add_src_rbuff(src, data)
|
|
1549
1544
|
end
|
|
1550
1545
|
|
|
1551
|
-
colon_idx = domain_port.rindex(
|
|
1552
|
-
close_idx = domain_port.rindex(
|
|
1546
|
+
colon_idx = domain_port.rindex(':')
|
|
1547
|
+
close_idx = domain_port.rindex(']')
|
|
1553
1548
|
|
|
1554
|
-
if colon_idx && (
|
|
1555
|
-
domain = domain_port[
|
|
1556
|
-
port = domain_port[
|
|
1549
|
+
if colon_idx && (close_idx.nil? || (colon_idx > close_idx))
|
|
1550
|
+
domain = domain_port[0...colon_idx]
|
|
1551
|
+
port = domain_port[(colon_idx + 1)..-1].to_i
|
|
1557
1552
|
else
|
|
1558
1553
|
domain = domain_port
|
|
1559
1554
|
port = 80
|
|
1560
1555
|
end
|
|
1561
1556
|
|
|
1562
|
-
domain = domain.gsub(
|
|
1563
|
-
src_info[
|
|
1564
|
-
src_info[
|
|
1565
|
-
src_info[
|
|
1566
|
-
resolve_domain(
|
|
1567
|
-
when :checking
|
|
1568
|
-
add_src_rbuff(
|
|
1569
|
-
when :negotiation
|
|
1557
|
+
domain = domain.gsub(/\[|\]/, '')
|
|
1558
|
+
src_info[:proxy_proto] = :http
|
|
1559
|
+
src_info[:destination_domain] = domain
|
|
1560
|
+
src_info[:destination_port] = port
|
|
1561
|
+
resolve_domain(domain, src)
|
|
1562
|
+
when :checking
|
|
1563
|
+
add_src_rbuff(src, data)
|
|
1564
|
+
when :negotiation
|
|
1570
1565
|
# +----+-----+-------+------+----------+----------+
|
|
1571
1566
|
# |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
|
|
1572
1567
|
# +----+-----+-------+------+----------+----------+
|
|
1573
1568
|
# | 1 | 1 | X'00' | 1 | Variable | 2 |
|
|
1574
1569
|
# +----+-----+-------+------+----------+----------+
|
|
1575
|
-
ver, cmd, rsv, atyp = data[
|
|
1570
|
+
ver, cmd, rsv, atyp = data[0, 4].unpack('C4')
|
|
1576
1571
|
|
|
1577
|
-
if cmd == 1
|
|
1578
|
-
if atyp == 1
|
|
1579
|
-
destination_host, destination_port = data[
|
|
1572
|
+
if cmd == 1
|
|
1573
|
+
if atyp == 1
|
|
1574
|
+
destination_host, destination_port = data[4, 6].unpack('Nn')
|
|
1580
1575
|
|
|
1581
1576
|
begin
|
|
1582
|
-
destination_addr = Socket.sockaddr_in(
|
|
1583
|
-
destination_addrinfo = Addrinfo.new(
|
|
1577
|
+
destination_addr = Socket.sockaddr_in(destination_port, destination_host)
|
|
1578
|
+
destination_addrinfo = Addrinfo.new(destination_addr)
|
|
1584
1579
|
rescue Exception => e
|
|
1585
|
-
puts "new addrinfo #{
|
|
1586
|
-
close_src(
|
|
1580
|
+
puts "new addrinfo #{e.class}"
|
|
1581
|
+
close_src(src)
|
|
1587
1582
|
return
|
|
1588
1583
|
end
|
|
1589
1584
|
|
|
1590
1585
|
destination_ip = destination_addrinfo.ip_address
|
|
1591
|
-
puts "read src cmd #{
|
|
1592
|
-
src_info[
|
|
1593
|
-
src_info[
|
|
1594
|
-
make_tunnel(
|
|
1595
|
-
elsif atyp == 3
|
|
1596
|
-
domain_len = data[
|
|
1597
|
-
|
|
1598
|
-
if (
|
|
1599
|
-
domain = data[
|
|
1600
|
-
port = data[
|
|
1601
|
-
puts "read src cmd #{
|
|
1602
|
-
src_info[
|
|
1603
|
-
src_info[
|
|
1604
|
-
resolve_domain(
|
|
1586
|
+
puts "read src cmd #{cmd} atyp #{atyp} #{destination_ip} #{destination_port}" if @is_debug
|
|
1587
|
+
src_info[:destination_domain] = destination_ip
|
|
1588
|
+
src_info[:destination_port] = destination_port
|
|
1589
|
+
make_tunnel(destination_ip, src)
|
|
1590
|
+
elsif atyp == 3
|
|
1591
|
+
domain_len = data[4].unpack('C').first
|
|
1592
|
+
|
|
1593
|
+
if (domain_len + 7) == data.bytesize
|
|
1594
|
+
domain = data[5, domain_len]
|
|
1595
|
+
port = data[(5 + domain_len), 2].unpack('n').first
|
|
1596
|
+
puts "read src cmd #{cmd} atyp #{atyp} #{domain} #{port}" if @is_debug
|
|
1597
|
+
src_info[:destination_domain] = domain
|
|
1598
|
+
src_info[:destination_port] = port
|
|
1599
|
+
resolve_domain(domain, src)
|
|
1605
1600
|
end
|
|
1606
1601
|
else
|
|
1607
|
-
puts "socks5 atyp #{
|
|
1608
|
-
close_src(
|
|
1602
|
+
puts "socks5 atyp #{atyp} not implement"
|
|
1603
|
+
close_src(src)
|
|
1609
1604
|
end
|
|
1610
1605
|
else
|
|
1611
|
-
puts "socks5 cmd #{
|
|
1612
|
-
close_src(
|
|
1606
|
+
puts "socks5 cmd #{cmd} not implement"
|
|
1607
|
+
close_src(src)
|
|
1613
1608
|
end
|
|
1614
|
-
when :remote
|
|
1615
|
-
src_id = src_info[
|
|
1616
|
-
add_proxy_wbuff(
|
|
1617
|
-
|
|
1618
|
-
unless @proxy.closed?
|
|
1619
|
-
proxy_info = @proxy_infos[
|
|
1620
|
-
bytesize = proxy_info[
|
|
1621
|
-
|
|
1622
|
-
if (
|
|
1623
|
-
puts "proxy overflow pause src #{
|
|
1624
|
-
@reads.delete(
|
|
1625
|
-
proxy_info[
|
|
1609
|
+
when :remote
|
|
1610
|
+
src_id = src_info[:src_id]
|
|
1611
|
+
add_proxy_wbuff(pack_traffic(src_id, data))
|
|
1612
|
+
|
|
1613
|
+
unless @proxy.closed?
|
|
1614
|
+
proxy_info = @proxy_infos[@proxy]
|
|
1615
|
+
bytesize = proxy_info[:wbuff].bytesize
|
|
1616
|
+
|
|
1617
|
+
if (bytesize >= WBUFF_LIMIT) && !proxy_info[:paused_srcs].include?(src)
|
|
1618
|
+
puts "proxy overflow pause src #{src_id} #{src_info[:destination_domain]}"
|
|
1619
|
+
@reads.delete(src)
|
|
1620
|
+
proxy_info[:paused_srcs] << src
|
|
1626
1621
|
end
|
|
1627
1622
|
end
|
|
1628
|
-
when :direct
|
|
1629
|
-
dst = src_info[
|
|
1623
|
+
when :direct
|
|
1624
|
+
dst = src_info[:dst]
|
|
1630
1625
|
|
|
1631
|
-
if dst
|
|
1632
|
-
add_dst_wbuff(
|
|
1626
|
+
if dst
|
|
1627
|
+
add_dst_wbuff(dst, data)
|
|
1633
1628
|
else
|
|
1634
|
-
add_src_rbuff(
|
|
1629
|
+
add_src_rbuff(src, data)
|
|
1635
1630
|
end
|
|
1636
1631
|
end
|
|
1637
1632
|
end
|
|
1638
1633
|
|
|
1639
|
-
def read_tspd(
|
|
1634
|
+
def read_tspd(tspd)
|
|
1640
1635
|
check_expire_srcs
|
|
1641
1636
|
|
|
1642
1637
|
begin
|
|
1643
1638
|
src, addrinfo = tspd.accept_nonblock
|
|
1644
1639
|
rescue IO::WaitReadable, Errno::EINTR => e
|
|
1645
|
-
puts "tspd accept #{
|
|
1640
|
+
puts "tspd accept #{e.class}"
|
|
1646
1641
|
return
|
|
1647
1642
|
end
|
|
1648
1643
|
|
|
1649
|
-
puts "tspd accept a src #{
|
|
1644
|
+
puts "tspd accept a src #{addrinfo.ip_unpack.inspect}" if @is_debug
|
|
1650
1645
|
|
|
1651
1646
|
begin
|
|
1652
1647
|
# /usr/include/linux/netfilter_ipv4.h
|
|
1653
|
-
option = src.getsockopt(
|
|
1648
|
+
option = src.getsockopt(Socket::SOL_IP, 80)
|
|
1654
1649
|
rescue Exception => e
|
|
1655
|
-
puts "get SO_ORIGINAL_DST #{
|
|
1650
|
+
puts "get SO_ORIGINAL_DST #{e.class} #{addrinfo.ip_unpack.inspect}"
|
|
1656
1651
|
src.close
|
|
1657
1652
|
return
|
|
1658
1653
|
end
|
|
1659
1654
|
|
|
1660
|
-
dest_family, dest_port, dest_host = option.unpack(
|
|
1661
|
-
dest_addr = Socket.sockaddr_in(
|
|
1662
|
-
dest_addrinfo = Addrinfo.new(
|
|
1655
|
+
dest_family, dest_port, dest_host = option.unpack('nnN')
|
|
1656
|
+
dest_addr = Socket.sockaddr_in(dest_port, dest_host)
|
|
1657
|
+
dest_addrinfo = Addrinfo.new(dest_addr)
|
|
1663
1658
|
dest_ip = dest_addrinfo.ip_address
|
|
1664
|
-
src_id = rand(
|
|
1659
|
+
src_id = rand((2 ** 64) - 2) + 1
|
|
1665
1660
|
|
|
1666
1661
|
src_info = {
|
|
1667
1662
|
addrinfo: addrinfo,
|
|
@@ -1678,66 +1673,64 @@ module Girl
|
|
|
1678
1673
|
wbuff: ''
|
|
1679
1674
|
}
|
|
1680
1675
|
|
|
1681
|
-
@src_infos[
|
|
1682
|
-
add_read(
|
|
1683
|
-
make_tunnel(
|
|
1676
|
+
@src_infos[src] = src_info
|
|
1677
|
+
add_read(src, :src)
|
|
1678
|
+
make_tunnel(dest_ip, src)
|
|
1684
1679
|
end
|
|
1685
1680
|
|
|
1686
|
-
def resolve_domain(
|
|
1681
|
+
def resolve_domain(domain, src)
|
|
1687
1682
|
return if src.nil? || src.closed?
|
|
1688
1683
|
|
|
1689
|
-
unless domain =~ /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/
|
|
1684
|
+
unless domain =~ /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/
|
|
1690
1685
|
# 忽略非法域名
|
|
1691
|
-
puts "ignore #{
|
|
1692
|
-
close_src(
|
|
1686
|
+
puts "ignore #{domain}"
|
|
1687
|
+
close_src(src)
|
|
1693
1688
|
return
|
|
1694
1689
|
end
|
|
1695
1690
|
|
|
1696
|
-
if domain == 'localhost'
|
|
1697
|
-
domain = "127.0.0.1"
|
|
1698
|
-
end
|
|
1691
|
+
domain = "127.0.0.1" if domain == 'localhost'
|
|
1699
1692
|
|
|
1700
|
-
if domain =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$/
|
|
1693
|
+
if domain =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$/
|
|
1701
1694
|
# ipv4
|
|
1702
|
-
make_tunnel(
|
|
1695
|
+
make_tunnel(domain, src)
|
|
1703
1696
|
return
|
|
1704
1697
|
end
|
|
1705
1698
|
|
|
1706
|
-
if @remotes.any?{
|
|
1707
|
-
set_remote(
|
|
1699
|
+
if @remotes.any?{|remote| (domain.size >= remote.size) && (domain[(remote.size * -1)..-1] == remote)}
|
|
1700
|
+
set_remote(src)
|
|
1708
1701
|
return
|
|
1709
1702
|
end
|
|
1710
1703
|
|
|
1711
|
-
resolv_cache = @resolv_caches[
|
|
1704
|
+
resolv_cache = @resolv_caches[domain]
|
|
1712
1705
|
|
|
1713
|
-
if resolv_cache
|
|
1706
|
+
if resolv_cache
|
|
1714
1707
|
ip, created_at = resolv_cache
|
|
1715
1708
|
|
|
1716
|
-
if Time.new - created_at < @expire_resolv_cache
|
|
1717
|
-
make_tunnel(
|
|
1709
|
+
if Time.new - created_at < @expire_resolv_cache
|
|
1710
|
+
make_tunnel(ip, src)
|
|
1718
1711
|
return
|
|
1719
1712
|
end
|
|
1720
1713
|
|
|
1721
|
-
@resolv_caches.delete(
|
|
1714
|
+
@resolv_caches.delete(domain)
|
|
1722
1715
|
end
|
|
1723
1716
|
|
|
1724
1717
|
begin
|
|
1725
|
-
data = pack_a_query(
|
|
1718
|
+
data = pack_a_query(domain)
|
|
1726
1719
|
rescue Exception => e
|
|
1727
|
-
puts "pack a query #{
|
|
1728
|
-
close_src(
|
|
1720
|
+
puts "pack a query #{e.class} #{e.message} #{domain}"
|
|
1721
|
+
close_src(src)
|
|
1729
1722
|
return
|
|
1730
1723
|
end
|
|
1731
1724
|
|
|
1732
1725
|
check_expire_dnses
|
|
1733
|
-
dns = Socket.new(
|
|
1726
|
+
dns = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
1734
1727
|
|
|
1735
1728
|
begin
|
|
1736
|
-
@nameserver_addrs.each{
|
|
1729
|
+
@nameserver_addrs.each{|addr| dns.sendmsg(data, 0, addr)}
|
|
1737
1730
|
rescue Exception => e
|
|
1738
|
-
puts "dns send data #{
|
|
1731
|
+
puts "dns send data #{e.class}"
|
|
1739
1732
|
dns.close
|
|
1740
|
-
close_src(
|
|
1733
|
+
close_src(src)
|
|
1741
1734
|
return
|
|
1742
1735
|
end
|
|
1743
1736
|
|
|
@@ -1746,126 +1739,123 @@ module Girl
|
|
|
1746
1739
|
src: src
|
|
1747
1740
|
}
|
|
1748
1741
|
|
|
1749
|
-
@dns_infos[
|
|
1750
|
-
add_read(
|
|
1751
|
-
src_info = @src_infos[
|
|
1752
|
-
src_info[
|
|
1742
|
+
@dns_infos[dns] = dns_info
|
|
1743
|
+
add_read(dns, :dns)
|
|
1744
|
+
src_info = @src_infos[src]
|
|
1745
|
+
src_info[:proxy_type] = :checking
|
|
1753
1746
|
end
|
|
1754
1747
|
|
|
1755
|
-
def send_data(
|
|
1748
|
+
def send_data(sock, data, target_addr)
|
|
1756
1749
|
begin
|
|
1757
|
-
sock.sendmsg(
|
|
1750
|
+
sock.sendmsg(data, 0, target_addr)
|
|
1758
1751
|
rescue Exception => e
|
|
1759
|
-
puts "sendmsg #{
|
|
1752
|
+
puts "sendmsg #{e.class}"
|
|
1760
1753
|
end
|
|
1761
1754
|
end
|
|
1762
1755
|
|
|
1763
|
-
def set_dst_closing(
|
|
1756
|
+
def set_dst_closing(dst)
|
|
1764
1757
|
return if dst.nil? || dst.closed?
|
|
1765
|
-
dst_info = @dst_infos[
|
|
1766
|
-
return if dst_info.nil? || dst_info[
|
|
1767
|
-
dst_info[
|
|
1768
|
-
add_write(
|
|
1758
|
+
dst_info = @dst_infos[dst]
|
|
1759
|
+
return if dst_info.nil? || dst_info[:closing]
|
|
1760
|
+
dst_info[:closing] = true
|
|
1761
|
+
add_write(dst)
|
|
1769
1762
|
end
|
|
1770
1763
|
|
|
1771
|
-
def set_girl_closing(
|
|
1764
|
+
def set_girl_closing(girl)
|
|
1772
1765
|
return if girl.nil? || girl.closed?
|
|
1773
|
-
girl_info = @girl_infos[
|
|
1774
|
-
return if girl_info.nil? || girl_info[
|
|
1775
|
-
girl_info[
|
|
1776
|
-
add_write(
|
|
1766
|
+
girl_info = @girl_infos[girl]
|
|
1767
|
+
return if girl_info.nil? || girl_info[:closing]
|
|
1768
|
+
girl_info[:closing] = true
|
|
1769
|
+
add_write(girl)
|
|
1777
1770
|
end
|
|
1778
1771
|
|
|
1779
|
-
def set_p1_closing(
|
|
1772
|
+
def set_p1_closing(p1)
|
|
1780
1773
|
return if p1.nil? || p1.closed?
|
|
1781
|
-
p1_info = @p1_infos[
|
|
1782
|
-
return if p1_info.nil? || p1_info[
|
|
1783
|
-
p1_info[
|
|
1784
|
-
add_write(
|
|
1774
|
+
p1_info = @p1_infos[p1]
|
|
1775
|
+
return if p1_info.nil? || p1_info[:closing]
|
|
1776
|
+
p1_info[:closing] = true
|
|
1777
|
+
add_write(p1)
|
|
1785
1778
|
end
|
|
1786
1779
|
|
|
1787
|
-
def set_relay_closing(
|
|
1780
|
+
def set_relay_closing(relay)
|
|
1788
1781
|
return if relay.nil? || relay.closed?
|
|
1789
|
-
relay_info = @relay_infos[
|
|
1790
|
-
return if relay_info.nil? || relay_info[
|
|
1791
|
-
relay_info[
|
|
1792
|
-
add_write(
|
|
1782
|
+
relay_info = @relay_infos[relay]
|
|
1783
|
+
return if relay_info.nil? || relay_info[:closing]
|
|
1784
|
+
relay_info[:closing] = true
|
|
1785
|
+
add_write(relay)
|
|
1793
1786
|
end
|
|
1794
1787
|
|
|
1795
|
-
def set_remote(
|
|
1788
|
+
def set_remote(src)
|
|
1796
1789
|
return if src.nil? || src.closed?
|
|
1797
1790
|
|
|
1798
|
-
if @proxy.closed?
|
|
1799
|
-
close_src(
|
|
1791
|
+
if @proxy.closed?
|
|
1792
|
+
close_src(src)
|
|
1800
1793
|
return
|
|
1801
1794
|
end
|
|
1802
1795
|
|
|
1803
|
-
src_info = @src_infos[
|
|
1804
|
-
src_info[
|
|
1796
|
+
src_info = @src_infos[src]
|
|
1797
|
+
src_info[:proxy_type] = :remote
|
|
1805
1798
|
|
|
1806
|
-
if src_info[
|
|
1807
|
-
if src_info[
|
|
1808
|
-
puts "add HTTP_OK #{
|
|
1809
|
-
add_src_wbuff(
|
|
1799
|
+
if src_info[:proxy_proto] == :http
|
|
1800
|
+
if src_info[:is_connect]
|
|
1801
|
+
puts "add HTTP_OK #{src_info[:proxy_type]}" if @is_debug
|
|
1802
|
+
add_src_wbuff(src, HTTP_OK)
|
|
1810
1803
|
end
|
|
1811
|
-
elsif src_info[
|
|
1812
|
-
puts "add_socks5_conn_reply #{
|
|
1813
|
-
add_socks5_conn_reply(
|
|
1804
|
+
elsif src_info[:proxy_proto] == :socks5
|
|
1805
|
+
puts "add_socks5_conn_reply #{src_info[:proxy_type]}" if @is_debug
|
|
1806
|
+
add_socks5_conn_reply(src)
|
|
1814
1807
|
end
|
|
1815
1808
|
|
|
1816
|
-
src_id = src_info[
|
|
1817
|
-
domain = src_info[
|
|
1818
|
-
port = src_info[
|
|
1819
|
-
domain_port = [
|
|
1820
|
-
puts "add h_a_new_source #{
|
|
1821
|
-
msg = "#{
|
|
1822
|
-
add_proxy_wbuff(
|
|
1823
|
-
data = src_info[
|
|
1809
|
+
src_id = src_info[:src_id]
|
|
1810
|
+
domain = src_info[:destination_domain]
|
|
1811
|
+
port = src_info[:destination_port]
|
|
1812
|
+
domain_port = [domain, port].join(':')
|
|
1813
|
+
puts "add h_a_new_source #{src_id} #{domain_port}" if @is_debug
|
|
1814
|
+
msg = "#{@h_a_new_source}#{[src_id].pack('Q>')}#{domain_port}"
|
|
1815
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
1816
|
+
data = src_info[:rbuff].dup
|
|
1824
1817
|
|
|
1825
|
-
unless data.empty?
|
|
1826
|
-
puts "move src rbuff to proxy #{
|
|
1827
|
-
add_proxy_wbuff(
|
|
1818
|
+
unless data.empty?
|
|
1819
|
+
puts "move src rbuff to proxy #{domain} #{data.bytesize}" if @is_debug
|
|
1820
|
+
add_proxy_wbuff(pack_traffic(src_id, data))
|
|
1828
1821
|
end
|
|
1829
1822
|
end
|
|
1830
1823
|
|
|
1831
|
-
def set_src_closing(
|
|
1824
|
+
def set_src_closing(src)
|
|
1832
1825
|
return if src.nil? || src.closed?
|
|
1833
|
-
src_info = @src_infos[
|
|
1834
|
-
return if src_info.nil? || src_info[
|
|
1835
|
-
src_info[
|
|
1836
|
-
add_write(
|
|
1826
|
+
src_info = @src_infos[src]
|
|
1827
|
+
return if src_info.nil? || src_info[:closing]
|
|
1828
|
+
src_info[:closing] = true
|
|
1829
|
+
add_write(src)
|
|
1837
1830
|
end
|
|
1838
1831
|
|
|
1839
|
-
def set_update(
|
|
1840
|
-
@updates[
|
|
1841
|
-
|
|
1842
|
-
if @updates_limit - @updates.size <= 20 then
|
|
1843
|
-
puts "updates #{ @updates.size }"
|
|
1844
|
-
end
|
|
1832
|
+
def set_update(sock)
|
|
1833
|
+
@updates[sock] = Time.new
|
|
1834
|
+
puts "updates #{@updates.size}" if @updates_limit - @updates.size <= 20
|
|
1845
1835
|
|
|
1846
|
-
if @updates.size >= @updates_limit
|
|
1836
|
+
if @updates.size >= @updates_limit
|
|
1847
1837
|
puts "eliminate updates"
|
|
1848
1838
|
|
|
1849
|
-
@updates.keys.each do |
|
|
1850
|
-
case @roles[
|
|
1839
|
+
@updates.keys.each do |_sock|
|
|
1840
|
+
case @roles[_sock]
|
|
1851
1841
|
when :dns
|
|
1852
|
-
close_dns(
|
|
1842
|
+
close_dns(_sock)
|
|
1853
1843
|
when :dst
|
|
1854
|
-
close_dst(
|
|
1844
|
+
close_dst(_sock)
|
|
1855
1845
|
when :girl
|
|
1856
|
-
close_girl(
|
|
1846
|
+
close_girl(_sock)
|
|
1857
1847
|
when :mem
|
|
1858
|
-
close_mem(
|
|
1848
|
+
close_mem(_sock)
|
|
1859
1849
|
when :p1
|
|
1860
|
-
close_p1(
|
|
1850
|
+
close_p1(_sock)
|
|
1861
1851
|
when :relay
|
|
1862
|
-
close_relay(
|
|
1852
|
+
close_relay(_sock)
|
|
1863
1853
|
when :rsv
|
|
1864
|
-
close_rsv(
|
|
1854
|
+
close_rsv(_sock)
|
|
1865
1855
|
when :src
|
|
1866
|
-
close_src(
|
|
1856
|
+
close_src(_sock)
|
|
1867
1857
|
else
|
|
1868
|
-
close_sock(
|
|
1858
|
+
close_sock(_sock)
|
|
1869
1859
|
end
|
|
1870
1860
|
end
|
|
1871
1861
|
|
|
@@ -1873,301 +1863,301 @@ module Girl
|
|
|
1873
1863
|
end
|
|
1874
1864
|
end
|
|
1875
1865
|
|
|
1876
|
-
def write_dst(
|
|
1877
|
-
if dst.closed?
|
|
1866
|
+
def write_dst(dst)
|
|
1867
|
+
if dst.closed?
|
|
1878
1868
|
puts "write closed dst?"
|
|
1879
1869
|
return
|
|
1880
1870
|
end
|
|
1881
1871
|
|
|
1882
|
-
dst_info = @dst_infos[
|
|
1883
|
-
dst_info[
|
|
1884
|
-
data = dst_info[
|
|
1872
|
+
dst_info = @dst_infos[dst]
|
|
1873
|
+
dst_info[:connected] = true
|
|
1874
|
+
data = dst_info[:wbuff]
|
|
1885
1875
|
|
|
1886
|
-
if data.empty?
|
|
1887
|
-
if dst_info[
|
|
1888
|
-
close_dst(
|
|
1876
|
+
if data.empty?
|
|
1877
|
+
if dst_info[:closing]
|
|
1878
|
+
close_dst(dst)
|
|
1889
1879
|
else
|
|
1890
|
-
@writes.delete(
|
|
1880
|
+
@writes.delete(dst)
|
|
1891
1881
|
end
|
|
1892
1882
|
|
|
1893
1883
|
return
|
|
1894
1884
|
end
|
|
1895
1885
|
|
|
1896
1886
|
begin
|
|
1897
|
-
written = dst.write_nonblock(
|
|
1887
|
+
written = dst.write_nonblock(data)
|
|
1898
1888
|
rescue Errno::EINPROGRESS
|
|
1899
1889
|
return
|
|
1900
1890
|
rescue Exception => e
|
|
1901
|
-
close_dst(
|
|
1891
|
+
close_dst(dst)
|
|
1902
1892
|
return
|
|
1903
1893
|
end
|
|
1904
1894
|
|
|
1905
|
-
set_update(
|
|
1906
|
-
data = data[
|
|
1907
|
-
dst_info[
|
|
1908
|
-
bytesize = dst_info[
|
|
1895
|
+
set_update(dst)
|
|
1896
|
+
data = data[written..-1]
|
|
1897
|
+
dst_info[:wbuff] = data
|
|
1898
|
+
bytesize = dst_info[:wbuff].bytesize
|
|
1909
1899
|
|
|
1910
|
-
if dst_info[
|
|
1911
|
-
puts "dst underhalf #{
|
|
1912
|
-
add_read(
|
|
1913
|
-
dst_info[
|
|
1900
|
+
if dst_info[:overflowing] && (bytesize < RESUME_BELOW)
|
|
1901
|
+
puts "dst underhalf #{dst_info[:domain]}"
|
|
1902
|
+
add_read(dst_info[:src])
|
|
1903
|
+
dst_info[:overflowing] = false
|
|
1914
1904
|
end
|
|
1915
1905
|
end
|
|
1916
1906
|
|
|
1917
|
-
def write_girl(
|
|
1918
|
-
if girl.closed?
|
|
1907
|
+
def write_girl(girl)
|
|
1908
|
+
if girl.closed?
|
|
1919
1909
|
puts "write closed girl?"
|
|
1920
1910
|
return
|
|
1921
1911
|
end
|
|
1922
1912
|
|
|
1923
|
-
girl_info = @girl_infos[
|
|
1924
|
-
girl_info[
|
|
1925
|
-
data = girl_info[
|
|
1913
|
+
girl_info = @girl_infos[girl]
|
|
1914
|
+
girl_info[:connected] = true
|
|
1915
|
+
data = girl_info[:wbuff]
|
|
1926
1916
|
|
|
1927
|
-
if data.empty?
|
|
1928
|
-
if girl_info[
|
|
1929
|
-
close_girl(
|
|
1917
|
+
if data.empty?
|
|
1918
|
+
if girl_info[:closing]
|
|
1919
|
+
close_girl(girl)
|
|
1930
1920
|
else
|
|
1931
|
-
@writes.delete(
|
|
1921
|
+
@writes.delete(girl)
|
|
1932
1922
|
end
|
|
1933
1923
|
|
|
1934
1924
|
return
|
|
1935
1925
|
end
|
|
1936
1926
|
|
|
1937
1927
|
begin
|
|
1938
|
-
if girl_info[
|
|
1939
|
-
written = girl.sendmsg_nonblock(
|
|
1940
|
-
girl_info[
|
|
1928
|
+
if girl_info[:is_syn]
|
|
1929
|
+
written = girl.sendmsg_nonblock(data, 536870912, @proxyd_addr)
|
|
1930
|
+
girl_info[:is_syn] = false
|
|
1941
1931
|
else
|
|
1942
|
-
written = girl.write_nonblock(
|
|
1932
|
+
written = girl.write_nonblock(data)
|
|
1943
1933
|
end
|
|
1944
1934
|
rescue Errno::EINPROGRESS
|
|
1945
1935
|
return
|
|
1946
1936
|
rescue Exception => e
|
|
1947
|
-
close_girl(
|
|
1937
|
+
close_girl(girl)
|
|
1948
1938
|
return
|
|
1949
1939
|
end
|
|
1950
1940
|
|
|
1951
|
-
set_update(
|
|
1952
|
-
data = data[
|
|
1953
|
-
girl_info[
|
|
1954
|
-
bytesize = girl_info[
|
|
1941
|
+
set_update(girl)
|
|
1942
|
+
data = data[written..-1]
|
|
1943
|
+
girl_info[:wbuff] = data
|
|
1944
|
+
bytesize = girl_info[:wbuff].bytesize
|
|
1955
1945
|
|
|
1956
|
-
if girl_info[
|
|
1946
|
+
if girl_info[:overflowing] && (bytesize < RESUME_BELOW)
|
|
1957
1947
|
puts "girl underhalf"
|
|
1958
|
-
add_read(
|
|
1959
|
-
girl_info[
|
|
1948
|
+
add_read(girl_info[:relay])
|
|
1949
|
+
girl_info[:overflowing] = false
|
|
1960
1950
|
end
|
|
1961
1951
|
end
|
|
1962
1952
|
|
|
1963
|
-
def write_mem(
|
|
1964
|
-
if mem.closed?
|
|
1953
|
+
def write_mem(mem)
|
|
1954
|
+
if mem.closed?
|
|
1965
1955
|
puts "write closed mem?"
|
|
1966
1956
|
return
|
|
1967
1957
|
end
|
|
1968
1958
|
|
|
1969
|
-
mem_info = @mem_infos[
|
|
1970
|
-
data = mem_info[
|
|
1959
|
+
mem_info = @mem_infos[mem]
|
|
1960
|
+
data = mem_info[:wbuff]
|
|
1971
1961
|
|
|
1972
|
-
if data.empty?
|
|
1973
|
-
@writes.delete(
|
|
1974
|
-
close_mem(
|
|
1962
|
+
if data.empty?
|
|
1963
|
+
@writes.delete(mem)
|
|
1964
|
+
close_mem(mem)
|
|
1975
1965
|
return
|
|
1976
1966
|
end
|
|
1977
1967
|
|
|
1978
1968
|
begin
|
|
1979
|
-
written = mem.write_nonblock(
|
|
1969
|
+
written = mem.write_nonblock(data)
|
|
1980
1970
|
rescue Errno::EINPROGRESS
|
|
1981
1971
|
return
|
|
1982
1972
|
rescue Exception => e
|
|
1983
|
-
close_mem(
|
|
1973
|
+
close_mem(mem)
|
|
1984
1974
|
return
|
|
1985
1975
|
end
|
|
1986
1976
|
|
|
1987
|
-
set_update(
|
|
1988
|
-
data = data[
|
|
1989
|
-
mem_info[
|
|
1977
|
+
set_update(mem)
|
|
1978
|
+
data = data[written..-1]
|
|
1979
|
+
mem_info[:wbuff] = data
|
|
1990
1980
|
end
|
|
1991
1981
|
|
|
1992
|
-
def write_p1(
|
|
1993
|
-
if p1.closed?
|
|
1982
|
+
def write_p1(p1)
|
|
1983
|
+
if p1.closed?
|
|
1994
1984
|
puts "write closed p1?"
|
|
1995
1985
|
return
|
|
1996
1986
|
end
|
|
1997
1987
|
|
|
1998
|
-
p1_info = @p1_infos[
|
|
1999
|
-
p1_info[
|
|
2000
|
-
data = p1_info[
|
|
1988
|
+
p1_info = @p1_infos[p1]
|
|
1989
|
+
p1_info[:connected] = true
|
|
1990
|
+
data = p1_info[:wbuff]
|
|
2001
1991
|
|
|
2002
|
-
if data.empty?
|
|
2003
|
-
if p1_info[
|
|
2004
|
-
close_p1(
|
|
1992
|
+
if data.empty?
|
|
1993
|
+
if p1_info[:closing]
|
|
1994
|
+
close_p1(p1)
|
|
2005
1995
|
else
|
|
2006
|
-
@writes.delete(
|
|
1996
|
+
@writes.delete(p1)
|
|
2007
1997
|
end
|
|
2008
1998
|
|
|
2009
1999
|
return
|
|
2010
2000
|
end
|
|
2011
2001
|
|
|
2012
2002
|
begin
|
|
2013
|
-
written = p1.write_nonblock(
|
|
2003
|
+
written = p1.write_nonblock(data)
|
|
2014
2004
|
rescue Errno::EINPROGRESS
|
|
2015
2005
|
return
|
|
2016
2006
|
rescue Exception => e
|
|
2017
|
-
close_p1(
|
|
2007
|
+
close_p1(p1)
|
|
2018
2008
|
return
|
|
2019
2009
|
end
|
|
2020
2010
|
|
|
2021
|
-
set_update(
|
|
2022
|
-
data = data[
|
|
2023
|
-
p1_info[
|
|
2024
|
-
bytesize = p1_info[
|
|
2011
|
+
set_update(p1)
|
|
2012
|
+
data = data[written..-1]
|
|
2013
|
+
p1_info[:wbuff] = data
|
|
2014
|
+
bytesize = p1_info[:wbuff].bytesize
|
|
2025
2015
|
|
|
2026
|
-
if p1_info[
|
|
2027
|
-
p2_id = p1_info[
|
|
2028
|
-
puts "add h_p1_underhalf #{
|
|
2029
|
-
msg = "#{
|
|
2030
|
-
add_proxy_wbuff(
|
|
2031
|
-
p1_info[
|
|
2016
|
+
if p1_info[:overflowing] && (bytesize < RESUME_BELOW)
|
|
2017
|
+
p2_id = p1_info[:p2_id]
|
|
2018
|
+
puts "add h_p1_underhalf #{p2_id}"
|
|
2019
|
+
msg = "#{@h_p1_underhalf}#{[p2_id].pack('Q>')}"
|
|
2020
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
2021
|
+
p1_info[:overflowing] = false
|
|
2032
2022
|
end
|
|
2033
2023
|
end
|
|
2034
2024
|
|
|
2035
|
-
def write_proxy(
|
|
2036
|
-
if proxy.closed?
|
|
2025
|
+
def write_proxy(proxy)
|
|
2026
|
+
if proxy.closed?
|
|
2037
2027
|
puts "write closed proxy?"
|
|
2038
2028
|
return
|
|
2039
2029
|
end
|
|
2040
2030
|
|
|
2041
|
-
proxy_info = @proxy_infos[
|
|
2042
|
-
data = proxy_info[
|
|
2031
|
+
proxy_info = @proxy_infos[proxy]
|
|
2032
|
+
data = proxy_info[:wbuff]
|
|
2043
2033
|
|
|
2044
|
-
if data.empty?
|
|
2045
|
-
@writes.delete(
|
|
2034
|
+
if data.empty?
|
|
2035
|
+
@writes.delete(proxy)
|
|
2046
2036
|
return
|
|
2047
2037
|
end
|
|
2048
2038
|
|
|
2049
2039
|
begin
|
|
2050
|
-
if proxy_info[
|
|
2051
|
-
written = proxy.sendmsg_nonblock(
|
|
2052
|
-
proxy_info[
|
|
2040
|
+
if proxy_info[:is_syn]
|
|
2041
|
+
written = proxy.sendmsg_nonblock(data, 536870912, @proxyd_addr)
|
|
2042
|
+
proxy_info[:is_syn] = false
|
|
2053
2043
|
else
|
|
2054
|
-
written = proxy.write_nonblock(
|
|
2044
|
+
written = proxy.write_nonblock(data)
|
|
2055
2045
|
end
|
|
2056
2046
|
rescue Errno::EINPROGRESS
|
|
2057
2047
|
return
|
|
2058
2048
|
rescue Exception => e
|
|
2059
|
-
puts "write proxy #{
|
|
2060
|
-
close_proxy(
|
|
2049
|
+
puts "write proxy #{e.class}"
|
|
2050
|
+
close_proxy(proxy)
|
|
2061
2051
|
return
|
|
2062
2052
|
end
|
|
2063
2053
|
|
|
2064
|
-
set_update(
|
|
2065
|
-
data = data[
|
|
2066
|
-
proxy_info[
|
|
2067
|
-
bytesize = proxy_info[
|
|
2054
|
+
set_update(proxy)
|
|
2055
|
+
data = data[written..-1]
|
|
2056
|
+
proxy_info[:wbuff] = data
|
|
2057
|
+
bytesize = proxy_info[:wbuff].bytesize
|
|
2068
2058
|
|
|
2069
|
-
if bytesize < RESUME_BELOW
|
|
2070
|
-
if proxy_info[
|
|
2071
|
-
puts "proxy underhalf resume srcs #{
|
|
2072
|
-
proxy_info[
|
|
2073
|
-
proxy_info[
|
|
2059
|
+
if bytesize < RESUME_BELOW
|
|
2060
|
+
if proxy_info[:paused_srcs].any?
|
|
2061
|
+
puts "proxy underhalf resume srcs #{proxy_info[:paused_srcs].size}"
|
|
2062
|
+
proxy_info[:paused_srcs].each{|src| add_read(src)}
|
|
2063
|
+
proxy_info[:paused_srcs].clear
|
|
2074
2064
|
end
|
|
2075
2065
|
|
|
2076
|
-
if proxy_info[
|
|
2077
|
-
puts "proxy underhalf resume p1s #{
|
|
2078
|
-
proxy_info[
|
|
2079
|
-
proxy_info[
|
|
2066
|
+
if proxy_info[:paused_p1s].any?
|
|
2067
|
+
puts "proxy underhalf resume p1s #{proxy_info[:paused_p1s].size}"
|
|
2068
|
+
proxy_info[:paused_p1s].each{|p1| add_read(p1)}
|
|
2069
|
+
proxy_info[:paused_p1s].clear
|
|
2080
2070
|
end
|
|
2081
2071
|
end
|
|
2082
2072
|
end
|
|
2083
2073
|
|
|
2084
|
-
def write_src(
|
|
2085
|
-
if src.closed?
|
|
2074
|
+
def write_src(src)
|
|
2075
|
+
if src.closed?
|
|
2086
2076
|
puts "write closed src?"
|
|
2087
2077
|
return
|
|
2088
2078
|
end
|
|
2089
2079
|
|
|
2090
|
-
src_info = @src_infos[
|
|
2091
|
-
data = src_info[
|
|
2080
|
+
src_info = @src_infos[src]
|
|
2081
|
+
data = src_info[:wbuff]
|
|
2092
2082
|
|
|
2093
|
-
if data.empty?
|
|
2094
|
-
if src_info[
|
|
2095
|
-
close_src(
|
|
2083
|
+
if data.empty?
|
|
2084
|
+
if src_info[:closing]
|
|
2085
|
+
close_src(src)
|
|
2096
2086
|
else
|
|
2097
|
-
@writes.delete(
|
|
2087
|
+
@writes.delete(src)
|
|
2098
2088
|
end
|
|
2099
2089
|
|
|
2100
2090
|
return
|
|
2101
2091
|
end
|
|
2102
2092
|
|
|
2103
2093
|
begin
|
|
2104
|
-
written = src.write_nonblock(
|
|
2094
|
+
written = src.write_nonblock(data)
|
|
2105
2095
|
rescue Errno::EINPROGRESS
|
|
2106
2096
|
return
|
|
2107
2097
|
rescue Exception => e
|
|
2108
|
-
close_src(
|
|
2098
|
+
close_src(src)
|
|
2109
2099
|
return
|
|
2110
2100
|
end
|
|
2111
2101
|
|
|
2112
|
-
set_update(
|
|
2113
|
-
data = data[
|
|
2114
|
-
src_info[
|
|
2115
|
-
bytesize = src_info[
|
|
2102
|
+
set_update(src)
|
|
2103
|
+
data = data[written..-1]
|
|
2104
|
+
src_info[:wbuff] = data
|
|
2105
|
+
bytesize = src_info[:wbuff].bytesize
|
|
2116
2106
|
|
|
2117
|
-
if src_info[
|
|
2118
|
-
src_id = src_info[
|
|
2119
|
-
domain = src_info[
|
|
2107
|
+
if src_info[:overflowing] && (bytesize < RESUME_BELOW)
|
|
2108
|
+
src_id = src_info[:src_id]
|
|
2109
|
+
domain = src_info[:destination_domain]
|
|
2120
2110
|
|
|
2121
|
-
if src_info[
|
|
2122
|
-
puts "src underhalf #{
|
|
2123
|
-
add_read(
|
|
2111
|
+
if src_info[:proxy_type] == :direct
|
|
2112
|
+
puts "src underhalf #{src_id} #{domain}"
|
|
2113
|
+
add_read(src_info[:dst])
|
|
2124
2114
|
else
|
|
2125
|
-
puts "add h_src_underhalf #{
|
|
2126
|
-
msg = "#{
|
|
2127
|
-
add_proxy_wbuff(
|
|
2115
|
+
puts "add h_src_underhalf #{src_id} #{domain}"
|
|
2116
|
+
msg = "#{@h_src_underhalf}#{[src_id].pack('Q>')}"
|
|
2117
|
+
add_proxy_wbuff(pack_a_chunk(msg))
|
|
2128
2118
|
end
|
|
2129
2119
|
|
|
2130
|
-
src_info[
|
|
2120
|
+
src_info[:overflowing] = false
|
|
2131
2121
|
end
|
|
2132
2122
|
end
|
|
2133
2123
|
|
|
2134
|
-
def write_relay(
|
|
2135
|
-
if relay.closed?
|
|
2124
|
+
def write_relay(relay)
|
|
2125
|
+
if relay.closed?
|
|
2136
2126
|
puts "write closed relay?"
|
|
2137
2127
|
return
|
|
2138
2128
|
end
|
|
2139
2129
|
|
|
2140
|
-
relay_info = @relay_infos[
|
|
2141
|
-
data = relay_info[
|
|
2130
|
+
relay_info = @relay_infos[relay]
|
|
2131
|
+
data = relay_info[:wbuff]
|
|
2142
2132
|
|
|
2143
|
-
if data.empty?
|
|
2144
|
-
if relay_info[
|
|
2145
|
-
close_relay(
|
|
2133
|
+
if data.empty?
|
|
2134
|
+
if relay_info[:closing]
|
|
2135
|
+
close_relay(relay)
|
|
2146
2136
|
else
|
|
2147
|
-
@writes.delete(
|
|
2137
|
+
@writes.delete(relay)
|
|
2148
2138
|
end
|
|
2149
2139
|
|
|
2150
2140
|
return
|
|
2151
2141
|
end
|
|
2152
2142
|
|
|
2153
2143
|
begin
|
|
2154
|
-
written = relay.write_nonblock(
|
|
2144
|
+
written = relay.write_nonblock(data)
|
|
2155
2145
|
rescue Errno::EINPROGRESS
|
|
2156
2146
|
return
|
|
2157
2147
|
rescue Exception => e
|
|
2158
|
-
close_relay(
|
|
2148
|
+
close_relay(relay)
|
|
2159
2149
|
return
|
|
2160
2150
|
end
|
|
2161
2151
|
|
|
2162
|
-
set_update(
|
|
2163
|
-
data = data[
|
|
2164
|
-
relay_info[
|
|
2165
|
-
bytesize = relay_info[
|
|
2152
|
+
set_update(relay)
|
|
2153
|
+
data = data[written..-1]
|
|
2154
|
+
relay_info[:wbuff] = data
|
|
2155
|
+
bytesize = relay_info[:wbuff].bytesize
|
|
2166
2156
|
|
|
2167
|
-
if relay_info[
|
|
2157
|
+
if relay_info[:overflowing] && (bytesize < RESUME_BELOW)
|
|
2168
2158
|
puts "relay underhalf"
|
|
2169
|
-
add_read(
|
|
2170
|
-
relay_info[
|
|
2159
|
+
add_read(relay_info[:girl])
|
|
2160
|
+
relay_info[:overflowing] = false
|
|
2171
2161
|
end
|
|
2172
2162
|
end
|
|
2173
2163
|
|