-
Notifications
You must be signed in to change notification settings - Fork 1
/
openvpnc.php
222 lines (193 loc) · 8.77 KB
/
openvpnc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
session_start();
if(!isset($_SESSION['login']) || $_SESSION['login'] != true)
{
header("location: login.php");
exit;
}
$errmsg = $okmsg = "";
if(!is_dir("/etc/openvpn/client"))
{
$do = `sudo mkdir -p "/etc/openvpn/client"`;
}
if(isset($_POST['remove']))
{
$do = `sudo rm -f "/etc/openvpn/client/client1.key"`;
$do = `sudo rm -f "/etc/openvpn/client/client1.ovpn"`;
$do = `sudo rm -f "/etc/openvpn/client/client1.active"`;
}
if(isset($_POST['tbUpload']))
{
$ovpn = trim(file_get_contents($_FILES['ovpnfile']['tmp_name']));
$ca = trim(file_get_contents($_FILES['CAfile']['tmp_name']));
$cert = trim(file_get_contents($_FILES['Certfile']['tmp_name']));
$key = trim(file_get_contents($_FILES['Key']['tmp_name']));
list($crud, $cert) = explode("-----BEGIN CERTIFICATE-----", $cert, 2);
$cert = "-----BEGIN CERTIFICATE-----".$cert;
list($start, $end) = explode("auth-user-pass", $ovpn, 2);
$ovpn = $start."auth-user-pass /etc/openvpn/client/client1.key".$end;
$ovpn .= "\n<ca>\n$ca\n</ca>\n<cert>\n$cert\n</cert>\n<key>\n$key\n</key>";
$ovpn = escapeshellarg(trim($ovpn));
$auth = escapeshellarg(trim($_REQUEST['username'])."\n".trim($_REQUEST['passphrase']));
$do = `echo -n $auth | sudo tee '/etc/openvpn/client/client1.key'`;
$do = `echo -n $ovpn | sudo tee '/etc/openvpn/client/client1.ovpn'`;
restartOVPN();
}
if(isset($_POST['button']))
{
$do = `sudo killall -TERM openvpn`;
if(isset($_FILES['file']['tmp_name']) && $_FILES['file']['tmp_name'] != "")
{
$file = escapeshellarg($_FILES['file']['tmp_name']);
$do = `sudo mv $file "/etc/openvpn/client/client1.ovpn"`;
$do = `sudo chown -R root: "/etc/openvpn/"`;
if(isset($_POST['passphrase']) || strpos($ovpn, "-----BEGIN ENCRYPTED PRIVATE KEY-----") !== false)
{
$passphrase = substr(escapeshellarg(trim($_POST['passphrase'])), 1, -1);
$ovpn = trim(`sudo cat "/etc/openvpn/client/client1.ovpn"`);
if(strpos($ovpn, "-----BEGIN ENCRYPTED PRIVATE KEY-----") !== false)
{
list($start, $rest) = explode("<key>", $ovpn, 2);
list($key, $rest) = explode("</key>", $rest, 2);
$start = trim($start);
$key = trim($key);
$rest = trim($rest);
$fn1 = tempnam("/tmp", "ovpn");
$fn2 = tempnam("/tmp", "ovpn");
$fp = fopen($fn1, "w");
fputs($fp, $key);
fclose($fp);
$cmd = "openssl rsa -in '$fn1' -out '$fn2' -passin 'pass:$passphrase'";
$do = `$cmd`;
$key = trim(`sudo cat '$fn2'`);
if($key != "")
{
$ovpn = $start."\n<key>\n$key\n</key>\n$rest";
$do = `echo "$ovpn" | sudo tee "/etc/openvpn/client/client1.ovpn"`;
} else {
$errmsg = "Invalid passphrase supplied, please check your passphrase and retry uploading your ovpn file.";
$do = `sudo rm -f "/etc/openvpn/client/client1.ovpn"`;
$do = `sudo rm -f "/etc/openvpn/client/client1.active"`;
}
unlink($fn1);
unlink($fn2);
}
}
if((!isset($_POST['passphrase']) || $_POST['passphrase'] == "") && strpos($ovpn, "-----BEGIN ENCRYPTED PRIVATE KEY-----") !== false)
{
$errmsg = "ovpn file requires a passphrase, but you didn't supply one.";
$do = `sudo rm -f "/etc/openvpn/client/client1.ovpn"`;
$do = `sudo rm -f "/etc/openvpn/client/client1.active"`;
}
} else {
$errmsg = "No ovpn file was uploaded, can't continue";
$do = `sudo rm -f "/etc/openvpn/client/client1.ovpn"`;
$do = `sudo rm -f "/etc/openvpn/client/client1.active"`;
}
if($errmsg == "")
{
if(file_exists("/etc/openvpn/client/client1.active"))
{
restartOVPN();
$okmsg = ".ovpn file was successfully imported and openvpn was successfully started";
} else {
$okmsg = ".ovpn file was successfully imported";
}
} else {
$do = `sudo rm -f "/etc/openvpn/client/client1.active"`;
$do = `sudo rm -f "/etc/openvpn/client/client1.ovpn"`;
}
}
if(isset($_POST['update']))
{
if(isset($_POST['enableCli']))
{
$do = `sudo touch "/etc/openvpn/client/client1.active"`;
restartOVPN();
$okmsg = "openvpn was successfully started";
} else {
$do = `sudo rm -f "/etc/openvpn/client/client1.active"`;
$do = `sudo killall openvpn`;
sleep(4);
$okmsg = "openvpn was successfully stopped";
}
}
function restartOVPN()
{
if(intval(trim(`ps auxww|grep -v grep|grep openvpn|wc -l`)) > 0)
{
$do = `sudo killall openvpn`;
$do = `sudo killall openvpn`;
$do = `sudo killall openvpn`;
sleep(1);
}
$do = `sudo /usr/sbin/openvpn --verb 6 --config "/etc/openvpn/client/client1.ovpn" --daemon`;
sleep(5);
}
$enableCli = "no";
if(file_exists("/etc/openvpn/client/client1.active"))
$enableCli = "yes";
$tunnel = "Down";
if(trim(`sudo ifconfig tun0|grep UP 2>&1`) != "")
$tunnel = "Up";
$page = 9;
$pageTitle = "OpenVPN Settings";
include_once("header.php");
?>
<div id="page-wrapper">
<div id="page-inner">
<div class="row" style="padding-right:15px;padding-left:15px;">
<div class="col-md-12">
<h2><?=$pageTitle?></h2>
</div>
</div>
<hr />
<div class="row" style="padding-right:15px;padding-left:15px;">
<?php if($errmsg != "") { ?>
<p><div class="alert alert-warning alert-dismissable"><?=$errmsg?><button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button></div></p>
<?php } ?>
<?php if($okmsg != "") { ?>
<p><div class="alert alert-success alert-dismissable"><?=$okmsg?><button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button></div></p>
<?php } ?>
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<div style="width:120px;float:left">Select .ovpn File:</div>
<input type="file" style="width:220px;float:left;margin-left:20px;" class="form-control" name="file" /><br style="clear:left;"/>
<div style="width:120px;float:left">.ovpn Passphrase:</div>
<input type="password" style="width:220px;float:left;margin-left:20px;" class="form-control" name="passphrase" placeholder="Enter the Passphrase" /><br style="clear:left;"/>
<input type="submit" class="btn btn-primary" name="button" value="Upload File" />
<?php if(file_exists("/etc/openvpn/client/client1.ovpn")) { ?>
<input type="submit" class="btn btn-primary" name="remove" value="Remove Files" />
<?php } ?>
</form>
<hr />
<form enctype="multipart/form-data" action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<div style="width:120px;float:left">Select .ovpn File:</div>
<input type="file" style="width:220px;float:left;margin-left:20px;" class="form-control" name="ovpnfile" /><br style="clear:left;"/>
<div style="width:120px;float:left">Select CA File:</div>
<input type="file" style="width:220px;float:left;margin-left:20px;" class="form-control" name="CAfile" /><br style="clear:left;"/>
<div style="width:120px;float:left">Select Cert File:</div>
<input type="file" style="width:220px;float:left;margin-left:20px;" class="form-control" name="Certfile" /><br style="clear:left;"/>
<div style="width:120px;float:left">Select Private File:</div>
<input type="file" style="width:220px;float:left;margin-left:20px;" class="form-control" name="Key" /><br style="clear:left;"/>
<div style="width:120px;float:left">VPN Username:</div>
<input type="text" style="width:220px;float:left;margin-left:20px;" class="form-control" name="username" placeholder="Enter the Username" /><br style="clear:left;"/>
<div style="width:120px;float:left">VPN Passphrase:</div>
<input type="text" style="width:220px;float:left;margin-left:20px;" class="form-control" name="passphrase" placeholder="Enter the Passphrase" /><br style="clear:left;"/>
<input type="submit" class="btn btn-primary" name="tbUpload" value="Upload Files" />
<?php if(file_exists("/etc/openvpn/client/client1.ovpn")) { ?>
<input type="submit" class="btn btn-primary" name="remove" value="Remove Files" />
<?php } ?>
</form>
<hr/>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<div style="width:120px;float:left">Tunnel is:</div><?=$tunnel?><br style="clear:left;"/>
<div style="width:120px;float:left">Enable:</div>
<input type="checkbox" style="width:25px;float:left;" class="form-control" name="enableCli" value="yes"<?php if($enableCli == "yes") { echo " checked"; } ?>/><br style="clear:left;"/>
<input type="submit" class="btn btn-primary" name="update" value="Update" />
</form>
</div>
</div>
</div>
<?php include_once("footer.php"); ?>