girl 9.1.2 → 9.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 673184bfb0785b65e56e4715c2b007481ae91d16620fbc11e3f6e12c50f511b5
4
- data.tar.gz: 9a37c381bbf266cac12239b479214979202a8b715a1db0c5f443f8ecd3ca046a
3
+ metadata.gz: 336d5a273c06df15d5c88139e32066b3c9a574bcc97fcf9c1690cb7aeb8034e7
4
+ data.tar.gz: 339782aa3b87b31535897ccf5c4201cbfa2b53487625de16bccef9123ae71fad
5
5
  SHA512:
6
- metadata.gz: d291af3cfc1ff8813e187062a55b93ad9f0c6a52f7bd14e19c4638becd98c96451da21146a167111c5da01d2f20b6fc31711143e71d82e82bf5cf68d8e420efb
7
- data.tar.gz: 9a6e13e0f756afd3a024f7c7c386fd18e5978488bd1909fd69dbabffacd25e71d8d0c286920ec86665ed69766cee1464ba1d4a8fcab94055fdf82d405392d96b
6
+ metadata.gz: 8c478838350d1862edcfd99d629f4c88d09d892816f73f6505ec567ba481d2774ee05dfbd018055629b4b709c55e2a0cf2b1f7ee8025d09c1d05ac7d0be621f3
7
+ data.tar.gz: 95202954f953df2d1de2d53d9c4cfb8d4ed73d393e7f9d5b2418c9391bdbefea4e0294dc3c54d277519db789be211528232496a47899fc5a208e0e72f1a23883
data/lib/girl/dns.rb CHANGED
@@ -1,46 +1,46 @@
1
1
  module Girl
2
2
  module Dns
3
3
 
4
- def pack_a_query( domain, type = 1 )
4
+ def pack_a_query(domain, type = 1)
5
5
  # https://www.ietf.org/rfc/rfc1035.txt
6
6
  # https://www.ietf.org/rfc/rfc3596.txt
7
7
  raise "domain may not exceed 255 chars" if domain.bytesize > 255
8
8
  raise "invalid domain" if domain =~ /[^\w\.\-]/
9
- data = [ rand( 65_535 ), 1, 0, 1, 0, 0, 0 ].pack( 'nCCnnnn' )
10
- data << [ pack_domain( domain ), type, 1 ].pack( 'a*nn' )
9
+ data = [rand(65_535), 1, 0, 1, 0, 0, 0].pack('nCCnnnn')
10
+ data << [pack_domain(domain), type, 1].pack('a*nn')
11
11
  data
12
12
  end
13
13
 
14
- def pack_domain( domain )
14
+ def pack_domain(domain)
15
15
  data = ''
16
16
 
17
- domain.split( '.' ).each do | label |
17
+ domain.split('.').each do |label|
18
18
  raise "label may not exceed 63 chars" if label.bytesize > 63
19
- data << [ label.bytesize, label ].pack( 'Ca*' )
19
+ data << [label.bytesize, label].pack('Ca*')
20
20
  end
21
21
 
22
- data << [ 0 ].pack( 'C' )
22
+ data << [0].pack('C')
23
23
  data
24
24
  end
25
25
 
26
- def seek_dn( data, offset )
26
+ def seek_dn(data, offset)
27
27
  name = ""
28
28
  datalen = data.bytesize
29
29
 
30
30
  loop do
31
- raise "offset is greater than datalen" if datalen < ( offset + 1 )
31
+ raise "offset is greater than datalen" if datalen < (offset + 1)
32
32
 
33
- len = data.unpack( "@#{ offset } C" ).first
33
+ len = data.unpack("@#{offset} C").first
34
34
 
35
35
  if len == 0
36
36
  offset += 1
37
37
  break
38
- elsif ( len & 0xC0 ) == 0xC0
39
- raise "data ended before offset expand" if datalen < ( offset + 2 )
38
+ elsif (len & 0xC0) == 0xC0
39
+ raise "data ended before offset expand" if datalen < (offset + 2)
40
40
 
41
- ptr = data.unpack( "@#{ offset } n" ).first
41
+ ptr = data.unpack("@#{offset} n").first
42
42
  ptr &= 0x3FFF
