ruby-hidden-service 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +58 -0
  4. data/lib/tor/hidden-service.rb +102 -0
  5. metadata +60 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d3aa95dec3863a6310b2b6460d7a6a44d0296c5c
4
+ data.tar.gz: 9fbc3b5e0ed61b76bf6a77aa094e05184d67e102
5
+ SHA512:
6
+ metadata.gz: 6e7101cd38223e1cca8c31cd48cd4efc180a12b3191dbc73f8c2bd5943a372aedc68ad7cee3ea152150c2e5bf7fbd4a5348f4010dd4a696d1f1d78860ce1ae5d
7
+ data.tar.gz: a59fb9b9f6ca002037db1b26cbc42a0907debf1341ae03b9984ae413005d9cc69a225e3e3fa16015e0177d959c754f34139da27ae79acfdc071ab9b43fe2ed97
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2015 Warren Guy <warren@guy.net.au>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Tor::HiddenService
2
+
3
+ Bring up a Tor hidden service from within your Ruby app.
4
+
5
+ You might find this useful to run a hidden service in your Heroku/Dokku or
6
+ other containerised infrastructure.
7
+
8
+ ## Usage
9
+
10
+ Add the gem to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ruby-hidden-service'
14
+ ```
15
+
16
+ and start the hidden service during startup of your web backend, probably
17
+ in `config.ru`, for example:
18
+
19
+ ```ruby
20
+ require 'ruby-hidden-service'
21
+
22
+ hidden_service = Tor::HiddenService.new(
23
+ private_key: ENV['HIDDEN_SERVICE_PRIVATE_KEY'],
24
+ server_port: ENV['PORT'] || 5000
25
+ )
26
+
27
+ hidden_service.start
28
+ ```
29
+ ### Options
30
+
31
+ All configuration options and their defaults:
32
+ ```ruby
33
+ tor_executable: (Tor.available? ? Tor.program_path : nil),
34
+ temp_dir: "#{ENV['PWD']}/tmp" || nil,
35
+ private_key: nil,
36
+ server_host: 'localhost',
37
+ server_port: ENV['PORT'],
38
+ hidden_service_port: 80,
39
+ tor_control_port: rand(10000..65000)
40
+ ```
41
+
42
+ ### Tor executable
43
+
44
+ This relies on Tor being in your path, or otherwise having the path to the
45
+ Tor binary specified in the options hash. If you're running on Heroku or
46
+ Dokku, you can use the [heroku-buildpack-apt](https://github.com/ddollar/heroku-buildpack-apt) and [heroku-buildpack-multi](https://github.com/ddollar/heroku-buildpack-multi)
47
+ buildpacks to install the `tor` package. This will place the Tor binary in
48
+ the path where this library can discover it.
49
+
50
+ ## License
51
+
52
+ MIT license. See [LICENSE](https://github.com/warrenguy/ruby-hidden-service/blob/master/LICENSE).
53
+
54
+ ## Author
55
+
56
+ Warren Guy <warren@guy.net.au>
57
+
58
+ https://warrenguy.me
@@ -0,0 +1,102 @@
1
+ require 'tor'
2
+ require 'openssl'
3
+ require 'fileutils'
4
+
5
+ module Tor
6
+ class HiddenService
7
+
8
+ attr_reader :options
9
+
10
+ def initialize(options={})
11
+ @options = parse_options(options)
12
+ @base_dir = "#{@options[:temp_dir]}/tor_#{@options[:tor_control_port]}"
13
+ end
14
+
15
+ def start
16
+ generate_tor_config
17
+
18
+ @pid = fork do
19
+ spawn @options[:tor_executable], '-f', "#{@base_dir}/torrc"
20
+ end
21
+ Process.wait(@pid)
22
+
23
+ begin
24
+ @tor_pid = File.read(open("#{@base_dir}/pid")).strip.to_i
25
+ rescue Errno::ENOENT
26
+ log_message "Waiting for Tor PID to appear..."
27
+ sleep 1
28
+ retry
29
+ end
30
+
31
+ log_message "Started Tor with PID #{@tor_pid} on control port #{@options[:tor_control_port]}"
32
+
33
+ at_exit do
34
+ log_message "Killing Tor PID #{@tor_pid}..."
35
+ Process.kill :SIGTERM, @tor_pid
36
+ Process.wait
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def parse_options(options)
43
+ parsed_options = {
44
+ tor_executable: (Tor.available? ? Tor.program_path : nil),
45
+ temp_dir: "#{ENV['PWD']}/tmp" || nil,
46
+ private_key: nil,
47
+ server_host: 'localhost',
48
+ server_port: ENV['PORT'],
49
+ hidden_service_port: 80,
50
+ tor_control_port: rand(10000..65000)
51
+ }.merge(options)
52
+
53
+ raise ArgumentError, "No tor executable found" unless parsed_options[:tor_executable]
54
+ raise ArgumentError, "temp_dir #{parsed_options[:temp_dir]} does not exist or is not writable" unless (
55
+ File.writable? parsed_options[:temp_dir] or
56
+ FileUtils.mkdir_p parsed_options[:temp_dir])
57
+ raise ArgumentError, "Private key is not a valid 1024 bit RSA private key" unless (
58
+ OpenSSL::PKey::RSA.new(parsed_options[:private_key]).private? and
59
+ OpenSSL::PKey::RSA.new(parsed_options[:private_key]).n.num_bits == 1024)
60
+ raise ArgumentError, "Must provide option: #{parsed_options.key(nil).to_s}" if parsed_options.values.include? nil
61
+
62
+ return parsed_options
63
+ end
64
+
65
+ def generate_tor_config
66
+ tor_config = {
67
+ DataDirectory: "#{@base_dir}/data",
68
+ ControlPort: "#{@options[:tor_control_port]}",
69
+ HiddenServiceDir: "#{@base_dir}/hidden_service",
70
+ HiddenServicePort: "#{@options[:hidden_service_port]} #{@options[:server_host]}:#{@options[:server_port]}",
71
+ PidFile: "#{@base_dir}/pid",
72
+ RunAsDaemon: 1,
73
+ SocksPort: 0
74
+ }
75
+
76
+ begin
77
+ FileUtils.mkdir_p tor_config[:DataDirectory]
78
+ FileUtils.mkdir_p tor_config[:HiddenServiceDir]
79
+ File.write("#{tor_config[:HiddenServiceDir]}/private_key", @options[:private_key])
80
+ FileUtils.chmod 0700, tor_config[:HiddenServiceDir]
81
+ FileUtils.chmod 0600, "#{tor_config[:HiddenServiceDir]}/private_key"
82
+
83
+ File.write(
84
+ "#{@base_dir}/torrc",
85
+ tor_config.map{|k,v| "#{k} #{v}"}.join("\n"))
86
+ rescue => e
87
+ raise "Error creating configuration: #{e}"
88
+ end
89
+
90
+ return true
91
+ end
92
+
93
+ def log_message(message)
94
+ $stdout.puts "Tor::HiddenService: [#{@options[:tor_control_port]}]: #{message}"
95
+ end
96
+
97
+ def log_error(message)
98
+ $stderr.puts "Tor::HiddenService: [#{@options[:tor_control_port]}]: ERROR: #{message}"
99
+ end
100
+
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-hidden-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Warren Guy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Automatically set up and tear down a Tor hidden service
28
+ email: warren@guy.net.au
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/tor/hidden-service.rb
36
+ homepage: https://github.com/warrenguy/ruby-hidden-service
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.2
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Automatically set up and tear down a Tor hidden service
60
+ test_files: []