tunnelbroker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6b32e7145250ea944c77ae905064d8ac32191ce
4
+ data.tar.gz: fc3e5e2ea04d423a63febddc8d92314ce6ce7635
5
+ SHA512:
6
+ metadata.gz: 578e56159628fec166c5b0f0fba69d40ef8a7b95bf75c1e7e93230e99627b30e01b6d765d91bc818a57127ab71cee332238e7f0912830d3fb0b7b455af937b68
7
+ data.tar.gz: 4ba2792de994d30fdf09d9119707dd6d85f07e09c01f2d15344a98e0de69b903f8bf9a4935aca41560f27f7cf79dfec7385299b17845c7afbb5bc296fb784d7d
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+ Gemfile.lock
12
+ test.rb
13
+
14
+ ## Specific to RubyMotion:
15
+ .dat*
16
+ .repl_history
17
+ build/
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalisation:
26
+ /.bundle/
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format Fuubar
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ branches:
2
+ only:
3
+ - master
4
+ language: ruby
5
+ rvm:
6
+ - 2.1.1
7
+ - 2.1.0
8
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # -*- coding: UTF-8 -*-
2
+ source 'https://rubygems.org/'
3
+
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tim Heckman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ tunnelbroker
2
+ ============
3
+ [![Build Status](https://img.shields.io/travis/theckman/tunnelbroker/master.svg)](https://travis-ci.org/theckman/tunnelbroker)
4
+ [![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://tldrlegal.com/license/mit-license)
5
+ [![Coveralls Coverage](https://img.shields.io/coveralls/theckman/tunnelbroker/master.svg)](https://coveralls.io/r/theckman/tunnelbroker)
6
+ [![Code Climate](https://img.shields.io/codeclimate/github/theckman/tunnelbroker.svg)](https://codeclimate.com/github/theckman/tunnelbroker)
7
+ [![Gemnasium](https://img.shields.io/gemnasium/theckman/tunnelbroker.svg)](https://gemnasium.com/theckman/tunnelbroker)
8
+
9
+ Ruby TunnelBroker API client
10
+
11
+ LICENSE
12
+ -------
13
+ [tunnelbroker](https://github.com/theckman/tunnelbroker) is released under
14
+ the [MIT](http://opensource.org/licenses/MIT) license. The full license has
15
+ been provided in the
16
+ [LICENSE](https://github.com/theckman/tunnelbroker/blob/master/LICENSE) file.
17
+
18
+ CONTRIBUTING
19
+ ------------
20
+ See [CONTRIBUTION.md](https://github.com/theckman/tunnelbroker/blob/master/CONTRIBUTING.md)
21
+ for information on contributing back to this project.
22
+
23
+ INSTALLATION
24
+ ------------
25
+ To install on the commandline using `gem`:
26
+
27
+ ```Bash
28
+ gem install tunnelbroker
29
+ ```
30
+
31
+ If you are including this in your project's `Gemfile`:
32
+
33
+ ```Ruby
34
+ gem 'tunnelbroker'
35
+ ```
36
+
37
+ USAGE
38
+ -----
39
+ ### Nitty-gritty overview
40
+ ```Ruby
41
+ # require the gem
42
+ require 'tunnelbroker'
43
+ => true
44
+
45
+ # instantiate a new client
46
+ client = TunnelBroker::Client.new
47
+ => #<TunnelBroker::Client:0x007fa2fbaac5d0>
48
+
49
+ # configure the client
50
+ client.configure do |c|
51
+ c.tunnelid = 42
52
+ c.username = 'theckman'
53
+ c.update_key = 'YourUpdateKey'
54
+ end
55
+
56
+ # check your config options
57
+ client.config.username
58
+ => "theckman"
59
+
60
+ # update the API, assign the response object
61
+ response = client.submit_update
62
+ => #<TunnelBroker::APIResponse:0x007fa2fba20e40 @success=true, @changed=false, @response={:msg=>"nochg", :data=>{:ip=>"50.161.84.35"}}>
63
+
64
+ # was it successful?
65
+ response.success?
66
+ => true
67
+
68
+ # did the IP address change?
69
+ response.changed?
70
+ => false
71
+
72
+ # configure the client, specifying an ip4addr this time
73
+ client.configure do |c|
74
+ c.ip4addr = '127.0.0.1'
75
+ end
76
+
77
+ # update the API, assign the response object
78
+ response = client.submit_update
79
+ => #<TunnelBroker::APIResponse:0x007fa2fb9d9c98 @success=true, @changed=true, @response={:msg=>"good", :data=>{:ip=>"127.0.0.1"}}>
80
+
81
+ # was it successful?
82
+ response.success?
83
+ => true
84
+
85
+ # did the IP address change?
86
+ response.changed?
87
+ => true
88
+ ```
89
+
90
+ ### Configuration
91
+ The configuration of the client is done by sending a Ruby block to the client's
92
+ `configure` method:
93
+
94
+ ```Ruby
95
+ # henceforth, any refences to client will be this:
96
+ client = TunnelBroker::Client.new
97
+
98
+ client.configure do |cfg|
99
+ cfg.username = 'theckman'
100
+ end
101
+ ```
102
+
103
+ Here are the available configuration items:
104
+
105
+ | Option | Explanation |
106
+ | ------------ | ----------- |
107
+ | `ip4addr` | Specify the ipv4 address that will connect to the tunnel. If not specified, the IP address of which the request came from is specified. |
108
+ | `username` | This is your [tunnelbroker.net](https://www.tunnelbroker.net/) username |
109
+ | `update_key` | This is the update_key for your tunnel, you can set this on the 'Advanced' tab for your tunnel. |
110
+ | `tunnelid` | The tunnelid from the 'IPv6 Tunnel' tab of your tunnel. |
111
+ | `url` | If you want to use a custom endpoint URL, you can specify it here. Defaults to the TunnelBroker API endpoint. |
112
+
113
+ ### Updating the API
114
+ There is a simple method to update the API. It returns a `TunnelBroker::APIResponse` object.
115
+ ```Ruby
116
+ client.submit_update
117
+ ```
118
+
119
+ ### Checking the response object
120
+ The response object has two primary methods for telling what happened:
121
+
122
+ * `.success?` - returns true/false as to whether this was successful or not
123
+ * `.changed?` - returns true/false as to whether IP had changed.
124
+ * `.response` - Hash containing the response from the server split in to sections.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- coding: UTF-8 -*_
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ Rubocop::RakeTask.new(:rubocop) do |t|
8
+ t.patterns =
9
+ %w( Rakefile Gemfile tunnelbroker.gemspec lib/**/*.rb spec/*_spec.rb )
10
+ t.fail_on_error = true
11
+ end
12
+
13
+ task default: [:rubocop, :spec]
@@ -0,0 +1,28 @@
1
+ # -*- coding: UTF-8 -*-
2
+ # The MIT License (MIT)
3
+
4
+ # Copyright (c) 2014 Tim Heckman
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ require 'tunnelbroker/version'
25
+ require 'tunnelbroker/api_response'
26
+ require 'tunnelbroker/configuration'
27
+ require 'tunnelbroker/messenger'
28
+ require 'tunnelbroker/client'
@@ -0,0 +1,68 @@
1
+ # -*- coding: UTF-8 -*-
2
+ require 'English'
3
+
4
+ module TunnelBroker
5
+ # class to give some helpful handling of the TunnelBroker API response
6
+ #
7
+ class APIResponse
8
+ attr_reader :response
9
+
10
+ BADAUTH = /^(badauth)$/
11
+ CHANGE = /^(good)\s(\d+\.\d+\.\d+\.\d+)$/
12
+ NO_CHANGE = /^(nochg)\s(\d+\.\d+\.\d+\.\d+)$/
13
+
14
+ def initialize(response)
15
+ parse_response(response.lines.first)
16
+ end
17
+
18
+ def success?
19
+ if @success.nil?
20
+ false
21
+ else
22
+ @success
23
+ end
24
+ end
25
+
26
+ def changed?
27
+ if @changed.nil?
28
+ false
29
+ else
30
+ @changed
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def parse_response(firstline)
37
+ if BADAUTH.match(firstline)
38
+ bad_auth($LAST_MATCH_INFO)
39
+ elsif CHANGE.match(firstline)
40
+ change($LAST_MATCH_INFO)
41
+ elsif NO_CHANGE.match(firstline)
42
+ no_change($LAST_MATCH_INFO)
43
+ end
44
+ end
45
+
46
+ def bad_auth(match)
47
+ @success = false
48
+ @changed = false
49
+ @response = { msg: match[1], data: {} }
50
+ end
51
+
52
+ def change(match)
53
+ @success = true
54
+ @changed = true
55
+ matched_response(match)
56
+ end
57
+
58
+ def no_change(match)
59
+ @success = true
60
+ @changed = false
61
+ matched_response(match)
62
+ end
63
+
64
+ def matched_response(match)
65
+ @response = { msg: match[1], data: { ip: match[2] } }
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,46 @@
1
+ # -*- coding: UTF-8 -*-
2
+ require 'tunnelbroker/configuration'
3
+ require 'tunnelbroker/messenger'
4
+
5
+ module TunnelBroker
6
+ # TunnelBroker client for communication with the API
7
+ #
8
+ class Client
9
+ ENDPOINT ||= 'https://ipv4.tunnelbroker.net/nic/update'
10
+
11
+ def configure
12
+ yield(config) if Kernel.block_given?
13
+ end
14
+
15
+ def submit_update
16
+ c = build_messenger_config
17
+ tb = TunnelBroker::Messenger.new(c)
18
+ tb.call_api
19
+ end
20
+
21
+ def config
22
+ @config ||= TunnelBroker::Configuration.new
23
+ end
24
+
25
+ private
26
+
27
+ def build_messenger_config
28
+ conf = {}
29
+ TunnelBroker::Configuration::FIELDS.each do |k|
30
+ conf.merge!(config_hash_item(k))
31
+ end
32
+ conf
33
+ end
34
+
35
+ def config_hash_item(key)
36
+ c = config.send(key)
37
+ if c.nil? && key == :url
38
+ { key => ENDPOINT }
39
+ elsif c.nil?
40
+ {}
41
+ else
42
+ { key => c }
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ module TunnelBroker
4
+ # Configuration class for the TunnelBroker client
5
+ #
6
+ class Configuration
7
+ FIELDS = [:url, :ip4addr, :username, :update_key, :tunnelid]
8
+ attr_accessor(*FIELDS)
9
+
10
+ def initialize
11
+ set_default_values
12
+ end
13
+
14
+ private
15
+
16
+ def set_default_values
17
+ @ip4addr = nil
18
+ @username = nil
19
+ @update_key = nil
20
+ @tunnelid = nil
21
+ @url = nil
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,34 @@
1
+ # -*- coding: UTF-8 -*-
2
+ require 'tunnelbroker/api_response'
3
+ require 'httparty'
4
+
5
+ module TunnelBroker
6
+ # The class for communicating with the API
7
+ #
8
+ class Messenger
9
+ include HTTParty
10
+
11
+ def initialize(opts)
12
+ opts_to_inst(opts)
13
+ end
14
+
15
+ def call_api
16
+ TunnelBroker::APIResponse.new(call_endpoint)
17
+ end
18
+
19
+ private
20
+
21
+ def opts_to_inst(opts)
22
+ opts.each do |k, v|
23
+ instance_variable_set(:"@#{k}", v)
24
+ end
25
+ end
26
+
27
+ def call_endpoint
28
+ auth = { username: @username, password: @update_key }
29
+ query = { hostname: @tunnelid }
30
+ query.merge!(myip: @ip4addr) unless @ip4addr.nil?
31
+ self.class.get(@url, basic_auth: auth, query: query)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ # -*- coding: UTF-8 -*-
2
+ # The MIT License (MIT)
3
+
4
+ # Copyright (c) 2014 Tim Heckman
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ # The TunnelBroker API client namespace.
25
+ #
26
+ module TunnelBroker
27
+ VERSION ||= '0.1.0'
28
+ end