cocproxy 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/cocproxy +190 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 22de903f2dc0427e89597b9d4a1dc8dcca415320
|
4
|
+
data.tar.gz: 85707fe5dafebe581262f34a5853bc83d1756cdd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 711f1c7a1726e41d74914ba8117900e59a969d3a1a72239adba821b0389d4e3c923ae64e94533ffb4b92b8f7adf9cbbd9ab025c8f86fbdc68369ec83b0e42b03
|
7
|
+
data.tar.gz: ffaff8eb1747b445ffc726c75e7cd78cd9b8ab7cac544c035fd7f7bce2f3bba7378590cf965fbc570aa273d299951370f8c09d407f29e3f5bf631652499dc238
|
data/bin/cocproxy
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'webrick'
|
4
|
+
require 'webrick/httpproxy'
|
5
|
+
require 'uri'
|
6
|
+
require 'yaml'
|
7
|
+
require "pp"
|
8
|
+
require "pathname"
|
9
|
+
require "stringio"
|
10
|
+
require "zlib"
|
11
|
+
require "optparse"
|
12
|
+
|
13
|
+
class CocProxyCommand
|
14
|
+
VERSION = "$Revision$"
|
15
|
+
DEFAULT_CONFIG = {
|
16
|
+
:Port => 5432,
|
17
|
+
:ProxyVia => false,
|
18
|
+
:Logger => WEBrick::Log.new(nil, 0),
|
19
|
+
:AccessLog => WEBrick::Log.new(nil, 0),
|
20
|
+
:FilterDir => "files",
|
21
|
+
:Rules => [
|
22
|
+
"\#{File.basename(req.path_info)}",
|
23
|
+
"\#{req.host}\#{req.path_info}",
|
24
|
+
"\#{req.host}/\#{File.basename(req.path_info)}",
|
25
|
+
".\#{req.path_info}",
|
26
|
+
]
|
27
|
+
}
|
28
|
+
|
29
|
+
def self.run(argv)
|
30
|
+
new(argv.dup).run
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(argv)
|
34
|
+
@argv = argv
|
35
|
+
@parser = OptionParser.new do |parser|
|
36
|
+
parser.banner = <<-EOB.gsub(/^\t+/, "")
|
37
|
+
Usage: #$0 [options]
|
38
|
+
EOB
|
39
|
+
|
40
|
+
parser.separator ""
|
41
|
+
parser.separator "Options:"
|
42
|
+
|
43
|
+
parser.on("-c", "--config CONFIG.yaml") do |config|
|
44
|
+
begin
|
45
|
+
@config = YAML.load_file(config)
|
46
|
+
rescue Errno::ENOENT
|
47
|
+
puts "#{config} is not found"
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
parser.on("-p", "--port PORT", "Specify port number. This option overrides any config.") do |port|
|
53
|
+
@port = port.to_i
|
54
|
+
end
|
55
|
+
|
56
|
+
parser.on("-n", "--no-cache", "Disable cache.") do |port|
|
57
|
+
@nocache = true
|
58
|
+
end
|
59
|
+
|
60
|
+
parser.on("--disable-double-screen", "Disable loading double_screen.rb") do |c|
|
61
|
+
@disable_double_screen = c
|
62
|
+
end
|
63
|
+
|
64
|
+
parser.on("--version", "Show version string `#{VERSION}'") do
|
65
|
+
puts VERSION
|
66
|
+
exit
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def run
|
72
|
+
@parser.order!(@argv)
|
73
|
+
$stdout.sync = true
|
74
|
+
|
75
|
+
unless @config
|
76
|
+
begin
|
77
|
+
@config = YAML.load_file("proxy-config.yaml")
|
78
|
+
puts "proxy-config.yaml was found. Use it."
|
79
|
+
rescue Errno::ENOENT
|
80
|
+
@config = {
|
81
|
+
"server" => {
|
82
|
+
},
|
83
|
+
}
|
84
|
+
puts "Use default configuration."
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
server_config = DEFAULT_CONFIG.update(@config["server"])
|
89
|
+
server_config[:Port] = @port if @port
|
90
|
+
server_config[:nocache] = @nocache
|
91
|
+
server_config[:ProxyURI] = URI.parse(server_config[:ProxyURI]) if server_config[:ProxyURI]
|
92
|
+
|
93
|
+
unless @disable_double_screen
|
94
|
+
begin
|
95
|
+
require "double_screen.rb"
|
96
|
+
rescue LoadError => e
|
97
|
+
end
|
98
|
+
end
|
99
|
+
puts "Port : #{server_config[:Port]}"
|
100
|
+
puts "Dir : #{server_config[:FilterDir]}/"
|
101
|
+
puts "Cache: #{!server_config[:nocache]}"
|
102
|
+
puts "Rules:"
|
103
|
+
server_config[:Rules].each_with_index do |item, index|
|
104
|
+
puts " #{index+1}. #{item}"
|
105
|
+
end
|
106
|
+
|
107
|
+
srv = ArrogationProxyServer.new(server_config)
|
108
|
+
trap(:INT) { srv.shutdown }
|
109
|
+
srv.start
|
110
|
+
end
|
111
|
+
|
112
|
+
class ArrogationProxyServer < WEBrick::HTTPProxyServer
|
113
|
+
def proxy_service(req, res)
|
114
|
+
|
115
|
+
referer = req.header["referer"]
|
116
|
+
dir = @config[:FilterDir]
|
117
|
+
$stderr.puts req.path_info if $DEBUG
|
118
|
+
$stderr.puts req.query.inspect if $DEBUG
|
119
|
+
|
120
|
+
content = ""
|
121
|
+
local_path = ""
|
122
|
+
|
123
|
+
@config[:Rules].each do |path|
|
124
|
+
path = "#{dir}/#{eval("%Q(#{path})")}"
|
125
|
+
$stderr.puts "Checking #{path.to_s}"
|
126
|
+
if FileTest.file? path
|
127
|
+
puts "Hit Arrogation: #{req.path_info}"
|
128
|
+
local_path = path
|
129
|
+
content = File.open(path).binmode.read
|
130
|
+
break
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
req.header.delete("HTTP_IF_MODIFIED_SINCE")
|
135
|
+
|
136
|
+
case
|
137
|
+
when content =~ /proxy-replace:\s*(.+)\s*/
|
138
|
+
content.sub!(/proxy-replace:\s*(.+)\s*/, "")
|
139
|
+
regexp = Regexp.new(Regexp.last_match[1])
|
140
|
+
puts "Replace Regexp: #{regexp.source}"
|
141
|
+
puts " <= #{local_path}"
|
142
|
+
super
|
143
|
+
case (res["Content-Encoding"] || "").downcase
|
144
|
+
#when "deflate"
|
145
|
+
#when "compress"
|
146
|
+
when "gzip"
|
147
|
+
res["Content-Encoding"] = nil
|
148
|
+
res.body = Zlib::GzipReader.wrap(StringIO.new(res.body)) {|gz| gz.read }
|
149
|
+
end
|
150
|
+
|
151
|
+
p res
|
152
|
+
|
153
|
+
m = res.body.match(regexp)
|
154
|
+
if m && m[1]
|
155
|
+
res.body[m.begin(1)..(m.end(1)-1)] = content
|
156
|
+
else
|
157
|
+
puts "In-place Regexp match failed..."
|
158
|
+
end
|
159
|
+
res["Content-Length"] = res.body.length
|
160
|
+
when content !~ /\A\s*\Z/
|
161
|
+
mime_types = WEBrick::HTTPUtils::DefaultMimeTypes.update(@config[:MimeTypes])
|
162
|
+
res.header["Content-Type"] = WEBrick::HTTPUtils.mime_type(req.path_info, mime_types)
|
163
|
+
res.body = content
|
164
|
+
puts "Rewrote: <= #{local_path}"
|
165
|
+
else
|
166
|
+
@cache = {} if !@cache || req.query.key?("clearcache")
|
167
|
+
r = @cache[req.request_uri.to_s]
|
168
|
+
|
169
|
+
if r
|
170
|
+
r.instance_variables.each do |i|
|
171
|
+
res.instance_variable_set(i, r.instance_variable_get(i))
|
172
|
+
end
|
173
|
+
$stderr.puts "From Cache: #{req.request_uri}"
|
174
|
+
else
|
175
|
+
super
|
176
|
+
unless @config[:nocache]
|
177
|
+
@cache[req.request_uri.to_s] = res.dup
|
178
|
+
$stderr.puts "Cached: #{req.request_uri}"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
req.header["referer"] = ["http://#{req.header["host"][0]}"]
|
183
|
+
rescue Exception => e
|
184
|
+
puts $@
|
185
|
+
puts $!
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
CocProxyCommand.run(ARGV)
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cocproxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Public Domain
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Proxy which can stub certain URL to local file.
|
14
|
+
email: me@kaiinui.com
|
15
|
+
executables:
|
16
|
+
- cocproxy
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/cocproxy
|
21
|
+
homepage: https://github.com/kaiinui/cocproxy
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.3.0
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Proxy which can stub certain URL to local file.
|
45
|
+
test_files: []
|
46
|
+
has_rdoc:
|