43
- name2 = seek_dn( data, ptr ).first
43
+ name2 = seek_dn(data, ptr).first
44
44
  raise "data is malformed" if name2.nil?
45
45
 
46
46
  name += name2
@@ -48,89 +48,86 @@ module Girl
48
48
  break
49
49
  else
50
50
  offset += 1
51
- raise "no expansion found" if datalen < ( offset + len )
51
+ raise "no expansion found" if datalen < (offset + len)
52
52
 
53
- elem = data[ offset..offset + len - 1 ]
54
- name += "#{ elem }."
53
+ elem = data[offset..offset + len - 1]
54
+ name += "#{elem}."
55
55
  offset += len
56
56
  end
57
57
  end
58
58
 
59
- [ name, offset ]
59
+ [name, offset]
60
60
  end
61
61
 
62
- def seek_ip( data )
62
+ def seek_ip(data)
63
63
  ip = nil
64
- answer_count = data.unpack( "@6 n" ).first
65
- # puts "debug answer count #{ answer_count }"
66
- offset = seek_question( data )
67
- # puts "debug offset #{ offset }"
64
+ answer_count = data.unpack("@6 n").first
65
+ # puts "debug answer count #{answer_count}"
66
+ offset = seek_question(data)
67
+ # puts "debug offset #{offset}"
68
68
 
69
69
  answer_count.times do
70
- ip, offset = seek_rr_ip( data, offset )
71
-
72
- if ip then
73
- break
74
- end
70
+ ip, offset = seek_rr_ip(data, offset)
71
+ break if ip
75
72
  end
76
73
 
77
74
  ip
78
75
  end
79
76
 
80
- def seek_question( data )
77
+ def seek_question(data)
81
78
  offset = 12
82
79
 
83
80
  loop do
84
- len = data.unpack( "@#{ offset } C" ).first
85
- # puts "debug len #{ len } #{ data[ offset + 1, len ] }"
81
+ len = data.unpack("@#{offset} C").first
82
+ # puts "debug len #{len} #{data[offset + 1, len]}"
86
83
  break if len == 0
87
- offset += ( 1 + len )
84
+ offset += (1 + len)
88
85
  end
89
86
 
90
87
  offset += 5
91
88
  offset
92
89
  end
93
90
 
94
- def seek_question_dn( data )
95
- id = data[ 0, 2 ]
91
+ def seek_question_dn(data)
92
+ id = data[0, 2]
96
93
  parts = []
97
94
  offset = 12
98
95
 
99
96
  loop do
100
- len = data.unpack( "@#{ offset } C" ).first
101
- # puts "debug len #{ len } #{ data[ offset + 1, len ] }"
97
+ len = data.unpack("@#{offset} C").first
98
+ # puts "debug len #{len} #{data[offset + 1, len]}"
102
99
  break if len == 0
103
- parts << data[ offset + 1, len ]
104
- offset += ( 1 + len )
100
+ parts << data[offset + 1, len]
101
+ offset += (1 + len)
105
102
  end
106
103
 
107
- type = data.unpack( "@#{ offset + 1 } n" ).first
108
- # puts "debug id #{ id.inspect } dn #{ parts.join( '.' ).inspect } type #{ type }"
109
- [ id, parts.join( '.' ), type ]
104
+ type = data.unpack("@#{offset + 1} n").first
105
+ # puts "debug id #{id.inspect} dn #{parts.join('.').inspect} type #{type}"
106
+ [id, parts.join('.'), type]
110
107
  end
111
108
 
112
- def seek_rr_ip( data, offset )
109
+ def seek_rr_ip(data, offset)
113
110
  ip = nil
114
- name, offset = seek_dn( data, offset )
115
- # puts "debug seek_dn #{ name }, #{ offset }"
116
- type = data.unpack( "@#{ offset } n" ).first
117
- # puts "debug type #{ type }"
111
+ name, offset = seek_dn(data, offset)
112
+ # puts "debug seek_dn #{name}, #{offset}"
113
+ type = data.unpack("@#{offset} n").first
114
+ # puts "debug type #{type}"
118
115
  offset += 8
