proxy_pac_rb 0.0.2 → 0.0.3

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.
data/tmp/functions.js ADDED
@@ -0,0 +1,61 @@
1
+ function dnsDomainIs(host, domain) {
2
+ return (host.length >= domain.length &&
3
+ host.substring(host.length - domain.length) == domain);
4
+ }
5
+
6
+ function dnsDomainLevels(host) {
7
+ return host.split('.').length - 1;
8
+ }
9
+
10
+ function convert_addr(ipchars) {
11
+ var bytes = ipchars.split('.');
12
+ var result = ((bytes[0] & 0xff) << 24) |
13
+ ((bytes[1] & 0xff) << 16) |
14
+ ((bytes[2] & 0xff) << 8) |
15
+ (bytes[3] & 0xff);
16
+ return result;
17
+ }
18
+
19
+ function isInNet(ipaddr, pattern, maskstr) {
20
+ var test = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipaddr);
21
+ if (test == null) {
22
+ ipaddr = dnsResolve(ipaddr);
23
+ if (ipaddr == null)
24
+ return false;
25
+ } else if (test[1] > 255 || test[2] > 255 ||
26
+ test[3] > 255 || test[4] > 255) {
27
+ return false; // not an IP address
28
+ }
29
+ var host = convert_addr(ipaddr);
30
+ var pat = convert_addr(pattern);
31
+ var mask = convert_addr(maskstr);
32
+ return ((host & mask) == (pat & mask));
33
+
34
+ }
35
+
36
+ function isPlainHostName(host) {
37
+ return (host.search('\\\\.') == -1);
38
+ }
39
+
40
+ function isResolvable(host) {
41
+ var ip = dnsResolve(host);
42
+ return (ip != null);
43
+ }
44
+
45
+ function localHostOrDomainIs(host, hostdom) {
46
+ return (host == hostdom) ||
47
+ (hostdom.lastIndexOf(host + '.', 0) == 0);
48
+ }
49
+
50
+ function shExpMatch(url, pattern) {
51
+ pattern = pattern.replace(/\\./g, '\\\\.');
52
+ pattern = pattern.replace(/\\*/g, '.*');
53
+ pattern = pattern.replace(/\\?/g, '.');
54
+ var newRe = new RegExp('^'+pattern+'$');
55
+ return newRe.test(url);
56
+ }
57
+
58
+ var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};
59
+ var months = {JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11};
60
+
61
+ ;
data/tmp/functions.rb ADDED
@@ -0,0 +1,11 @@
1
+
2
+ expect(file.find('http://localhost')).to eq('PROXY localhost:8080')
3
+ expect(file.find('http://localhost')).to eq('DIRECT')
4
+ end
5
+
6
+ it 'respects time' do
7
+ javascript = double('javascript')
8
+ file = ProxyPacRb::File.new(javascript)
9
+
10
+ expect(file.find('http://localhost', time: '2014-03-07 12:00:00')).to eq('PROXY localhost:8080')
11
+ expect(file.find('http://localhost', time: '2014-03-07 19:00:00')).to eq('DIRECT')
data/tmp/script.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pry'
3
+
4
+ require 'proxy_pac_rb'
5
+
6
+ string = <<-EOS
7
+ function FindProxyForURL(url, host) {
8
+ if (weekdayRange("FRI", "SUN")) {
9
+ return "PROXY localhost:8080";
10
+ } else {
11
+ return "DIRECT";
12
+ }
13
+ }
14
+ EOS
15
+
16
+ environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-06 12:00'))
17
+ file = ProxyPacRb::Parser.new(environment).source(string)
18
+ puts file.find('http://localhost')
19
+
20
+ environment = ProxyPacRb::Environment.new(time: Time.parse('2014-03-08 6:00'))
21
+ file = ProxyPacRb::Parser.new(environment).source(string)
22
+ puts file.find('http://localhost')
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-07 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2014-03-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
14
46
  description: ! 'This gem uses a JavaScript runtime to evaulate a proxy auto-config
