Class: WireGuard::Admin::Templates::Server

Inherits:
ERB
  • Object
show all
Defined in:
lib/wire_guard/admin/templates/server.rb

Overview

Configuration template for a WireGuard::Admin::Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server:, network:, clients:) ⇒ Server

Returns a new instance of Server.



35
36
37
38
39
40
# File 'lib/wire_guard/admin/templates/server.rb', line 35

def initialize(server:, network:, clients:)
  @server = server
  @network = network
  @clients = clients
  super(self.class.template, trim_mode: '<>')
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



33
34
35
# File 'lib/wire_guard/admin/templates/server.rb', line 33

def clients
  @clients
end

#networkObject (readonly)

Returns the value of attribute network.



33
34
35
# File 'lib/wire_guard/admin/templates/server.rb', line 33

def network
  @network
end

#serverObject (readonly)

Returns the value of attribute server.



33
34
35
# File 'lib/wire_guard/admin/templates/server.rb', line 33

def server
  @server
end

Class Method Details

.templateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wire_guard/admin/templates/server.rb', line 12

def self.template
  <<~SERVER_TEMPLATE
    # WireGuard configuration for <%= server.name %>
    # generated by wg-admin

    [Interface]
    Address = <%= server.ip %>/<%= network.prefix %>
    ListenPort = <%= server.port %>
    PrivateKey = <%= server.private_key %>
    # PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o <%= server.device %> -j MASQUERADE
    # PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o <%= server.device %> -j MASQUERADE
    <% clients.each do |client| %>

    [Peer]
    # Name = <%= client.name %>
    PublicKey = <%= client.public_key %>
    AllowedIPs = <%= client.ip %>/<%= client.ip.prefix %>
    <% end %>
  SERVER_TEMPLATE
end

Instance Method Details

#renderObject



42
43
44
# File 'lib/wire_guard/admin/templates/server.rb', line 42

def render
  result(binding)
end