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 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
- *Simple*
47
+ *Load from website*
48
48
 
49
49
  ```ruby
50
- require "rubygems"
51
- require "pac"
50
+ require 'proxy_pac_rb'
52
51
 
53
- pac = PAC.load('https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample.pac')
54
- pac.find('https://github.com') # => "PROXY proxy:8080"
55
- pac.find('http://ruby-lang.com') # => "PROXY proxy:8080; DIRECT"
56
- pac.find('http://samuel.kadolph.com') # => "DIRECT"
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
- pac = PAC.read("sample.pac")
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
- pac = PAC.source <<-JS
74
+ file = ProxyPacRb::Parser.new.source <<-JS
61
75
  function FindProxyForURL(url, host) {
62
76
  return "DIRECT";
63
77
  }
64
78
  JS
65
- pac.find('http://localhost') # => "DIRECT"
79
+
80
+ file.find('http://localhost') # => "DIRECT"
81
+ ```
66
82
 
67
83
  *Use Client IP*
68
- pac = PAC.load('https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample2.pac')
69
- pac.find('https://github.com', client_ip: '127.0.0.1') # => "PROXY localhost:8080"
70
- pac.find('https://github.com', client_ip: '127.0.0.2') # => "DIRECT"
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
- pac = PAC.load('https://github.com/dg-vrnetze/proxy_pac_rb/raw/master/files/sample2.pac')
74
- pac.find('https://github.com', time: '2014-01-02 08:00:00') # => "PROXY localhost:8080"
75
- pac.find('https://github.com', time: '2014-01-02 19:00:00') # => "DIRECT"
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
- if ARGV.size < 2
4
- puts "usage: parsepac pac-url-or-file url-to-test"
5
- exit 1
6
- else
7
- require "pac"
8
- puts PAC.load(ARGV.shift).find(ARGV.shift)
9
- exit 0
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, :my_ip_address, :time, :io, :javascript_function_templates
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
- @my_ip_address = options.fetch(:my_ip_address, '127.0.0.1')
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(my_ip_address).to_s
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 my_ip_address
183
+ if client_ip
184
184
  string << "\n\n"
185
- string << javascript_function_templates.my_ip_address_template(my_ip_address)
185
+ string << javascript_function_templates.my_ip_address_template(client_ip)
186
186
  end
187
187
 
188
188
  if time
@@ -9,5 +9,8 @@ module ProxyPacRb
9
9
 
10
10
  #raise on invalid argument
11
11
  class InvalidArgument < StandardError; end
12
+
13
+ #raise on invalid argument
14
+ class UrlInvalid < StandardError; end
12
15
  end
13
16
  end
@@ -13,7 +13,7 @@ module ProxyPacRb
13
13
 
14
14
  def find(url)
15
15
  uri = Addressable::URI.heuristic_parse(url)
16
- fail ArgumentError, "url is missing host" unless uri.host
16
+ fail Exceptions::UrlInvalid, "url is missing host" unless uri.host
17
17
 
18
18
  javascript.call("FindProxyForURL", url, uri.host)
19
19
  end
@@ -1,4 +1,4 @@
1
1
  #main ProxyPacRb
2
2
  module ProxyPacRb
3
- VERSION = '0.0.3'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe ProxyPacRb::Environment do
5
5
  let(:environment) do
6
- Environment.new(my_ip_address: '127.0.0.1', time: Time.now)
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(my_ip_address: ip, time: Time.now)
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(my_ip_address: '127.0.0.1', time: Time.parse('1991-08-25 12:00')) }
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(my_ip_address: '127.0.0.1', time: Time.parse('1991-08-25 12:00')) }
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(my_ip_address: '127.0.0.1', time: Time.parse('1991-08-25 12:00')) }
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(my_ip_address: '127.0.0.1')
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(my_ip_address: '127.0.0.2')
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 (weekdayRange("FRI", "SUN")) {
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(time: Time.parse('2014-03-06 12:00'))
16
+ environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.1')
17
17
  file = ProxyPacRb::Parser.new(environment).source(string)
18
- puts file.find('http://localhost')
18
+ puts(file.find('http://localhost'))
19
19
 
20
- environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00'))
20
+ environment = ProxyPacRb::Environment.new(client_ip: '127.0.0.2')
21
21
  file = ProxyPacRb::Parser.new(environment).source(string)
22
- puts file.find('http://localhost')
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.3
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-08 00:00:00.000000000 Z
12
+ date: 2014-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable