fetch-socks
Advanced tools
Comparing version 1.0.0 to 1.0.1
import { SocksProxy } from "socks"; | ||
import { buildConnector, Agent } from "undici"; | ||
export declare function socksConnector(proxies: SocksProxy | SocksProxy[], tlsOptions?: any): buildConnector.connector; | ||
import { Agent, buildConnector } from "undici"; | ||
import Connector = buildConnector.connector; | ||
import TLSOptions = buildConnector.BuildOptions; | ||
/** | ||
* Create an undici connector which establish the connection through socks proxies. | ||
* | ||
* @param proxies The proxy server to use or the list of proxy servers to chain. | ||
* @param tlsOpts TLS upgrade options. | ||
*/ | ||
export declare function socksConnector(proxies: SocksProxy | SocksProxy[], tlsOpts?: TLSOptions): Connector; | ||
export interface SocksDispatcherOptions extends Agent.Options { | ||
/** | ||
* The socks proxy server to use or the list of proxy servers to chain. | ||
*/ | ||
proxy: SocksProxy | SocksProxy[]; | ||
/** | ||
* TLS upgrade options, see: | ||
* https://undici.nodejs.org/#/docs/api/Client?id=parameter-connectoptions | ||
* | ||
* The connect function is not supported. | ||
* If you want to create a custom connector, you can use `socksConnector`. | ||
*/ | ||
connect?: TLSOptions; | ||
} | ||
/** | ||
* Create a undici Agent with socks connector. | ||
*/ | ||
export declare function socksDispatcher(options: SocksDispatcherOptions): Agent; |
29
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.socksDispatcher = exports.socksConnector = void 0; | ||
const tls = require("tls"); | ||
const socks_1 = require("socks"); | ||
const undici_1 = require("undici"); | ||
/** | ||
* Since socks does not guess HTTP ports, we need to do that. | ||
* | ||
* @param protocol Upper layer protocol, "http:" or "https:" | ||
* @param port A string containing the port number of the URL, maybe empty. | ||
*/ | ||
function resolvePort(protocol, port) { | ||
return port ? Number.parseInt(port) : protocol === "http:" ? 80 : 443; | ||
} | ||
function socksConnector(proxies, tlsOptions) { | ||
/** | ||
* Create an undici connector which establish the connection through socks proxies. | ||
* | ||
* @param proxies The proxy server to use or the list of proxy servers to chain. | ||
* @param tlsOpts TLS upgrade options. | ||
*/ | ||
function socksConnector(proxies, tlsOpts = {}) { | ||
const { timeout = 10e3 } = tlsOpts; | ||
const tlsUpgrade = (0, undici_1.buildConnector)(tlsOpts); | ||
return async (options, callback) => { | ||
@@ -15,2 +28,3 @@ const { protocol, hostname, port } = options; | ||
command: "connect", | ||
timeout, | ||
destination: { | ||
@@ -25,9 +39,7 @@ host: hostname, | ||
} | ||
let { socket } = connection; | ||
const { socket } = connection; | ||
if (protocol !== "https:") { | ||
return callback(null, socket); | ||
return callback(null, socket.setNoDelay()); | ||
} | ||
socket = tls.connect({ ...tlsOptions, socket, servername: hostname }); | ||
socket.on("error", error => callback(error, null)) | ||
.on("secureConnect", () => callback(null, socket)); | ||
return tlsUpgrade({ ...options, httpSocket: socket }, callback); | ||
}; | ||
@@ -45,2 +57,5 @@ if (Array.isArray(proxies)) { | ||
exports.socksConnector = socksConnector; | ||
/** | ||
* Create a undici Agent with socks connector. | ||
*/ | ||
function socksDispatcher(options) { | ||
@@ -47,0 +62,0 @@ const { connect, proxy, ...rest } = options; |
{ | ||
"name": "fetch-socks", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Socks proxy for Node builtin `fetch`", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Kaciras/fetch-socks" | ||
}, | ||
"keywords": [ | ||
"fetch", | ||
"socks", | ||
"proxy", | ||
"undici" | ||
], | ||
"files": [ | ||
@@ -26,3 +36,7 @@ "index.js", | ||
"typescript": "^4.9.3" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --fix .", | ||
"test": "jest" | ||
} | ||
} |
# fetch-socks | ||
[![npm package](https://img.shields.io/npm/v/fetch-socks.svg)](https://npmjs.com/package/fetch-socks) | ||
[![Test](https://github.com/Kaciras/fetch-socks/actions/workflows/test.yml/badge.svg)](https://github.com/Kaciras/fetch-socks/actions/workflows/test.yml) | ||
[![codecov](https://codecov.io/gh/Kaciras/fetch-socks/branch/master/graph/badge.svg?token=DJLSKIKYBJ)](https://codecov.io/gh/Kaciras/fetch-socks) | ||
Socks proxy for Node builtin (also [undici](https://github.com/nodejs/undici)) `fetch`. | ||
@@ -17,10 +21,10 @@ | ||
const dispatcher = socksDispatcher({ | ||
proxy: { | ||
type: 5, | ||
host: "::1", | ||
port: 1080, | ||
}, | ||
proxy: { | ||
type: 5, | ||
host: "::1", | ||
port: 1080, | ||
}, | ||
}); | ||
const response = await fetch("http://example.com", { dispatcher }); | ||
const response = await fetch("https://example.com", { dispatcher }); | ||
console.log(response.status); | ||
@@ -36,19 +40,20 @@ console.log(await response.text()); | ||
const dispatcher = socksDispatcher({ | ||
proxy: [ | ||
{ | ||
type: 5, | ||
host: "::1", | ||
port: 1080, | ||
}, | ||
{ | ||
type: 5, | ||
host: "::1", | ||
port: 1081, | ||
//userId: "foo", | ||
//password: "bar", | ||
}, | ||
], | ||
proxy: [{ | ||
type: 5, | ||
host: "::1", | ||
port: 1080, | ||
}, { | ||
type: 5, | ||
host: "::1", | ||
port: 1081, | ||
//userId: "foo", | ||
//password: "bar", | ||
}], | ||
// set some TLS options | ||
connect: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
const response = await fetch("http://example.com", { dispatcher }); | ||
const response = await fetch("https://example.com", { dispatcher }); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
6865
91
58
0
0