From 8bcce7defe2a289fe215148dc02794a02a1499ba Mon Sep 17 00:00:00 2001 From: Julien Vaubourg Date: Tue, 29 Sep 2015 00:17:23 +0200 Subject: [PATCH] Fix #24 --- sources/controller.php | 185 +++++++++++++------ sources/public/css/style.css | 9 + sources/public/js/custom.js | 13 ++ sources/views/settings.html.php | 308 +++++++++++++++++--------------- 4 files changed, 322 insertions(+), 193 deletions(-) diff --git a/sources/controller.php b/sources/controller.php index 8921c8a..17d92d2 100644 --- a/sources/controller.php +++ b/sources/controller.php @@ -64,6 +64,33 @@ function ipv6_compressed($ip) { return $output[0]; } +function readAutoConf($file) { + $json = file_get_contents($file); + $config = json_decode($json, true); + + if(!empty($config['crt_server_ca'])) { + $config['crt_server_ca'] = str_replace('|', "\n", $config['crt_server_ca']); + } + + if(!empty($config['crt_client'])) { + $config['crt_client'] = str_replace('|', "\n", $config['crt_client']); + } + + if(!empty($config['crt_client_key'])) { + $config['crt_client_key'] = str_replace('|', "\n", $config['crt_client_key']); + } + + if(!empty($config['crt_client_ta'])) { + $config['crt_client_ta'] = str_replace('|', "\n", $config['crt_client_ta']); + } + + if(!empty($config['openvpn_add'])) { + $config['openvpn_add'] = str_replace('|', "\n", $config['openvpn_add']); + } + + return $config; +} + dispatch('/', function() { $ip6_net = ynh_setting_get('ip6_net'); $ip6_net = ($ip6_net == 'none') ? '' : $ip6_net; @@ -89,47 +116,56 @@ dispatch('/', function() { }); dispatch_put('/settings', function() { - $crt_client_exists = file_exists('/etc/openvpn/keys/user.crt'); - $crt_client_key_exists = file_exists('/etc/openvpn/keys/user.key'); - $crt_server_ca_exists = file_exists('/etc/openvpn/keys/ca-server.crt'); - $service_enabled = isset($_POST['service_enabled']) ? 1 : 0; - $ip6_net = empty($_POST['ip6_net']) ? 'none' : $_POST['ip6_net']; - $ip6_addr = 'none'; if($service_enabled == 1) { + $crt_client_exists = file_exists('/etc/openvpn/keys/user.crt'); + $crt_client_key_exists = file_exists('/etc/openvpn/keys/user.key'); + $crt_server_ca_exists = file_exists('/etc/openvpn/keys/ca-server.crt'); + + $config = $_POST; + $autoconf = false; + + if($_FILES['cubefile']['error'] == UPLOAD_ERR_OK) { + $config = readAutoConf($_FILES['cubefile']['tmp_name']); + $autoconf = true; + } + $ip6_net = empty($config['ip6_net']) ? 'none' : $config['ip6_net']; + $ip6_addr = 'none'; + try { - if(empty($_POST['server_name']) || empty($_POST['server_port']) || empty($_POST['server_proto'])) { + if(empty($config['server_name']) || empty($config['server_port']) || empty($config['server_proto'])) { throw new Exception(_('The Server Address, the Server Port and the Protocol cannot be empty')); } - if(!preg_match('/^\d+$/', $_POST['server_port'])) { + if(!preg_match('/^\d+$/', $config['server_port'])) { throw new Exception(_('The Server Port must be only composed of digits')); } - if($_POST['server_proto'] != 'udp' && $_POST['server_proto'] != 'tcp') { + if($config['server_proto'] != 'udp' && $config['server_proto'] != 'tcp') { throw new Exception(_('The Protocol must be "udp" or "tcp"')); } - if(empty($_POST['dns0']) || empty($_POST['dns1'])) { + if(empty($config['dns0']) || empty($config['dns1'])) { throw new Exception(_('You need to define two DNS resolver addresses')); } - - if(($_FILES['crt_client']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1)) - || ($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client']['error'] != UPLOAD_ERR_OK && (!$crt_client_exists || $_POST['crt_client_delete'] == 1))) { - - throw new Exception(_('A Client Certificate is needed when you suggest a Key, or vice versa')); - } - - if(empty($_POST['login_user']) xor empty($_POST['login_passphrase'])) { + + if(empty($config['login_user']) xor empty($config['login_passphrase'])) { throw new Exception(_('A Password is needed when you suggest a Username, or vice versa')); } - - if($_FILES['crt_server_ca']['error'] != UPLOAD_ERR_OK && !$crt_server_ca_exists) { + + if((!$autoconf && (($_FILES['crt_client']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1)) + || ($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK && $_FILES['crt_client']['error'] != UPLOAD_ERR_OK && (!$crt_client_exists || $_POST['crt_client_delete'] == 1)))) + || ($autoconf && (empty($config['crt_client']) xor empty($config['crt_client_key'])))) { + + throw new Exception(_('A Client Certificate is needed when you suggest a Key, or vice versa')); + } + + if((!$autoconf && $_FILES['crt_server_ca']['error'] != UPLOAD_ERR_OK && !$crt_server_ca_exists) || ($autoconf && empty($config['crt_server_ca']))) { throw new Exception(_('You need a Server CA.')); } - - if(($_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1)) && empty($_POST['login_user'])) { + + if(((!$autoconf && $_FILES['crt_client_key']['error'] != UPLOAD_ERR_OK && (!$crt_client_key_exists || $_POST['crt_client_key_delete'] == 1)) || ($autoconf && empty($config['crt_client_key']))) && empty($config['login_user'])) { throw new Exception(_('You need either a Client Certificate, either a Username, or both')); } @@ -158,42 +194,87 @@ dispatch_put('/settings', function() { ynh_setting_set('service_enabled', $service_enabled); if($service_enabled == 1) { - ynh_setting_set('server_name', $_POST['server_name']); - ynh_setting_set('server_port', $_POST['server_port']); - ynh_setting_set('server_proto', $_POST['server_proto']); - ynh_setting_set('dns0', $_POST['dns0']); - ynh_setting_set('dns1', $_POST['dns1']); - ynh_setting_set('login_user', $_POST['login_user']); - ynh_setting_set('login_passphrase', $_POST['login_passphrase']); + ynh_setting_set('server_name', $config['server_name']); + ynh_setting_set('server_port', $config['server_port']); + ynh_setting_set('server_proto', $config['server_proto']); + ynh_setting_set('dns0', $config['dns0']); + ynh_setting_set('dns1', $config['dns1']); + ynh_setting_set('login_user', $config['login_user']); + ynh_setting_set('login_passphrase', $config['login_passphrase']); ynh_setting_set('ip6_net', $ip6_net); ynh_setting_set('ip6_addr', $ip6_addr); - - file_put_contents('/etc/openvpn/client.conf.tpl', $_POST['raw_openvpn']); - if($_FILES['crt_client']['error'] == UPLOAD_ERR_OK) { - move_uploaded_file($_FILES['crt_client']['tmp_name'], '/etc/openvpn/keys/user.crt'); - } elseif($_POST['crt_client_delete'] == 1) { - unlink('/etc/openvpn/keys/user.crt'); - } - - if($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK) { - move_uploaded_file($_FILES['crt_client_key']['tmp_name'], '/etc/openvpn/keys/user.key'); - } elseif($_POST['crt_client_key_delete'] == 1) { - unlink('/etc/openvpn/keys/user.key'); - } + if($autoconf) { + if(!empty($config['openvpn_add'])) { + copy('/etc/openvpn/client.conf.tpl.restore', '/etc/openvpn/client.conf.tpl'); - if($_FILES['crt_client_ta']['error'] == UPLOAD_ERR_OK) { - move_uploaded_file($_FILES['crt_client_ta']['tmp_name'], '/etc/openvpn/keys/user_ta.key'); - } elseif($_POST['crt_client_ta_delete'] == 1) { - unlink('/etc/openvpn/keys/user_ta.key'); + $raw_openvpn = file_get_contents('/etc/openvpn/client.conf.tpl'); + $raw_openvpn .= "\n# Custom\n".$config['openvpn_add']; + + file_put_contents('/etc/openvpn/client.conf.tpl', $raw_openvpn); + } + + if(empty($config['crt_client'])) { + if(file_exists('/etc/openvpn/keys/user.crt')) { + unlink('/etc/openvpn/keys/user.crt'); + } + } else { + file_put_contents('/etc/openvpn/keys/user.crt', $config['crt_client']); + } + + if(empty($config['crt_client_key'])) { + if(file_exists('/etc/openvpn/keys/user.key')) { + unlink('/etc/openvpn/keys/user.key'); + } + } else { + file_put_contents('/etc/openvpn/keys/user.key', $config['crt_client_key']); + } + + if(empty($config['crt_client_ta'])) { + if(file_exists('/etc/openvpn/keys/user_ta.key')) { + unlink('/etc/openvpn/keys/user_ta.key'); + } + } else { + file_put_contents('/etc/openvpn/keys/user_ta.key', $config['crt_client_ta']); + } + + if(empty($config['crt_server_ca'])) { + if(file_exists('/etc/openvpn/keys/ca-server.crt')) { + unlink('/etc/openvpn/keys/ca-server.crt'); + } + } else { + file_put_contents('/etc/openvpn/keys/ca-server.crt', $config['crt_server_ca']); + } + + } else { + + file_put_contents('/etc/openvpn/client.conf.tpl', $_POST['raw_openvpn']); + + if($_FILES['crt_client']['error'] == UPLOAD_ERR_OK) { + move_uploaded_file($_FILES['crt_client']['tmp_name'], '/etc/openvpn/keys/user.crt'); + } elseif($_POST['crt_client_delete'] == 1) { + unlink('/etc/openvpn/keys/user.crt'); + } + + if($_FILES['crt_client_key']['error'] == UPLOAD_ERR_OK) { + move_uploaded_file($_FILES['crt_client_key']['tmp_name'], '/etc/openvpn/keys/user.key'); + } elseif($_POST['crt_client_key_delete'] == 1) { + unlink('/etc/openvpn/keys/user.key'); + } + + if($_FILES['crt_client_ta']['error'] == UPLOAD_ERR_OK) { + move_uploaded_file($_FILES['crt_client_ta']['tmp_name'], '/etc/openvpn/keys/user_ta.key'); + } elseif($_POST['crt_client_ta_delete'] == 1) { + unlink('/etc/openvpn/keys/user_ta.key'); + } + + if($_FILES['crt_server_ca']['error'] == UPLOAD_ERR_OK) { + move_uploaded_file($_FILES['crt_server_ca']['tmp_name'], '/etc/openvpn/keys/ca-server.crt'); + } } - if($_FILES['crt_server_ca']['error'] == UPLOAD_ERR_OK) { - move_uploaded_file($_FILES['crt_server_ca']['tmp_name'], '/etc/openvpn/keys/ca-server.crt'); - } - - if(!empty($_POST['login_user'])) { - file_put_contents('/etc/openvpn/keys/credentials', "${_POST['login_user']}\n${_POST['login_passphrase']}"); + if(!empty($config['login_user'])) { + file_put_contents('/etc/openvpn/keys/credentials', "${config['login_user']}\n${config['login_passphrase']}"); } else { file_put_contents('/etc/openvpn/keys/credentials', ''); } diff --git a/sources/public/css/style.css b/sources/public/css/style.css index 059d2ab..7cd8a02 100644 --- a/sources/public/css/style.css +++ b/sources/public/css/style.css @@ -96,3 +96,12 @@ textarea#raw_openvpn { height: 300px; border: 1px solid #D9534F; } + +ul.nav { + margin-top: 30px; + margin-bottom: 20px; +} + +ul.nav a { + outline: none; +} diff --git a/sources/public/js/custom.js b/sources/public/js/custom.js index 3e33836..c918ab6 100644 --- a/sources/public/js/custom.js +++ b/sources/public/js/custom.js @@ -16,11 +16,24 @@ * along with this program. If not, see . */ +function tabsClick() { + var tab = $(this).parent().attr('data-tab'); + + $('.nav').find('li.active').removeClass('active'); + $(this).parent().addClass('active'); + + $('.tabs').hide(); + $('.tab' + tab).show(); + + return false; +} + $(document).ready(function() { $('.btn-group').button(); $('[data-toggle="tooltip"]').tooltip(); $('.switch').bootstrapToggle(); + $('.nav-tabs a').click(tabsClick); $('.fileinput').click(function() { if(!$(this).hasClass('btn-danger')) { diff --git a/sources/views/settings.html.php b/sources/views/settings.html.php index 97a5113..e5936a8 100644 --- a/sources/views/settings.html.php +++ b/sources/views/settings.html.php @@ -55,167 +55,193 @@ -
> -
-

-
+ -
-
- -
- -
-
- -
- -
- -
-
- -
- -
- - - -
+
> +
+
+

-
- -
- -
-
- -
- -
- -
-
- -
- -
-
-
-
-
-
- - -
style="margin: 2px 0px 17px" role="alert"> - - : -
- - -
> -
-

-
- -
-
- -