Skip to content

Commit 9858a01

Browse files
authored
Merging latest bits from devel (#22)
* Fixes some more interface handling bits * Fixes an MTU war between pfSense and wg-quick. wg-quick will now always honor pfSense for assigned interfaces, and only fall back to the default 1420 for unassigned interfaces * Syntax bug * Created a new global variable to generalize more things
1 parent dde6583 commit 9858a01

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
PORTNAME= pfSense-pkg-WireGuard
44
PORTVERSION= 0.0.5
5-
PORREVISION= 0
5+
PORREVISION= 1
66
CATEGORIES= net
77
MASTER_SITES= # empty
88
DISTFILES= # empty

src/files/usr/local/pkg/wireguard/wg.inc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function wg_delete_tunnel($tunidx) {
158158

159159
// Write new tunnel values to the configuration system
160160
function wg_do_post($post) {
161-
global $config;
161+
global $config, $wgg;
162162

163163
init_config_arr(array('installedpackages', 'wireguard', 'tunnel'));
164164

@@ -169,12 +169,22 @@ function wg_do_post($post) {
169169
$pconfig = &$config['installedpackages']['wireguard']['tunnel'][$index];
170170

171171
if (empty($pconfig['name'])) {
172+
172173
$pconfig['name'] = next_wg_if();
174+
173175
}
174-
$pconfig['enabled'] = empty($post['enabled']) ? 'no':'yes';
176+
177+
if (empty($pconfig['mtu'])) {
178+
179+
$pconfig['mtu'] = $wgg['default_mtu'];
180+
181+
}
182+
183+
$pconfig['enabled'] = empty($post['enabled']) ? 'no' : 'yes';
184+
175185
$pconfig['descr'] = $post['descr'];
176186

177-
// Interface section
187+
// Tunnel section
178188
$pconfig['interface']['address'] = $post['address'];
179189
$pconfig['interface']['listenport'] = $post['listenport'];
180190
$pconfig['interface']['privatekey'] = $post['privatekey'];
@@ -238,7 +248,6 @@ function wg_resync() {
238248
foreach ($wg_tunnels as $tunnel) {
239249

240250
if (isset($tunnel['enabled']) && $tunnel['enabled'] == 'yes') {
241-
242251
$is_assigned = is_wg_tunnel_assigned($tunnel);
243252

244253
wg_configure_if($tunnel, !($is_assigned));

src/files/usr/local/pkg/wireguard/wg_api.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ function genPSK() {
157157

158158
// Return the next available WireGuard port
159159
function next_wg_port() {
160-
global $config;
160+
global $config, $wgg;
161161

162162
init_config_arr(array('installedpackages', 'wireguard', 'tunnel'));
163163

164164
$tunnels = &$config['installedpackages']['wireguard']['tunnel'];
165165

166-
for ($idx=51820; $idx<65535; $idx++) {
166+
for ($idx=$wgg['default_port']; $idx<65535; $idx++) {
167167

168168
// Check to see if the port is already in use
169169
$found = false;
@@ -189,7 +189,7 @@ function next_wg_port() {
189189

190190
}
191191

192-
return 51820;
192+
return $wgg['default_port'];
193193

194194
}
195195

src/files/usr/local/pkg/wireguard/wg_globals.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $wgg = array(
3939
'if_group' => 'WireGuard',
4040
'ifgroupentry' => array('ifname' => 'WireGuard', 'descr' => 'Dynamic Group for WireGuard', 'members' => null),
4141
'default_mtu' => 1420,
42+
'default_port' => 51820,
4243

4344
'script_path' => '/usr/local/etc',
4445
'earlyshellcmds' => array('/usr/local/etc/rc.bootstrap_wireguard', '/usr/local/etc/rc.reload_wireguard'),

src/files/usr/local/www/wg/vpn_wg_edit.php

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,60 @@
5858
if ($_POST) {
5959

6060
if ($_POST['save']) {
61+
6162
if (empty($_POST['listenport'])) {
63+
6264
$_POST['listenport'] = next_wg_port();
65+
6366
}
64-
if (empty($_POST['mtu'])) {
65-
$_POST['mtu'] = $wgg['default_mtu'];
66-
}
67+
6768
$res = wg_do_post($_POST);
69+
6870
$input_errors = $res['input_errors'];
71+
6972
$pconfig = $res['pconfig'];
7073

7174
if (!$input_errors) {
75+
7276
// Create the new WG config files
7377
wg_create_config_files();
7478

75-
// Create interface group
79+
// Attempt to reinstall the interface group to keep things clean
7680
wg_ifgroup_install();
7781

78-
// Setup and start the new WG tunnel
79-
if (isset($pconfig['enabled']) &&
80-
($pconfig['enabled'] == 'yes')) {
81-
wg_configure_if($pconfig);
82+
// Configure the new WG tunnel
83+
if (isset($pconfig['enabled']) && $pconfig['enabled'] == 'yes') {
84+
85+
// Should we soft configure?
86+
$is_assigned = is_wg_tunnel_assigned($pconfig);
87+
88+
wg_configure_if($pconfig, !($is_assigned));
89+
8290
} else {
91+
8392
wg_destroy_if($pconfig);
93+
8494
}
8595

8696
// Go back to the tunnel table
8797
header("Location: /wg/vpn_wg.php");
98+
8899
}
100+
89101
} elseif ($_POST['action'] == 'genkeys') {
102+
90103
// Process ajax call requesting new key pair
91104
print(genKeyPair(true));
105+
92106
exit;
107+
93108
}
94109

95110
} else {
96111

97112
if (isset($index)) {
98113

99-
if ($tunnels[$index]) {
114+
if (is_array($tunnels[$index])) {
100115

101116
$pconfig = &$tunnels[$index];
102117
}
@@ -109,6 +124,9 @@
109124

110125
}
111126

127+
// Save the MTU settings prior to re(saving)
128+
$pconfig['mtu'] = get_interface_mtu($pconfig['name']);
129+
112130
}
113131

114132
$shortcut_section = "wireguard";
@@ -156,8 +174,17 @@
156174
if (is_wg_tunnel_assigned($pconfig)) {
157175

158176
$tun_enable->setDisabled();
177+
159178
$tun_enable->setHelp('<span class="text-danger">Note: </span>Tunnel cannot be <b>disabled</b> when assigned to an interface');
160179

180+
// We still want to POST this field, make a a hidden field now
181+
$section->addInput(new Form_Input(
182+
'enabled',
183+
'',
184+
'hidden',
185+
'yes'
186+
));
187+
161188
}
162189

163190
$section->addInput($tun_enable);
@@ -251,6 +278,14 @@
251278

252279
}
253280

281+
// We still need to keep track of this otherwise wg-quick and pfSense will fight
282+
$section->addInput(new Form_Input(
283+
'mtu',
284+
'',
285+
'hidden',
286+
$pconfig['mtu']
287+
));
288+
254289
$form->add($section);
255290

256291
print($form);

0 commit comments

Comments
 (0)