proxy_pac_rb 0.0.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +60 -16
- data/bin/parsepac +107 -7
- data/lib/proxy_pac_rb/environment.rb +5 -5
- data/lib/proxy_pac_rb/exceptions.rb +3 -0
- data/lib/proxy_pac_rb/file.rb +1 -1
- data/lib/proxy_pac_rb/version.rb +1 -1
- data/spec/environment_spec.rb +5 -5
- data/spec/parser_spec.rb +2 -2
- data/tmp/script.rb +9 -7
- metadata +2 -2
data/README.md
CHANGED
@@ -44,35 +44,78 @@ parsepac https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample.pac
|
|
44
44
|
|
45
45
|
### Ruby
|
46
46
|
|
47
|
-
*
|
47
|
+
*Load from website*
|
48
48
|
|
49
49
|
```ruby
|
50
|
-
require
|
51
|
-
require "pac"
|
50
|
+
require 'proxy_pac_rb'
|
52
51
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
file = ProxyPacRb::Parser.new.load('https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample.pac')
|
53
|
+
file.find('https://github.com') # => "DIRECT"
|
54
|
+
```
|
55
|
+
|
56
|
+
*Load from filesystem*
|
57
|
+
|
58
|
+
```bash
|
59
|
+
curl -L -o sample.pac https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample.pac
|
60
|
+
```
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'proxy_pac_rb'
|
57
64
|
|
58
|
-
|
65
|
+
file = ProxyPacRb::Parser.new.read("sample.pac")
|
66
|
+
file.find('https://github.com') # => "DIRECT"
|
67
|
+
```
|
68
|
+
|
69
|
+
*Use string*
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
require 'proxy_pac_rb'
|
59
73
|
|
60
|
-
|
74
|
+
file = ProxyPacRb::Parser.new.source <<-JS
|
61
75
|
function FindProxyForURL(url, host) {
|
62
76
|
return "DIRECT";
|
63
77
|
}
|
64
78
|
JS
|
65
|
-
|
79
|
+
|
80
|
+
file.find('http://localhost') # => "DIRECT"
|
81
|
+
```
|
66
82
|
|
67
83
|
*Use Client IP*
|
68
|
-
|
69
|
-
|
70
|
-
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
require 'proxy_pac_rb'
|
87
|
+
|
88
|
+
environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.1')
|
89
|
+
file = ProxyPacRb::Parser.new(environment).load('https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample2.pac')
|
90
|
+
file.find('https://github.com') # => "PROXY localhost:8080"
|
91
|
+
|
92
|
+
environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.2')
|
93
|
+
file = ProxyPacRb::Parser.new(environment).load('https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample2.pac')
|
94
|
+
file.find('https://github.com') # => "DIRECT"
|
95
|
+
```
|
71
96
|
|
72
97
|
*Use Date Time*
|
73
|
-
|
74
|
-
|
75
|
-
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
require 'proxy_pac_rb'
|
101
|
+
|
102
|
+
string = <<-EOS
|
103
|
+
function FindProxyForURL(url, host) {
|
104
|
+
if (dateRange("JUL", "SEP")) {
|
105
|
+
return "PROXY localhost:8080";
|
106
|
+
} else {
|
107
|
+
return "DIRECT";
|
108
|
+
}
|
109
|
+
}
|
110
|
+
EOS
|
111
|
+
|
112
|
+
environment = ProxyPacRb::Environment.new(time: Time.parse('2014-07-06 12:00'))
|
113
|
+
file = ProxyPacRb::Parser.new(environment).source(string)
|
114
|
+
file.find('http://localhost') # => 'PROXY localhost:8080'
|
115
|
+
|
116
|
+
environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00'))
|
117
|
+
file = ProxyPacRb::Parser.new(environment).source(string)
|
118
|
+
file.find('http://localhost') # => 'DIRECT'
|
76
119
|
```
|
77
120
|
|
78
121
|
## Available JavaScript Functions
|
@@ -89,6 +132,7 @@ pac.find('https://github.com', time: '2014-01-02 19:00:00') # => "DIRECT"
|
|
89
132
|
* weekdayRange(wd1, wd2, gmt)
|
90
133
|
* dateRange(*args)
|
91
134
|
* timeRange(*args)
|
135
|
+
* alert(msg) (output on stderr by default)
|
92
136
|
|
93
137
|
## Developers
|
94
138
|
|
data/bin/parsepac
CHANGED
@@ -1,10 +1,110 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
4
|
+
|
5
|
+
require 'proxy_pac_rb'
|
6
|
+
require 'ostruct'
|
7
|
+
require 'ipaddr'
|
8
|
+
require 'optparse'
|
9
|
+
|
10
|
+
module ProxyPacRb
|
11
|
+
class Cli
|
12
|
+
|
13
|
+
class Validator
|
14
|
+
private
|
15
|
+
|
16
|
+
attr_reader :options
|
17
|
+
|
18
|
+
public
|
19
|
+
|
20
|
+
def initialize(options)
|
21
|
+
@options = options
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate
|
25
|
+
exit_with_message 'You need to provide at least one url. Multiple urls need to be separated by a space.' if empty_url?
|
26
|
+
exit_with_message 'You need to provide a proxy pac file.' if empty_pac_file?
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def empty_url?
|
32
|
+
options.urls.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
def empty_pac_file?
|
36
|
+
options.proxy_pac_file.nil?
|
37
|
+
end
|
38
|
+
|
39
|
+
def exit_with_message(msg)
|
40
|
+
$stderr.puts msg
|
41
|
+
exit 1
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
attr_accessor :options
|
47
|
+
|
48
|
+
def self.start(argv = $*)
|
49
|
+
cli = new
|
50
|
+
cli.parse(argv)
|
51
|
+
|
52
|
+
Validator.new(cli.options).validate
|
53
|
+
|
54
|
+
environment = ProxyPacRb::Environment.new(client_ip: cli.options.client_ip, time: cli.options.time)
|
55
|
+
file = ProxyPacRb::Parser.new(environment).read(cli.options.proxy_pac_file)
|
56
|
+
|
57
|
+
$stderr.printf("%30s: %-s\n", 'url', 'result')
|
58
|
+
cli.options.urls.each do |u|
|
59
|
+
begin
|
60
|
+
$stderr.printf("%30s: %-s\n", u, file.find(u))
|
61
|
+
rescue Exceptions::UrlInvalid
|
62
|
+
$stderr.puts "You provide an invalid url \"#{u}\". Please use a correct one."
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize
|
68
|
+
@options = OpenStruct.new
|
69
|
+
|
70
|
+
@options.client_ip = IPAddr.new('127.0.0.1')
|
71
|
+
@options.time = Time.now
|
72
|
+
end
|
73
|
+
|
74
|
+
def parse(argv)
|
75
|
+
parser = OptionParser.new do |opts|
|
76
|
+
opts.banner = "Usage: parsepac [options] url1 [url2..urln]"
|
77
|
+
|
78
|
+
opts.on('-c', '--client-ip IP', 'Client ip address') do |i|
|
79
|
+
options.client_ip = IPAddr.new(i)
|
80
|
+
end
|
81
|
+
|
82
|
+
opts.on('-t', '--time YYYY-MM-DD HH:MM:SS', 'Time to use during lookup url') do |t|
|
83
|
+
options.time = Time.parse(t)
|
84
|
+
end
|
85
|
+
|
86
|
+
opts.on('-p', '--proxy-pac FILE', 'Proxy.pac-file') do |f|
|
87
|
+
options.proxy_pac_file = ::File.expand_path(::File.join(Dir.getwd, f))
|
88
|
+
end
|
89
|
+
|
90
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
91
|
+
$stderr.puts opts
|
92
|
+
exit
|
93
|
+
end
|
94
|
+
|
95
|
+
opts.on_tail("--version", "Show version") do
|
96
|
+
$stderr.puts ProxyPacRb::VERSION
|
97
|
+
exit
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
$stderr.puts parser.help if argv.empty?
|
102
|
+
|
103
|
+
parser.parse!(argv)
|
104
|
+
options.urls = argv
|
105
|
+
end
|
106
|
+
end
|
10
107
|
end
|
108
|
+
|
109
|
+
ProxyPacRb::Cli.start
|
110
|
+
exit 0
|
@@ -4,7 +4,7 @@ module ProxyPacRb
|
|
4
4
|
|
5
5
|
private
|
6
6
|
|
7
|
-
attr_reader :days, :months, :
|
7
|
+
attr_reader :days, :months, :client_ip, :time, :io, :javascript_function_templates
|
8
8
|
|
9
9
|
public
|
10
10
|
|
@@ -14,7 +14,7 @@ module ProxyPacRb
|
|
14
14
|
@days = { "MON" => 1, "TUE" => 2, "WED" => 3, "THU" => 4, "FRI" => 5, "SAT" => 6, "SUN" => 0 }
|
15
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)
|
19
19
|
@io = options.fetch(:io, $stderr)
|
20
20
|
|
@@ -66,7 +66,7 @@ module ProxyPacRb
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def MyIpAddress
|
69
|
-
IPAddr.new(
|
69
|
+
IPAddr.new(client_ip).to_s
|
70
70
|
end
|
71
71
|
|
72
72
|
def dnsDomainLevels(host)
|
@@ -180,9 +180,9 @@ module ProxyPacRb
|
|
180
180
|
public
|
181
181
|
|
182
182
|
def prepare(string)
|
183
|
-
if
|
183
|
+
if client_ip
|
184
184
|
string << "\n\n"
|
185
|
-
string << javascript_function_templates.my_ip_address_template(
|
185
|
+
string << javascript_function_templates.my_ip_address_template(client_ip)
|
186
186
|
end
|
187
187
|
|
188
188
|
if time
|
data/lib/proxy_pac_rb/file.rb
CHANGED
@@ -13,7 +13,7 @@ module ProxyPacRb
|
|
13
13
|
|
14
14
|
def find(url)
|
15
15
|
uri = Addressable::URI.heuristic_parse(url)
|
16
|
-
fail
|
16
|
+
fail Exceptions::UrlInvalid, "url is missing host" unless uri.host
|
17
17
|
|
18
18
|
javascript.call("FindProxyForURL", url, uri.host)
|
19
19
|
end
|
data/lib/proxy_pac_rb/version.rb
CHANGED
data/spec/environment_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe ProxyPacRb::Environment do
|
5
5
|
let(:environment) do
|
6
|
-
Environment.new(
|
6
|
+
Environment.new(client_ip: '127.0.0.1', time: Time.now)
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "#isResolvable()" do
|
@@ -80,7 +80,7 @@ describe ProxyPacRb::Environment do
|
|
80
80
|
describe '#MyIpAddress ' do
|
81
81
|
it 'returns the given ip address' do
|
82
82
|
ip = '127.0.0.1'
|
83
|
-
environment = Environment.new(
|
83
|
+
environment = Environment.new(client_ip: ip, time: Time.now)
|
84
84
|
|
85
85
|
expect(environment.MyIpAddress).to eq(ip)
|
86
86
|
end
|
@@ -106,7 +106,7 @@ describe ProxyPacRb::Environment do
|
|
106
106
|
end
|
107
107
|
|
108
108
|
describe '#weekdayRange' do
|
109
|
-
let(:environment) { Environment.new(
|
109
|
+
let(:environment) { Environment.new(client_ip: '127.0.0.1', time: Time.parse('1991-08-25 12:00')) }
|
110
110
|
|
111
111
|
it 'returns true for SUN - MON' do
|
112
112
|
result = environment.weekdayRange("SUN", "MON")
|
@@ -127,7 +127,7 @@ describe ProxyPacRb::Environment do
|
|
127
127
|
end
|
128
128
|
|
129
129
|
describe '#dateRange' do
|
130
|
-
let(:environment) { Environment.new(
|
130
|
+
let(:environment) { Environment.new(client_ip: '127.0.0.1', time: Time.parse('1991-08-25 12:00')) }
|
131
131
|
|
132
132
|
it 'returns true for JUL - SEP' do
|
133
133
|
result = environment.dateRange("JUL", "SEP")
|
@@ -147,7 +147,7 @@ describe ProxyPacRb::Environment do
|
|
147
147
|
end
|
148
148
|
|
149
149
|
describe '#timeRange' do
|
150
|
-
let(:environment) { Environment.new(
|
150
|
+
let(:environment) { Environment.new(client_ip: '127.0.0.1', time: Time.parse('1991-08-25 12:00')) }
|
151
151
|
|
152
152
|
it 'returns true for 8 - 18h' do
|
153
153
|
result = environment.timeRange(8, 18)
|
data/spec/parser_spec.rb
CHANGED
@@ -43,11 +43,11 @@ describe ProxyPacRb::Parser do
|
|
43
43
|
}
|
44
44
|
EOS
|
45
45
|
|
46
|
-
environment = ProxyPacRb::Environment.new(
|
46
|
+
environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.1')
|
47
47
|
file = ProxyPacRb::Parser.new(environment).source(string)
|
48
48
|
expect(file.find('http://localhost')).to eq('PROXY localhost:8080')
|
49
49
|
|
50
|
-
environment = ProxyPacRb::Environment.new(
|
50
|
+
environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.2')
|
51
51
|
file = ProxyPacRb::Parser.new(environment).source(string)
|
52
52
|
expect(file.find('http://localhost')).to eq('DIRECT')
|
53
53
|
end
|
data/tmp/script.rb
CHANGED
@@ -5,18 +5,20 @@ require 'proxy_pac_rb'
|
|
5
5
|
|
6
6
|
string = <<-EOS
|
7
7
|
function FindProxyForURL(url, host) {
|
8
|
-
if (
|
9
|
-
return "PROXY localhost:8080";
|
10
|
-
} else {
|
8
|
+
if ( MyIpAddress() == '127.0.0.2' ) {
|
11
9
|
return "DIRECT";
|
10
|
+
} else {
|
11
|
+
return "PROXY localhost:8080";
|
12
12
|
}
|
13
13
|
}
|
14
14
|
EOS
|
15
15
|
|
16
|
-
environment = ProxyPacRb::Environment.new(
|
16
|
+
environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.1')
|
17
17
|
file = ProxyPacRb::Parser.new(environment).source(string)
|
18
|
-
puts
|
18
|
+
puts(file.find('http://localhost'))
|
19
19
|
|
20
|
-
environment = ProxyPacRb::Environment.new(
|
20
|
+
environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.2')
|
21
21
|
file = ProxyPacRb::Parser.new(environment).source(string)
|
22
|
-
puts
|
22
|
+
puts(file.find('http://localhost'))
|
23
|
+
|
24
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxy_pac_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: addressable
|