diff --git a/package.json b/package.json index 6a31831..b962afd 100644 --- a/package.json +++ b/package.json @@ -6,15 +6,16 @@ "scripts": { "test": "jest", "start": "node app.js", - "publish": "node ./scripts/publish.js" + "publish": "node ./scripts/publish.js" }, "author": "ShinyLight", "license": "ISC", "dependencies": { "copy-dir": "^1.3.0", + "http-string-parser": "^0.0.6", "jest": "^26.6.3", "moment": "^2.29.1", "sqlite3": "^5.0.0", "sqlite3-helper": "^2.0.1" - } -} \ No newline at end of file + } +} diff --git a/proxy/proxy.handlers.js b/proxy/proxy.handlers.js index 76f04bd..e50d70a 100644 --- a/proxy/proxy.handlers.js +++ b/proxy/proxy.handlers.js @@ -11,6 +11,21 @@ exports.BlockRequest = BlockRequest; exports.AllowSecureRequest = AllowSecureRequest; exports.AllowUnsecureRequest = AllowUnsecureRequest; exports.RunForEachConnection = RunForEachConnection; +exports.IsNormalInteger = IsNormalInteger; + + + +/** + * Checks to see if it's a positive integer. + * + * @param {*} str + * @returns + */ +function IsNormalInteger(str) { + return /^\+?(0|[1-9]\d*)$/.test(str); +} + + /** * Parses buffer data into Json object. @@ -36,12 +51,12 @@ function GetDetailsFromBuffer(buffer, isOnWhiteList) { // ALWAYS INCLUDE INDEX 0 // ON INDEX 1 and above, loop through all lines until you // only have User-Agent and Host - - const isHttps = Utils.GetToken(bufferLines[0], " ", 0) === "CONNECT"; - const domain = Utils.GetToken(bufferLines[1], ":", 1).trim(); - const port = Utils.GetToken(bufferLines[1], ":", 2).trim() || defaultPort; - const userAgent = Utils.GetToken(bufferLines[3], ":", 1).trim(); - const domainRoot = Utils.GetDomainRoot(domain); + + const isHttps = request.method == "CONNECT" && request.uri.endsWith(":443"); + const port = isHttps ? "443" : ( IsNormalInteger(GetToken(request.uri, ":", 1)) ? GetToken(request.uri, ":", 1) : defaultPort) || ""; + const domain = (request.headers['Host'] || "").replace(":" + port, ""); + const userAgent = request.headers['User-Agent'] || ""; + const domainRoot = Utils.GetDomainRoot(domain); return { isHttps: isHttps ? 1 : 0,