girl 9.1.2 → 9.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/girl/proxyd.rb CHANGED
@@ -11,63 +11,56 @@ require 'socket'
11
11
  module Girl
12
12
  class Proxyd
13
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 ]
14
+ def initialize(config_path = nil)
15
+ if config_path
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
+ bigd_port = conf[:bigd_port]
21
+ memd_port = conf[:memd_port]
22
+ nameserver = conf[:nameserver]
23
+ reset_traff_day = conf[:reset_traff_day]
24
+ ims = conf[:ims]
25
+ p2d_host = conf[:p2d_host]
26
+ p2d_port = conf[:p2d_port]
27
+ head_len = conf[:head_len] # 头长度
28
+ h_a_new_source = conf[:h_a_new_source] # A
29
+ h_a_new_p2 = conf[:h_a_new_p2] # B
30
+ h_dst_close = conf[:h_dst_close] # D
31
+ h_heartbeat = conf[:h_heartbeat] # H
32
+ h_p1_close = conf[:h_p1_close] # I
33
+ h_p2_close = conf[:h_p2_close] # J
34
+ h_p2_traffic = conf[:h_p2_traffic] # K
35
+ h_query = conf[:h_query] # Q
36
+ h_response = conf[:h_response] # R
37
+ h_src_close = conf[:h_src_close] # S
38
+ h_traffic = conf[:h_traffic] # T
39
+ expire_connecting = conf[:expire_connecting] # 连接多久没有建成关闭(秒)
40
+ expire_long_after = conf[:expire_long_after] # 长连接多久没有新流量关闭(秒)
41
+ expire_resolv_cache = conf[:expire_resolv_cache] # dns查询结果缓存多久(秒)
42
+ expire_short_after = conf[:expire_short_after] # 短连接创建多久后关闭(秒)
43
+ is_debug = conf[:is_debug]
52
44
  end
53
45
 
54
46
  proxyd_port = proxyd_port ? proxyd_port.to_i : 6060
55
- memd_port = memd_port ? memd_port.to_i : proxyd_port + 1
47
+ bigd_port = bigd_port ? bigd_port.to_i : proxyd_port + 1
48
+ memd_port = memd_port ? memd_port.to_i : proxyd_port + 2
56
49
 
57
- if nameserver then
58
- nameservers = nameserver.split( ' ' )
50
+ if nameserver
51
+ nameservers = nameserver.split(' ')
59
52
  else
60
53
  nameservers = []
61
54
  resolv_path = '/etc/resolv.conf'
62
55
 
63
- if File.exist?( resolv_path ) then
64
- text = IO.read( '/etc/resolv.conf' )
56
+ if File.exist?(resolv_path)
57
+ text = IO.read('/etc/resolv.conf')
65
58
 
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 )
59
+ text.split("\n").each do |line|
60
+ match_data = /^nameserver \d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}/.match(line)
68
61
 
69
- if match_data then
70
- nameservers << match_data[ 0 ].split(' ')[ 1 ].strip
62
+ if match_data
63
+ nameservers << match_data[0].split(' ')[1].strip
71
64
  break if nameservers.size >= 3
72
65
  end
73
66
  end
@@ -78,7 +71,7 @@ module Girl
78
71
  reset_traff_day = reset_traff_day ? reset_traff_day.to_i : 1
79
72
  ims = [] unless ims
80
73
  p2d_host = p2d_host ? p2d_host.to_s : '127.0.0.1'
81
- p2d_port = p2d_port ? p2d_port.to_i : proxyd_port + 2
74
+ p2d_port = p2d_port ? p2d_port.to_i : proxyd_port + 3
82
75
  head_len = head_len ? head_len.to_i : 59
83
76
  h_a_new_source = h_a_new_source ? h_a_new_source.to_s : 'A'
84
77
  h_a_new_p2 = h_a_new_p2 ? h_a_new_p2.to_s : 'B'
@@ -87,46 +80,35 @@ module Girl
87
80
  h_p1_close = h_p1_close ? h_p1_close.to_s : 'I'
88
81
  h_p2_close = h_p2_close ? h_p2_close.to_s : 'J'
89
82
  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
83
  h_query = h_query ? h_query.to_s : 'Q'
95
84
  h_response = h_response ? h_response.to_s : 'R'
96
85
  h_src_close = h_src_close ? h_src_close.to_s : 'S'
97
86
  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
87
  expire_connecting = expire_connecting ? expire_connecting.to_i : 5
103
88
  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
89
  expire_resolv_cache = expire_resolv_cache ? expire_resolv_cache.to_i : 600
106
90
  expire_short_after = expire_short_after ? expire_short_after.to_i : 5
107
91
  is_server_fastopen = false
108
92
 
109
- if RUBY_PLATFORM.include?( 'linux' ) then
110
- IO.popen( 'sysctl -n net.ipv4.tcp_fastopen' ) do | io |
93
+ if RUBY_PLATFORM.include?('linux')
94
+ IO.popen('sysctl -n net.ipv4.tcp_fastopen') do |io|
111
95
  output = io.read
112
96
  val = output.to_i % 4
113
-
114
- if [ 2, 3 ].include?( val ) then
115
- is_server_fastopen = true
116
- end
97
+ is_server_fastopen = true if [2, 3].include?(val)
117
98
  end
118
99
  end
119
100
 
120
- puts "girl proxyd #{ Girl::VERSION }"
121
- puts "#{ proxyd_port } #{ memd_port } #{ p2d_host } #{ p2d_port } #{ nameservers.inspect } #{ reset_traff_day } #{ is_server_fastopen }"
101
+ puts "girl proxyd #{Girl::VERSION}"
102
+ puts "#{proxyd_port} #{bigd_port} #{memd_port} #{p2d_host} #{p2d_port} #{nameservers.inspect} #{reset_traff_day} #{is_server_fastopen}"
122
103
 
123
- if %w[ darwin linux ].any?{ | plat | RUBY_PLATFORM.include?( plat ) } then
124
- Process.setrlimit( :NOFILE, RLIMIT )
125
- puts "NOFILE #{ Process.getrlimit( :NOFILE ).inspect }"
104
+ if %w[darwin linux].any?{|plat| RUBY_PLATFORM.include?(plat)}
105
+ Process.setrlimit(:NOFILE, RLIMIT)
106
+ puts "NOFILE #{Process.getrlimit(:NOFILE).inspect}"
126
107
  end
127
108
 
128
109
  worker = Girl::ProxydWorker.new(
129
110
  proxyd_port,
111
+ bigd_port,
130
112
  memd_port,
131
113
  nameservers,
132
114
  reset_traff_day,
@@ -141,27 +123,18 @@ module Girl
141
123
  h_p1_close,
142
124
  h_p2_close,
143
125
  h_p2_traffic,
144
- h_p1_overflow,
145
- h_p1_underhalf,
146
- h_p2_overflow,
147
- h_p2_underhalf,
148
126
  h_query,
149
127
  h_response,
150
128
  h_src_close,
151
129
  h_traffic,
152
- h_src_overflow,
153
- h_src_underhalf,
154
- h_dst_overflow,
155
- h_dst_underhalf,
156
130
  expire_connecting,
157
131
  expire_long_after,
158
- expire_proxy_after,
159
132
  expire_resolv_cache,
160
133
  expire_short_after,
161
134
  is_debug,
162
- is_server_fastopen )
135
+ is_server_fastopen)
163
136
 
164
- Signal.trap( :TERM ) do
137
+ Signal.trap(:TERM) do
165
138
  puts 'exit'
166
139
  worker.quit!
167
140
  end