proxy_pac_rb 0.3.4 → 0.3.6
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 +5 -13
- data/.gitignore +0 -1
- data/.rubocop.yml +4 -0
- data/Gemfile +30 -19
- data/Gemfile.lock +220 -0
- data/README.md +3 -0
- data/Rakefile +49 -13
- data/bin/pprb +1 -116
- data/config/license_finder.yml +12 -0
- data/config/rubocop/exclude.yml +8 -0
- data/config/rubocop/include.yml +27 -0
- data/cucumber.yml +2 -0
- data/doc/dependencies/dependencies.db +0 -0
- data/features/resolve_proxy.feature +17 -0
- data/features/support/aruba.rb +13 -0
- data/features/support/env.rb +10 -1
- data/features/support/fixtures.rb +15 -0
- data/features/support/reporting.rb +2 -0
- data/lib/proxy_pac_rb/cli.rb +75 -0
- data/lib/proxy_pac_rb/cli_validator.rb +34 -0
- data/lib/proxy_pac_rb/encoding.rb +1 -1
- data/lib/proxy_pac_rb/environment.rb +10 -8
- data/lib/proxy_pac_rb/exceptions.rb +4 -4
- data/lib/proxy_pac_rb/file.rb +3 -3
- data/lib/proxy_pac_rb/parser.rb +11 -2
- data/lib/proxy_pac_rb/proxy_pac_js.rb +5 -5
- data/lib/proxy_pac_rb/runtime.rb +6 -5
- data/lib/proxy_pac_rb/runtimes/rubyracer.rb +42 -35
- data/lib/proxy_pac_rb/runtimes/rubyrhino.rb +27 -22
- data/lib/proxy_pac_rb/runtimes.rb +13 -15
- data/lib/proxy_pac_rb/version.rb +3 -2
- data/lib/proxy_pac_rb.rb +7 -3
- data/proxy_pac_rb.gemspec +1 -1
- data/script/ci +0 -4
- data/script/test_web +2 -1
- data/spec/environment_spec.rb +23 -23
- data/spec/file_spec.rb +5 -5
- data/spec/parser_spec.rb +16 -13
- data/spec/support/aruba.rb +50 -0
- data/spec/support/rspec.rb +18 -4
- data/spec/support/{string.rb → strip.rb} +0 -0
- data/tmp/script.rb +0 -2
- metadata +37 -29
- data/spec/support/environment.rb +0 -17
- data/spec/support/filesystem.rb +0 -20
data/bin/pprb
CHANGED
@@ -1,124 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: utf-8
|
3
|
-
|
4
3
|
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
5
4
|
|
6
5
|
require 'proxy_pac_rb'
|
7
|
-
require 'ostruct'
|
8
|
-
require 'ipaddr'
|
9
|
-
require 'optparse'
|
10
|
-
require 'addressable/uri'
|
11
|
-
require 'open-uri'
|
12
|
-
|
13
|
-
module ProxyPacRb
|
14
|
-
class Cli
|
15
|
-
|
16
|
-
class Validator
|
17
|
-
private
|
18
|
-
|
19
|
-
attr_reader :options
|
20
|
-
|
21
|
-
public
|
22
|
-
|
23
|
-
def initialize(options)
|
24
|
-
@options = options
|
25
|
-
end
|
26
|
-
|
27
|
-
def validate
|
28
|
-
exit_with_message 'You need to provide at least one url. Multiple urls need to be separated by a space.' if empty_url?
|
29
|
-
exit_with_message 'You need to provide a proxy pac file.' if empty_pac_file?
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def empty_url?
|
35
|
-
options.urls.empty?
|
36
|
-
end
|
37
|
-
|
38
|
-
def empty_pac_file?
|
39
|
-
options.proxy_pac_file.nil?
|
40
|
-
end
|
41
|
-
|
42
|
-
def exit_with_message(msg)
|
43
|
-
$stderr.puts msg
|
44
|
-
exit 1
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
attr_accessor :options
|
50
|
-
|
51
|
-
def self.start(argv = $*)
|
52
|
-
cli = new
|
53
|
-
cli.parse(argv)
|
54
|
-
|
55
|
-
Validator.new(cli.options).validate
|
56
|
-
|
57
|
-
environment = ProxyPacRb::Environment.new(client_ip: cli.options.client_ip, time: cli.options.time)
|
58
|
-
file = ProxyPacRb::Parser.new(environment).source(cli.options.proxy_pac_file)
|
59
|
-
|
60
|
-
$stderr.printf("%30s: %-s\n", 'url', 'result')
|
61
|
-
cli.options.urls.each do |u|
|
62
|
-
begin
|
63
|
-
$stderr.printf("%30s: %-s\n", u, file.find(u))
|
64
|
-
rescue Exceptions::UrlInvalid
|
65
|
-
$stderr.puts "You provide an invalid url \"#{u}\". Please use a correct one."
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def initialize
|
71
|
-
@options = OpenStruct.new
|
72
|
-
|
73
|
-
@options.client_ip = IPAddr.new('127.0.0.1')
|
74
|
-
@options.time = Time.now
|
75
|
-
end
|
76
|
-
|
77
|
-
def parse(argv)
|
78
|
-
parser = OptionParser.new do |opts|
|
79
|
-
opts.banner = "Usage: parsepac [options] url1 [url2..urln]"
|
80
|
-
|
81
|
-
opts.on('-c', '--client-ip IP', 'Client ip address') do |i|
|
82
|
-
options.client_ip = IPAddr.new(i)
|
83
|
-
end
|
84
|
-
|
85
|
-
opts.on('-t', '--time YYYY-MM-DD HH:MM:SS', 'Time to use during lookup url') do |t|
|
86
|
-
options.time = Time.parse(t)
|
87
|
-
end
|
88
|
-
|
89
|
-
opts.on('-p', '--proxy-pac FILE|URL', 'Proxy.pac-file') do |f|
|
90
|
-
uri = Addressable::URI.parse(f)
|
91
|
-
|
92
|
-
if uri.host.nil?
|
93
|
-
uri.path = ::File.expand_path(uri.path)
|
94
|
-
end
|
95
|
-
|
96
|
-
ENV.delete 'HTTP_PROXY'
|
97
|
-
ENV.delete 'HTTPS_PROXY'
|
98
|
-
ENV.delete 'http_proxy'
|
99
|
-
ENV.delete 'https_proxy'
|
100
|
-
|
101
|
-
options.proxy_pac_file = open(uri, { proxy: false }).read
|
102
|
-
end
|
103
|
-
|
104
|
-
opts.on_tail("-h", "--help", "Show this message") do
|
105
|
-
$stderr.puts opts
|
106
|
-
exit
|
107
|
-
end
|
108
|
-
|
109
|
-
opts.on_tail("--version", "Show version") do
|
110
|
-
$stderr.puts ProxyPacRb::VERSION
|
111
|
-
exit
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
$stderr.puts parser.help if argv.empty?
|
116
|
-
|
117
|
-
parser.parse!(argv)
|
118
|
-
options.urls = argv
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
6
|
|
123
7
|
ProxyPacRb::Cli.start
|
8
|
+
|
124
9
|
exit 0
|
@@ -0,0 +1,12 @@
|
|
1
|
+
---
|
2
|
+
whitelist:
|
3
|
+
#- MIT
|
4
|
+
#- Apache 2.0
|
5
|
+
ignore_groups:
|
6
|
+
#- test
|
7
|
+
#- development
|
8
|
+
ignore_dependencies:
|
9
|
+
#- bundler
|
10
|
+
dependencies_file_dir: './doc/dependencies'
|
11
|
+
project_name: # project name
|
12
|
+
gradle_command: # only meaningful if used with a Java/gradle project. Defaults to "gradle".
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Configuration parameters: AllowURI.
|
2
|
+
Metrics/LineLength:
|
3
|
+
Max: 225
|
4
|
+
|
5
|
+
# Configuration parameters: CountKeywordArgs.
|
6
|
+
Metrics/ParameterLists:
|
7
|
+
Max: 6
|
8
|
+
|
9
|
+
Metrics/BlockNesting:
|
10
|
+
Max: 4
|
11
|
+
|
12
|
+
# Configuration parameters: CountComments.
|
13
|
+
Metrics/ClassLength:
|
14
|
+
Max: 174
|
15
|
+
|
16
|
+
Metrics/CyclomaticComplexity:
|
17
|
+
Max: 7
|
18
|
+
|
19
|
+
# Configuration parameters: CountComments.
|
20
|
+
Metrics/MethodLength:
|
21
|
+
Max: 76
|
22
|
+
|
23
|
+
Metrics/PerceivedComplexity:
|
24
|
+
Max: 8
|
25
|
+
|
26
|
+
Style/RegexpLiteral:
|
27
|
+
MaxSlashes: 0
|
data/cucumber.yml
ADDED
Binary file
|
@@ -136,3 +136,20 @@ Feature: Resolve proxy
|
|
136
136
|
"""
|
137
137
|
DIRECT
|
138
138
|
"""
|
139
|
+
|
140
|
+
Scenario: dnsResolve + isInNet
|
141
|
+
Given a file named "proxy.pac" with:
|
142
|
+
"""
|
143
|
+
function FindProxyForURL(url, host) {
|
144
|
+
hostip = dnsResolve(host);
|
145
|
+
if (isInNet(hostip, "127.0.0.1", "255.255.255.255") ||
|
146
|
+
isInNet(hostip, "127.0.0.1", "255.255.255.255") ||
|
147
|
+
isInNet(hostip, "127.0.0.1", "255.255.255.255") ||
|
148
|
+
isInNet(hostip, "127.0.0.1", "255.255.255.255")) return 'proxy.example.com';
|
149
|
+
}
|
150
|
+
"""
|
151
|
+
When I successfully run `pprb -p proxy.pac http://localhost`
|
152
|
+
Then the output should contain:
|
153
|
+
"""
|
154
|
+
proxy.example.com
|
155
|
+
"""
|
data/features/support/env.rb
CHANGED
@@ -1,3 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
2
|
require 'aruba/cucumber'
|
3
|
+
|
4
|
+
require 'simplecov'
|
5
|
+
SimpleCov.command_name 'cucumber'
|
6
|
+
SimpleCov.start
|
7
|
+
|
8
|
+
# Pull in all of the gems including those in the `test` group
|
9
|
+
require 'bundler'
|
10
|
+
Bundler.require :default, :test, :development
|
11
|
+
|
12
|
+
ENV['TEST'] = 'true'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module FeatureHelper
|
3
|
+
# Helper for fixtures
|
4
|
+
module Fixtures
|
5
|
+
def fixtures_manager
|
6
|
+
@fixtures_manager ||= FeduxOrgStdlib::FixturesManagement::FixturesManager.new
|
7
|
+
|
8
|
+
@fixtures_manager.load_fixtures(File.expand_path('../../../fixtures', __FILE__))
|
9
|
+
|
10
|
+
@fixtures_manager
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
World(FeatureHelper::Fixtures)
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module ProxyPacRb
|
3
|
+
# Command line interface
|
4
|
+
class Cli
|
5
|
+
attr_accessor :options
|
6
|
+
|
7
|
+
def self.start(argv = ARGV)
|
8
|
+
cli = new
|
9
|
+
cli.parse(argv)
|
10
|
+
|
11
|
+
CliValidator.new(cli.options).validate
|
12
|
+
|
13
|
+
environment = ProxyPacRb::Environment.new(client_ip: cli.options.client_ip, time: cli.options.time)
|
14
|
+
file = ProxyPacRb::Parser.new(environment).source(cli.options.proxy_pac_file)
|
15
|
+
|
16
|
+
$stderr.printf("%30s: %-s\n", 'url', 'result')
|
17
|
+
cli.options.urls.each do |u|
|
18
|
+
begin
|
19
|
+
$stderr.printf("%30s: %-s\n", u, file.find(u))
|
20
|
+
rescue Exceptions::UrlInvalid
|
21
|
+
$stderr.puts "You provide an invalid url \"#{u}\". Please use a correct one."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@options = OpenStruct.new
|
28
|
+
|
29
|
+
@options.client_ip = IPAddr.new('127.0.0.1')
|
30
|
+
@options.time = Time.now
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse(argv)
|
34
|
+
parser = OptionParser.new do |opts|
|
35
|
+
opts.banner = 'Usage: pprb [options] url1 [url2..urln]'
|
36
|
+
|
37
|
+
opts.on('-c', '--client-ip IP', 'Client ip address') do |i|
|
38
|
+
options.client_ip = IPAddr.new(i)
|
39
|
+
end
|
40
|
+
|
41
|
+
opts.on('-t', '--time YYYY-MM-DD HH:MM:SS', 'Time to use during lookup url') do |t|
|
42
|
+
options.time = Time.parse(t)
|
43
|
+
end
|
44
|
+
|
45
|
+
opts.on('-p', '--proxy-pac FILE|URL', 'Proxy.pac-file') do |f|
|
46
|
+
uri = Addressable::URI.parse(f)
|
47
|
+
|
48
|
+
uri.path = ::File.expand_path(uri.path) if uri.host.nil?
|
49
|
+
|
50
|
+
ENV.delete 'HTTP_PROXY'
|
51
|
+
ENV.delete 'HTTPS_PROXY'
|
52
|
+
ENV.delete 'http_proxy'
|
53
|
+
ENV.delete 'https_proxy'
|
54
|
+
|
55
|
+
options.proxy_pac_file = open(uri, proxy: false).read
|
56
|
+
end
|
57
|
+
|
58
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
59
|
+
$stderr.puts opts
|
60
|
+
exit
|
61
|
+
end
|
62
|
+
|
63
|
+
opts.on_tail('--version', 'Show version') do
|
64
|
+
$stderr.puts ProxyPacRb::VERSION
|
65
|
+
exit
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
$stderr.puts parser.help if argv.blank?
|
70
|
+
|
71
|
+
parser.parse!(argv)
|
72
|
+
options.urls = argv
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ProxyPacRb
|
2
|
+
# Validator for commandline options
|
3
|
+
class CliValidator
|
4
|
+
private
|
5
|
+
|
6
|
+
attr_reader :options
|
7
|
+
|
8
|
+
public
|
9
|
+
|
10
|
+
def initialize(options)
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate
|
15
|
+
exit_with_message 'You need to provide at least one url. Multiple urls need to be separated by a space.' if empty_url?
|
16
|
+
exit_with_message 'You need to provide a proxy pac file.' if empty_pac_file?
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def empty_url?
|
22
|
+
options.urls.blank?
|
23
|
+
end
|
24
|
+
|
25
|
+
def empty_pac_file?
|
26
|
+
options.proxy_pac_file.blank?
|
27
|
+
end
|
28
|
+
|
29
|
+
def exit_with_message(msg)
|
30
|
+
$stderr.puts msg
|
31
|
+
exit 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ProxyPacRb
|
2
2
|
# Encodes strings as UTF-8
|
3
3
|
module Encoding
|
4
|
-
if
|
4
|
+
if ''.respond_to?(:encode)
|
5
5
|
if RUBY_ENGINE == 'jruby' || RUBY_ENGINE == 'rbx'
|
6
6
|
# workaround for jruby bug http://jira.codehaus.org/browse/JRUBY-6588
|
7
7
|
# workaround for rbx bug https://github.com/rubinius/rubinius/issues/1729
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module ProxyPacRb
|
3
|
+
# Environment in which a proxy.pac will be evaluated
|
3
4
|
class Environment
|
4
|
-
|
5
5
|
private
|
6
6
|
|
7
7
|
attr_reader :days, :months, :client_ip, :time, :io, :javascript_function_templates
|
@@ -11,8 +11,8 @@ module ProxyPacRb
|
|
11
11
|
attr_reader :available_methods
|
12
12
|
|
13
13
|
def initialize(options = {})
|
14
|
-
@days = {
|
15
|
-
@months = {
|
14
|
+
@days = { 'MON' => 1, 'TUE' => 2, 'WED' => 3, 'THU' => 4, 'FRI' => 5, 'SAT' => 6, 'SUN' => 0 }
|
15
|
+
@months = { 'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, 'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12 }
|
16
16
|
|
17
17
|
@client_ip = options.fetch(:client_ip, '127.0.0.1')
|
18
18
|
@time = options.fetch(:time, Time.now)
|
@@ -29,7 +29,7 @@ module ProxyPacRb
|
|
29
29
|
:isInNet,
|
30
30
|
:dnsResolve,
|
31
31
|
:dnsDomainLevels,
|
32
|
-
:shExpMatch
|
32
|
+
:shExpMatch
|
33
33
|
]
|
34
34
|
end
|
35
35
|
|
@@ -38,7 +38,7 @@ module ProxyPacRb
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def isPlainHostName(host)
|
41
|
-
|
41
|
+
!host.include? '.'
|
42
42
|
end
|
43
43
|
|
44
44
|
def dnsDomainIs(host, domain)
|
@@ -46,11 +46,11 @@ module ProxyPacRb
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def localHostOrDomainIs(host, hostdom)
|
49
|
-
host == hostdom
|
49
|
+
host == hostdom || hostdom.include?(host)
|
50
50
|
end
|
51
51
|
|
52
52
|
def isResolvable(host)
|
53
|
-
|
53
|
+
!resolve_host(host).blank?
|
54
54
|
end
|
55
55
|
|
56
56
|
def isInNet(host, pattern, mask)
|
@@ -62,7 +62,7 @@ module ProxyPacRb
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def dnsDomainLevels(host)
|
65
|
-
host.scan(
|
65
|
+
host.scan('.').size
|
66
66
|
end
|
67
67
|
|
68
68
|
def shExpMatch(str, shexp)
|
@@ -77,8 +77,10 @@ module ProxyPacRb
|
|
77
77
|
Resolv.each_address(host.force_encoding('ASCII-8BIT')) do |address|
|
78
78
|
begin
|
79
79
|
return address if IPAddr.new(address).ipv4?
|
80
|
+
# rubocop:disable Lint/HandleExceptions
|
80
81
|
rescue ArgumentError
|
81
82
|
end
|
83
|
+
# rubocop:enable Lint/HandleExceptions
|
82
84
|
end
|
83
85
|
|
84
86
|
# We couldn't find an IPv4 address for the host
|