stun 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0432349b210e505023175475962ad5b769c4d4784dd2cae050b1d18b011dd48d
4
+ data.tar.gz: 5f88e1c6ffbfa5fb0a999de66ece90d9c7c408718b04fd0b1873222ad078cc36
5
+ SHA512:
6
+ metadata.gz: 3aaa137e9a8b45a4ae39b41862b7503bbe4459fada220e20044b2ba41fdf5bfe6ba660c592d8947bf636ae7d8a46440b60167acdebde051c030c410c06ed7045
7
+ data.tar.gz: 6617c411813c0e5971c3ee4616d77f2f71ddb3820bb406acc4c2694ccac5da494ec749266561b193ccc95eae031599c7ff7321acf4769455e59fac51f49c7464
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in stun-client.gemspec
6
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stun-client (0.1.0)
5
+ bindata (~> 2.4.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ bindata (2.4.6)
11
+ diff-lcs (1.3)
12
+ rake (10.5.0)
13
+ rspec (3.9.0)
14
+ rspec-core (~> 3.9.0)
15
+ rspec-expectations (~> 3.9.0)
16
+ rspec-mocks (~> 3.9.0)
17
+ rspec-core (3.9.1)
18
+ rspec-support (~> 3.9.1)
19
+ rspec-expectations (3.9.1)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-mocks (3.9.1)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-support (3.9.2)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.17)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.9.0)
34
+ stun-client!
35
+
36
+ BUNDLED WITH
37
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mateusz Kluge
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Stun::Client
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/stun/client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'stun-client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install stun-client
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake respec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stun-client.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "stun/client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './stun/client.rb'
4
+ require_relative './stun/version.rb'
5
+
6
+ module Stun
7
+ end
8
+
@@ -0,0 +1,99 @@
1
+ require 'socket'
2
+ require 'securerandom'
3
+ require 'bindata'
4
+
5
+ #https://tools.ietf.org/html/rfc3489
6
+
7
+ module Stun
8
+ class Client
9
+ class AddressAttributeValue < BinData::Record
10
+ endian :big
11
+
12
+ skip length: 1
13
+ uint8 :family
14
+ uint16 :port
15
+ uint8 :ip_a
16
+ uint8 :ip_b
17
+ uint8 :ip_c
18
+ uint8 :ip_d
19
+
20
+ def ip
21
+ [ip_a, ip_b, ip_c, ip_d].map(&:to_s).join('.')
22
+ end
23
+ end
24
+
25
+ class MappedAddress < AddressAttributeValue
26
+ end
27
+
28
+ class ResponseAddress < AddressAttributeValue
29
+ end
30
+
31
+ class StunMessageAttribute < BinData::Record
32
+ VALUE_TYPES = {
33
+ 0x0001 => :mapped_address,
34
+ 0x0002 => :response_address,
35
+ 0x0003 => :change_request,
36
+ 0x0004 => :source_address,
37
+ 0x0005 => :changed_address,
38
+ 0x0006 => :username,
39
+ 0x0007 => :password,
40
+ 0x0008 => :message_integrity,
41
+ 0x0009 => :error_code,
42
+ 0x000A => :unknown_attributes,
43
+ 0x000B => :reflected_from,
44
+ }.freeze
45
+
46
+ endian :big
47
+
48
+ uint16 :value_type
49
+ uint16 :value_length
50
+ # buffer :attribute_value, type: :uint8, length: :value_length
51
+ choice :attribute_value, selection: lambda { value_type }, choices: {
52
+ 0x0001 => :mapped_address,
53
+ 0x0002 => :response_address,
54
+ }
55
+
56
+ def value_type_name
57
+ VALUE_TYPES.fetch(value_type)
58
+ end
59
+ end
60
+
61
+ class StunMessage < BinData::Record
62
+ MESSAGE_TYPES = {
63
+ 0x0001 => :binding_request,
64
+ 0x0101 => :binding_response,
65
+ 0x0111 => :binding_error_response,
66
+ 0x0002 => :shared_secret_request,
67
+ 0x0102 => :shared_secret_response,
68
+ 0x0112 => :shared_secret_error_response
69
+ }.freeze
70
+
71
+ endian :big
72
+
73
+ uint16 :message_type
74
+ uint16 :message_length, initial_value: 0
75
+ string :transaction_id, length: 16, initial_value: lambda { SecureRandom.alphanumeric(16) }
76
+ buffer :attributes, type: :stun_message_attribute, length: lambda { message_length }, onlyif: lambda { message_length > 0 }
77
+
78
+ def message_type_name
79
+ MESSAGE_TYPES.fetch(message_type)
80
+ end
81
+ end
82
+
83
+ def initialize(options)
84
+ @host = options.fetch(:host, '108.177.14.127')
85
+ @port = options.fetch(:port, 19302)
86
+ end
87
+
88
+ def query_address
89
+ payload = StunMessage.new(message_type: 0x0001).to_binary_s
90
+ socket = UDPSocket.new
91
+ socket.connect(@host, @port)
92
+ socket.send(payload, 0, @host, @port)
93
+ resp = socket.recv(1024)
94
+
95
+ msg = StunMessage.read(resp)
96
+ puts "Response #{msg.inspect}"
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stun
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "stun/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stun"
8
+ spec.version = Stun::VERSION
9
+ spec.authors = ["Mateusz Kluge"]
10
+ spec.email = ["inclooder@gmail.com"]
11
+
12
+ spec.summary = %q{Stun client}
13
+ spec.description = %q{Stun client}
14
+ spec.homepage = "http://github.com"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "bindata", "~> 2.4.6"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.17"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.9.0"
31
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mateusz Kluge
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bindata
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.9.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.9.0
69
+ description: Stun client
70
+ email:
71
+ - inclooder@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/stun.rb
85
+ - lib/stun/client.rb
86
+ - lib/stun/version.rb
87
+ - stun.gemspec
88
+ homepage: http://github.com
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.0.8
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Stun client
111
+ test_files: []