-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.php
201 lines (173 loc) · 5.54 KB
/
index.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
<?php
/**
* Handle VPN-Key-Exchange request for Freifunk-Franken.
*
* @author RedDog <[email protected]>
* @author delphiN <[email protected]>
* @author Mose <[email protected]>
*
* @license https://www.gnu.org/licenses/agpl-3.0.txt AGPL-3.0
*/
const DEFAULT_HOOD_ID = 0;
const INVALID_MAC = 'AAAAAAAAAAAA';
const DEBUG = false;
/**
* Singelton DB instance
*/
class db{
private static $instance = NULL;
private function __construct(){
}
public static function getInstance(){
if(!self::$instance){
require('config.inc.php');
self::$instance = new PDO('mysql:host='.$mysql_server.';dbname='.$mysql_db,$mysql_user,$mysql_pass);
self::$instance->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
private function __clone(){
}
}
/**
* returns details error msg (as json)
*
* @param integer $code
* HTTP error 400, 500 or 503
* @param string $msg
* Error message text
*/
function showError($code,$msg){
if($code == 400)
header('HTTP/1.0 400 Bad Request');
elseif($code == 500)
header('HTTP/1.0 500 Internal Server Error');
elseif($code == 503)
header('HTTP/1.0 503 Service Unavailable');
header('Content-Type: application/json');
$errorObject = array('error'=>array('msg'=>$msg,'url'=>'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
print_r(json_encode($errorObject));
}
function debug($msg){
if(DEBUG)
print_r($msg."\n");
}
// ----------------------------------------------------------------------------
// get parameters and initialice settings
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'])
$ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
if(isset($_GET['mac']) && $_GET['mac'])
$mac = $_GET ['mac'];
else
$mac = INVALID_MAC;
if(isset($_GET['name']) && $_GET['name'])
$name = $_GET ['name'];
if(isset($_GET['key']) && $_GET['key'])
$key = $_GET ['key'];
if(isset($_GET['port']) && $_GET['port'])
$port = $_GET ['port'];
else
$port = 10000;
$hood = DEFAULT_HOOD_ID;
// insert or update the current node in the database
if(isset($ip) && $ip && isset($name) && $name && isset($key) && $key) {
if(!preg_match('/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$/',$name))
exit(showError(400,'invalid name'));
if($mac != INVALID_MAC)
$sql = 'SELECT * FROM nodes WHERE mac=:mac;';
else
$sql = 'SELECT * FROM nodes WHERE (mac=\'000000000000\' OR mac=\''.INVALID_MAC.'\') AND (name=:name);';
try{
$rs = db::getInstance()->prepare($sql);
if($mac != INVALID_MAC)
$rs->bindParam(':mac',$mac);
else
$rs->bindParam(':name',$name);
$rs->execute ();
}
catch(PDOException $e) {
exit(showError(500,$e));
}
if($rs->rowCount() > 1)
exit(showError(500,'To much nodes with mac='.$mac.', name='.$name));
if($rs->rowCount() == 1){
$result = $rs->fetch(PDO::FETCH_ASSOC);
$hood = $result['hood_ID'];
if (!$result['readonly'] && !$result['notrain']) {
$updateHood=false;
if (!$result['isgateway']) {
if ($result['hood_ID'] != 0) {
$updateHood=true;
$hood = 0;
}
}
if ($updateHood)
$sql = 'UPDATE nodes SET ip=:ip, mac=:mac, name=:name, `key`=:key, port=:port, timestamp=CURRENT_TIMESTAMP, hood_ID=:hood WHERE ID=:id';
else
$sql = 'UPDATE nodes SET ip=:ip, mac=:mac, name=:name, `key`=:key, port=:port, timestamp=CURRENT_TIMESTAMP WHERE ID=:id';
try{
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':id',$result['ID'],PDO::PARAM_INT);
$rs->bindParam(':ip',$ip);
$rs->bindParam(':mac',$mac);
$rs->bindParam(':name',$name);
$rs->bindParam(':key',$key);
$rs->bindParam(':port',$port);
if ($updateHood)
$rs->bindParam(':hood',$hood);
$rs->execute();
}
catch(PDOException $e) {
exit(showError(500,$e));
}
}
}
else{
$sql = 'INSERT INTO nodes(ip,mac,name,`key`,port,readonly,isgateway,hood_ID) VALUES (:ip,:mac,:name,:key,:port,0,0,:hood);';
try{
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':ip',$ip);
$rs->bindParam(':mac',$mac);
$rs->bindParam(':name',$name);
$rs->bindParam(':key',$key);
$rs->bindParam(':port',$port);
$rs->bindParam(':hood',$hood);
$rs->execute ();
}
catch(PDOException $e) {
exit(showError(500,$e));
}
}
}
// return either all nodes (if gateway) or all gateways (if node) from the hood
try{
if (isset($result) && is_array($result) && $result['isgateway'])
$sql = 'SELECT * FROM nodes WHERE hood_ID=:hood;';
else
$sql = 'SELECT * FROM nodes WHERE hood_ID=:hood AND isgateway=\'1\';';
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':hood',$hood);
$rs->execute();
}
catch(PDOException $e) {
exit(showError(500,$e));
}
// return results in a easy parsable way
if($rs->rowCount() > 0){
while($result = $rs->fetch(PDO::FETCH_ASSOC)){
$filename = $result['mac'];
if($filename == INVALID_MAC || $filename == '000000000000')
$filename = $result['name'];
echo '####'.$filename.".conf\n";
echo '#name "'.$result['name']."\";\n";
echo 'key "'.$result['key']."\";\n";
if(preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/",$result['ip']))
echo 'remote ipv4 "'.$result['ip'].'" port '.$result['port']." float;\n";
else
echo 'remote ipv6 "'.$result['ip'].'" port '.$result['port']." float;\n";
echo "\n";
}
echo "###\n";
}
// vim: expandtab:sw=2
?>