diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..396fc0e --- /dev/null +++ b/.gitignore @@ -0,0 +1,105 @@ +.idea/* +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and *not* Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3aa7bd3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Ricardo Chavarria + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/bin/node-linux b/bin/node-linux new file mode 100755 index 0000000..db06290 Binary files /dev/null and b/bin/node-linux differ diff --git a/bin/node-mac b/bin/node-mac new file mode 100755 index 0000000..b11f79a Binary files /dev/null and b/bin/node-mac differ diff --git a/cert.pem b/cert.pem new file mode 100644 index 0000000..024d05b --- /dev/null +++ b/cert.pem @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDGTCCAgECFDXH4l48DoLOFvSz9rR7PLAJSfRiMA0GCSqGSIb3DQEBCwUAMEkx +CzAJBgNVBAYTAkRLMRQwEgYDVQQIDAtDb3BlbmhhbmdlbjEQMA4GA1UECgwHUGVy +ZnRlazESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIwMDUxODA5MDUzMloXDTQ3MTAw +MzA5MDUzMlowSTELMAkGA1UEBhMCREsxFDASBgNVBAgMC0NvcGVuaGFuZ2VuMRAw +DgYDVQQKDAdQZXJmdGVrMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3 +DQEBAQUAA4IBDwAwggEKAoIBAQDABCar8xSDUAuxU6L1Kk9A5iAWOFsQ1RLwn1gp +fEu8zVMIdWYF1zxkjB/WCuYgGXfRdUJriWrDKpsBWeAsnlxmq7QIxvVK4+IQb3Ar +CGC5LB9IWVvqjUtqppz/kKsjWa6BSX8wigWUbH+mWpClDAT3aZOW1WvPO+cVkH/E +euIOorS56rNAEnfczv2+UfRVAxASSqa5TViO49+OuooMIzxWF1k1ZwmI6BuHG5mC +qwDMVYnsNfq2k3t2vnb8g5hCwJANhuK9RXDXgkvTQyfsu923l/lrhw7kZazrOwKC +NUNb5bomeyLuqIJrmfktTUnWJ9FiAG9f59WgziQ39mC4MMOvAgMBAAEwDQYJKoZI +hvcNAQELBQADggEBAFtJPuBZksvVihoJ8+z7ENKOlULH21sbXIKGslQdxL4L5iEW +8lyQ0UqU2e3ZeyVUD+qk76EjcjUsB8Cq2RjBJG1/wQUbCgUs6AwP0QFqkob9dgYp +5XGrI99TX//al6wxuUmxg0LMPJjz1t+l3wL8Ve6bVo0Xz6gLiN6bB1yH8VxswzvD +v7SIJknRBRPDqJ4Yc+Hm5yAtHzwaRgM7uEez6Ss+H08kq4fCyOOhMByTsn0Oe3t6 +o+seSFgNDVoXDmZ7n7Xj+cx3Y7gu7+VSnwW7j0twVIV222cgwi1E1CpDrD8IOnSi +kA45pQVoufgNIjQSMay0oDmbE0tYKNlElRy4D8g= +-----END CERTIFICATE----- diff --git a/certs/sandbox.tradeshift.com.crt b/certs/sandbox.tradeshift.com.crt new file mode 100644 index 0000000..abfe69f --- /dev/null +++ b/certs/sandbox.tradeshift.com.crt @@ -0,0 +1,21 @@ +-----BEGIN CERTIFICATE----- +MIIDgjCCAmqgAwIBAgIQVpF4/ufATbUEFZOpwHVkzTANBgkqhkiG9w0BAQsFADBd +MQswCQYDVQQGEwJDTjERMA8GA1UEChMIQW55UHJveHkxCzAJBgNVBAgTAlNIMRsw +GQYDVQQLExJBbnlQcm94eSBTU0wgUHJveHkxETAPBgNVBAMTCEFueVByb3h5MB4X +DTEwMDQzMDEwMjM1MFoXDTMwMDQzMDEwMjM1MFowazELMAkGA1UEBhMCQ04xETAP +BgNVBAoTCEFueVByb3h5MQswCQYDVQQIEwJTSDEbMBkGA1UECxMSQW55UHJveHkg +U1NMIFByb3h5MR8wHQYDVQQDExZzYW5kYm94LnRyYWRlc2hpZnQuY29tMIIBIjAN +BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAincj23AiLe9IEwVRnXX1gPbShKcX +XZp/FatgQjRmgyv0OYBaVNhceLUiXD8qewMu9OCNaa5ktDvL4ME7UuYyKQVgvahQ +i4If0waeh64rMye0GxdTDmj2efkA77RB2Z2Ltd4o+8np0iPYElxA+KtSpn5HtIdi +9VZzHtEP97+gMQgQMnE7tMf2Q/CrbA9Hety2d307OUm4VdrcMMWfuRZINjpkPNBD +4IurYI+eWBbW2FVnp4jlcodLlodIDsfnAYGFyaJd/b8B/gpn+0WjkwiJSRmxbnZC +9qJxc5bfOl1FV3ybFDROFfBrT1RtXySGpsP/3q2mATE+ayP1LufJ9W1+TQIDAQAB +ozAwLjAJBgNVHRMEAjAAMCEGA1UdEQQaMBiCFnNhbmRib3gudHJhZGVzaGlmdC5j +b20wDQYJKoZIhvcNAQELBQADggEBAGHJHLpc978+Oe9miS0Oqcv+kmwT4qzQ2GVZ +uLJIeOCe+fOvOxku5tUjYTxdUegJ/lKAcvR4432bWt90Jh+W+Mn5CTwwpGJ7Yg6X +1kzhxlHeR0Ug7l2Uydgqvaqq4ljBgHeLyL88gUjZ+kbohTs1rEP5xm5KKlLsDEok +sPcnLvrxkLqZkd8hMbnRi7x1GXUCCHTO8LwMaaK3uTgin0Hz94ih8DsOhT7carS3 +dEP6ADUgxvXRjukprcMCENloAKZTwqwfZH0TJZz2TZXITmgNq4FDKBlKN2pNYn3G +4nbV+2NRkLsrVZZ5lc2dL0atv8pFAJsWXeQB88lQLuJwoUPK3ko= +-----END CERTIFICATE----- diff --git a/certs/sandbox.tradeshift.com.key b/certs/sandbox.tradeshift.com.key new file mode 100644 index 0000000..5c86d35 --- /dev/null +++ b/certs/sandbox.tradeshift.com.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAincj23AiLe9IEwVRnXX1gPbShKcXXZp/FatgQjRmgyv0OYBa +VNhceLUiXD8qewMu9OCNaa5ktDvL4ME7UuYyKQVgvahQi4If0waeh64rMye0GxdT +Dmj2efkA77RB2Z2Ltd4o+8np0iPYElxA+KtSpn5HtIdi9VZzHtEP97+gMQgQMnE7 +tMf2Q/CrbA9Hety2d307OUm4VdrcMMWfuRZINjpkPNBD4IurYI+eWBbW2FVnp4jl +codLlodIDsfnAYGFyaJd/b8B/gpn+0WjkwiJSRmxbnZC9qJxc5bfOl1FV3ybFDRO +FfBrT1RtXySGpsP/3q2mATE+ayP1LufJ9W1+TQIDAQABAoIBACkPT1jBFcGdVTge +uacB1b1ac+7WRb2CFPFuEmtPcp6vKArtHtxOFI3O2NOV/5jexsvi0sxl5IL1wG1z +n43jqTg02g0jnk7gVWOUcOo3IaRXv7bQ4dZCFeTxi++I+oX3i/hwWqLfqzCfZHF9 +Xd7fnvC0Pbsw8o1iB106DYwgDPa8xnZASnHDh+PCVOuektBSLEdSlM9/dAWB2pfS +Al7QnChi8AXwDP4gObSQnQiOsYQVOCE8rDHKLgKkToP1qAk26ZGjLg8Yuc64hV0/ +xQHC2MbqE/bT2FFBEsHNW2NWbeRRRvAnzEy4dF75bmuDKzFzJeDoX+MoG2YsgW3m +WCBynxkCgYEA2rIYye6P+QyOUZoF1vNBSUjz4g14i0HOISk/QetFeV7WkWzg0R4H +3FM6ND/f2RfQ/aoVgmMuJEZTH0HVFeDV5t9y7x0wGlY3d3tP5sZgBFOxcSsBud8k +lOuGTmenyA2tgCuXwSb576nPa7ortosviqZnPjpqKHATMJqTWwRRUcMCgYEAohWV +UeU6T+XrtTAEqlH0XHw+oWjh9uztnXlUcGShlXlSYJe2gNN3I4gty8TBb6+5tVUX +hwP9BpTNr6j8CbTaDsVYJBrTBc1cswnj82uGKFtn8xyZFo2Un7bhGgWO7fwgeFZp +AzfgoM0lWoWgSFCA8dVsTmKw3oUDIEh1ILzbXq8CgYEAiIoLCgAk6VQagv4LyVEk +oLGoeqj4atFR9cg/01MllApPIop8rnL2a2uW+koq4uqIs+b9+OMdSs6RVQGQHVG7 +mqJFqt5pSvgrNzDJCQcvqWPrdAFq2XZk64lE2CzFRxSrD0uoOpKwFx7I/rREWHr7 +2q/b47Eq2EZo5L6NY5UFhGsCgYEAku5h6FWWoOta22tmL0gP7u8qj4PWo9NwK/rb +EvAGksviY3HknbAVJn7ClSAOAsMX5Ev5AgoZi6fFNeJIaDwsGIvJNSGj/nSF/j+P +BxGIEnmaKAbauFksTASq1r4HLbILdUtP8vzqgpq1q/PKswNXgOLqRbytfWqyxMcq +drz9WD0CgYBeQSn+bJEmu9cNuD+r46XOmvc7xS9wdS8S9b9GrBnJGA5tm8nnNS6S +Yyr/W5pZqSA7c+odqiiq/tapYgaZdCJp5JJUdMte+Rz+ENNknixxgT9cu36azB1F +CNuSyDUQ4+7tmuIRZqWv4TdIt8mScECkLCDNuzhwSyqPA1jZksYU9A== +-----END RSA PRIVATE KEY----- diff --git a/config.json b/config.json new file mode 100644 index 0000000..0c7ac78 --- /dev/null +++ b/config.json @@ -0,0 +1,10 @@ +{ + "Control_Port": 4241, + "Plain_Port": 4242, + "Tls_Port": 4243, + "Remote_Addr_IP": "52.50.108.226", + "Intercepted_Apps": [ + "BranchManager", + "Configurator" + ] +} diff --git a/key.pem b/key.pem new file mode 100644 index 0000000..35419a4 --- /dev/null +++ b/key.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAwAQmq/MUg1ALsVOi9SpPQOYgFjhbENUS8J9YKXxLvM1TCHVm +Bdc8ZIwf1grmIBl30XVCa4lqwyqbAVngLJ5cZqu0CMb1SuPiEG9wKwhguSwfSFlb +6o1Laqac/5CrI1mugUl/MIoFlGx/plqQpQwE92mTltVrzzvnFZB/xHriDqK0ueqz +QBJ33M79vlH0VQMQEkqmuU1YjuPfjrqKDCM8VhdZNWcJiOgbhxuZgqsAzFWJ7DX6 +tpN7dr52/IOYQsCQDYbivUVw14JL00Mn7Lvdt5f5a4cO5GWs6zsCgjVDW+W6Jnsi +7qiCa5n5LU1J1ifRYgBvX+fVoM4kN/ZguDDDrwIDAQABAoIBABKIFAdiVWd68TXz +AXYx2CZGT3B1jexo9AP/DO4sDc8x7cKSOf90T2LQ1z28HYuZiaPQclGYmJIvaXhO +6UDFI9vQ1HZIZWIN7kJxztyIWXVlmbtMzH8tlcq8O8i6eJnXzOyzYDAaEgHKGuO+ +N9mF+HC8Yr8a5+W/PUUqhfo7dde9l1CwAPoonQ32qQVO9gaWh+UFBzABUDKEoszc +WlkuCOrY0UWh/gabq96Un8WKRqd9lqksHamFprwGlnqnsSeyqOHYiuYItwXKVFgo +6Ji5KtE+ox3oEFF38CjIQATR/BtwGaFuxk6aL0RyuCCbh7yCYMQUAPnbySRqsEuy +X270yYkCgYEA76uX9qrjzD/3I3J/l9tXjVmByvBQ7caaEFStbnXpDhDoycCyVDD2 +h8edMJNwh9WmH7GpHSnUm8+kOw85tqqEcMLBLMt8q12g3EGIUuyj4hZbMxtnzr2y +w6r+DFVpGe/gh7A6yv8e+rpJ+M72dy0abVh3zn89cBP8oqrM3H/9k9UCgYEAzRlc +GYuv+iZi7E76vbZ69mAHDyVJokeL2cU8dEjyQQO+4w69i6vRlUaeFkeAPgrfP9ye +UQmi+flfquXLPqt3t6hKjshQUUtcg0KsWiKguUaY1s3zCyYU1WOtU4vfG1HdketT +vcD4zJhCa1Qxp0+PGYvKErzGSI/xFnDX9Jp2b3MCgYEAjsjn6+i7df/VuKSKJ2ph +qrBchw7M6BudxYTtHhXZFqw0Q54tn8Lb3OCrYTk1nXdHVTJ+0gUNyRXXfegANjc4 +WAVvGMTSLTXGk/HmyaB5skQnaTc2jRCu46uHVsm7p9wlJDF1lWKQjkdVsol6hF22 +lNZT6g4cCB2gMTkYe3U1GVECgYEAj/rIi+pi/FGsSkXbggizY3fgHMLJYVGmnJb4 +rI10+y0c+Rqk94MD2y461lsVbPiG5rcCsNL6BVAJ19f/iRwoK9bJmFiJAbjM1hyd +D52VhNtJeioT/3SZVX/m8mqTkgMzbzdrjx8DyeTtY5k0TkbspztZ/TNmYxQhY+aN +w9oXYH8CgYAOhFv5KDKshdtrr7QNgawM/igQAb6cIzLat2xIYDvfjsTNwX+EbCSL +IncrzEpzJCXYFfwnd++ly+Ws2TrN+aTD1RfiV4j69zRzsHFk7K2rSj0jljnK2o7g +VeKTLxQ8OyA48nAZUeDuEYi4MIznavghvBwEvHtb5G+Xy8sESxcuDg== +-----END RSA PRIVATE KEY----- diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b23a160 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,505 @@ +{ + "name": "node-https", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@babel/code-frame": { + "version": "7.8.3", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.8.3.tgz", + "integrity": "sha512-a9gxpmdXtZEInkCSHUJDLHZVBgb1QS0jhss4cPP93EW7s+uC5bikET2twEF3KV+7rDblJcmNvTR7VJejqd2C2g==", + "dev": true, + "requires": { + "@babel/highlight": "^7.8.3" + } + }, + "@babel/helper-validator-identifier": { + "version": "7.9.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.5.tgz", + "integrity": "sha512-/8arLKUFq882w4tWGj9JYzRpAlZgiWUJ+dtteNTDqrRBz9Iguck9Rn3ykuBDoUwh2TO4tSAJlrxDUOXWklJe4g==", + "dev": true + }, + "@babel/highlight": { + "version": "7.9.0", + "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.9.0.tgz", + "integrity": "sha512-lJZPilxX7Op3Nv/2cvFdnlepPXDxi29wxteT57Q965oc5R9v86ztx0jfxVrTcBk8C2kcPkkDa2Z4T3ZsPPVWsQ==", + "dev": true, + "requires": { + "@babel/helper-validator-identifier": "^7.9.0", + "chalk": "^2.0.0", + "js-tokens": "^4.0.0" + } + }, + "@types/node": { + "version": "14.0.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.1.tgz", + "integrity": "sha512-FAYBGwC+W6F9+huFIDtn43cpy7+SzG+atzRiTfdp3inUKL2hXnd4rG8hylJLIh4+hqrQy1P17kvJByE/z825hA==" + }, + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, + "requires": { + "color-convert": "^1.9.0" + } + }, + "arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "dev": true, + "requires": { + "sprintf-js": "~1.0.2" + } + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "boolbase": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz", + "integrity": "sha1-aN/1++YMUes3cl6p4+0xDcwed24=" + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "buffer-from": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", + "dev": true + }, + "builtin-modules": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", + "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", + "dev": true + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "cheerio": { + "version": "1.0.0-rc.3", + "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.3.tgz", + "integrity": "sha512-0td5ijfUPuubwLUu0OBoe98gZj8C/AA+RW3v67GPlGOrvxWjZmBXiBCRU+I8VEiNyJzjth40POfHiz2RB3gImA==", + "requires": { + "css-select": "~1.2.0", + "dom-serializer": "~0.1.1", + "entities": "~1.1.1", + "htmlparser2": "^3.9.1", + "lodash": "^4.15.0", + "parse5": "^3.0.1" + } + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dev": true, + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", + "dev": true + }, + "colors": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" + }, + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, + "css-select": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz", + "integrity": "sha1-KzoRBTnFNV8c2NMUYj6HCxIeyFg=", + "requires": { + "boolbase": "~1.0.0", + "css-what": "2.1", + "domutils": "1.5.1", + "nth-check": "~1.0.1" + } + }, + "css-what": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/css-what/-/css-what-2.1.3.tgz", + "integrity": "sha512-a+EPoD+uZiNfh+5fxw2nO9QwFa6nJe2Or35fGY6Ipw1R3R4AGz1d1TEZrCegvw2YTmZ0jXirGYlzxxpYSHwpEg==" + }, + "diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true + }, + "dom-serializer": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.1.1.tgz", + "integrity": "sha512-l0IU0pPzLWSHBcieZbpOKgkIn3ts3vAh7ZuFyXNwJxJXk/c4Gwj9xaTJwIDVQCXawWD0qb3IzMGH5rglQaO0XA==", + "requires": { + "domelementtype": "^1.3.0", + "entities": "^1.1.1" + } + }, + "domelementtype": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.3.1.tgz", + "integrity": "sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w==" + }, + "domhandler": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-2.4.2.tgz", + "integrity": "sha512-JiK04h0Ht5u/80fdLMCEmV4zkNh2BcoMFBmZ/91WtYZ8qVXSKjiw7fXMgFPnHcSZgOo3XdinHvmnDUeMf5R4wA==", + "requires": { + "domelementtype": "1" + } + }, + "domutils": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-1.5.1.tgz", + "integrity": "sha1-3NhIiib1Y9YQeeSMn3t+Mjc2gs8=", + "requires": { + "dom-serializer": "0", + "domelementtype": "1" + } + }, + "entities": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz", + "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==" + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "dev": true + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true + }, + "htmlparser2": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-3.10.1.tgz", + "integrity": "sha512-IgieNijUMbkDovyoKObU1DUhm1iwNYE/fuifEoEHfd1oZKZDaONBSkal7Y01shxsM49R4XaMdGez3WnF9UfiCQ==", + "requires": { + "domelementtype": "^1.3.1", + "domhandler": "^2.3.0", + "domutils": "^1.5.1", + "entities": "^1.1.1", + "inherits": "^2.0.1", + "readable-stream": "^3.1.1" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "dev": true + }, + "js-yaml": { + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "dev": true, + "requires": { + "argparse": "^1.0.7", + "esprima": "^4.0.0" + } + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, + "make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", + "dev": true + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true + }, + "mkdirp": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", + "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", + "dev": true, + "requires": { + "minimist": "^1.2.5" + } + }, + "nth-check": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-1.0.2.tgz", + "integrity": "sha512-WeBOdju8SnzPN5vTUJYxYUxLeXpCaVP5i5e0LF8fg7WORF2Wd7wFX/pk0tYZk7s8T+J7VLy0Da6J1+wCT0AtHg==", + "requires": { + "boolbase": "~1.0.0" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "parse5": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/parse5/-/parse5-3.0.3.tgz", + "integrity": "sha512-rgO9Zg5LLLkfJF9E6CCmXlSE4UVceloys8JrFqCcHloC3usd/kJCyPDwH2SOlzix2j3xaP9sUX3e8+kvkuleAA==", + "requires": { + "@types/node": "*" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, + "path-parse": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true + }, + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + }, + "resolve": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz", + "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==", + "dev": true, + "requires": { + "path-parse": "^1.0.6" + } + }, + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + }, + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", + "dev": true + }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + }, + "source-map-support": { + "version": "0.5.19", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", + "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", + "dev": true, + "requires": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", + "dev": true + }, + "string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "requires": { + "safe-buffer": "~5.2.0" + } + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, + "requires": { + "has-flag": "^3.0.0" + } + }, + "ts-node": { + "version": "8.10.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.10.1.tgz", + "integrity": "sha512-bdNz1L4ekHiJul6SHtZWs1ujEKERJnHs4HxN7rjTyyVOFf3HaJ6sLqe6aPG62XTzAB/63pKRh5jTSWL0D7bsvw==", + "dev": true, + "requires": { + "arg": "^4.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "source-map-support": "^0.5.17", + "yn": "3.1.1" + } + }, + "tslib": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", + "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==", + "dev": true + }, + "tslint": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/tslint/-/tslint-6.1.2.tgz", + "integrity": "sha512-UyNrLdK3E0fQG/xWNqAFAC5ugtFyPO4JJR1KyyfQAyzR8W0fTRrC91A8Wej4BntFzcvETdCSDa/4PnNYJQLYiA==", + "dev": true, + "requires": { + "@babel/code-frame": "^7.0.0", + "builtin-modules": "^1.1.1", + "chalk": "^2.3.0", + "commander": "^2.12.1", + "diff": "^4.0.1", + "glob": "^7.1.1", + "js-yaml": "^3.13.1", + "minimatch": "^3.0.4", + "mkdirp": "^0.5.3", + "resolve": "^1.3.2", + "semver": "^5.3.0", + "tslib": "^1.10.0", + "tsutils": "^2.29.0" + } + }, + "tsutils": { + "version": "2.29.0", + "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz", + "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==", + "dev": true, + "requires": { + "tslib": "^1.8.1" + } + }, + "typescript": { + "version": "3.9.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.2.tgz", + "integrity": "sha512-q2ktq4n/uLuNNShyayit+DTobV2ApPEo/6so68JaD5ojvc/6GClBipedB9zNWYxRSAlZXAe405Rlijzl6qDiSw==", + "dev": true + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true + }, + "yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..6db5685 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "node-https", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "start-linux": "clear && rm -rf dist && tsc --resolveJsonModule && bin/node-linux dist/src/app.js", + "start-mac": "clear && rm -rf dist && tsc --resolveJsonModule && bin/node-mac dist/src/app.js", + "start": "clear && npx nodemon --watch . --exec ts-node src/app.ts", + "build": "tsc", + "lbuild": "tslint ./src/* && tsc", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "MIT", + "devDependencies": { + "@types/node": "^14.0.1", + "ts-node": "^8.10.1", + "tslint": "^6.1.2", + "typescript": "^3.9.2" + }, + "dependencies": { + "cheerio": "^1.0.0-rc.3", + "colors": "^1.4.0" + } +} diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..fd66071 --- /dev/null +++ b/src/app.ts @@ -0,0 +1,177 @@ +import configBlob from '../config.json'; +import net from 'net' +import fs from 'fs' +import tls from 'tls' +import { TLSSocket } from 'tls' +import { Socket, Server } from 'net' +import { SocketMux } from './netMux' +import { asyncTimeout } from './utils' +const { log, debug, error } = require('./utils').init('MAIN') + +let config +try { + config = {...configBlob} +} catch (err) { + error("Could not read from configuration file. Exiting process.") + process.exit(-1) +} + +log('Config loaded\n', config) +SocketMux.config = config + + +const CNTRL_PORT: number = config.Control_Port +const PLAIN_PORT: number = config.Plain_Port +const TLS_PORT: number = config.Tls_Port +const REMOTE_ADDR: string = config.Remote_Addr_IP +const APP_PATH: string = process.cwd() + +log('WORKING DIR:', APP_PATH) + +// ************************************************** +// Create the plain (non-secure) listening server +// ************************************************** +const plainServer = net.createServer(async (conSock: Socket) => { + log('client connected to PLAIN port') + + const mux = new SocketMux(conSock, REMOTE_ADDR, 'localhost') + const initialized = await mux.init() + + if (!initialized) { + error('Could not initialize mux. Closing new connection') + conSock.end() + return + } + + mux.server = plainServer + + if (!plainServer['muxs']) + plainServer['muxs'] = {} + plainServer['muxs'][mux.ID] = mux + +}) + +plainServer.on('error', (err) => { + error('Plain Server error', err) + plainServer && plainServer.close() + tlsServer && tlsServer.close() +}) + +plainServer.on('close', () => { +}) + +plainServer.listen(PLAIN_PORT, () => { + log(`Server listening on port ${PLAIN_PORT}`) +}) + +// ************************************************** +// Create the TLS (secure) listening server +// ************************************************** +const options = { + key: fs.readFileSync(`${APP_PATH}/certs/sandbox.tradeshift.com.key`), + cert: fs.readFileSync(`${APP_PATH}/certs/sandbox.tradeshift.com.crt`) +}; + +const tlsServer = tls.createServer(options, async (tlsSock: TLSSocket) => { + try { + const mux = new SocketMux(tlsSock, REMOTE_ADDR, 'localhost') + const initialized = await mux.init() + + if (!initialized) { + error('Client connected but could not initialize mux. Closing new connection') + tlsSock.end() + return + } + + mux.server = tlsServer + + if (!tlsServer['muxs']) + tlsServer['muxs'] = {} + tlsServer['muxs'][mux.ID] = mux + + log(`Client connected to TLS port. Created ${mux.ID} to handle it.`) + debug(`Current open muxes: ${Object.keys(tlsServer['muxs']).length}`) + + } catch (ex) { + error('ERROR:', ex) + } +}) + +tlsServer.on('error', (err) => { + error('TLS Server error', err) + plainServer && plainServer.close() + tlsServer && tlsServer.close() +}) + +tlsServer.on('close', () => { +}) + +tlsServer.listen(TLS_PORT, () => { + log(`TLS Secure Server listening on port ${TLS_PORT}`) +}) + + + + + + + + +process.on( 'SIGINT', async () => { + log( "Starting gracefullly shutting down from SIGINT (Ctrl-C)" ) + setTimeout(() => { + error('Timeout while trying to close servers. Forcing process exit') + process.exit(1) + }, 10000) + finalizeServer('Plain Server', plainServer) + finalizeServer('TLS Server', tlsServer) + await closeSocket(plainServer, 'Plain Server stopped') + await closeSocket(tlsServer, 'TLS Server stopped') + log('Servers were closed. Exiting process ...') + process.exit(0) +}) + +process.on('SIGTERM', () => { + log('SIGTERM') +}) +process.on('SIGUSR2', () => { + log('SIGUSR2') +}) +process.on('uncaughtException', (err) => { + error('UNCAUGHT-EXCEPTION', err) + plainServer && plainServer.close() + tlsServer && tlsServer.close() +}) +process.on('exit', (code) => { + log('Process Exit with code:', code) +}); + +const event = 'unhandledRejection' + +process.on(event, function (err) { + module.exports.logError(err) + if (module.exports.abort) { + process.abort() + } + process.exit(1) +}) + +module.exports.abort = false +module.exports.logError = error + +const closeSocket = (skt, msg) => new Promise((resolve, reject) => { + skt.close(() => { + log(msg) + resolve() + }) +}) + +const finalizeServer = (TAG: string, server: Server) => { + log('Finalizing server:', TAG) + if (server['muxs']) + for (const m in server['muxs']) + server['muxs'][m].kill() +} + + +debug('MAIN', 'Init done') diff --git a/src/netMux.ts b/src/netMux.ts new file mode 100644 index 0000000..f4ce89a --- /dev/null +++ b/src/netMux.ts @@ -0,0 +1,266 @@ +import { connect, Socket, Server } from 'net' +import { TLSSocket } from 'tls' +import cheerio from 'cheerio' +import tls from 'tls' +import fs from 'fs' +const { log, debug, error, plain } = require('./utils').init('MUX') + +type Config = { + Control_Port: number; + Plain_Port: number; + Tls_Port: number; + Remote_Addr_IP: string; + Intercepted_Apps: Array +} + +const options = { + rejectUnauthorized: false, +} + +const getTlsConn = (url: string): Promise => new Promise((resolve, reject) => { + const tlsConn = tls.connect(443, url, options, () => { + if (tlsConn.authorized) { + debug('TLS:', "Connection authorized by a Certificate Authority.") + resolve(tlsConn) + } else { + error('TLS:', "Connection not authorized: " + tlsConn.authorizationError) + reject(tlsConn.authorizationError) + } + debug('TLS:', 'Handshake finished') + }) + tlsConn.on('error', (err) => { + error('Could not open OUT1 connection:', err) + reject(err) + }) +}) + +const getConn = (url: string): Promise => new Promise((resolve, reject) => { + const conn = connect(8321, url, () => { + debug('OUT2 Connection established') + resolve(conn) + }) + conn.on('error', (err) => { + error('Could not open OUT2 connection:', err) + reject(err) + }) +}) + +let GID: number = 0 + +export class SocketMux { + + static floatingThingy: string = `
+

Parsed by YAMM
Page served from: localhost

+ +
` + + static config: Config + static matchStr: string = '' + static interceptedApps: string = '' + + + #inSkt?: Socket | TLSSocket = null + #outSkt1?: Socket | TLSSocket = null + #outSkt2?: Socket | TLSSocket = null + #out2Created: boolean = false + + ID: string = 'NO-ID' + outUrl1: string + outUrl2: string + server?: Server = null + + constructor(conSkt: Socket, outUrl1: string, outUrl2: string) { + this.#inSkt = conSkt + this.outUrl1 = outUrl1 + this.outUrl2 = outUrl2 + this.ID = `MUX-${GID++}` + const interceptedApps = SocketMux.config.Intercepted_Apps.reduce( (acc, app) => `${acc}|${app}`, '').substring(1) + if (SocketMux.interceptedApps === '') + SocketMux.interceptedApps = interceptedApps + if (SocketMux.matchStr === '') + SocketMux.matchStr = `(GET|POST|PUT|DELETE)(\\s+?\\/)(v4\\/|)(apps\\/Tradeshift\\.)(${interceptedApps})(.*?)( HTTP\\/\\d\\.\\d)` + debug(`${this.ID} created -`, 'Intercepted Apps:', interceptedApps) + } + + ////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////// + + _dumpData(data: string, sktNum: number, targetSktNum: number, after: boolean) { + const fn = after ? error : plain + if ( (sktNum === 0 && targetSktNum === 2) || (sktNum === 2 && targetSktNum === 0) ) { + const prefix = sktNum > 0 ? '<<--<<--<<--<<--<<--<<--<<--<<--': '-->>-->>-->>-->>-->>-->>-->>-->>' + fn(this.ID, + `\n${prefix} ${sktNum > 0 ?'IN':'OUT'} DATA START (${this.ID} skt${sktNum} --> skt${targetSktNum})${prefix}\n`, + data, + `\n${prefix} ${sktNum > 0 ?'IN':'OUT'} DATA END (${this.ID} skt${sktNum} --> skt${targetSktNum}) ${prefix}` + ) + } + } + + _filterData(data: Buffer, sktNum: number, targetSktNum): Buffer { + let newData = data.toString() + this._dumpData(newData, sktNum, targetSktNum, false) + if (sktNum === 0) { + if (/(.*)Accept-Encoding:.*?\r?\n(.*)/s.test(newData)) { + newData = newData.replace(/(.*)Accept-Encoding:.*?\r?\n(.*)/s, '$1$2') + } + if (targetSktNum === 2) { + debug(this.ID, 'No gzip please !!') + // newData = newData.replace(/(HTTP\/\d\.\d.*Host: ).*?(\r?\n.*)/s, '$1localhost:8321$2') + // newData = newData.replace(/(HTTP\/\d\.\d.*Referer: ).*?(\r?\n.*)/s, '$1http://localhost:8321$2') + debug(this.ID, 'changing headers to reflect new url and path') + newData = newData.replace(new RegExp(SocketMux.matchStr), '$1$2$4$5$6$7') + /* newData = newData.replace(/(GET|POST|PUT|DELETE)(\s+?\/)v4\/(apps\/Tradeshift\.BranchManager HTTP\/\d\.\d)/, '$1$2$3') */ + newData = newData.replace(/(Connection:.*?\r?\n)/, '$1Cache-Control: no-cache\nCache-Control: no-store\n') + } + } + if (sktNum === 2) { + if ( + /HTTP\/\d\.\d 200 .*Content-Type: text\/html/s.test(newData) && // test if the result is html + /(HTTP\/\d\.\d.*Content-Length: )\d+(.*)/s.test(newData) // test if we have Content-Length (a body) + ) { + debug(this.ID, `Will inspect respose of OUT2`) + let header = newData.match(/HTTP.*?\r?\n\r?\n/s)[0] + let body = newData.replace(/HTTP.*?\r?\n\r?\n/s, '') + // error('HEADER', header) + // error('BODY', body) + + const configScript = this._getConfigScript(body) + // error('CONFIG-SCRIPT', configScript) + body.replace('__config', '__configOriginal') + body = this._insertConfig(body, configScript, 'http://localhost:8321') + // error('NEW-BODY', body) + + const newLength = body.length + header = header.replace(/(HTTP\/\d\.\d.*Content-Length: )\d+(.*)/s, `\$1${newLength}\$2`) + newData = `${header}${body}` + // error('NEW DATA NEW DATA', newData) + } else + debug(this.ID, 'Will NOT inspect OUT2') + } + this._dumpData(newData, sktNum, targetSktNum, true) + return Buffer.from(newData) + } + + _getConfigScript = html => { + const matches = html.match(/