Class: Zlown::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/zlown/core/core.rb

Class Method Summary collapse

Class Method Details

.init(args = [], opts = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/zlown/core/core.rb', line 180

def self.init(args = [], opts = {})
  Core.init_dirs(args, opts)

  Core.init_service_template(args, opts)

  Core.init_config_file(args, opts)

  Core.update_configs(args, opts)
end

.init_boot_script(args = [], opts = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/zlown/core/core.rb', line 136

def self.init_boot_script(args = [], opts = {})
  config = Core.load_config(args, opts)

  template = File.read(Zlown::Config::BOOT_SCRIPT_TEMPLATE)
  content = template
              .gsub('${IFACE_AP}', config[:ap])
              .gsub('${IFACE_UPSTREAM}', config[:upstream])

  # To write changes to the file, use:
  File.open(Zlown::Config::BOOT_SCRIPT, 'w') do |file|
    puts "Writting file #{Zlown::Config::BOOT_SCRIPT}"
    file.puts content
  end

  cmd = "chmod +x #{Zlown::Config::BOOT_SCRIPT}"
  puts cmd
  system cmd
end

.init_config_file(args = [], opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zlown/core/core.rb', line 63

def self.init_config_file(args = [], opts = {})
  config = Core.load_config(args, opts)

  cli = HighLine.new
  config[:upstream] = cli.ask('Upstream Interface?') { |q| q.default = config[:upstream] }
  config[:ap] = cli.ask('Wi-Fi ap Interface?') { |q| q.default = config[:ap] }
  config[:driver] = cli.ask('Wi-Fi Driver?') { |q| q.default = config[:driver] }
  config[:ssid] = cli.ask('Wi-Fi SSID?') { |q| q.default = config[:ssid] }
  config[:channel] = cli.ask('Wi-Fi Channel?') { |q| q.default = config[:channel] }

  puts "Writting config to #{Zlown::Config::CONFIG_FILE}"
  File.open(Zlown::Config::CONFIG_FILE, 'w') do |f|
    f.write config.to_yaml
  end

  config
end

.init_dirs(args = [], opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zlown/core/core.rb', line 35

def self.init_dirs(args = [], opts = {})
  unless File.directory?(Zlown::Config::APP_DIR)
    puts "Creating directory #{Zlown::Config::APP_DIR}"
    FileUtils.mkdir_p(Zlown::Config::APP_DIR)
  end

  unless File.directory?(Zlown::Config::DATA_DIR)
    puts "Creating directory #{Zlown::Config::DATA_DIR}"
    FileUtils.mkdir_p(Zlown::Config::DATA_DIR)
  end

  unless File.directory?(Zlown::Config::RUN_DIR)
    puts "Creating directory #{Zlown::Config::RUN_DIR}"
    FileUtils.mkdir_p(Zlown::Config::RUN_DIR)
  end
end

.init_dnsmaq(args = [], opts = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zlown/core/core.rb', line 101

def self.init_dnsmaq(args = [], opts = {})
  config = Core.load_config(args, opts)

  template = File.read(Zlown::Config::DNSMASQ_TEMPLATE)
  content = template.gsub('${IFACE_AP}', config[:ap])

  # To write changes to the file, use:
  File.open(Zlown::Config::DNSMASQ_CONFIG, 'w') do |file|
    puts "Writting file #{Zlown::Config::DNSMASQ_CONFIG}"
    file.puts content
  end
end

.init_hostapd(args = [], opts = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/zlown/core/core.rb', line 114

def self.init_hostapd(args = [], opts = {})
  # See https://www.offensive-security.com/kali-linux/kali-linux-evil-wireless-access-point/
  cmd = "sed -i 's#^DAEMON_CONF=.*#DAEMON_CONF=/etc/hostapd/hostapd.conf#' /etc/init.d/hostapd"
  puts cmd
  system cmd

  config = Core.load_config(args, opts)

  template = File.read(Zlown::Config::HOSTAPD_TEMPLATE)
  content = template
              .gsub('${IFACE}', config[:ap])
              .gsub('${DRIVER}', config[:driver])
              .gsub('${SSID}', config[:ssid])
              .gsub('${CHANNEL}', config[:channel])

  # To write changes to the file, use:
  File.open(Zlown::Config::HOSTAPD_CONFIG, 'w') do |file|
    puts "Writting file #{Zlown::Config::HOSTAPD_CONFIG}"
    file.puts content
  end
end

.init_rc_local(args = [], opts = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/zlown/core/core.rb', line 155

def self.init_rc_local(args = [], opts = {})
  Core.init_boot_script(args, opts)

  config = Core.load_config(args, opts)

  template = File.read(Zlown::Config::RCLOCAL_TEMPLATE)
  content = template

  # To write changes to the file, use:
  File.open(Zlown::Config::RCLOCAL_CONFIG, 'w') do |file|
    puts "Writting file #{Zlown::Config::RCLOCAL_CONFIG}"
    file.puts content
  end
end

.init_service_template(args = [], opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/zlown/core/core.rb', line 52

def self.init_service_template(args = [], opts = {})
  template = File.read(Zlown::Config::SERVICE_TEMPLATE)
  content = template.gsub('#{RUN_CMD}', Zlown::Config::RUN_CMD)

  # To write changes to the file, use:
  File.open(Zlown::Config::SERVICE_FILE, 'w') do |file|
    puts "Writting file #{Zlown::Config::SERVICE_FILE}"
    file.puts content
  end
end

.init_systemctl(args = [], opts = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/zlown/core/core.rb', line 81

def self.init_systemctl(args = [], opts = {})
  # TODO: Process dnsmasq.conf and hostapd.conf

  cmd = "systemctl enable #{Zlown::Config::HOSTAPD_SERVICE}"
  puts cmd
  system cmd

  cmd = "systemctl enable #{Zlown::Config::DNSMASQ_SERVICE}"
  puts cmd
  system cmd

  cmd = "systemctl start #{Zlown::Config::HOSTAPD_SERVICE}"
  puts cmd
  system cmd

  cmd = "systemctl start #{Zlown::Config::DNSMASQ_SERVICE}"
  puts cmd
  system cmd
end

.install(args = [], opts = {}) ⇒ Object



29
30
31
32
33
# File 'lib/zlown/core/core.rb', line 29

def self.install(args = [], opts = {})
  cmd = 'apt-get install -y hostapd dnsmasq wireless-tools iw wvdial'
  puts cmd
  system cmd
end

.load_config(args = [], opts = {}) ⇒ Object



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

def self.load_config(args = [], opts = {})
  config = {
    upstream: 'eth0',
    ap: 'wlan0',
    driver: 'nl80211',
    ssid: 'FreeWifi',
    channel: '6'
  }

  if File.exist?(Zlown::Config::CONFIG_FILE)
    config = config.merge(YAML.load(File.open(Zlown::Config::CONFIG_FILE)))
  end
end

.update_configs(args = [], opts = {}) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/zlown/core/core.rb', line 170

def self.update_configs(args = [], opts = {})
  Core.init_dnsmaq(args, opts)

  Core.init_hostapd(args, opts)

  Core.init_rc_local(args, opts)

  Core.init_systemctl(args, opts)
end