Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CoNETProject committed Jul 10, 2024
1 parent d06e352 commit fef90b9
Show file tree
Hide file tree
Showing 13 changed files with 1,806 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

.*
yarn*

test.*
.idea
.vscode
./test.*
tsconfig2.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"clean": "rm -rf ./node_modules ./build",
"local": "node build/localServer/index",
"buildRun": "tsc --project ./tsconfig.build.json && cp src/favicon.ico build/localServer/workers/ && cp src/localServer/workers/utilities/*.js build/localServer/workers/utilities && cp src/index.d.ts build && node build/localServer/index",
"build:docker": "docker buildx build --platform linux/amd64,linux/arm64 --file Dockerfile . --push --tag conetnetwork/conet-daemon:latest"
"build:docker": "docker buildx build --platform linux/amd64,linux/arm64 --file Dockerfile . --push --tag conetnetwork/conet-daemon:latest",
"build:test":"tsc --project ./tsconfig.build2.json"
},
"dependencies": {
"async": "^3.2.5",
Expand Down
226 changes: 226 additions & 0 deletions src/localServer/httpProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
"use strict";
/*!
* Copyright 2018 CoNET Technology Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var crypto_1 = require("crypto");
var net_1 = require("net");
var cacheFileType = /\.jpeg$|\.html$|\.css$|\.gif$|\.js$|\.jpg$|\.png$|\.svg$|\.xml$/i;
var httpProxy = /** @class */ (function () {
function httpProxy(buffer) {
this.buffer = buffer;
this.text = buffer.toString('utf8');
this._parts = this.text.split('\r\n\r\n');
this.commandWithLine = this._parts[0].split(/\r\n/);
var u = '{';
for (var i = 1, k = 0; i < this.commandWithLine.length; i++) {
var line = this.commandWithLine[i].split(': ');
if (line.length !== 2) {
if (/^host$/i.test(line[0]))
continue;
break;
}
if (k++ !== 0)
u += ',';
u += "\"".concat(line[0].toLowerCase(), "\": ").concat(JSON.stringify(line[1]));
}
u += '}';
this.headers = JSON.parse(u);
}
Object.defineProperty(httpProxy.prototype, "parts", {
get: function () {
return Math.round(this._parts.length / 2);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "nextPart", {
get: function () {
var part = '\r\n\r\n';
if (this.parts > 1) {
var part1 = this.text.indexOf(part);
var part2 = this.text.indexOf(part, part1 + 1);
var kk = this.buffer.slice(part2 + 4);
if (kk.length)
return kk;
}
return Buffer.alloc(0);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "isHttps", {
get: function () {
return (this.isConnect);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "isHttpRequest", {
get: function () {
return (/^connect|^get|^put|^delete|^post|^OPTIONS|^HEAD|^TRACE/i.test(this.commandWithLine[0]));
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "methods", {
get: function () {
return this.commandWithLine[0].split(' ')[0];
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "isConnect", {
get: function () {
return (/^connect /i.test(this.commandWithLine[0]));
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "hostIpAddress", {
get: function () {
if (!(0, net_1.isIP)(this.host)) {
return '';
}
return this.host;
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "isGet", {
get: function () {
return /^GET /i.test(this.commandWithLine[0]);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "isPost", {
get: function () {
return /^port/i.test(this.commandWithLine[0]);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "host", {
get: function () {
if (!this.headers['host']) {
return '';
}
return this.headers['host'].split(':')[0];
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "cachePath", {
get: function () {
if (!this.isGet || !this.isCanCacheFile)
return null;
return (0, crypto_1.createHash)('md5').update(this.host + this.commandWithLine[0]).digest('hex');
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "isCanCacheFile", {
get: function () {
return cacheFileType.test(this.commandWithLine[0].split(' ')[1]);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "getProxyAuthorization", {
get: function () {
for (var i = 1; i < this.commandWithLine.length; i++) {
var y = this.commandWithLine[i];
if (/^Proxy-Authorization: Basic /i.test(y)) {
var n = y.split(' ');
if (n.length === 3) {
return Buffer.from(n[2], 'base64').toString();
}
return;
}
}
return;
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "BufferWithOutKeepAlife", {
get: function () {
if (!this.isGet || !this.isCanCacheFile)
return this.buffer;
var ss = '';
this.commandWithLine.forEach(function (n) {
ss += n.replace('keep-alive', 'close') + '\r\n';
});
ss += '\r\n\r\n';
return Buffer.from(ss);
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "Body", {
get: function () {
var length = parseInt(this.headers['content-length']);
if (!length)
return null;
var body = this._parts[1];
if (body && body.length && body.length === length)
return body;
return null;
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "preBodyLength", {
get: function () {
var body = this._parts[1];
return body.length;
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "Port", {
get: function () {
//console.log ( this.commandWithLine )
var uu = this.commandWithLine[0].split(/\/\//);
if (uu.length > 1) {
var kk = uu[1].split(':');
if (kk.length > 1) {
var ret = kk[1].split(' ')[0];
console.log("ret = [".concat(ret, "]"));
return parseInt(ret);
}
return 80;
}
var vv = this.commandWithLine[0].split(':');
if (vv.length > 1) {
var kk = vv[1].split(' ')[0];
return parseInt(kk);
}
return 443;
},
enumerable: false,
configurable: true
});
Object.defineProperty(httpProxy.prototype, "BodyLength", {
get: function () {
return parseInt(this.headers['content-length']);
},
enumerable: false,
configurable: true
});
return httpProxy;
}());
exports.default = httpProxy;
39 changes: 39 additions & 0 deletions src/localServer/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hexDebug = exports.loggerToStream = exports.logger = void 0;
var hexdump_nodejs_1 = require("hexdump-nodejs");
var safe_1 = require("colors/safe");
var logger = function () {
var argv = [];
for (var _i = 0; _i < arguments.length; _i++) {
argv[_i] = arguments[_i];
}
var date = new Date();
var dateStrang = "%c [CONET-worker INFO ".concat(date.getHours(), ":").concat(date.getMinutes(), ":").concat(date.getSeconds(), ":").concat(date.getMilliseconds(), "]");
return console.log.apply(console, __spreadArray([dateStrang, 'color: #dcde56'], argv, false));
};
exports.logger = logger;
var loggerToStream = function (logStream) {
var argv = [];
for (var _i = 1; _i < arguments.length; _i++) {
argv[_i - 1] = arguments[_i];
}
var date = new Date();
logStream += "Proxy [".concat(date.getHours(), ":").concat(date.getMinutes(), ":").concat(date.getSeconds(), ":").concat(date.getMilliseconds(), "] ").concat(__spreadArray([], argv, true));
};
exports.loggerToStream = loggerToStream;
var hexDebug = function (buffer, length) {
if (length === void 0) { length = 256; }
console.log(safe_1.default.underline(safe_1.default.green("TOTAL LENGTH [".concat(buffer.length, "]"))));
console.log(safe_1.default.grey((0, hexdump_nodejs_1.default)(buffer.slice(0, length))));
};
exports.hexDebug = hexDebug;
Loading

0 comments on commit fef90b9

Please sign in to comment.