class WireGuard::Admin::Client

A host that connects to the VPN and registers a VPN subnet address such as 192.0.2.3 for itself.

@see github.com/pirate/wireguard-docs#peernodedevice

Attributes

ip[R]
name[R]
private_key[R]
public_key[R]

Public Class Methods

new(name:, ip:, private_key: nil) click to toggle source
# File lib/wire_guard/admin/client.rb, line 27
def initialize(name:, ip:, private_key: nil)
  raise ArgumentError, 'name must be present' if name.nil?
  raise ArgumentError, 'name must not be empty' if name.empty?
  raise ArgumentError, 'ip must be present' if ip.nil?
  raise ArgumentError, 'private_key must not be empty' if private_key&.empty?

  @name = name
  @ip = ip
  self.private_key = private_key || generate_private_key
end

Public Instance Methods

==(other) click to toggle source
# File lib/wire_guard/admin/client.rb, line 55
def ==(other)
  name == if other.respond_to?(:name)
            other.name
          else
            other
          end
end
eql?(other) click to toggle source
# File lib/wire_guard/admin/client.rb, line 51
def eql?(other)
  hash == other.hash
end
hash() click to toggle source
# File lib/wire_guard/admin/client.rb, line 47
def hash
  name.hash
end
private_key=(private_key) click to toggle source
# File lib/wire_guard/admin/client.rb, line 38
def private_key=(private_key)
  @private_key = private_key
  @public_key = generate_public_key
end
to_s() click to toggle source
# File lib/wire_guard/admin/client.rb, line 43
def to_s
  "#{self.class.name.split('::').last} #{name}: #{ip}"
end

Private Instance Methods

generate_private_key() click to toggle source
# File lib/wire_guard/admin/client.rb, line 79
def generate_private_key
  Open3.popen3('wg genkey') do |_, stdout, stderr, waiter|
    raise InvocationError, stderr.lines unless waiter.value.success?

    stdout.read.chomp
  end
rescue SystemCallError => e
  raise ProgramNotFoundError if e.message =~ /No such file or directory/

  raise
end
generate_public_key() click to toggle source
# File lib/wire_guard/admin/client.rb, line 65
def generate_public_key
  Open3.popen3('wg pubkey') do |stdin, stdout, stderr, waiter|
    stdin.write(private_key)
    stdin.close
    raise InvocationError, stderr.read.lines unless waiter.value.success?

    stdout.read.chomp
  end
rescue SystemCallError => e
  raise ProgramNotFoundError if e.message =~ /No such file or directory/

  raise
end