-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
101 lines (79 loc) · 3 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# encoding: UTF-8
# Wayback WiFi
# A XOlator R&D Creative Project
# http://www.xolator.com/
#
# (C) 2013-2014 XOlator. All Rights Reserved
# License information: LICENSE.md
APP_ROOT = File.dirname(__FILE__)
Encoding.default_external = "UTF-8"
Encoding.default_internal = "UTF-8"
require "rubygems"
require "bundler"
Bundler.setup
requires = ['openssl', 'optparse', 'socket', 'net/http', 'uri', 'wayback', 'redis', 'addressable/uri', File.join(APP_ROOT, 'wayback_proxy_server.rb'), File.join(APP_ROOT, 'version.rb')]
requires.each{|r| require r}
WAYBACK_PROXY_USER_AGENT = "Wayback/#{WaybackProxyServerVersion.to_s} <http://www.xolator.com/>"
WAYBACK_PROXY_MAX_REDIRECTS = 5
WAYBACK_PROXY_MAX_RETRIES = 5
# Ensure the proper options are set on start
options = {:port => 8888, :cache_db => 1}
OptionParser.new do |opts|
opts.banner = "Usage: app.rb [options]"
opts.on("-h H", "--host H", String, "Host IP/domain") {|v| options[:host] = v}
opts.on("-p P", "--port P", Integer, "Port") {|v| options[:port] = v}
opts.on("-c DB", "--cache-db DB", Integer, "Cache Database") {|v| options[:cache_db] = v}
opts.on("--debug", "Debug Mode") {|v| DEBUG = true}
opts.on("-d", "--daemon", "Daemon Mode") {|v| options[:daemon] = true}
opts.on("-s", "--ssl", "Allow SSL") {|v| options[:ssl] = {}}
opts.on("-l L", "--logger L", "Log requests?") {|v| options[:logger] = Logger.new(v)}
end.parse!
DEBUG ||= false
raise "Host IP/domain required" if options[:host].nil? || options[:host] == ''
# SSL Certificate
if options[:ssl]
options[:ssl][:key] = File.join(APP_ROOT, '.ssl', 'server.key')# if File.exists?(File.expand_path(APP_ROOT, '.ssl/wayback.key'))
options[:ssl][:cert] = File.join(APP_ROOT, '.ssl', 'server.pem')# if File.exists?(File.expand_path(APP_ROOT, '.ssl/wayback.crt'))
end
# Hack for array extract_options! stype
class Array
def extract_options!; last.is_a?(::Hash) ? pop : {}; end unless defined? Array.new.extract_options!
end
# Cache setup
begin
# $wayback_cache = Diskcached.new(File.join(APP_ROOT, 'cache'))
# $wayback_cache.flush if DEBUG # ensure caches are empty on startup
$wayback_cache = Redis.new(:db => options[:cache_db])
$wayback_cache.flushdb if DEBUG # flush db on start
rescue
nil
end
# Use our User-agent (Wayback config)
Wayback.configure do |c|
c.connection_options[:headers][:user_agent] = WAYBACK_PROXY_USER_AGENT
end
# --- BEGIN ---
result = Proc.new{|opts, cache|
server = WaybackProxyServer.new(:host => opts[:host], :port => opts[:port], :ssl => opts[:ssl], :allow_ssl => true, :logger => opts[:logger], :cache => $wayback_cache)
server.run
}
begin
if options[:daemon]
puts "Forking process..."
p = fork { result.call(options) }
sleep 2
s = Process.getpgid(p) rescue nil
if s
Process.detach(p)
File.open('./proxy.pid', "w") {|f| f.write p}
puts " running as #{p}."
else
puts " did not start"
end
else
result.call(options)
end
rescue => err
puts "ERROR: #{err}"
err.backtrace.map{|l| puts " #{l}"} if DEBUG
end