Class: Spior::Tor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/spior/tor/config.rb

Overview

Generate a config file (torrc) for Spior

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Config

Attributes

  • filename - A reference to a tempfile like filename=Tempfile.new(‘foo’)



15
16
17
18
19
20
21
22
# File 'lib/spior/tor/config.rb', line 15

def initialize(filename)
  @filename = filename
  @config_torrc = '/etc/tor/torrc'
  @config_dir = '/etc/torrc.d'
  @config_spiorrc = "#{@config_dir}/spior.conf"
  @content = ['# Generated by Spior, don\'t edit.']
  @content_torrc = []
end

Instance Method Details

#backupObject

Save current Tor options (Spior::CONFIG) in /etc/tor/torrc Only if theses options are not alrealy present



53
54
55
56
57
58
59
# File 'lib/spior/tor/config.rb', line 53

def backup
  generate_content(@content_torrc)
  write_file @content_torrc, @filename.path, 'w'

  Msg.p 'Saving Tor options...'
  Helpers.mv(@filename.path, @config_spiorrc)
end

#generateObject

Generate a ‘torrc` compatible file for Spior Use value from Spior::CONFIG



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spior/tor/config.rb', line 26

def generate
  create_config_dir
  configure_torrc
  generate_content(@content)
  return if @content.length == 1

  cn = @content.join("\n")
  File.write(@filename.path, "#{cn}\n")
  Msg.p "Generating #{@config_spiorrc}..."
  Helpers.mv(@filename.path, @config_spiorrc)
end

#write_file(content, file, mode = 'a') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spior/tor/config.rb', line 38

def write_file(content, file, mode = 'a')
  return if content.nil?

  File.open(file, mode) do |f|
    if content.is_a?(Array)
      f.puts(content.join("\n"))
    else
      f.puts(content)
    end
    # f.chmod(644)
  end
end