forward-proxy 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/exe/forward-proxy +2 -1
- data/lib/forward_proxy/server.rb +20 -17
- data/lib/forward_proxy/thread_pool.rb +7 -18
- data/lib/forward_proxy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccf19df114eeec55eaef8765999561904d8f5b6cd91b7ed2016053ec9e717408
|
4
|
+
data.tar.gz: ce712f520c0ed32b93ea211ff658d330bef7eeb26c2ddfcea1246d955ce78175
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b71608455b079161950b09521d8fd99496767463f3e2aa0f0d293f21e3955b03d7b0ba12f06f3b3df1ff6674d598ed2e7eec7492ffb8fd9047ebb19771625251
|
7
|
+
data.tar.gz: '092035474f162e29d2c7d0735b43cd38e41580aadca7ae50e97e02dee8e6f3825f50393497c0a8db8a5e0b3622d3e44e25e69e03ae02b56eba1cad55abdc1388'
|
data/README.md
CHANGED
@@ -6,8 +6,8 @@ Minimal forward proxy using 150LOC and only standard libraries. Useful for devel
|
|
6
6
|
|
7
7
|
```
|
8
8
|
$ forward-proxy --binding 0.0.0.0 --port 3182 --threads 2
|
9
|
-
[2021-
|
10
|
-
[2021-
|
9
|
+
I, [2021-07-04T10:33:32.947653 #1790] INFO -- : Listening 0.0.0.0:3182
|
10
|
+
I, [2021-07-04T10:33:32.998298 #1790] INFO -- : CONNECT raw.githubusercontent.com:443 HTTP/1.1
|
11
11
|
```
|
12
12
|
|
13
13
|
## Installation
|
data/exe/forward-proxy
CHANGED
@@ -15,7 +15,8 @@ OptionParser.new do |parser|
|
|
15
15
|
options[:bind_address] = bind_address
|
16
16
|
end
|
17
17
|
|
18
|
-
parser.on("-tTHREADS", "--threads=THREADS", Integer,
|
18
|
+
parser.on("-tTHREADS", "--threads=THREADS", Integer,
|
19
|
+
"Specify the number of client threads. Default: 128") do |threads|
|
19
20
|
options[:threads] = threads
|
20
21
|
end
|
21
22
|
|
data/lib/forward_proxy/server.rb
CHANGED
@@ -10,11 +10,11 @@ module ForwardProxy
|
|
10
10
|
class Server
|
11
11
|
attr_reader :bind_address, :bind_port, :logger
|
12
12
|
|
13
|
-
def initialize(bind_address: "127.0.0.1", bind_port: 9292, threads:
|
14
|
-
@logger = logger
|
15
|
-
@thread_pool = ThreadPool.new(threads)
|
13
|
+
def initialize(bind_address: "127.0.0.1", bind_port: 9292, threads: 128, logger: default_logger)
|
16
14
|
@bind_address = bind_address
|
17
15
|
@bind_port = bind_port
|
16
|
+
@logger = logger
|
17
|
+
@thread_pool = ThreadPool.new(threads)
|
18
18
|
end
|
19
19
|
|
20
20
|
def start
|
@@ -29,7 +29,7 @@ module ForwardProxy
|
|
29
29
|
begin
|
30
30
|
req = parse_req(client_conn)
|
31
31
|
|
32
|
-
logger.info(req.request_line)
|
32
|
+
logger.info(req.request_line.strip)
|
33
33
|
|
34
34
|
case req.request_method
|
35
35
|
when METHOD_CONNECT then handle_tunnel(client_conn, req)
|
@@ -37,8 +37,8 @@ module ForwardProxy
|
|
37
37
|
else
|
38
38
|
raise Errors::HTTPMethodNotImplemented
|
39
39
|
end
|
40
|
-
rescue =>
|
41
|
-
handle_error(
|
40
|
+
rescue => e
|
41
|
+
handle_error(e, client_conn)
|
42
42
|
ensure
|
43
43
|
client_conn.close
|
44
44
|
end
|
@@ -130,20 +130,20 @@ module ForwardProxy
|
|
130
130
|
# "Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content"
|
131
131
|
# https://tools.ietf.org/html/rfc7231#section-4.3.6
|
132
132
|
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
133
|
+
# An intermediary MAY combine an ordered subsequence of Via header
|
134
|
+
# field entries into a single such entry if the entries have identical
|
135
|
+
# received-protocol values. For example,
|
136
136
|
#
|
137
|
-
#
|
137
|
+
# Via: 1.0 ricky, 1.1 ethel, 1.1 fred, 1.0 lucy
|
138
138
|
#
|
139
|
-
#
|
139
|
+
# could be collapsed to
|
140
140
|
#
|
141
|
-
#
|
141
|
+
# Via: 1.0 ricky, 1.1 mertz, 1.0 lucy
|
142
142
|
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
143
|
+
# A sender SHOULD NOT combine multiple entries unless they are all
|
144
|
+
# under the same organizational control and the hosts have already been
|
145
|
+
# replaced by pseudonyms. A sender MUST NOT combine entries that have
|
146
|
+
# different received-protocol values.
|
147
147
|
headers = resp.to_hash.merge(Via: [HEADER_VIA, resp['Via']].compact.join(', '))
|
148
148
|
|
149
149
|
client_conn.puts <<~eos.chomp
|
@@ -173,10 +173,13 @@ module ForwardProxy
|
|
173
173
|
eos
|
174
174
|
|
175
175
|
logger.error(err.message)
|
176
|
-
|
177
176
|
logger.debug(err.backtrace.join("\n"))
|
178
177
|
end
|
179
178
|
|
179
|
+
def default_logger
|
180
|
+
Logger.new(STDOUT, level: :info)
|
181
|
+
end
|
182
|
+
|
180
183
|
def map_webrick_to_net_http_req(req)
|
181
184
|
req_headers = Hash[req.header.map { |k, v| [k, v.first] }]
|
182
185
|
|
@@ -1,21 +1,18 @@
|
|
1
1
|
module ForwardProxy
|
2
2
|
class ThreadPool
|
3
|
-
attr_reader :queue, :
|
3
|
+
attr_reader :queue, :size
|
4
4
|
|
5
5
|
def initialize(size)
|
6
|
-
@size
|
7
|
-
@queue
|
8
|
-
@threads = []
|
6
|
+
@size = size
|
7
|
+
@queue = Queue.new
|
9
8
|
end
|
10
9
|
|
11
10
|
def start
|
12
11
|
size.times do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
job.call(*args)
|
18
|
-
end
|
12
|
+
Thread.new do
|
13
|
+
loop do
|
14
|
+
job, args = queue.pop
|
15
|
+
job.call(*args)
|
19
16
|
end
|
20
17
|
end
|
21
18
|
end
|
@@ -24,13 +21,5 @@ module ForwardProxy
|
|
24
21
|
def schedule(*args, &block)
|
25
22
|
queue.push([block, args])
|
26
23
|
end
|
27
|
-
|
28
|
-
def shutdown
|
29
|
-
threads.each do
|
30
|
-
schedule { throw :exit }
|
31
|
-
end
|
32
|
-
|
33
|
-
threads.each(&:join)
|
34
|
-
end
|
35
24
|
end
|
36
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: forward-proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Moriarty
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Forward proxy using just Ruby standard libraries.
|
14
14
|
email:
|