girl 9.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/girl.gemspec +30 -0
- data/lib/girl/dns.rb +137 -0
- data/lib/girl/head.rb +20 -0
- data/lib/girl/proxy.rb +191 -0
- data/lib/girl/proxy_worker.rb +1860 -0
- data/lib/girl/proxyd.rb +173 -0
- data/lib/girl/proxyd_worker.rb +1409 -0
- data/lib/girl/version.rb +3 -0
- data/lib/girl.rb +2 -0
- metadata +52 -0
data/lib/girl/proxyd.rb
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'girl/dns'
|
2
|
+
require 'girl/head'
|
3
|
+
require 'girl/proxyd_worker'
|
4
|
+
require 'girl/version'
|
5
|
+
require 'json'
|
6
|
+
require 'socket'
|
7
|
+
|
8
|
+
##
|
9
|
+
# Girl::Proxyd - 远端
|
10
|
+
#
|
11
|
+
module Girl
|
12
|
+
class Proxyd
|
13
|
+
|
14
|
+
def initialize( config_path = nil )
|
15
|
+
if config_path then
|
16
|
+
raise "not found config file #{ config_path }" unless File.exist?( config_path )
|
17
|
+
conf = JSON.parse( IO.binread( config_path ), symbolize_names: true )
|
18
|
+
puts "load #{ config_path } #{ conf.inspect }"
|
19
|
+
proxyd_port = conf[ :proxyd_port ]
|
20
|
+
memd_port = conf[ :memd_port ]
|
21
|
+
nameserver = conf[ :nameserver ]
|
22
|
+
reset_traff_day = conf[ :reset_traff_day ]
|
23
|
+
ims = conf[ :ims ]
|
24
|
+
p2d_host = conf[ :p2d_host ]
|
25
|
+
p2d_port = conf[ :p2d_port ]
|
26
|
+
head_len = conf[ :head_len ] # 头长度
|
27
|
+
h_a_new_source = conf[ :h_a_new_source ] # A
|
28
|
+
h_a_new_p2 = conf[ :h_a_new_p2 ] # B
|
29
|
+
h_dst_close = conf[ :h_dst_close ] # D
|
30
|
+
h_heartbeat = conf[ :h_heartbeat ] # H
|
31
|
+
h_p1_close = conf[ :h_p1_close ] # I
|
32
|
+
h_p2_close = conf[ :h_p2_close ] # J
|
33
|
+
h_p2_traffic = conf[ :h_p2_traffic ] # K
|
34
|
+
h_p1_overflow = conf[ :h_p1_overflow ] # L
|
35
|
+
h_p1_underhalf = conf[ :h_p1_underhalf ] # M
|
36
|
+
h_p2_overflow = conf[ :h_p2_overflow ] # N
|
37
|
+
h_p2_underhalf = conf[ :h_p2_underhalf ] # O
|
38
|
+
h_query = conf[ :h_query ] # Q
|
39
|
+
h_response = conf[ :h_response ] # R
|
40
|
+
h_src_close = conf[ :h_src_close ] # S
|
41
|
+
h_traffic = conf[ :h_traffic ] # T
|
42
|
+
h_src_overflow = conf[ :h_src_overflow ] # U
|
43
|
+
h_src_underhalf = conf[ :h_src_underhalf ] # V
|
44
|
+
h_dst_overflow = conf[ :h_dst_overflow ] # W
|
45
|
+
h_dst_underhalf = conf[ :h_dst_underhalf ] # X
|
46
|
+
expire_connecting = conf[ :expire_connecting ] # 连接多久没有建成关闭(秒)
|
47
|
+
expire_long_after = conf[ :expire_long_after ] # 长连接多久没有新流量关闭(秒)
|
48
|
+
expire_proxy_after = conf[ :expire_proxy_after ] # proxy多久没有收到流量重建(秒)
|
49
|
+
expire_resolv_cache = conf[ :expire_resolv_cache ] # dns查询结果缓存多久(秒)
|
50
|
+
expire_short_after = conf[ :expire_short_after ] # 短连接创建多久后关闭(秒)
|
51
|
+
is_debug = conf[ :is_debug ]
|
52
|
+
end
|
53
|
+
|
54
|
+
proxyd_port = proxyd_port ? proxyd_port.to_i : 6060
|
55
|
+
memd_port = memd_port ? memd_port.to_i : proxyd_port + 1
|
56
|
+
|
57
|
+
if nameserver then
|
58
|
+
nameservers = nameserver.split( ' ' )
|
59
|
+
else
|
60
|
+
nameservers = []
|
61
|
+
resolv_path = '/etc/resolv.conf'
|
62
|
+
|
63
|
+
if File.exist?( resolv_path ) then
|
64
|
+
text = IO.read( '/etc/resolv.conf' )
|
65
|
+
|
66
|
+
text.split( "\n" ).each do | line |
|
67
|
+
match_data = /^nameserver \d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}/.match( line )
|
68
|
+
|
69
|
+
if match_data then
|
70
|
+
nameservers << match_data[ 0 ].split(' ')[ 1 ].strip
|
71
|
+
break if nameservers.size >= 3
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
nameservers << '8.8.8.8' if nameservers.empty?
|
78
|
+
reset_traff_day = reset_traff_day ? reset_traff_day.to_i : 1
|
79
|
+
ims = [] unless ims
|
80
|
+
p2d_host = p2d_host ? p2d_host.to_s : '127.0.0.1'
|
81
|
+
p2d_port = p2d_port ? p2d_port.to_i : proxyd_port + 2
|
82
|
+
head_len = head_len ? head_len.to_i : 59
|
83
|
+
h_a_new_source = h_a_new_source ? h_a_new_source.to_s : 'A'
|
84
|
+
h_a_new_p2 = h_a_new_p2 ? h_a_new_p2.to_s : 'B'
|
85
|
+
h_dst_close = h_dst_close ? h_dst_close.to_s : 'D'
|
86
|
+
h_heartbeat = h_heartbeat ? h_heartbeat.to_s : 'H'
|
87
|
+
h_p1_close = h_p1_close ? h_p1_close.to_s : 'I'
|
88
|
+
h_p2_close = h_p2_close ? h_p2_close.to_s : 'J'
|
89
|
+
h_p2_traffic = h_p2_traffic ? h_p2_traffic.to_s : 'K'
|
90
|
+
h_p1_overflow = h_p1_overflow ? h_p1_overflow.to_s : 'L'
|
91
|
+
h_p1_underhalf = h_p1_underhalf ? h_p1_underhalf.to_s : 'M'
|
92
|
+
h_p2_overflow = h_p2_overflow ? h_p2_overflow.to_s : 'N'
|
93
|
+
h_p2_underhalf = h_p2_underhalf ? h_p2_underhalf.to_s : 'O'
|
94
|
+
h_query = h_query ? h_query.to_s : 'Q'
|
95
|
+
h_response = h_response ? h_response.to_s : 'R'
|
96
|
+
h_src_close = h_src_close ? h_src_close.to_s : 'S'
|
97
|
+
h_traffic = h_traffic ? h_traffic.to_s : 'T'
|
98
|
+
h_src_overflow = h_src_overflow ? h_src_overflow.to_s : 'U'
|
99
|
+
h_src_underhalf = h_src_underhalf ? h_src_underhalf.to_s : 'V'
|
100
|
+
h_dst_overflow = h_dst_overflow ? h_dst_overflow.to_s : 'W'
|
101
|
+
h_dst_underhalf = h_dst_underhalf ? h_dst_underhalf.to_s : 'X'
|
102
|
+
expire_connecting = expire_connecting ? expire_connecting.to_i : 5
|
103
|
+
expire_long_after = expire_long_after ? expire_long_after.to_i : 3600
|
104
|
+
expire_proxy_after = expire_proxy_after ? expire_proxy_after.to_i : 60
|
105
|
+
expire_resolv_cache = expire_resolv_cache ? expire_resolv_cache.to_i : 600
|
106
|
+
expire_short_after = expire_short_after ? expire_short_after.to_i : 5
|
107
|
+
is_server_fastopen = false
|
108
|
+
|
109
|
+
if RUBY_PLATFORM.include?( 'linux' ) then
|
110
|
+
IO.popen( 'sysctl -n net.ipv4.tcp_fastopen' ) do | io |
|
111
|
+
output = io.read
|
112
|
+
val = output.to_i % 4
|
113
|
+
|
114
|
+
if [ 2, 3 ].include?( val ) then
|
115
|
+
is_server_fastopen = true
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
puts "girl proxyd #{ Girl::VERSION }"
|
121
|
+
puts "#{ proxyd_port } #{ memd_port } #{ p2d_host } #{ p2d_port } #{ nameservers.inspect } #{ reset_traff_day } #{ is_server_fastopen }"
|
122
|
+
|
123
|
+
if %w[ darwin linux ].any?{ | plat | RUBY_PLATFORM.include?( plat ) } then
|
124
|
+
Process.setrlimit( :NOFILE, RLIMIT )
|
125
|
+
puts "NOFILE #{ Process.getrlimit( :NOFILE ).inspect }"
|
126
|
+
end
|
127
|
+
|
128
|
+
worker = Girl::ProxydWorker.new(
|
129
|
+
proxyd_port,
|
130
|
+
memd_port,
|
131
|
+
nameservers,
|
132
|
+
reset_traff_day,
|
133
|
+
ims,
|
134
|
+
p2d_host,
|
135
|
+
p2d_port,
|
136
|
+
head_len,
|
137
|
+
h_a_new_source,
|
138
|
+
h_a_new_p2,
|
139
|
+
h_dst_close,
|
140
|
+
h_heartbeat,
|
141
|
+
h_p1_close,
|
142
|
+
h_p2_close,
|
143
|
+
h_p2_traffic,
|
144
|
+
h_p1_overflow,
|
145
|
+
h_p1_underhalf,
|
146
|
+
h_p2_overflow,
|
147
|
+
h_p2_underhalf,
|
148
|
+
h_query,
|
149
|
+
h_response,
|
150
|
+
h_src_close,
|
151
|
+
h_traffic,
|
152
|
+
h_src_overflow,
|
153
|
+
h_src_underhalf,
|
154
|
+
h_dst_overflow,
|
155
|
+
h_dst_underhalf,
|
156
|
+
expire_connecting,
|
157
|
+
expire_long_after,
|
158
|
+
expire_proxy_after,
|
159
|
+
expire_resolv_cache,
|
160
|
+
expire_short_after,
|
161
|
+
is_debug,
|
162
|
+
is_server_fastopen )
|
163
|
+
|
164
|
+
Signal.trap( :TERM ) do
|
165
|
+
puts 'exit'
|
166
|
+
worker.quit!
|
167
|
+
end
|
168
|
+
|
169
|
+
worker.looping
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
end
|