dress_socks 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5b123ce6c20f6502e2d9113a4776a23460b0decdd1aa47becebccc4aa682ed85
4
+ data.tar.gz: d866a719eae28e9841f2969ebe9873a5bff63cb85044f2e8253632cf4f8c86a8
5
+ SHA512:
6
+ metadata.gz: eb343ce13ca2a3974703538709958e7dc974e630d7caa7c928a3494cdaf473d2f156f195becb274f2d7286d93aa770a992696c11730fedef31d0e3d6bc2c8f27
7
+ data.tar.gz: e989aa22f362ac3f7f74290866a86b52b06bd48d219852bc4573770c64e6696abb62b35c2b23b94b8cb60310bfe114d121424fb52eb174a2074cfdb49979cb82
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ # Changelog
2
+
3
+ 11-30-2019: Project created
data/Gemfile ADDED
@@ -0,0 +1,8 @@
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 dress_socks.gemspec
6
+ gemspec
7
+
8
+ gem "pry", "~> 0.12.2"
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dress_socks (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.2)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-expectations (3.9.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.17)
35
+ dress_socks!
36
+ pry (~> 0.12.2)
37
+ rake (~> 10.0)
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Justin McNally
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,41 @@
1
+ # DressSocks
2
+
3
+ A pure ruby implementation of SOCKSSocket, allowing tunneling a socket through a SOCKS proxy.
4
+
5
+ Based heavily on Socksify, we needed more flexibility in how we setup the tcp connection and only setting it up for certain pieces of code.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dress_socks'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dress_socks
22
+
23
+ ## Usage
24
+
25
+ DressSocks::Socket.new(remote_host, remote_port, socks_username: nil, socks_password: nil, socks_server: nil, socks_port: nil, socks_ignore: [], socks_version: '5')
26
+
27
+ Creates a new TCP Socket that tunnels through the socks configuration passed.
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ 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).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chowly/dress_socks.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dress_socks"
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,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dress_socks/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dress_socks"
8
+ spec.version = DressSocks::VERSION
9
+ spec.authors = ["Justin McNally"]
10
+ spec.email = ["justin@chowlyinc.com"]
11
+
12
+ spec.summary = 'A pure ruby implementation of SOCKSSocket'
13
+ spec.description = 'SOCKSSocket is great but require a lot of C gymnastics. This aims to do the same thing in all Ruby code.'
14
+ spec.homepage = 'https://github.com/chowly/dress_socks'
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/chowly/dress_socks"
24
+ spec.metadata["changelog_uri"] = "https://github.com/Chowly/dress_socks/blob/master/Changelog.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.17"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ end
@@ -0,0 +1,10 @@
1
+ require "socket"
2
+ require "resolv"
3
+
4
+ require "dress_socks/version"
5
+ require "dress_socks/socket"
6
+ require 'dress_socks/errors'
7
+
8
+ module DressSocks
9
+
10
+ end
@@ -0,0 +1,71 @@
1
+ module DressSocks
2
+ class SOCKSError < RuntimeError
3
+ def initialize(msg)
4
+ super
5
+ end
6
+
7
+ class ServerFailure < SOCKSError
8
+ def initialize
9
+ super("general SOCKS server failure")
10
+ end
11
+ end
12
+ class NotAllowed < SOCKSError
13
+ def initialize
14
+ super("connection not allowed by ruleset")
15
+ end
16
+ end
17
+ class NetworkUnreachable < SOCKSError
18
+ def initialize
19
+ super("Network unreachable")
20
+ end
21
+ end
22
+ class HostUnreachable < SOCKSError
23
+ def initialize
24
+ super("Host unreachable")
25
+ end
26
+ end
27
+ class ConnectionRefused < SOCKSError
28
+ def initialize
29
+ super("Connection refused")
30
+ end
31
+ end
32
+ class TTLExpired < SOCKSError
33
+ def initialize
34
+ super("TTL expired")
35
+ end
36
+ end
37
+ class CommandNotSupported < SOCKSError
38
+ def initialize
39
+ super("Command not supported")
40
+ end
41
+ end
42
+ class AddressTypeNotSupported < SOCKSError
43
+ def initialize
44
+ super("Address type not supported")
45
+ end
46
+ end
47
+
48
+ def self.for_response_code(code)
49
+ case code
50
+ when 1
51
+ ServerFailure
52
+ when 2
53
+ NotAllowed
54
+ when 3
55
+ NetworkUnreachable
56
+ when 4
57
+ HostUnreachable
58
+ when 5
59
+ ConnectionRefused
60
+ when 6
61
+ TTLExpired
62
+ when 7
63
+ CommandNotSupported
64
+ when 8
65
+ AddressTypeNotSupported
66
+ else
67
+ self
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,164 @@
1
+ module DressSocks
2
+ class Socket < ::TCPSocket
3
+ attr_accessor :socks_server, :socks_port, :socks_version, :socks_ignores, :socks_username, :socks_password
4
+
5
+ alias :initialize_tcp :initialize
6
+
7
+
8
+ def encoded_socks_version
9
+ (self.socks_version == "4a" or self.socks_version == "4") ? "\004" : "\005"
10
+ end
11
+
12
+ # See http://tools.ietf.org/html/rfc1928
13
+ def initialize(remote_host=nil, remote_port=0, local_host=nil, local_port=nil,
14
+ socks_username: nil, socks_password: nil, socks_server: nil, socks_port: nil,
15
+ socks_ignores: [], socks_version: '5')
16
+
17
+ self.socks_server = socks_server
18
+ self.socks_port = socks_port
19
+ self.socks_username = socks_username
20
+ self.socks_password = socks_password
21
+ self.socks_ignores = socks_ignores
22
+ self.socks_version = socks_version
23
+
24
+
25
+ if socks_server and socks_port and not socks_ignores.include?(remote_host)
26
+ initialize_tcp socks_server, socks_port
27
+
28
+ socks_authenticate unless socks_version =~ /^4/
29
+
30
+ if remote_host
31
+ socks_connect(remote_host, remote_port)
32
+ end
33
+ else
34
+ initialize_tcp remote_host, remote_port, local_host, local_port
35
+ end
36
+ end
37
+
38
+ # Authentication
39
+ def socks_authenticate
40
+ if self.socks_username || self.socks_password
41
+ write "\005\001\002"
42
+ else
43
+ write "\005\001\000"
44
+ end
45
+ auth_reply = recv(2)
46
+ if auth_reply.empty?
47
+ raise DressSocks::SOCKSError.new("Server doesn't reply authentication")
48
+ end
49
+ if auth_reply[0..0] != "\004" and auth_reply[0..0] != "\005"
50
+ raise DressSocks::SOCKSError.new("SOCKS version #{auth_reply[0..0]} not supported")
51
+ end
52
+ if self.socks_username || self.socks_password
53
+ if auth_reply[1..1] != "\002"
54
+ raise DressSocks::SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
55
+ end
56
+ auth = "\001"
57
+ auth += self.socks_username.to_s.length.chr
58
+ auth += self.socks_username.to_s
59
+ auth += self.socks_password.to_s.length.chr
60
+ auth += self.socks_password.to_s
61
+ write auth
62
+ auth_reply = recv(2)
63
+ if auth_reply[1..1] != "\000"
64
+ raise DressSocks::SOCKSError.new("SOCKS authentication failed")
65
+ end
66
+ else
67
+ if auth_reply[1..1] != "\000"
68
+ raise DressSocks::SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
69
+ end
70
+ end
71
+ end
72
+
73
+ # Connect
74
+ def socks_connect(host, port)
75
+ port = ::Socket.getservbyname(port) if port.is_a?(String)
76
+ req = String.new
77
+ req << self.encoded_socks_version
78
+ req << "\001"
79
+ req << "\000" if self.socks_version == "5"
80
+ req << [port].pack('n') if self.socks_version =~ /^4/
81
+
82
+ if self.socks_version == "4"
83
+ host = Resolv::DNS.new.getaddress(host).to_s
84
+ end
85
+ if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ # to IPv4 address
86
+ req << "\001" if self.socks_version == "5"
87
+ _ip = [$1.to_i,
88
+ $2.to_i,
89
+ $3.to_i,
90
+ $4.to_i
91
+ ].pack('CCCC')
92
+ req << _ip
93
+ elsif host =~ /^[:0-9a-f]+$/ # to IPv6 address
94
+ raise "TCP/IPv6 over SOCKS is not yet supported (inet_pton missing in Ruby & not supported by Tor"
95
+ req << "\004"
96
+ else # to hostname
97
+ if self.socks_version == "5"
98
+ req << "\003" + [host.size].pack('C') + host
99
+ else
100
+ req << "\000\000\000\001"
101
+ req << "\007\000"
102
+ req << host
103
+ req << "\000"
104
+ end
105
+ end
106
+ req << [port].pack('n') if self.socks_version == "5"
107
+
108
+ write req
109
+
110
+ socks_receive_reply
111
+ end
112
+
113
+ # returns [bind_addr: String, bind_port: Fixnum]
114
+ def socks_receive_reply
115
+ if self.socks_version == "5"
116
+ connect_reply = recv(4)
117
+ if connect_reply.empty?
118
+ raise DressSocks::SOCKSError.new("Server doesn't reply")
119
+ end
120
+ if connect_reply[0..0] != "\005"
121
+ raise DressSocks::SOCKSError.new("SOCKS version #{connect_reply[0..0]} is not 5")
122
+ end
123
+ if connect_reply[1..1] != "\000"
124
+ raise DressSocks::SOCKSError.for_response_code(connect_reply.bytes.to_a[1])
125
+ end
126
+ bind_addr_len = case connect_reply[3..3]
127
+ when "\001"
128
+ 4
129
+ when "\003"
130
+ recv(1).bytes.first
131
+ when "\004"
132
+ 16
133
+ else
134
+ raise DressSocks::SOCKSError.for_response_code(connect_reply.bytes.to_a[3])
135
+ end
136
+ bind_addr_s = recv(bind_addr_len)
137
+ bind_addr = case connect_reply[3..3]
138
+ when "\001"
139
+ bind_addr_s.bytes.to_a.join('.')
140
+ when "\003"
141
+ bind_addr_s
142
+ when "\004" # Untested!
143
+ i = 0
144
+ ip6 = ""
145
+ bind_addr_s.each_byte do |b|
146
+ if i > 0 and i % 2 == 0
147
+ ip6 += ":"
148
+ end
149
+ i += 1
150
+
151
+ ip6 += b.to_s(16).rjust(2, '0')
152
+ end
153
+ end
154
+ bind_port = recv(bind_addr_len + 2)
155
+ [bind_addr, bind_port.unpack('n')]
156
+ else
157
+ connect_reply = recv(8)
158
+ unless connect_reply[0] == "\000" and connect_reply[1] == "\x5A"
159
+ raise DressSocks::SOCKSError.new("Failed while connecting througth socks")
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,3 @@
1
+ module DressSocks
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dress_socks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Justin McNally
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: SOCKSSocket is great but require a lot of C gymnastics. This aims to
56
+ do the same thing in all Ruby code.
57
+ email:
58
+ - justin@chowlyinc.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rspec_status"
66
+ - Changelog.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - dress_socks.gemspec
75
+ - lib/dress_socks.rb
76
+ - lib/dress_socks/errors.rb
77
+ - lib/dress_socks/socket.rb
78
+ - lib/dress_socks/version.rb
79
+ homepage: https://github.com/chowly/dress_socks
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ allowed_push_host: https://rubygems.org
84
+ homepage_uri: https://github.com/chowly/dress_socks
85
+ source_code_uri: https://github.com/chowly/dress_socks
86
+ changelog_uri: https://github.com/Chowly/dress_socks/blob/master/Changelog.md
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.0.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A pure ruby implementation of SOCKSSocket
106
+ test_files: []