diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/HttpServer.hx | 31 | ||||
| -rw-r--r-- | src/server/Utils.hx | 4 |
2 files changed, 33 insertions, 2 deletions
diff --git a/src/server/HttpServer.hx b/src/server/HttpServer.hx index ec6ddda..0fc46fd 100644 --- a/src/server/HttpServer.hx +++ b/src/server/HttpServer.hx @@ -4,6 +4,9 @@ import sys.FileSystem; import js.node.Buffer; import haxe.io.Path; import js.node.Fs; +import js.node.Https; +import js.node.Http; +import js.node.Url; import js.node.http.IncomingMessage; import js.node.http.ServerResponse; import js.node.Path as JsPath; @@ -60,6 +63,11 @@ class HttpServer { return; } + if (url.startsWith("/proxy")) { + if (!proxyUrl(req, res)) res.end('Cannot proxy ${req.url}'); + return; + } + if (hasCustomRes) { final path = customDir + url; if (Fs.existsSync(path)) { @@ -126,6 +134,29 @@ class HttpServer { return data; } + static function proxyUrl(req:IncomingMessage, res:ServerResponse):Bool { + final url = req.url.replace("/proxy?url=", ""); + final url = Url.parse(js.Node.global.decodeURI(url)); + if (url.host == req.headers["host"]) return false; + final options = { + host: url.host, + port: Std.parseInt(url.port), + path: url.path, + method: req.method, + // headers: req.headers + }; + final request = url.protocol == "https:" ? Https.request : Http.request; + final proxy = request(options, proxyRes -> { + res.writeHead(proxyRes.statusCode, proxyRes.headers); + proxyRes.pipe(res, {end: true}); + }); + proxy.on("error", err -> { + res.end('Proxy error for ${url.href}'); + }); + req.pipe(proxy, {end: true}); + return true; + } + static function isChildOf(parent:String, child:String):Bool { final rel = JsPath.relative(parent, child); return rel.length > 0 && !rel.startsWith('..') && !JsPath.isAbsolute(rel); diff --git a/src/server/Utils.hx b/src/server/Utils.hx index 218184d..ebe928b 100644 --- a/src/server/Utils.hx +++ b/src/server/Utils.hx @@ -1,12 +1,12 @@ package server; -import js.node.Http; +import js.node.Https; import js.node.Os; class Utils { public static function getGlobalIp(callback:(ip:String)->Void):Void { - Http.get("http://myexternalip.com/raw", r -> { + Https.get("https://myexternalip.com/raw", r -> { r.setEncoding("utf8"); r.on("data", callback); }).on("error", e -> { |