119
- rdlen = data.unpack( "@#{ offset } n" ).first
120
- # puts "debug rdlen #{ rdlen }"
116
+ rdlen = data.unpack("@#{offset} n").first
117
+ # puts "debug rdlen #{rdlen}"
121
118
  offset += 2
122
119
 
123
- if type == 1 then
120
+ if type == 1
124
121
  raise "rdlen not 4?" if rdlen != 4
125
- a, b, c, d = data.unpack( "@#{ offset } CCCC" )
126
- ip = "#{ a }.#{ b }.#{ c }.#{ d }"
127
- elsif type == 28 then
128
- tokens = data.unpack("@#{ offset } n8")
129
- ip = format( "%x:%x:%x:%x:%x:%x:%x:%x", *tokens )
122
+ a, b, c, d = data.unpack("@#{offset} CCCC")
123
+ ip = "#{a}.#{b}.#{c}.#{d}"
124
+ elsif type == 28
125
+ tokens = data.unpack("@#{offset} n8")
126
+ ip = format("%x:%x:%x:%x:%x:%x:%x:%x", *tokens)
130
127
  end
131
128
 
132
129
  offset += rdlen
133
- [ ip, offset ]
130
+ [ip, offset]
134
131
  end
135
132
 
136
133
  end
data/lib/girl/head.rb CHANGED
@@ -2,11 +2,10 @@ module Girl
2
2
  BACKLOG = 512 # 听队列大小,满后掉SYN包
3
3
  RLIMIT = 1024 # sock数上限
4
4
  READ_SIZE = 4 * 1024 * 1024 # 一次读多少
5
- WBUFF_LIMIT = 30 * 1024 * 1024 # 写前上限,超过上限暂停读
6
- RESUME_BELOW = WBUFF_LIMIT / 2 # 降到多少以下恢复读
7
- CLOSE_ABOVE = WBUFF_LIMIT * 3 # 超过多少强制关闭
5
+ WBUFF_LIMIT = 10 * 1024 * 1024 # 写前上限,超过上限暂停读另一头
6
+ CLOSE_ABOVE = 30 * 1024 * 1024 # 超过多少强制关闭
8
7
  HEARTBEAT_INTERVAL = 10 # 心跳间隔
9
- CHECK_TRAFF_INTERVAL = 3600 # 检查今天是否是流量计数重置日间隔
8
+ CHECK_TRAFF_INTERVAL = 3600 # 多久检查一次,今天是不是流量计数重置日
10
9
  HTTP_OK = "HTTP/1.1 200 OK\r\n\r\n"
11
10
  RESERVED_ROUTE = <<EOF
12
11
  0.0.0.0/8
data/lib/girl/proxy.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'fileutils'
2
1
  require 'girl/dns'
3
2
  require 'girl/head'
4
3
  require 'girl/proxy_worker'
@@ -14,81 +13,68 @@ require 'socket'
14
13
  module Girl
15
14
  class Proxy
16
15
 
