Bladeren bron

Prevents errors that occur when connecting to the destination in the SOCKS Proxy and HTTP Proxy from crashing the applications

Zachary Boyd 6 jaren geleden
bovenliggende
commit
7b90edabd0
6 gewijzigde bestanden met toevoegingen van 36 en 20 verwijderingen
  1. 6 2
      CHANGELOG.md
  2. 4 4
      package-lock.json
  3. 2 2
      package.json
  4. 20 10
      src/HTTPServer.js
  5. 3 1
      src/SOCKSServer.js
  6. 1 1
      src/launch.js

+ 6 - 2
CHANGELOG.md

@@ -1,13 +1,17 @@
 # Changelog
 
+## [4.0.5] - 2018-10-15
+### Changed
+- Prevents errors that occur when connecting to the destination in the SOCKS Proxy and HTTP Proxy from crashing the applications
+
 ## [4.0.4] - 2018-09-24
 ### Changed
-- Replaces `jrpc2` with `multi-rpc` for providing the RPC Interface. No changes to the application or API.
+- Replaces `jrpc2` with `multi-rpc` for providing the RPC Interface. No changes to the application or API
 
 ## [4.0.3] - 2018-09-15
 
 ### Changed
-- References granax in `default_config.js` to comply with licensing requirements.
+- References granax in `default_config.js` to comply with licensing requirements
 
 ## [4.0.2] - 2018-09-15
 

+ 4 - 4
package-lock.json

@@ -1,6 +1,6 @@
 {
   "name": "tor-router",
-  "version": "4.0.4",
+  "version": "4.0.5",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
@@ -374,7 +374,7 @@
       "dependencies": {
         "async": {
           "version": "0.2.10",
-          "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz",
+          "resolved": "http://registry.npmjs.org/async/-/async-0.2.10.tgz",
           "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E="
         },
         "winston": {
@@ -2442,8 +2442,8 @@
       }
     },
     "socksv5": {
-      "version": "git+https://github.com/znetstar/socksv5.git#431e541390314adbbe765650d8d810ec1df38d8a",
-      "from": "git+https://github.com/znetstar/socksv5.git#431e541390314adbbe765650d8d810ec1df38d8a",
+      "version": "git+https://github.com/znetstar/socksv5.git#1480422215cf1464fa06f5aec4a3e7f2117e3403",
+      "from": "git+https://github.com/znetstar/socksv5.git#1480422215cf1464fa06f5aec4a3e7f2117e3403",
       "requires": {
         "ipv6": "*"
       },

+ 2 - 2
package.json

@@ -1,6 +1,6 @@
 {
   "name": "tor-router",
-  "version": "4.0.4",
+  "version": "4.0.5",
   "main": "src/index.js",
   "repository": "git@github.com:znetstar/tor-router.git",
   "author": "Zachary Boyd <zachary@zacharyboyd.nyc>",
@@ -43,7 +43,7 @@
     "native-dns": "git+https://github.com/znetstar/node-dns.git#336f1d3027b2a3da719b5cd65380219267901aeb",
     "nconf": "^0.10.0",
     "shelljs": "^0.8.2",
-    "socksv5": "git+https://github.com/znetstar/socksv5.git#431e541390314adbbe765650d8d810ec1df38d8a",
+    "socksv5": "git+https://github.com/znetstar/socksv5.git#1480422215cf1464fa06f5aec4a3e7f2117e3403",
     "temp": "^0.8.3",
     "winston": "^3.0.0-rc5",
     "yargs": "^11.0.0"

+ 20 - 10
src/HTTPServer.js

@@ -147,6 +147,12 @@ class HTTPServer extends Server {
 
 			let buffer = [];
 
+			const onError = (err) => {
+				this.logger.error("[http-proxy]: an error occured: "+err.message);
+				res.writeHead(500);
+				res.end();
+			}
+
 			function onIncomingData(chunk) {
 				buffer.push(chunk);
 			}
@@ -157,26 +163,26 @@ class HTTPServer extends Server {
 
 			req.on('data', onIncomingData);
 			req.on('end', preConnectClosed);
-			req.on('error', function (err) {
-				this.logger.error("[http-proxy]: an error occured: "+err.message);
-			});
+			req.on('error', onError);
 
 			let connect = (tor_instance) => {
 				let source = { hostname: req.connection.remoteAddress, port: req.connection.remotePort, proto: 'http', by_name: Boolean(instance) };
 				let socks_port = tor_instance.socks_port;
 				
+				const agent =  socks.HttpAgent({
+					proxyHost: '127.0.0.1',
+					proxyPort: socks_port,
+					auths: [ socks.auth.None() ],
+					localDNS: false
+				});
+
 				let proxy_req = http.request({
 					method: req.method,
 					hostname: url.hostname, 
 					port: url.port,
 					path: url.path,
 					headers: req.headers,
-					agent: socks.HttpAgent({
-						proxyHost: '127.0.0.1',
-						proxyPort: socks_port,
-						auths: [ socks.auth.None() ],
-						localDNS: false
-					})
+					agent
 				}, (proxy_res) => {
 					/**
 					 * Fires when the proxy has made a connection through an instance using HTTP or HTTP-Connect.
@@ -199,6 +205,8 @@ class HTTPServer extends Server {
 					res.writeHead(proxy_res.statusCode, proxy_res.headers);
 				});
 
+				proxy_req.on("error", onError);
+				
 				req.removeListener('data', onIncomingData);
 
 				req.on('data', (chunk) => {
@@ -271,7 +279,7 @@ class HTTPServer extends Server {
 				inbound_socket.on('error', onClose);
 				inbound_socket.on('close', onClose);
 
-				socks.connect({
+				const client = socks.connect({
 					host: hostname,
 					port: port,
 					proxyHost: '127.0.0.1',
@@ -292,6 +300,8 @@ class HTTPServer extends Server {
 					outbound_socket.pipe(inbound_socket);
 					inbound_socket.pipe(outbound_socket);
 				});
+
+				client.on('error', onClose);	
 			};
 
 			if (instance) {

+ 3 - 1
src/SOCKSServer.js

@@ -146,7 +146,7 @@ class SOCKSServer extends Server{
 				let source = { hostname: info.srcAddr, port: info.srcPort, proto: 'socks', by_name: Boolean(instance) };
 				let socks_port = tor_instance.socks_port;
 
-				socks.connect({
+				let client = socks.connect({
 					host: info.dstAddr,
 					port: info.dstPort,
 					proxyHost: '127.0.0.1',
@@ -182,6 +182,8 @@ class SOCKSServer extends Server{
 						outbound_socket.write(buffer.shift());
 					}
 				});
+
+				client.on('error', onClose);
 			};
 			
 			if (instance) {

+ 1 - 1
src/launch.js

@@ -132,7 +132,7 @@ async function main(nconf, logger) {
         }
 
         if (error instanceof Error) {
-            logger.error(`[global]: error shutting down: ${error.message}`);
+            logger.error(`[global]: ${error.stack || 'An unknown error occured'}`);
         } else {
             error = 0;
         }