forked from MikeTsenatek/fff_extendFileName
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extendFileName.php
235 lines (202 loc) · 6.23 KB
/
extendFileName.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
<?php
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// regex for filename verification
define('FILE_REGEX', '/(fff-[\w.]+|openwrt|franken-[\w.]+)-.*\.bin(\.md5|\.sha256)?/');
function extractBoard($filename) {
// remove everything other than boardname
$search = ['/(.*)-generic-/', '/(.*)-tiny-/', '/(.*)-g-/', '/(.*)-t-/', '/fff-([^-]*)-([^-]*)-/'];
$replace = '';
$filename = preg_replace($search, $replace, $filename);
$search = ['/-squashfs(.*)/', '/-sysupgrade(.*)/'];
$replace = '';
$filename = preg_replace($search, $replace, $filename);
return $filename;
}
function extractVariant($filename) {
if (strpos($filename, 'layer3') !== false) {
return 'layer3';
} else {
return 'node';
}
}
/*
* Extract version from release.nfo file in "current" directory for requested variant
*/
function getServerVersion($variant) {
$releaseFile = "$variant/current/release.nfo";
if (file_exists($releaseFile)) {
$version = explode(':', file_get_contents($releaseFile));
return trim($version[1]);
} else {
trigger_error($releaseFile . " not found. Firmware version on server unknown.", E_USER_ERROR);
}
}
/*
* Outputs a 404 error page
*/
function return404($filename, $reason="") {
http_response_code(404);
if ($reason != "") {
$reason = "<p>Reason: $reason.</p>";
}
echo <<<EOL
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested file {$filename} was not found on this server.</p>
{$reason}
</body></html>
EOL;
exit;
}
/*
* Outputs the file given in arguemnt
*/
function returnFile($filename, $oldfilename) {
$filecontent = file_get_contents($filename);
// replace filename in sha256 and md5 sum
if (strpos($filename, '.sha256') !== false || strpos($filename, '.md5') !== false) {
$shasum = strtok($filecontent, ' ');
if ($shasum === false) {
return;
}
// remove .sha256 or .md5
$filename = preg_replace(array('/\.sha256/', '/\.md5/'), array(''), $oldfilename);
if ($filename === false) {
return;
}
$filecontent = "$shasum $filename" . "\n";
}
header('Content-disposition: attachment; filename=' . $oldfilename);
header('Content-type: application/octet-stream');
header('Content-Length: ' . strlen($filecontent));
header("Pragma: no-cache");
header("Expires: 0");
echo $filecontent;
}
function rewriteBoard($board) {
$search = ['/-wr841n-/i', '/-cpe210-220-510-520-/i'];
$replace = ['-wr841-', '-cpe210-220-'];
$board = preg_replace($search, $replace, $board);
switch ($board) {
case "gl-ar150":
return "glinet_" . $board;
case "cpe210-v2":
case "cpe210-v3":
case "archer-c25-1":
case "archer-c60-v1":
case "archer-c60-v2":
case "archer-c7-v2":
case "archer-c7-v5":
case "tl-mr3020-v1":
case "tl-wa850re-v1":
case "tl-wa860re-v1":
case "tl-wa901nd-v2":
case "tl-wdr3500-v1":
case "tl-wdr3600-v1":
case "tl-wdr4300-v1":
case "tl-wdr4310-v1":
case "tl-wdr4900-v1":
case "tl-wr1043n-v5":
case "tl-wr1043nd-v1":
case "tl-wr1043nd-v2":
case "tl-wr1043nd-v3":
case "tl-wr1043nd-v4":
case "tl-wr1043n-v5":
case "tl-wr740n-v4":
case "tl-wr741nd-v2":
case "tl-wr741nd-v4":
case "tl-wr841-v7":
case "tl-wr841-v8":
case "tl-wr841-v9":
case "tl-wr841-v10":
case "tl-wr841-v11":
case "tl-wr841-v12":
case "tl-wr841n-v13":
case "tl-wr842n-v2":
return "tplink_" . $board;
case "tplink_c50-v3":
return "tplink_archer-c50-v3";
case "tplink_c50-v4":
return "tplink_archer-c50-v4";
case "ubnt-bullet-m":
return "ubnt_bullet-m";
case "ubnt-pico-m":
return "ubnt_picostation-m";
case "ubnt-loco-m":
return "ubnt_nanostation-loco-m";
case "ubnt-loco-m-xw":
return "ubnt_nanostation-loco-m-xw";
case "ubnt-nano-m":
return "ubnt_nanostation-m";
case "ubnt-unifi":
return "ubnt_unifi";
case "ubnt-unifiac-lite":
# TODO: only ac-mesh. not distinguishable -> return error
return "ubnt_unifiac";
case "ubnt-erx":
return "ubnt_edgerouter-x";
case "ubnt-erx-sfp":
return "ubnt_edgerouter-x-sfp";
case "r6220":
return "netgear_r6220";
default:
return $board;
}
}
if ($_GET["file"] === 'release.nfo') {
returnFile('node/current/release.nfo', 'release.nfo');
exit;
}
$oldfilename = filter_input(INPUT_GET, 'file', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => FILE_REGEX)));
//$oldfilename = $argv[1];
if (empty($oldfilename)) {
return404($oldfilename, "Filename doesn't match verification pattern");
}
# extract wanted variant and boardname from filename
$variant = extractVariant($oldfilename);
$board = extractBoard($oldfilename);
# account for changed boardnames
$board = rewriteBoard($board);
# get most recent firmware version for selected variant
$version = getServerVersion($variant);
# generate new filename
$newfilename = "$variant/current/fff-$version-$board-sysupgrade.bin";
# workaround for x86 images
if (strpos($newfilename, "x86_64") != false) {
$search = ['/x86_64-sysupgrade.bin/i'];
$replace = ['x86-64-generic-combined-efi.img.gz'];
$newfilename = preg_replace($search, $replace, $newfilename);
}
# workaround for .tar images
if (strpos($newfilename, "ubnt_edgerouter-4") != false) {
$search = ['/sysupgrade.bin/i'];
$replace = ['sysupgrade.tar'];
$newfilename = preg_replace($search, $replace, $newfilename);
}
if (!file_exists($newfilename)) {
return404($oldfilename, "Can't find file with rewritten name: $newfilename");
}
# append correct extension to filename if checksum file was requested
if (substr($oldfilename, -4) === '.md5') {
$newfilename = $newfilename . '.md5';
} else if (substr($oldfilename, -7) === '.sha256') {
$newfilename = $newfilename . '.sha256';
}
returnFile($newfilename, $oldfilename);
exit;