17
- def initialize( config_path = nil )
18
- unless config_path then
19
- config_path = File.expand_path( '../girl.conf.json', __FILE__ )
20
- end
21
-
22
- raise "missing config file #{ config_path }" unless File.exist?( config_path )
23
-
24
- conf = JSON.parse( IO.binread( config_path ), symbolize_names: true )
25
- puts "load #{ config_path } #{ conf.inspect }"
26
- redir_host = conf[ :redir_host ]
27
- redir_port = conf[ :redir_port ]
28
- memd_port = conf[ :memd_port ]
29
- relayd_host = conf[ :relayd_host ]
30
- relayd_port = conf[ :relayd_port ]
31
- tspd_host = conf[ :tspd_host ]
32
- tspd_port = conf[ :tspd_port ]
33
- proxyd_host = conf[ :proxyd_host ]
34
- proxyd_port = conf[ :proxyd_port ]
35
- nameserver = conf[ :nameserver ]
36
- im = conf[ :im ]
37
- direct_path = conf[ :direct_path ]
38
- remote_path = conf[ :remote_path ]
39
- appd_host = conf[ :appd_host ]
40
- appd_port = conf[ :appd_port ]
41
- head_len = conf[ :head_len ] # 头长度
42
- h_a_new_source = conf[ :h_a_new_source ] # A
43
- h_a_new_p2 = conf[ :h_a_new_p2 ] # B
44
- h_dst_close = conf[ :h_dst_close ] # D
45
- h_heartbeat = conf[ :h_heartbeat ] # H
46
- h_p1_close = conf[ :h_p1_close ] # I
47
- h_p2_close = conf[ :h_p2_close ] # J
48
- h_p2_traffic = conf[ :h_p2_traffic ] # K
49
- h_p1_overflow = conf[ :h_p1_overflow ] # L
50
- h_p1_underhalf = conf[ :h_p1_underhalf ] # M
51
- h_p2_overflow = conf[ :h_p2_overflow ] # N
52
- h_p2_underhalf = conf[ :h_p2_underhalf ] # O
53
- h_query = conf[ :h_query ] # Q
54
- h_response = conf[ :h_response ] # R
55
- h_src_close = conf[ :h_src_close ] # S
56
- h_traffic = conf[ :h_traffic ] # T
57
- h_src_overflow = conf[ :h_src_overflow ] # U
58
- h_src_underhalf = conf[ :h_src_underhalf ] # V
59
- h_dst_overflow = conf[ :h_dst_overflow ] # W
60
- h_dst_underhalf = conf[ :h_dst_underhalf ] # X
61
- expire_connecting = conf[ :expire_connecting ] # 连接多久没有建成关闭(秒)
62
- expire_long_after = conf[ :expire_long_after ] # 长连接多久没有新流量关闭(秒)
63
- expire_proxy_after = conf[ :expire_proxy_after ] # proxy多久没有收到流量重建(秒)
64
- expire_resolv_cache = conf[ :expire_resolv_cache ] # dns查询结果缓存多久(秒)
65
- expire_short_after = conf[ :expire_short_after ] # 短连接创建多久后关闭(秒)
66
- is_debug = conf[ :is_debug ]
16
+ def initialize(config_path = nil)
17
+ config_path = File.expand_path('../girl.conf.json', __FILE__) unless config_path
18
+ raise "missing config file #{config_path}" unless File.exist?(config_path)
19
+
20
+ conf = JSON.parse(IO.binread(config_path), symbolize_names: true)
21
+ puts "load #{config_path} #{conf.inspect}"
22
+ redir_host = conf[:redir_host]
23
+ redir_port = conf[:redir_port]
24
+ memd_port = conf[:memd_port]
25
+ tspd_host = conf[:tspd_host]
26
+ tspd_port = conf[:tspd_port]
27
+ proxyd_host = conf[:proxyd_host]
28
+ proxyd_port = conf[:proxyd_port]
29
+ bigd_port = conf[:bigd_port]
30
+ nameserver = conf[:nameserver]
31
+ im = conf[:im]
32
+ direct_path = conf[:direct_path]
33
+ remote_path = conf[:remote_path]
34
+ appd_host = conf[:appd_host]
35
+ appd_port = conf[:appd_port]
36
+ head_len = conf[:head_len] # 头长度
37
+ h_a_new_source = conf[:h_a_new_source] # A
38
+ h_a_new_p2 = conf[:h_a_new_p2] # B
39
+ h_dst_close = conf[:h_dst_close] # D
40
+ h_heartbeat = conf[:h_heartbeat] # H
41
+ h_p1_close = conf[:h_p1_close] # I
42
+ h_p2_close = conf[:h_p2_close] # J
43
+ h_p2_traffic = conf[:h_p2_traffic] # K
44
+ h_query = conf[:h_query] # Q
45
+ h_response = conf[:h_response] # R
46
+ h_src_close = conf[:h_src_close] # S
47
+ h_traffic = conf[:h_traffic] # T
48
+ expire_connecting = conf[:expire_connecting] # 连接多久没有建成关闭(秒)
49
+ expire_long_after = conf[:expire_long_after] # 长连接多久没有新流量关闭(秒)
50
+ expire_proxy_after = conf[:expire_proxy_after] # proxy多久没有收到流量重建(秒)
51
+ expire_resolv_cache = conf[:expire_resolv_cache] # dns查询结果缓存多久(秒)
52
+ expire_short_after = conf[:expire_short_after] # 短连接创建多久后关闭(秒)
53
+ is_debug = conf[:is_debug]
67
54
 
