|
@@ -0,0 +1,48 @@
|
|
1
|
+#!/usr/bin/env ruby
|
|
2
|
+# minimal http 1.0 reverse proxy
|
|
3
|
+# tested on ruby 1.9 & 2.3
|
|
4
|
+
|
|
5
|
+require 'socket'
|
|
6
|
+# host/ip & port for proxy server
|
|
7
|
+proxy_addr = 'localhost'
|
|
8
|
+proxy_port = 8080
|
|
9
|
+# list of backend servers
|
|
10
|
+@backend_host = ["97.184.69.111","86.189.8.12,"97.184.66.131" ]
|
|
11
|
+@remote_port = "80"
|
|
12
|
+
|
|
13
|
+puts "HTTP 1.0 PROXY\n\nRunning on #{proxy_addr}:#{proxy_port}\n"
|
|
14
|
+
|
|
15
|
+proxy = TCPServer.new(proxy_addr, proxy_port)
|
|
16
|
+
|
|
17
|
+def make_request(backend, ip, met,req)
|
|
18
|
+ socket = TCPSocket.new(backend, @remote_port)
|
|
19
|
+ socket << "#{met} http://#{backend}#{req} HTTP/1.0\r\n"
|
|
20
|
+ socket << "Host: #{backend}\r\n"
|
|
21
|
+ socket << "Add X-Forwarded-For: #{ip}\r\n"
|
|
22
|
+ socket << "Connection: close\r\n"
|
|
23
|
+ socket << "\r\n"
|
|
24
|
+ response = socket.read()
|
|
25
|
+ socket.close()
|
|
26
|
+ response
|
|
27
|
+end
|
|
28
|
+
|
|
29
|
+#on TCPSERVER.accept enter loop, start a new thread
|
|
30
|
+loop do
|
|
31
|
+ Thread.start(proxy.accept) do |client|
|
|
32
|
+ # randomly pick a backend server
|
|
33
|
+ backend = @backend_host[rand(@backend_host.size)]
|
|
34
|
+ # capture HTTP request
|
|
35
|
+ data = client.gets.split()
|
|
36
|
+ method = data[0]
|
|
37
|
+ uri = data[1]
|
|
38
|
+ client_ip = client.peeraddr[2]
|
|
39
|
+ puts "REQUEST: #{client_ip} -> #{backend}\n"
|
|
40
|
+ # send request to backend server using the make_request method
|
|
41
|
+ response = make_request(backend, client_ip, method, uri)
|
|
42
|
+ header= response.lines.first
|
|
43
|
+ puts "RESPONSE: #{header}\n\n"
|
|
44
|
+ # send response and close client connection
|
|
45
|
+ client.write(response)
|
|
46
|
+ client.close
|
|
47
|
+ end
|
|
48
|
+end
|