return @free_addresses;
}
-sub generate_net_configuration($$) {
+sub generate_peer_configuration($$) {
my $key = shift;
my $private_key = shift;
+ my @conf = ();
+
# Load the peer
my %peer = &load_peer($key);
# Fetch the endpoint
my $endpoint = &get_endpoint();
- # Derive our own public key
- my $public_key = &derive_public_key($peer{'PRIVATE_KEY'});
-
- my @conf = (
- "[Interface]",
- "PrivateKey = $private_key",
- "Port = $peer{'ENDPOINT_PORT'}",
- "",
- "[Peer]",
- "Endpoint = ${endpoint}:$peer{'PORT'}",
- "PublicKey = $public_key",
- "PresharedKey = $peer{'PSK'}",
- "AllowedIPs = " . join(", ", @allowed_ips),
- "PersistentKeepalive = $peer{'KEEPALIVE'}",
- );
-
- return join("\n", @conf);
-}
-
-sub generate_host_configuration($) {
- my $key = shift;
- my $private_key = shift;
-
- # Load the peer
- my %peer = &load_peer($key);
-
- # Return if we could not find the peer
- return undef unless (%peer);
-
- # Return if this is not a roadwarrior peer
- return undef unless ($peer{'TYPE'} eq 'host');
-
- my @allowed_ips = ();
-
- # Convert all subnets into CIDR notation
- foreach my $subnet ($peer{'LOCAL_SUBNETS'}) {
- my $netaddress = &Network::get_netaddress($subnet);
- my $prefix = &Network::get_prefix($subnet);
-
- # Skip invalid subnets
- next if (!defined $netaddress || !defined $prefix);
-
- push(@allowed_ips, "${netaddress}/${prefix}");
- }
-
- # Fetch the endpoint
- my $endpoint = &get_endpoint();
-
- my $port = $settings{'PORT'};
-
- # Fetch any DNS servers for hosts
- my @dns = split(/\|/, $settings{'CLIENT_DNS'});
-
- my @conf = (
- "[Interface]",
- "PrivateKey = $private_key",
- "Address = $peer{'CLIENT_ADDRESS'}",
- );
+ # Net-2-Net
+ if ($peer{'TYPE'} eq "net") {
+ # Derive our own public key
+ my $public_key = &derive_public_key($peer{'PRIVATE_KEY'});
+
+ push(@conf,
+ "[Interface]",
+ "PrivateKey = $private_key",
+ "Port = $peer{'ENDPOINT_PORT'}",
+ "",
+ "[Peer]",
+ "Endpoint = ${endpoint}:$peer{'PORT'}",
+ "PublicKey = $public_key",
+ "PresharedKey = $peer{'PSK'}",
+ "AllowedIPs = " . join(", ", @allowed_ips),
+ "PersistentKeepalive = $peer{'KEEPALIVE'}",
+ );
+
+ # Host-2-Net
+ } elsif ($peer{'TYPE'} eq "host") {
+ # Fetch any DNS servers for hosts
+ my @dns = split(/\|/, $settings{'CLIENT_DNS'});
+
+ push(@conf,
+ "[Interface]",
+ "PrivateKey = $private_key",
+ "Address = $peer{'CLIENT_ADDRESS'}",
+ );
+
+ # Optionally add DNS servers
+ if (scalar @dns) {
+ push(@conf, "DNS = " . join(", ", @dns));
+ }
- # Optionally add DNS servers
- if (scalar @dns) {
- push(@conf, "DNS = " . join(", ", @dns));
+ # Finish the [Interface] section
+ push(@conf, "");
+
+ # Add peer configuration
+ push(@conf, (
+ "[Peer]",
+ "Endpoint = ${endpoint}:$settings{'PORT'}",
+ "PublicKey = $settings{'PUBLIC_KEY'}",
+ "PresharedKey = $peer{'PSK'}",
+ "AllowedIPs = " . join(", ", @allowed_ips),
+ "PersistentKeepalive = $DEFAULT_KEEPALIVE",
+ ));
}
- # Finish the [Interface] section
- push(@conf, "");
-
- # Add peer configuration
- push(@conf, (
- "[Peer]",
- "Endpoint = ${endpoint}:${port}",
- "PublicKey = $settings{'PUBLIC_KEY'}",
- "PresharedKey = $peer{'PSK'}",
- "AllowedIPs = " . join(", ", @allowed_ips),
- "PersistentKeepalive = $DEFAULT_KEEPALIVE",
- ));
-
return join("\n", @conf);
}
&Header::openpage($Lang::tr{'wireguard'}, 1, '');
# Generate the client configuration
- my $config = &Wireguard::generate_net_configuration($key, $remote_private_key);
+ my $config = &Wireguard::generate_peer_configuration($key, $remote_private_key);
# Encode the configuration as Base64
$config = &MIME::Base64::encode_base64($config);
my $filename = &Header::normalize($peer{'NAME'}) . ".conf";
# Generate the client configuration
- my $config = &Wireguard::generate_host_configuration($key);
+ my $config = &Wireguard::generate_peer_configuration($key);
# Send the configuration
if (defined $config) {
my %peer = &Wireguard::load_peer($key);
# Generate the client configuration
- my $config = &Wireguard::generate_host_configuration($key, $private_key);
+ my $config = &Wireguard::generate_peer_configuration($key, $private_key);
# Create a QR code generator
my $qrgen = Imager::QRCode->new(