68
55
  redir_host = redir_host ? redir_host.to_s : '0.0.0.0'
69
56
  redir_port = redir_port ? redir_port.to_i : 6666
70
- memd_port = memd_port ? memd_port.to_i : redir_port + 1
71
- relayd_host = relayd_host ? relayd_host.to_s : '0.0.0.0'
72
- relayd_port = relayd_port ? relayd_port.to_i : redir_port + 2
57
+ memd_port = memd_port ? memd_port.to_i : redir_port + 2
73
58
  tspd_host = tspd_host ? tspd_host.to_s : '0.0.0.0'
74
59
  tspd_port = tspd_port ? tspd_port.to_i : 7777
75
60
  raise "missing proxyd host" unless proxyd_host
76
61
  proxyd_port = proxyd_port ? proxyd_port.to_i : 6060
62
+ bigd_port = bigd_port ? bigd_port.to_i : proxyd_port + 1
77
63
  nameserver = '114.114.114.114' unless nameserver
78
- nameservers = nameserver.split( ' ' )
64
+ nameservers = nameserver.split(' ')
79
65
  im = 'office-pc' unless im
80
66
  directs = []
81
67
 
82
- if direct_path then
83
- raise "not found direct file #{ direct_path }" unless File.exist?( direct_path )
84
- directs = ( RESERVED_ROUTE.split( "\n" ) + IO.binread( direct_path ).split( "\n" ) ).map{ | line | IPAddr.new( line.strip ) }
68
+ if direct_path
69
+ raise "not found direct file #{direct_path}" unless File.exist?(direct_path)
70
+ directs = (RESERVED_ROUTE.split("\n") + IO.binread(direct_path).split("\n")).map{|line| IPAddr.new(line.strip)}
85
71
  end
86
72
 
87
73
  remotes = []
88
74
 
89
- if remote_path then
90
- raise "not found remote file #{ remote_path }" unless File.exist?( remote_path )
91
- remotes = IO.binread( remote_path ).split( "\n" ).map{ | line | line.strip }
75
+ if remote_path
76
+ raise "not found remote file #{remote_path}" unless File.exist?(remote_path)
77
+ remotes = IO.binread(remote_path).split("\n").map{|line| line.strip}
92
78
  end
93
79
 
94
80
  appd_host = appd_host ? appd_host.to_s : '127.0.0.1'
@@ -101,18 +87,10 @@ module Girl
101
87
  h_p1_close = h_p1_close ? h_p1_close.to_s : 'I'
102
88
  h_p2_close = h_p2_close ? h_p2_close.to_s : 'J'
103
89
  h_p2_traffic = h_p2_traffic ? h_p2_traffic.to_s : 'K'
104
- h_p1_overflow = h_p1_overflow ? h_p1_overflow.to_s : 'L'
105
- h_p1_underhalf = h_p1_underhalf ? h_p1_underhalf.to_s : 'M'
106
- h_p2_overflow = h_p2_overflow ? h_p2_overflow.to_s : 'N'
107
- h_p2_underhalf = h_p2_underhalf ? h_p2_underhalf.to_s : 'O'
108
90
  h_query = h_query ? h_query.to_s : 'Q'
109
91
  h_response = h_response ? h_response.to_s : 'R'
110
92
  h_src_close = h_src_close ? h_src_close.to_s : 'S'
111
93
  h_traffic = h_traffic ? h_traffic.to_s : 'T'
112
- h_src_overflow = h_src_overflow ? h_src_overflow.to_s : 'U'
113
- h_src_underhalf = h_src_underhalf ? h_src_underhalf.to_s : 'V'
114
- h_dst_overflow = h_dst_overflow ? h_dst_overflow.to_s : 'W'
115
- h_dst_underhalf = h_dst_underhalf ? h_dst_underhalf.to_s : 'X'
116
94
  expire_connecting = expire_connecting ? expire_connecting.to_i : 5
