-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.php
executable file
·252 lines (226 loc) · 9.17 KB
/
setup.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
//include the file with constant definitions
require('defines.php');
function generateAppId() {
$chars = "0123456789abcdef";
$val = '';
//repeat 32 times
for( $i=0; $i<32; $i++ ) {
//add a random character
$val .= $chars[rand(0, strlen($chars) - 1)];
}
return $val;
}
function getCredentials($ipaddress, $psk) {
//generate a random id with hexadecimal characters.
//That's what IKEA also seem to use in the app
$identity = generateAppId();
//construct the command we'll want to execute to query our credentials
$cmd = "coap-client -u 'Client_identity' -k '$psk' -m post -e '{\"" . CLIENT_IDENTITY_PROPOSED . "\": \"$identity\"}' 'coaps://$ipaddress:5684/" . GATEWAY . "/" . AUTH_PATH . "'";
//open the process
$process = proc_open($cmd, [STDOUT => ['pipe', 'w'], STDERR => ['pipe', 'w']], $output);
//read the outputs
$stdout = stream_get_contents($output[STDOUT]);
$stderr = stream_get_contents($output[STDERR]);
//clean up and properly close our handles
fclose($output[STDOUT]);
fclose($output[STDERR]);
$rc = proc_close($process);
if($rc > 0) {
//the command failed to be executed, abort
errorCommandFailed($rc, $stderr);
return;
}
//if we did read something from STDERR, discard the first line
//since that should be information from coap-client about the connection
if($stderr !== false) {
$lineEnd = strpos($stderr, PHP_EOL);
if($lineEnd > -1) {
$stderr = trim(substr($stderr, $lineEnd + strlen(PHP_EOL)));
}
}
//if we read something from STDOUT cut off any blank space/newlines
if($stdout !== false) {
$stdout = trim($stdout);
}
//did we get a 4.00 error?
if($stderr == "4.00") {//Bad Request
//This identity has been used before, so posting it again is considered a Bad Request.
//Though considering the type of random id we're using chances for this should be pretty low.
//Retry with a new identity.
return getCredentials($ipaddress, $psk);
} else if ($stderr !== "") {//is there a message in this line?
//not sure what error we got, just tell the user
errorUnknownResponse($stderr);
return;
}
//if we didn't get anything on STDERR and neither on STDOUT then
//coap-client must've been unable to reach the gateway with this address and key
if($stdout === "") {
errorGatewayUnreachable();
return;
}
//we should've received json from the gateway, parse that
$response = json_decode($stdout, true);
//finally return our result
return Array(
'identity' => $identity,
'key' => $response[NEW_PSK_BY_GW]
);
}
function saveCredentials($ipaddress, $identity, $newKey) {
//open the config file for writing
$file = fopen('./config.php', 'w');
//did it work?
if($file === false) {
//tell the user something went wrong
errorConfigFileOpen();
return;
}
//put the necessary data together into a string
$contents = "<?php" . PHP_EOL;
$contents .= "\$gw_address = '$ipaddress';" . PHP_EOL;
$contents .= "\$gw_user = '$identity';" . PHP_EOL;
$contents .= "\$gw_key = '$newKey';" . PHP_EOL;
//write it out
$success = fwrite($file, $contents);
//close the file, we're done
fclose($file);
//did writing fail (or didn't it write more than 0 bytes)?
if($success === false or $success <= 0) { //frwite failed
errorConfigFileWrite();
return;
}
//spread the word we succeeded
successConfigFileWrite();
}
//main HTML content
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Setup</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Setup</h1>
<p>
In order to use these scripts a config file with user credentials needs to be created. <br />
To simplify this process it can be performed with this page.
</p>
<p>In the fields below, enter the IP address of your gateway and the key printed on the bottom of your gateway.</p>
<?php
if(!isset($_POST['ipaddress']) or !isset($_POST['key'])) { //got no post data
?>
<form method="post" action="">
<div class="form-group">
<label for="ipaddress">IP Address</label>
<input type="text" value="" placeholder="IP Address" name="ipaddress" id="ipaddress" class="form-control" required="true">
</div>
<div class="form-group">
<label for="key">Gateway key</label>
<input type="text" value="" placeholder="Gateway key" name="key" id="key" class="form-control" required="true">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
} else { //got post data
$credentials = getCredentials($_POST['ipaddress'], $_POST['key']);
if($credentials != null)
saveCredentials($_POST['ipaddress'], $credentials['identity'], $credentials['key']);
}
?>
</div>
</body>
</html>
<?php
//HTML templates for error/success messages
function errorCommandFailed($rc, $stderr) {
?>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-warning-sign"></span> Error</h3>
</div>
<div class="panel-body">
<p>The command <code>coap-client</code> couldn't be executed. Check that you have it installed and that DTLS support is enabled. For convenience I recommend this script: <a href="https://github.com/ggravlingen/pytradfri/blob/master/script/install-coap-client.sh">install-coap-client.sh</a></p>
<p><samp>
Exit Code: <?= $rc ?><br />
<?= $stderr ?>
</samp></p>
<p><a href="javascript:window.history.back();"><span class="glyphicon glyphicon-repeat"></span> Retry</a></p>
</div>
</div>
<?php
}
function errorUnknownResponse($errorCode) {
?>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-warning-sign"></span> Error</h3>
</div>
<div class="panel-body">
<p>
Recieved an unkwown response from the gateway:<br />
<samp><?= $errorCode ?></samp>
</p>
<p><a href="javascript:window.history.back();"><span class="glyphicon glyphicon-repeat"></span> Retry</a></p>
</div>
</div>
<?php
}
function errorGatewayUnreachable() {
?>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-warning-sign"></span> Error</h3>
</div>
<div class="panel-body">
<p>Failed to connect to the gateway.</p>
<p>Check that the IP address and the key you entered are correct.</p>
<p><a href="javascript:window.history.back();"><span class="glyphicon glyphicon-repeat"></span> Retry</a></p>
</div>
</div>
<?php
}
function errorConfigFileOpen() {
?>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-warning-sign"></span> Error</h3>
</div>
<div class="panel-body">
<p>Failed to open config.php for writing.</p>
<p>Check that you have permissions.</p>
<p><a href="javascript:window.history.back();"><span class="glyphicon glyphicon-repeat"></span> Retry</a></p>
</div>
</div>
<?php
}
function errorConfigFileWrite() {
?>
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-warning-sign"></span> Error</h3>
</div>
<div class="panel-body">
<p>Failed to write to config.php.</p>
<p>Check that enough space is available.</p>
<p><a href="javascript:window.history.back();"><span class="glyphicon glyphicon-repeat"></span> Retry</a></p>
</div>
</div>
<?php
}
function successConfigFileWrite() {
?>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><span class="glyphicon glyphicon-ok-sign"></span> Success</h3>
</div>
<div class="panel-body">
<p>The configuration file has been written.</p>
</div>
</div>
<?php
}