net-ssh-socks 0.0.1
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/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +17 -0
- data/README.md +18 -0
- data/Rakefile +2 -0
- data/lib/net/ssh/socks.rb +103 -0
- data/net-ssh-socks.gemspec +23 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Net::SSH::Socks
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Net::SSH::Socks is a library for programmatically creating a SOCKS proxy that tunnels through SSH. Similar to Net::SSH::Service::Forward#local except the host is dynamic (determined by the client application, such as a browser).
|
6
|
+
|
7
|
+
## Synopsis
|
8
|
+
|
9
|
+
require 'net/ssh'
|
10
|
+
require 'net/ssh/socks'
|
11
|
+
|
12
|
+
Net::SSH.start('host', 'user') do |ssh|
|
13
|
+
ssh.forward.socks(8080)
|
14
|
+
end
|
15
|
+
|
16
|
+
## Install
|
17
|
+
|
18
|
+
gem install net-ssh-socks
|
data/Rakefile
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
|
3
|
+
module Net
|
4
|
+
module SSH
|
5
|
+
class Socks
|
6
|
+
VERSION = "0.0.1"
|
7
|
+
|
8
|
+
SOCKS_VERSION = 5
|
9
|
+
METHOD_NO_AUTH = 0
|
10
|
+
CMD_CONNECT = 1
|
11
|
+
REP_SUCCESS = 0
|
12
|
+
RESERVED = 0
|
13
|
+
ATYP_IPV4 = 1
|
14
|
+
ATYP_DOMAIN = 3
|
15
|
+
ATYP_IPV6 = 4
|
16
|
+
|
17
|
+
# client is an open socket
|
18
|
+
def initialize(client)
|
19
|
+
@client = client
|
20
|
+
end
|
21
|
+
|
22
|
+
# Communicates with a client application as described by the SOCKS 5
|
23
|
+
# specification: http://tools.ietf.org/html/rfc1928.
|
24
|
+
#
|
25
|
+
# returns the host and port requested by the client
|
26
|
+
def client_handshake
|
27
|
+
version, nmethods, *methods = @client.recv(8).unpack("C*")
|
28
|
+
|
29
|
+
if methods.include?(METHOD_NO_AUTH)
|
30
|
+
packet = [SOCKS_VERSION, METHOD_NO_AUTH].pack("C*")
|
31
|
+
@client.send packet, 0
|
32
|
+
else
|
33
|
+
@client.close
|
34
|
+
raise 'Unsupported authentication method. Only "No Authentication" is supported'
|
35
|
+
end
|
36
|
+
|
37
|
+
version, command, reserved, address_type, *destination = @client.recv(256).unpack("C*")
|
38
|
+
|
39
|
+
packet = ([SOCKS_VERSION, REP_SUCCESS, RESERVED, address_type] + destination).pack("C*")
|
40
|
+
@client.send packet, 0
|
41
|
+
|
42
|
+
remote_host, remote_port = case address_type
|
43
|
+
when ATYP_IPV4
|
44
|
+
host = destination[0..-3].join('.')
|
45
|
+
port = destination[-2..-1].pack('C*').unpack('n')
|
46
|
+
[host, port]
|
47
|
+
when ATYP_DOMAIN
|
48
|
+
@client.close
|
49
|
+
raise 'Unsupported address type. Only "IPv4" is supported'
|
50
|
+
when ATYP_IPV6
|
51
|
+
@client.close
|
52
|
+
raise 'Unsupported address type. Only "IPv4" is supported'
|
53
|
+
end
|
54
|
+
|
55
|
+
[remote_host, remote_port]
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Net::SSH::Service::Forward
|
63
|
+
# Starts listening for connections on the local host, and forwards them
|
64
|
+
# to the specified remote host/port via the SSH connection. This method
|
65
|
+
# accepts either one or two arguments. When two arguments are given,
|
66
|
+
# they are:
|
67
|
+
#
|
68
|
+
# * the local address to bind to
|
69
|
+
# * the local port to listen on
|
70
|
+
#
|
71
|
+
# If one argument is given, it is as if the local bind address is
|
72
|
+
# "127.0.0.1", and the rest are applied as above.
|
73
|
+
#
|
74
|
+
# ssh.forward.socks(8080)
|
75
|
+
# ssh.forward.socks("0.0.0.0", 8080)
|
76
|
+
def socks(*args)
|
77
|
+
if args.length < 1 || args.length > 2
|
78
|
+
raise ArgumentError, "expected 1 or 2 parameters, got #{args.length}"
|
79
|
+
end
|
80
|
+
|
81
|
+
bind_address = "127.0.0.1"
|
82
|
+
bind_address = args.shift if args.first.is_a?(String) && args.first =~ /\D/
|
83
|
+
local_port = args.shift.to_i
|
84
|
+
|
85
|
+
socket = TCPServer.new(bind_address, local_port)
|
86
|
+
session.listen_to(socket) do |socket|
|
87
|
+
client = socket.accept
|
88
|
+
|
89
|
+
socks = Net::SSH::Socks.new(client)
|
90
|
+
remote_host, remote_port = socks.client_handshake
|
91
|
+
|
92
|
+
channel = session.open_channel("direct-tcpip", :string, remote_host, :long, remote_port, :string, bind_address, :long, local_port) do |channel|
|
93
|
+
channel.info { "direct channel established" }
|
94
|
+
prepare_client(client, channel, :local)
|
95
|
+
end
|
96
|
+
|
97
|
+
channel.on_open_failed do |ch, code, description|
|
98
|
+
channel.error { "could not establish direct channel: #{description} (#{code})" }
|
99
|
+
client.close
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'net/ssh/socks'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "net-ssh-socks"
|
7
|
+
s.version = Net::SSH::Socks::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Mike Enriquez"]
|
10
|
+
s.email = ["mike@edgecase.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{An extension to Net::SSH that adds dynamic port forwarding through a SOCKS proxy}
|
13
|
+
s.description = %q{Net::SSH::Socks is a library for programmatically creating a SOCKS proxy. Similar to Net::SSH::Service::Forward#local except the host is dynamic (determined by the client application, such as a browser).}
|
14
|
+
|
15
|
+
s.rubyforge_project = "net-ssh-socks"
|
16
|
+
|
17
|
+
s.add_dependency "net-ssh", "~> 2.0.0"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-ssh-socks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Mike Enriquez
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-21 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: net-ssh
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 2.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Net::SSH::Socks is a library for programmatically creating a SOCKS proxy. Similar to Net::SSH::Service::Forward#local except the host is dynamic (determined by the client application, such as a browser).
|
38
|
+
email:
|
39
|
+
- mike@edgecase.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/net/ssh/socks.rb
|
53
|
+
- net-ssh-socks.gemspec
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: ""
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: net-ssh-socks
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: An extension to Net::SSH that adds dynamic port forwarding through a SOCKS proxy
|
88
|
+
test_files: []
|
89
|
+
|