117
95
  expire_long_after = expire_long_after ? expire_long_after.to_i : 3600
118
96
  expire_proxy_after = expire_proxy_after ? expire_proxy_after.to_i : 60
@@ -120,41 +98,34 @@ module Girl
120
98
  expire_short_after = expire_short_after ? expire_short_after.to_i : 5
121
99
  is_client_fastopen = is_server_fastopen = false
122
100
 
123
- if RUBY_PLATFORM.include?( 'linux' ) then
124
- IO.popen( 'sysctl -n net.ipv4.tcp_fastopen' ) do | io |
101
+ if RUBY_PLATFORM.include?('linux')
102
+ IO.popen('sysctl -n net.ipv4.tcp_fastopen') do |io|
125
103
  output = io.read
126
104
  val = output.to_i % 4
127
-
128
- if [ 1, 3 ].include?( val ) then
129
- is_client_fastopen = true
130
- end
131
-
132
- if [ 2, 3 ].include?( val ) then
133
- is_server_fastopen = true
134
- end
105
+ is_client_fastopen = true if [1, 3].include?(val)
106
+ is_server_fastopen = true if [2, 3].include?(val)
135
107
  end
136
108
  end
137
109
 
138
- puts "girl proxy #{ Girl::VERSION } #{ im } #{ redir_port } #{ relayd_port } #{ tspd_port }"
139
- puts "#{ proxyd_host } #{ proxyd_port } #{ appd_host } #{ appd_port } #{ nameservers.inspect } #{ is_client_fastopen } #{ is_server_fastopen }"
140
- puts "#{ direct_path } #{ directs.size } directs"
141
- puts "#{ remote_path } #{ remotes.size } remotes"
110
+ puts "girl proxy #{Girl::VERSION} #{im} #{redir_port} #{tspd_port}"
111
+ puts "#{proxyd_host} #{proxyd_port} #{appd_host} #{appd_port} #{nameservers.inspect} #{is_client_fastopen} #{is_server_fastopen}"
112
+ puts "#{direct_path} #{directs.size} directs"
113
+ puts "#{remote_path} #{remotes.size} remotes"
142
114
 
143
- if %w[ darwin linux ].any?{ | plat | RUBY_PLATFORM.include?( plat ) } then
144
- Process.setrlimit( :NOFILE, RLIMIT )
145
- puts "NOFILE #{ Process.getrlimit( :NOFILE ).inspect }"
115
+ if %w[darwin linux].any?{|plat| RUBY_PLATFORM.include?(plat)}
116
+ Process.setrlimit(:NOFILE, RLIMIT)
117
+ puts "NOFILE #{Process.getrlimit(:NOFILE).inspect}"
146
118
  end
147
119
 
148
120
  worker = Girl::ProxyWorker.new(
149
121
  redir_host,
150
122
  redir_port,
151
123
  memd_port,
152
- relayd_host,
153
- relayd_port,
154
124
  tspd_host,
155
125
  tspd_port,
156
126
  proxyd_host,
157
127
  proxyd_port,
128
+ bigd_port,
158
129
  nameservers,
159
130
  im,
160
131
  directs,
@@ -169,18 +140,10 @@ module Girl
169
140
  h_p1_close,
170
141
  h_p2_close,
171
142
  h_p2_traffic,
172
- h_p1_overflow,
173
- h_p1_underhalf,
174
- h_p2_overflow,
175
- h_p2_underhalf,
176
143
  h_query,
177
144
  h_response,
178
145
  h_src_close,
179
146
  h_traffic,
180
- h_src_overflow,
181
- h_src_underhalf,
182
- h_dst_overflow,
183
- h_dst_underhalf,
184
147
  expire_connecting,
185
148
  expire_long_after,
186
149
  expire_proxy_after,
@@ -188,9 +151,9 @@ module Girl
188
151
  expire_short_after,
189
152
  is_debug,
190
153
  is_client_fastopen,
191
- is_server_fastopen )
154
+ is_server_fastopen)
192
155
 
193
- Signal.trap( :TERM ) do
156
+ Signal.trap(:TERM) do
194
157
  puts 'exit'
195
158
  worker.quit!
196
159
  end