Page Menu
Home
VyOS Platform
Search
Configure Global Search
Log In
Files
F119062660
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
13 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/op-mode-definitions/wireguard.xml.in b/op-mode-definitions/wireguard.xml.in
index 1e9801252..0df838b50 100644
--- a/op-mode-definitions/wireguard.xml.in
+++ b/op-mode-definitions/wireguard.xml.in
@@ -1,169 +1,189 @@
<?xml version="1.0"?>
<!-- Wireguard key management -->
<interfaceDefinition>
<node name="generate">
<children>
<node name="wireguard">
<properties>
<help>Generate Wireguard keys</help>
</properties>
<children>
<leafNode name="default-keypair">
<properties>
<help>Generate the default Wireguard keypair</help>
</properties>
<command>sudo ${vyos_op_scripts_dir}/wireguard.py --genkey</command>
</leafNode>
<leafNode name="preshared-key">
<properties>
<help>Generate a Wireguard preshared key</help>
</properties>
<command>${vyos_op_scripts_dir}/wireguard.py --genpsk</command>
</leafNode>
<tagNode name="named-keypairs">
<properties>
<help>Generate specified Wireguard keypairs</help>
</properties>
<command>sudo ${vyos_op_scripts_dir}/wireguard.py --genkey --location "$4"</command>
</tagNode>
- <tagNode name="mobile-config">
+ <tagNode name="client-config">
<properties>
- <help>Generate QR code and movile configuration for Wireguard interface</help>
+ <help>Generate Client config QR code</help>
<completionHelp>
- <script>${vyos_completion_dir}/list_interfaces.py --type wireguard</script>
+ <list><client-name></list>
</completionHelp>
</properties>
<children>
- <tagNode name="server">
+ <tagNode name="interface">
<properties>
- <help>IP address or FQDN the client will connect to</help>
+ <help>Local interface used for connection</help>
<completionHelp>
- <script>${vyos_completion_dir}/list_local_ips.sh --both</script>
- <list><hostname></list>
+ <script>${vyos_completion_dir}/list_interfaces.py --type wireguard</script>
</completionHelp>
</properties>
- <command>sudo ${vyos_op_scripts_dir}/wireguard_client.py --interface "$4" --server "$6"</command>
<children>
- <tagNode name="address">
+ <tagNode name="server">
<properties>
- <help>IPv4/IPv6 address used on the client side</help>
+ <help>IP address/FQDN used for client connection</help>
<completionHelp>
<script>${vyos_completion_dir}/list_local_ips.sh --both</script>
<list><hostname></list>
</completionHelp>
</properties>
- <command>sudo ${vyos_op_scripts_dir}/wireguard_client.py --interface "$4" --server "$6" --address "$8"</command>
+ <command>sudo ${vyos_op_scripts_dir}/wireguard_client.py --name "$4" --interface "$6" --server "$8"</command>
+ <children>
+ <tagNode name="address">
+ <properties>
+ <help>IPv4/IPv6 address used by client</help>
+ <completionHelp>
+ <list><x.x.x.x> <h:h:h:h:h:h:h:h></list>
+ </completionHelp>
+ </properties>
+ <command>sudo ${vyos_op_scripts_dir}/wireguard_client.py --name "$4" --interface "$6" --server "$8" --address "${10}"</command>
+ <children>
+ <tagNode name="address">
+ <properties>
+ <help>IPv4/IPv6 address used by client</help>
+ <completionHelp>
+ <list><x.x.x.x> <h:h:h:h:h:h:h:h></list>
+ </completionHelp>
+ </properties>
+ <command>sudo ${vyos_op_scripts_dir}/wireguard_client.py --name "$4" --interface "$6" --server "$8" --address "${10}" --address "${12}"</command>
+ </tagNode>
+ </children>
+ </tagNode>
+ </children>
</tagNode>
</children>
</tagNode>
</children>
</tagNode>
</children>
</node>
</children>
</node>
<node name="show">
<children>
<node name="wireguard">
<properties>
<help>Show Wireguard properties</help>
</properties>
<children>
<node name="keypairs">
<properties>
<help>Show Wireguard keys</help>
</properties>
<children>
<tagNode name="pubkey">
<properties>
<help>Show specified Wireguard public key</help>
<completionHelp>
<script>${vyos_op_scripts_dir}/wireguard.py --listkdir</script>
</completionHelp>
</properties>
<command>${vyos_op_scripts_dir}/wireguard.py --showpub --location "$5"</command>
</tagNode>
<tagNode name="privkey">
<properties>
<help>Show specified Wireguard private key</help>
<completionHelp>
<script>${vyos_op_scripts_dir}/wireguard.py --listkdir</script>
</completionHelp>
</properties>
<command>${vyos_op_scripts_dir}/wireguard.py --showpriv --location "$5"</command>
</tagNode>
</children>
</node>
</children>
</node>
<node name="interfaces">
<children>
<tagNode name="wireguard">
<properties>
<help>Show Wireguard interface information</help>
<completionHelp>
<script>${vyos_completion_dir}/list_interfaces.py --type wireguard</script>
</completionHelp>
</properties>
<command>sudo ${vyos_op_scripts_dir}/wireguard.py --showinterface "$4"</command>
<children>
<leafNode name="allowed-ips">
<properties>
<help>Show all IP addresses allowed for the specified interface</help>
</properties>
<command>sudo wg show "$4" allowed-ips</command>
</leafNode>
<leafNode name="endpoints">
<properties>
<help>Show all endpoints for the specified interface</help>
</properties>
<command>sudo wg show "$4" endpoints</command>
</leafNode>
<leafNode name="peers">
<properties>
<help>Show all peer IDs for the specified interface</help>
</properties>
<command>sudo wg show "$4" peers</command>
</leafNode>
<!-- more commands upon request -->
</children>
</tagNode>
<node name="wireguard">
<properties>
<help>Show Wireguard interface information</help>
</properties>
<command>${vyos_op_scripts_dir}/show_interfaces.py --intf-type=wireguard --action=show-brief</command>
<children>
<leafNode name="detail">
<properties>
<help>Show detailed Wireguard interface information</help>
</properties>
<command>${vyos_op_scripts_dir}/show_interfaces.py --intf-type=wireguard --action=show</command>
</leafNode>
</children>
</node>
</children>
</node>
</children>
</node>
<node name="delete">
<children>
<node name="wireguard">
<properties>
<help>Delete Wireguard properties</help>
</properties>
<children>
<tagNode name="keypair">
<properties>
<help>Delete a Wireguard keypair</help>
<completionHelp>
<script>${vyos_op_scripts_dir}/wireguard.py --listkdir</script>
</completionHelp>
</properties>
<command>sudo ${vyos_op_scripts_dir}/wireguard.py --delkdir --location "$4"</command>
</tagNode>
</children>
</node>
</children>
</node>
</interfaceDefinition>
diff --git a/src/op_mode/wireguard_client.py b/src/op_mode/wireguard_client.py
index b25aac2c8..7a620a01e 100755
--- a/src/op_mode/wireguard_client.py
+++ b/src/op_mode/wireguard_client.py
@@ -1,92 +1,118 @@
#!/usr/bin/env python3
#
# Copyright (C) 2021 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import os
from jinja2 import Template
from ipaddress import ip_interface
+from vyos.ifconfig import Section
from vyos.template import is_ipv4
from vyos.template import is_ipv6
from vyos.util import cmd
from vyos.util import popen
if os.geteuid() != 0:
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
-tmpl = """
+server_config = """WireGuard client configuration for interface: {{ interface }}
+
+To enable this configuration on a VyOS router you can use the following commands:
+
+=== VyOS (server) configurtation ===
+
+{% for addr in address if address is defined %}
+set interfaces wireguard {{ interface }} peer {{ name }} allowed-ips '{{ addr }}'
+{% endfor %}
+set interfaces wireguard {{ interface }} peer {{ name }} pubkey '{{ pubkey }}'
+"""
+
+client_config = """
+=== RoadWarrior (client) configuration ===
+
[Interface]
PrivateKey = {{ privkey }}
{% if address is defined and address|length > 0 %}
Address = {{ address | join(', ')}}
{% endif %}
+DNS = 1.1.1.1
[Peer]
PublicKey = {{ system_pubkey }}
Endpoint = {{ server }}:{{ port }}
AllowedIPs = 0.0.0.0/0, ::/0
+
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser()
+ parser.add_argument("-n", "--name", type=str, help='WireGuard peer name', required=True)
parser.add_argument("-i", "--interface", type=str, help='WireGuard interface the client is connecting to', required=True)
parser.add_argument("-s", "--server", type=str, help='WireGuard server IPv4/IPv6 address or FQDN', required=True)
parser.add_argument("-a", "--address", type=str, help='WireGuard client IPv4/IPv6 address', action='append')
args = parser.parse_args()
interface = args.interface
+ if interface not in Section.interfaces('wireguard'):
+ exit(f'WireGuard interface "{interface}" does not exist!')
+
wg_pubkey = cmd(f'wg show {interface} | grep "public key"').split(':')[-1].lstrip()
wg_port = cmd(f'wg show {interface} | grep "listening port"').split(':')[-1].lstrip()
# Generate WireGuard private key
privkey,_ = popen('wg genkey')
# Generate public key portion from given private key
pubkey,_ = popen('wg pubkey', input=privkey)
config = {
+ 'name' : args.name,
+ 'interface' : interface,
'system_pubkey' : wg_pubkey,
'privkey': privkey,
'pubkey' : pubkey,
'server' : args.server,
'port' : wg_port,
'address' : [],
}
if args.address:
v4_addr = 0
v6_addr = 0
for tmp in args.address:
try:
- config['address'].append(str(ip_interface(tmp)))
+ ip = str(ip_interface(tmp).ip)
if is_ipv4(tmp):
+ config['address'].append(f'{ip}/32')
v4_addr += 1
elif is_ipv6(tmp):
+ config['address'].append(f'{ip}/128')
v6_addr += 1
except:
print(tmp)
exit('Client IP address invalid!')
if (v4_addr > 1) or (v6_addr > 1):
exit('Client can only have one IPv4 and one IPv6 address.')
- tmp = Template(tmpl, trim_blocks=True).render(config)
- qrcode,err = popen('qrencode -t ansiutf8', input=tmp)
-
- print(f'\nWireGuard client configuration for interface: {interface}')
- print(tmp)
- print('\n')
+ # Clear out terminal first
+ print('\x1b[2J\x1b[H')
+ server = Template(server_config, trim_blocks=True).render(config)
+ print(server)
+ client = Template(client_config, trim_blocks=True).render(config)
+ print(client)
+ qrcode,err = popen('qrencode -t ansiutf8', input=client)
print(qrcode)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Nov 6, 7:09 AM (1 d, 2 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4653007
Default Alt Text
(13 KB)
Attached To
Mode
rVYOSONEX vyos-1x
Attached
Detach File
Event Timeline
Log In to Comment