|
| 1 | +const http = require('http'); |
| 2 | +const url = require('url'); |
| 3 | +const mime = require('mime'); |
| 4 | +const path = require('path'); |
| 5 | +const fs = require('fs'); |
| 6 | + |
| 7 | +function get_ip (req) { |
| 8 | + let ip = null; |
| 9 | + if (req.headers['x-forwarded-for']) { |
| 10 | + ip = req.headers['x-forwarded-for'].split(",")[0]; |
| 11 | + } else if (req.connection && req.connection.remoteAddress) { |
| 12 | + ip = req.connection.remoteAddress; |
| 13 | + } else { |
| 14 | + ip = req.ip; |
| 15 | + } |
| 16 | + return ip; |
| 17 | +} |
| 18 | + |
| 19 | +function process_app_import(req, res, api, appname, appfiles, rename) { |
| 20 | + function random_tmp_name() { |
| 21 | + return 'tmp-' + Math.random(); |
| 22 | + } |
| 23 | + function download_file(api, appname, appfiles, index, tmpdir, cb) { |
| 24 | + if (index >= appfiles.length) { |
| 25 | + cb(); |
| 26 | + return; |
| 27 | + } |
| 28 | + if (!appfiles[index]) { |
| 29 | + download_file(api, appname, appfiles, index+1, tmpdir, cb); |
| 30 | + return; |
| 31 | + } |
| 32 | + let filename = appfiles[index]; |
| 33 | + let tmpfile = path.join(tmpdir, filename); |
| 34 | + let subtmpdir = path.dirname(tmpfile); |
| 35 | + if (!fs.existsSync(subtmpdir)) { |
| 36 | + fs.mkdirSync(subtmpdir); |
| 37 | + } |
| 38 | + let file = fs.createWriteStream(tmpfile); |
| 39 | + let request = http.get(api + '/download/' + appname + '/' + filename, (obj) => { |
| 40 | + obj.pipe(file); |
| 41 | + download_file(api, appname, appfiles, index+1, tmpdir, cb); |
| 42 | + }).on('error', (e) => { |
| 43 | + errors.push('failed to download: ' + appfiles[index]); |
| 44 | + download_file(api, appname, appfiles, index+1, tmpdir, cb); |
| 45 | + }); |
| 46 | + } |
| 47 | + let errors = []; |
| 48 | + let tmpdir = path.join(Storage.work_dir, random_tmp_name()); |
| 49 | + let targetdir = path.join(Storage.work_dir, rename); |
| 50 | + if (fs.existsSync(targetdir)) { |
| 51 | + res.end('app exists: ' + rename); |
| 52 | + return; |
| 53 | + } |
| 54 | + fs.mkdirSync(tmpdir); |
| 55 | + download_file(api, appname, appfiles, 0, tmpdir, () => { |
| 56 | + if (errors.length > 0) { |
| 57 | + Storage.rmtree(tmpdir); |
| 58 | + res.end(errors.join('\n')); |
| 59 | + return; |
| 60 | + } |
| 61 | + fs.renameSync(tmpdir, targetdir); |
| 62 | + res.end(''); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function route(req, res) { |
| 67 | + let r = url.parse(req.url); |
| 68 | + let f = router; |
| 69 | + let path = r.pathname.split('/'); |
| 70 | + let query = {}; |
| 71 | + r.query && r.query.split('&').forEach((one) => { |
| 72 | + let key, val; |
| 73 | + let i = one.indexOf('='); |
| 74 | + if (i < 0) { |
| 75 | + key = one; |
| 76 | + val = ''; |
| 77 | + } else { |
| 78 | + key = one.substring(0, i); |
| 79 | + val = one.substring(i+1); |
| 80 | + } |
| 81 | + if (key in query) { |
| 82 | + if(Array.isArray(query[key])) { |
| 83 | + query[key].push(val); |
| 84 | + } else { |
| 85 | + query[key] = [query[key], val]; |
| 86 | + } |
| 87 | + } else { |
| 88 | + query[key] = val; |
| 89 | + } |
| 90 | + }); |
| 91 | + path.shift(); |
| 92 | + while (path.length > 0) { |
| 93 | + let key = path.shift(); |
| 94 | + f = f[key]; |
| 95 | + if (!f) break; |
| 96 | + if (typeof(f) === 'function') { |
| 97 | + return f(req, res, { |
| 98 | + path: path, |
| 99 | + query: query |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + router.static(req, res, r.pathname); |
| 104 | + // router.code(req, res, 404, 'Not Found'); |
| 105 | +} |
| 106 | + |
| 107 | +const Storage = { |
| 108 | + work_dir: path.dirname(__dirname), |
| 109 | + list_directories: (dir) => { |
| 110 | + return fs.readdirSync(dir).filter((name) => { |
| 111 | + let subdir = path.join(dir, name); |
| 112 | + let state = fs.lstatSync(subdir); |
| 113 | + return state.isDirectory(); |
| 114 | + }); |
| 115 | + }, |
| 116 | + list_files: (dir) => { |
| 117 | + let queue = [dir], list = []; |
| 118 | + while (queue.length > 0) { |
| 119 | + list_dir(queue.shift(), queue, list); |
| 120 | + } |
| 121 | + return list; |
| 122 | + |
| 123 | + function list_dir(dir, queue, list) { |
| 124 | + fs.readdirSync(dir).forEach((name) => { |
| 125 | + let filename = path.join(dir, name); |
| 126 | + let state = fs.lstatSync(filename); |
| 127 | + if (state.isDirectory()) { |
| 128 | + queue.push(filename); |
| 129 | + } else { |
| 130 | + list.push(filename); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + }, |
| 135 | + rmtree: (dir) => { |
| 136 | + if (dir.length < Storage.work_dir.length) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + if (dir.indexOf(Storage.work_dir) !== 0) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + if (!fs.existsSync(dir)) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + fs.readdirSync(dir).forEach(function(file, index){ |
| 146 | + var curPath = path.join(dir, file); |
| 147 | + if (fs.lstatSync(curPath).isDirectory()) { |
| 148 | + // recurse |
| 149 | + rmtree(curPath); |
| 150 | + } else { // delete file |
| 151 | + fs.unlinkSync(curPath); |
| 152 | + } |
| 153 | + }); |
| 154 | + fs.rmdirSync(dir); |
| 155 | + return true; |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +const router = { |
| 160 | + app: { |
| 161 | + list: (req, res, options) => { |
| 162 | + let dir = path.dirname(__dirname); |
| 163 | + if (!fs.existsSync(dir)) { |
| 164 | + return router.code(req, res, 404, 'Not Found'); |
| 165 | + } |
| 166 | + let names = Storage.list_directories(Storage.work_dir).filter((name) => { |
| 167 | + if (name.startsWith('.')) return false; |
| 168 | + if (name === 'node_modules') return false; |
| 169 | + return true; |
| 170 | + }); |
| 171 | + res.end(names.join('\n')); |
| 172 | + }, |
| 173 | + files: (req, res, options) => { |
| 174 | + let name = options.path[0]; |
| 175 | + if (!name) { |
| 176 | + return router.code(req, res, 404, 'Not Found'); |
| 177 | + } |
| 178 | + let subdir = path.join(Storage.work_dir, name); |
| 179 | + if (!fs.existsSync(subdir)) { |
| 180 | + return router.code(req, res, 404, 'Not Found'); |
| 181 | + } |
| 182 | + let files = Storage.list_files(subdir).map((filename) => { |
| 183 | + return filename.substring(subdir.length+1); |
| 184 | + }); |
| 185 | + res.end(files.join('\n')); |
| 186 | + }, |
| 187 | + download: (req, res, options) => { |
| 188 | + if (options.path.indexOf('..') >= 0) { |
| 189 | + return router.code(req, res, 404, 'Not Found'); |
| 190 | + } |
| 191 | + let filename = path.join(...options.path); |
| 192 | + filename = path.join(Storage.work_dir, filename); |
| 193 | + if (!fs.existsSync(filename)) { |
| 194 | + return router.code(req, res, 404, 'Not Found'); |
| 195 | + } |
| 196 | + res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(filename)); |
| 197 | + //res.setHeader('Content-Type', 'application/octet-stream'); |
| 198 | + res.setHeader('Content-Type', 'text/plain'); |
| 199 | + let buf = fs.readFileSync(filename); |
| 200 | + res.end(buf, 'binary'); |
| 201 | + }, |
| 202 | + delete: (req, res, options) => { |
| 203 | + let name = options.path[0]; |
| 204 | + if (!name) { |
| 205 | + return router.code(req, res, 404, 'Not Found'); |
| 206 | + } |
| 207 | + let filename = path.join(Storage.work_dir, name); |
| 208 | + // !!!! dangerous action |
| 209 | + Storage.rmtree(filename); |
| 210 | + res.end(''); |
| 211 | + }, |
| 212 | + import: (req, res, options) => { |
| 213 | + // options.path [ip:port, appname, rename] |
| 214 | + let host = options.path[0]; |
| 215 | + let appname = options.path[1]; |
| 216 | + let name = options.path[2] || appname; |
| 217 | + let api = 'http://' + host + '/app'; |
| 218 | + http.get(api + '/files/' + appname, (obj) => { |
| 219 | + if (~~(obj.statusCode/100) !== 2) { |
| 220 | + obj.resume(); |
| 221 | + return router.code(req, res, 404, 'Not Found'); |
| 222 | + } |
| 223 | + let raw = ''; |
| 224 | + obj.on('data', (chunk) => { raw += chunk; }); |
| 225 | + obj.on('end', () => { |
| 226 | + process_app_import(req, res, api, appname, raw.split('\n'), name); |
| 227 | + }); |
| 228 | + }).on('error', (e) => { |
| 229 | + return router.code(req, res, 404, 'Not Found'); |
| 230 | + }); |
| 231 | + } |
| 232 | + }, |
| 233 | + test: (req, res, options) => { |
| 234 | + res.end('hello'); |
| 235 | + }, |
| 236 | + static: (req, res, filename) => { |
| 237 | + if (!filename || filename === '/') { |
| 238 | + filename = 'index.html'; |
| 239 | + } |
| 240 | + filename = filename.split('/'); |
| 241 | + if (!filename[0]) filename.shift(); |
| 242 | + if (filename.length === 0 || filename.indexOf('..') >= 0) { |
| 243 | + return router.code(req, res, 404, 'Not Found'); |
| 244 | + } |
| 245 | + filename = path.join(__dirname, 'static', ...filename); |
| 246 | + if (!fs.existsSync(filename)) { |
| 247 | + return router.code(req, res, 404, 'Not Found'); |
| 248 | + } |
| 249 | + res.setHeader('Content-Type', mime.lookup(filename)); |
| 250 | + let buf = fs.readFileSync(filename); |
| 251 | + res.end(buf, 'binary'); |
| 252 | + }, |
| 253 | + code: (req, res, code, text) => { |
| 254 | + res.writeHead(code || 404, text || ''); |
| 255 | + res.end(); |
| 256 | + } |
| 257 | +}; |
| 258 | + |
| 259 | +init(); |
| 260 | + |
| 261 | +const server = http.createServer((req, res) => { |
| 262 | + route(req, res); |
| 263 | +}); |
| 264 | + |
| 265 | +const instance = server.listen(20180, '0.0.0.0', () => { |
| 266 | + console.log(instance.address()); |
| 267 | + console.log(`NodeBase Application Manager is listening at 0.0.0.0:20180`); |
| 268 | +}); |
0 commit comments