15
47
  file the same way a browser does to determine what proxy (if
16
48
 
@@ -28,29 +60,49 @@ extensions: []
28
60
  extra_rdoc_files: []
29
61
  files:
30
62
  - .gitignore
63
+ - .rdebugrc
64
+ - .rspec
65
+ - .simplecov
66
+ - .yardopts
31
67
  - Gemfile
32
68
  - LICENSE.md
33
69
  - README.md
34
70
  - Rakefile
35
71
  - bin/parsepac
36
72
  - files/sample.pac
73
+ - files/sample2.pac
74
+ - files/sample3.pac
37
75
  - lib/proxy_pac_rb.rb
76
+ - lib/proxy_pac_rb/encoding.rb
77
+ - lib/proxy_pac_rb/environment.rb
38
78
  - lib/proxy_pac_rb/exceptions.rb
39
79
  - lib/proxy_pac_rb/file.rb
40
- - lib/proxy_pac_rb/functions.rb
80
+ - lib/proxy_pac_rb/parser.rb
81
+ - lib/proxy_pac_rb/proxy_pac_js.rb
82
+ - lib/proxy_pac_rb/runtime.rb
41
83
  - lib/proxy_pac_rb/runtimes.rb
42
84
  - lib/proxy_pac_rb/runtimes/rubyracer.rb
43
85
  - lib/proxy_pac_rb/runtimes/rubyrhino.rb
44
86
  - lib/proxy_pac_rb/version.rb
45
87
  - proxy_pac_rb.gemspec
46
- - spec/proxy_pac_rb/functions_spec.rb
47
- - spec/proxy_pac_rb_spec.rb
88
+ - script/acceptance_test
89
+ - script/bootstrap
90
+ - script/ci
91
+ - script/console
92
+ - script/release
93
+ - script/unit_test
94
+ - spec/environment_spec.rb
95
+ - spec/file_spec.rb
96
+ - spec/parser_spec.rb
48
97
  - spec/spec_helper.rb
49
98
  - spec/support/environment.rb
50
99
  - spec/support/filesystem.rb
51
100
  - spec/support/reporting.rb
52
101
  - spec/support/rspec.rb
53
102
  - spec/support/string.rb
103
+ - tmp/functions.js
104
+ - tmp/functions.rb
105
+ - tmp/script.rb
54
106
  homepage: https://github.com/dg-vrnetze/ruby_pac_rb
55
107
  licenses:
56
108
  - MIT
@@ -64,18 +116,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
116
  - - ! '>='
65
117
  - !ruby/object:Gem::Version
66
118
  version: '0'
67
- segments:
68
- - 0
69
- hash: 3395524059172055841
70
119
  required_rubygems_version: !ruby/object:Gem::Requirement
71
120
  none: false
72
121
  requirements:
73
122
  - - ! '>='
74
123
  - !ruby/object:Gem::Version
75
124
  version: '0'
76
- segments:
77
- - 0
78
- hash: 3395524059172055841
79
125
  requirements: []
80
126
  rubyforge_project:
81
127
  rubygems_version: 1.8.23
@@ -83,8 +129,9 @@ signing_key:
83
129
  specification_version: 3
84
130
  summary: gem to parse proxy auto-config files.
85
131
  test_files:
86
- - spec/proxy_pac_rb/functions_spec.rb
87
- - spec/proxy_pac_rb_spec.rb
132
+ - spec/environment_spec.rb
133
+ - spec/file_spec.rb
134
+ - spec/parser_spec.rb
88
135
  - spec/spec_helper.rb
89
136
  - spec/support/environment.rb
90
137
  - spec/support/filesystem.rb
@@ -1,128 +0,0 @@
1
- module ProxyPacRb
2
- module Functions
3
- DAYS = { "MON" => 1, "TUE" => 2, "WED" => 3, "THU" => 4, "FRI" => 5, "SAT" => 6, "SUN" => 7 }
4
- MONTHS = { "JAN" => 1, "FEB" => 2, "MAR" => 3, "APR" => 4, "MAY" => 5, "JUN" => 6,
5
- "JUL" => 7, "AUG" => 8, "SEP" => 9, "OCT" => 10, "NOV" => 11, "DEC" => 12 }
6
-
7
- class << self
8
- def isPlainHostName(host)
9
- not host.include? "."
10
- end
11
-
12
- def dnsDomainIs(host, domain)
13
- host.end_with? domain
14
- end
15
-
16
- def localHostOrDomainIs(host, hostdom)
17
- host == hostdom or hostdom.include? host
18
- end
19
-
20
- def isResolvable(host)
21
- !!resolve_host(host)
22
- end
23
-
24
- def isInNet(host, pattern, mask)
25
- IPAddr.new(pattern).mask(mask).include? resolve_host(host)
26
- end
27
-
28
- def dnsResolve(host)
29
- resolve_host(host)
30
- end
31
-
32
- def myIpAddress()
33
- resolve_host(Socket.gethostname)
34
- end
35
-
36
- def dnsDomainLevels(host)
37
- host.scan(".").size
38
- end
39
-
40
- def shExpMatch(str, shexp)
41
- ::File.fnmatch(shexp, str)
42
- end
43
-
44
- def weekdayRange(wd1, wd2 = nil, gmt = nil)
45
- time = Time.now
46
- time = time.utc if gmt == "GMT"
47
-
48
- (DAYS[wd1]..DAYS[wd2 || wd1]).include? time.wday
49
- end
50
-
51
- def dateRange(*args)
52
- time = Time.now
53
- time = time.utc if args.last == "GMT" and args.pop
54
-
55
- case args.size
56
- when 1
57
- check_date_part(time, args[0])
58
- when 2
59
- check_date_part(time, args[0]..args[1])
60
- when 4
61
- check_date_part(time, args[0]..args[2]) and
62
- check_date_part(time, args[1]..args[3])
63
- when 6
64
- check_date_part(time, args[0]..args[3]) and
65
- check_date_part(time, args[1]..args[4]) and
66
- check_date_part(time, args[2]..args[5])
67
- else
68
- raise ArgumentError, "wrong number of arguments"
69
- end
70
- end
71
-
72
- def timeRange(*args)
73
- time = Time.now
74
- time = time.utc if args.last == "GMT" and args.pop
75
-
76
- case args.size
77
- when 1
78
- time.hour == args[0]
79
- when 2
80
- (args[0]..args[1]).include? time.hour
81
- when 4
82
- (args[0]..args[2]).include? time.hour and
83
- (args[1]..args[3]).include? time.min
84
- when 6
85
- (args[0]..args[3]).include? time.hour and
86
- (args[1]..args[4]).include? time.min and
87
- (args[2]..args[5]).include? time.sec
88
- else
89
- raise ArgumentError, "wrong number of arguments"
90
- end
91
- end
92
-
93
- private
94
- def check_date_part(time, part, operation = :==)
95
- case part
96
- when String
97
- time.month.send(operation, MONTHS[part])
98
- when Integer
99
- if part < 100
100
- time.day.send(operation, part)
101
- else
102
- time.year.send(operation, part)
103
- end
104
- when Range
105
- check_date_part(time, part.begin, :>=) and check_date_part(time, part.end, :<=)
106
- else
107
- raise ArgumentError, "wrong type"
108
- end
109
- end
110
-
111
- def resolve_host(host)
112
- Resolv.each_address(host) do |address|
113
- begin
114
- return address if IPAddr.new(address).ipv4?
115
- rescue ArgumentError
116
- end
117
- end
118
-
119
- # We couldn't find an IPv4 address for the host
120
- nil
121
- rescue Resolv::ResolvError, NoMethodError
122
- # Have to rescue NoMethodError because jruby has a bug with non existant hostnames
123
- # See http://jira.codehaus.org/browse/JRUBY-6054
124
- nil
125
- end
126
- end
127
- end
128
- end
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe ProxyPacRb::Functions do
5
- describe "isResolvable()" do
6
- let(:tester) do
7
- ProxyPacRb::Functions
8
- end
9
-
10
- it "should return true for localhost" do
11
- expect(tester.isResolvable("localhost")).to be_true
12
- end
13
-
14
- it "should return false for awidhaowuhuiuhiuug" do
15
- expect(tester.isResolvable('asdfasdfasdfasdf')).to be_false
16
- end
17
- end
18
- end
@@ -1,112 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe ProxyPacRb do
5
-
6
- let(:sample_pac) do
7
- create_file 'sample.pac', <<-EOS.strip_heredoc
8
- function FindProxyForURL(url, host) {
9
- return "DIRECT";
10
- }
11
- EOS
12
- end
13
-
14
- let(:example_1) do
15
- create_file 'example_1.pac', <<-EOS.strip_heredoc
16
- // Taken from http://findproxyforurl.com/pac_file_examples.html
17
- function FindProxyForURL(url, host) {
18
-
19
- // If URL has no dots in host name, send traffic direct.
20
- if (isPlainHostName(host))
21
- return "DIRECT";
22
-
23
- // If specific URL needs to bypass proxy, send traffic direct.
24
- if (shExpMatch(url,"*domain.com*") ||
25
- shExpMatch(url,"*vpn.domain.com*"))
26
- return "DIRECT";
27
-
28
- // If IP address is internal or hostname resolves to internal IP, send direct.
29
- var resolved_ip = dnsResolve(host);
30
-
31
- if (isInNet(resolved_ip, "10.0.0.0", "255.0.0.0") ||
32
- isInNet(resolved_ip, "172.16.0.0", "255.240.0.0") ||
33
- isInNet(resolved_ip, "192.168.0.0", "255.255.0.0") ||
34
- isInNet(resolved_ip, "127.0.0.0", "255.255.255.0"))
35
- return "DIRECT";
36
-
37
- // If not on a internal/LAN IP address, send traffic direct.
38
- if (!isInNet(myIpAddress(), "10.10.1.0", "255.255.255.0"))
39
- return "DIRECT";
40
-
41
- // All other traffic uses below proxies, in fail-over order.
42
- return "PROXY 1.2.3.4:8080; PROXY 4.5.6.7:8080; DIRECT";
43
-
44
- }
45
- EOS
46
- end
47
-
48
- let(:example_2) do
49
- create_file 'example_2.pac', <<-EOS.strip_heredoc
50
- // Taken from http://findproxyforurl.com/pac_file_examples.html
51
- function FindProxyForURL(url, host) {
52
-
53
- // If IP address is internal or hostname resolves to internal IP, send direct.
54
- var resolved_ip = dnsResolve(host);
55
-
56
- if (isInNet(resolved_ip, "10.0.0.0", "255.0.0.0") ||
57
- isInNet(resolved_ip, "172.16.0.0", "255.240.0.0") ||
58
- isInNet(resolved_ip, "192.168.0.0", "255.255.0.0") ||
59
- isInNet(resolved_ip, "127.0.0.0", "255.255.255.0"))
60
- return "DIRECT";
61
-
62
- // Use a different proxy for each protocol.
63
- if (shExpMatch(url, "http:*")) return "PROXY proxy1.domain.com:3128";
64
- if (shExpMatch(url, "https:*")) return "PROXY proxy2.domain.com:3128";
65
- if (shExpMatch(url, "ftp:*")) return "PROXY proxy3.domain.com:3128";
66
-
67
- }
68
- EOS
69
- end
70
-
71
- let(:everything_but_local) do
72
- create_file 'everything_but_local.pac', <<-EOS.strip_heredoc
73
- function FindProxyForURL(url, host) {
74
- if (isPlainHostName(host))
75
- return "DIRECT";
76
- else
77
- return "PROXY proxy:8080; DIRECT";
78
- }
79
- EOS
80
- end
81
-
82
- describe ".read" do
83
- it "should load a file from a path" do
84
- pac = ProxyPacRb.read(sample_pac)
85
- expect(pac).not_to be_nil
86
- end
87
-
88
- it "should return DIRECT for a url" do
89
- pac = ProxyPacRb.read(sample_pac)
90
- expect(pac.find('http://localhost')).to eq('DIRECT')
91
- end
92
- end
93
-
94
- describe ".source" do
95
- let(:source) do <<-JS.strip_heredoc
96
- function FindProxyForURL(url, host) {
97
- return "DIRECT";
98
- }
99
- JS
100
- end
101
-
102
- it "should load source" do
103
- pac = ProxyPacRb.source(source)
104
- expect(pac).not_to be_nil
105
- end
106
-
107
- it "should return DIRECT for a url" do
108
- pac = ProxyPacRb.source(source)
109
- expect(pac.find('http://localhost')).to eq('DIRECT')
110
- end
111
- end
112
- end