Class: Trocla::Formats::Wireguard

Inherits:
Base
  • Object
show all
Defined in:
lib/trocla/formats/wireguard.rb

Instance Attribute Summary

Attributes inherited from Base

#trocla

Instance Method Summary collapse

Methods inherited from Base

expensive, #expensive?, expensive?, #initialize

Constructor Details

This class inherits a constructor from Trocla::Formats::Base

Instance Method Details

#format(plain_password, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/trocla/formats/wireguard.rb', line 7

def format(plain_password, options={})
  return YAML.safe_load(plain_password) if plain_password.match(/---/)

  wg_priv = nil
  wg_pub = nil
  begin
    Open3.popen3('wg genkey') do |_stdin, stdout, _stderr, _waiter|
      wg_priv = stdout.read.chomp
    end
  rescue SystemCallError => e
    raise 'trocla wireguard: wg binary not found' if e.message =~ /No such file or directory/

    raise "trocla wireguard: #{e.message}"
  end

  begin
    Open3.popen3('wg pubkey') do |stdin, stdout, _stderr, _waiter|
      stdin.write(wg_priv)
      stdin.close

      wg_pub = stdout.read.chomp
    end
  rescue SystemCallError => e
    raise "trocla wireguard: #{e.message}"
  end
  YAML.dump({ 'wg_priv' => wg_priv, 'wg_pub' => wg_pub })
end

#render(output, render_options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/trocla/formats/wireguard.rb', line 35

def render(output, render_options = {})
  data = YAML.safe_load(output)
  if render_options['privonly']
    data['wg_priv']
  elsif render_options['pubonly']
    data['wg_pub']
  else
    'pub: ' + data['wg_pub'] + "\npriv: " + data['wg_priv']
  end
end