Module: IptablesWeb::Configuration

Included in:
IptablesWeb
Defined in:
lib/iptables_web/configuration.rb

Instance Method Summary collapse

Instance Method Details

#access_tokenObject



147
148
149
150
# File 'lib/iptables_web/configuration.rb', line 147

def access_token
  raise 'Access_token is required' unless @access_token
  @access_token
end

#access_token=(access_token) ⇒ Object



152
153
154
155
# File 'lib/iptables_web/configuration.rb', line 152

def access_token=(access_token)
  @access_token = access_token
  IptablesWeb::Model::Base.access_token = access_token
end

#api_base_urlObject



137
138
139
140
# File 'lib/iptables_web/configuration.rb', line 137

def api_base_url
  # raise 'api_base_url is required' unless @api_base_url
  @api_base_url
end

#api_base_url=(api_base_url) ⇒ Object



142
143
144
145
# File 'lib/iptables_web/configuration.rb', line 142

def api_base_url=(api_base_url)
  @api_base_url = api_base_url
  IptablesWeb::Model::Base.api_base_url = api_base_url
end

#checksum=(checksum) ⇒ Object



113
114
115
# File 'lib/iptables_web/configuration.rb', line 113

def checksum=(checksum)
  File.write(checksum_path, make_checksum(checksum))
end

#checksum?(checksum) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/iptables_web/configuration.rb', line 109

def checksum?(checksum)
  File.exists?(checksum_path) && File.read(checksum_path) == make_checksum(checksum)
end

#checksum_pathObject



101
102
103
# File 'lib/iptables_web/configuration.rb', line 101

def checksum_path
  path(@checksum_path || 'checksum')
end

#checksum_path=(pid_path) ⇒ Object



105
106
107
# File 'lib/iptables_web/configuration.rb', line 105

def checksum_path=(pid_path)
  @checksum_path = pid_path
end

#config_pathObject



65
66
67
68
69
70
71
# File 'lib/iptables_web/configuration.rb', line 65

def config_path
  if root?
    '/etc/iptables_web/config.yml'
  else
    path(@config_path || 'config.yml')
  end
end

#config_path=(config_path) ⇒ Object



73
74
75
# File 'lib/iptables_web/configuration.rb', line 73

def config_path=(config_path)
  @config_path = config_path
end

#dirObject



42
43
44
45
46
47
48
49
50
# File 'lib/iptables_web/configuration.rb', line 42

def dir
  @dir ||= begin
    if root?
      '/var/run/iptables_web'
    else
      File.expand_path(File.join(home, '.iptables-web'))
    end
  end
end

#dir=(d) ⇒ Object



52
53
54
# File 'lib/iptables_web/configuration.rb', line 52

def dir=(d)
  @dir = d
end

#homeObject



34
35
36
# File 'lib/iptables_web/configuration.rb', line 34

def home
  @home || ENV['HOME']
end

#home=(home) ⇒ Object



38
39
40
# File 'lib/iptables_web/configuration.rb', line 38

def home=(home)
  @home = home
end

#log_pathObject



87
88
89
90
91
92
93
# File 'lib/iptables_web/configuration.rb', line 87

def log_path
  if root?
    '/var/log/iptables-web.log'
  else
    path(@log_path || 'run.log')
  end
end

#log_path=(pid_path) ⇒ Object



95
96
97
98
# File 'lib/iptables_web/configuration.rb', line 95

def log_path=(pid_path)
  @log_path = pid_path
  $terminal.reset if $terminal.present? && $terminal.is_a?(Cli::LoggedOutput)
end

#make_checksum(check_sum) ⇒ Object



117
118
119
120
121
# File 'lib/iptables_web/configuration.rb', line 117

def make_checksum(check_sum)
  check_sum = check_sum.to_s
  check_sum += Digest::MD5.file(static_rules_path).hexdigest if static_rules?
  Digest::MD5.hexdigest(check_sum)
end

#path(path) ⇒ Object



56
57
58
# File 'lib/iptables_web/configuration.rb', line 56

def path(path)
  File.expand_path(path, dir)
end

#pid_file(&block) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/iptables_web/configuration.rb', line 157

def pid_file(&block)
  pid_file = Cli::PidFile.new(pid_path)
  begin
    pid_file.create
    block.call(pid_file)
    pid_file.delete
  rescue Cli::PidFile::AnotherLaunched => e

    pid_file.delete
    logged_say(e.message)
    return
  rescue Exception => e
    pid_file.delete
    raise e
  end
end

#pid_pathObject



78
79
80
# File 'lib/iptables_web/configuration.rb', line 78

def pid_path
  path(@pid_path || 'run.pid')
end

#pid_path=(pid_path) ⇒ Object



82
83
84
# File 'lib/iptables_web/configuration.rb', line 82

def pid_path=(pid_path)
  @pid_path = pid_path
end

#reloadObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/iptables_web/configuration.rb', line 4

def reload
  if File.exists?(config_path)
    logged_say("Load config file #{config_path}")
    YAML.load_file(config_path).each do |method, value|
      send("#{method}=".to_sym, value)
    end
  else
    logged_say("Config file #{config_path} does not exist")
  end
end

#root?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/iptables_web/configuration.rb', line 60

def root?
  Process::UID.eid == 0
end

#static_rulesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/iptables_web/configuration.rb', line 15

def static_rules
  return {} unless static_rules?
  rules = File.read(static_rules_path)
  chains = rules.scan(/\*([a-z]+)(.*?)COMMIT/m)
  if chains && chains.size > 0
    chains.each_with_object({}) do |r, obj|
      chain = r[0]
      obj[chain] ||= []
      obj[chain] = obj[chain] | r[1].split("\n")
    end
  else
    { 'filter' => rules.split("\n") }
  end
end

#static_rules?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/iptables_web/configuration.rb', line 30

def static_rules?
  File.exist?(static_rules_path)
end

#static_rules_pathObject



124
125
126
127
128
129
130
# File 'lib/iptables_web/configuration.rb', line 124

def static_rules_path
  if root?
    '/etc/iptables_web/static_rules'
  else
    path(@static_rules_path || 'static_rules')
  end
end

#static_rules_path=(static_rules_path) ⇒ Object



132
133
134
# File 'lib/iptables_web/configuration.rb', line 132

def static_rules_path=(static_rules_path)
  @static_rules_path = static_rules_path
end