diff --git a/src/connect.ts b/src/connect.ts index 2bf4122..fd38d1c 100644 --- a/src/connect.ts +++ b/src/connect.ts @@ -194,7 +194,7 @@ export async function calculateActualConfig(config: FileSystemConfig): Promise { +export async function createSocket(config: FileSystemConfig): Promise { config = (await calculateActualConfig(config))!; if (!config) return null; const logging = Logging.scope(`createSocket(${config.name})`); @@ -213,7 +213,7 @@ export async function createSocket(config: FileSystemConfig): Promise ${hopConfigs.map((c, i) => `[${i + 1}] ${c.name}`).join(' -> ')} -> server`); - const stream = await reduceAsync(hopConfigs, async (sock: NodeJS.ReadableStream | null | undefined, hop, index) => { + const stream = await reduceAsync(hopConfigs, async (sock: NodeJS.ReadWriteStream | null | undefined, hop, index) => { if (sock === null) return null; const logger = logging.scope(`Hop#${index + 1}`); logger.debug(`Connecting to hop '${hop.name}'`); @@ -241,16 +241,16 @@ export async function createSocket(config: FileSystemConfig): Promise((resolve, reject) => { + return new Promise((resolve, reject) => { logging.debug(`Connecting to ${config.host}:${config.port || 22}`); const socket = new Socket(); - socket.connect(config.port || 22, config.host!, () => resolve(socket as NodeJS.ReadableStream)); + socket.connect(config.port || 22, config.host!, () => resolve(socket as NodeJS.ReadWriteStream)); socket.once('error', reject); }); } export interface CreateSSHOptions { - sock?: NodeJS.ReadableStream; + sock?: NodeJS.ReadWriteStream; logger?: Logger; } export async function createSSH(config: FileSystemConfig, options: CreateSSHOptions = {}): Promise { diff --git a/src/proxy.ts b/src/proxy.ts index d66bdb7..91467c1 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -1,7 +1,6 @@ import * as dns from 'dns'; import { request } from 'http'; -import { SocksClient } from 'socks'; import type { FileSystemConfig } from './fileSystemConfig'; import { Logging } from './logging'; import { toPromise } from './toPromise'; @@ -20,17 +19,18 @@ function validateConfig(config: FileSystemConfig) { if (!config.proxy.type) throw new Error(`Missing field 'config.proxy.type'`); } -export async function socks(config: FileSystemConfig): Promise { +export async function socks(config: FileSystemConfig): Promise { Logging.info(`Creating socks proxy connection for ${config.name}`); validateConfig(config); - if (config.proxy!.type !== 'socks4' && config.proxy!.type !== 'socks5') { + const proxy = config.proxy!; + if (proxy!.type !== 'socks4' && proxy!.type !== 'socks5') { throw new Error(`Expected 'config.proxy.type' to be 'socks4' or 'socks5'`); } try { - const ipaddress = (await resolveHostname(config.proxy!.host)); - if (!ipaddress) throw new Error(`Couldn't resolve '${config.proxy!.host}'`); - Logging.debug(`\tConnecting to ${config.host}:${config.port} over ${config.proxy!.type} proxy at ${ipaddress}:${config.proxy!.port}`); - const con = await SocksClient.createConnection({ + const ipaddress = (await resolveHostname(proxy!.host)); + if (!ipaddress) throw new Error(`Couldn't resolve '${proxy!.host}'`); + Logging.debug(`\tConnecting to ${config.host}:${config.port} over ${proxy!.type} proxy at ${ipaddress}:${proxy!.port}`); + const con = await (await import('socks')).SocksClient.createConnection({ command: 'connect', destination: { host: config.host!, @@ -38,34 +38,35 @@ export async function socks(config: FileSystemConfig): Promise { +export function http(config: FileSystemConfig): Promise { Logging.info(`Creating http proxy connection for ${config.name}`); validateConfig(config); - return new Promise((resolve, reject) => { - if (config.proxy!.type !== 'http') { - reject(new Error(`Expected config.proxy.type' to be 'http'`)); + return new Promise((resolve, reject) => { + const proxy = config.proxy!; + if (proxy!.type !== 'http') { + return reject(new Error(`Expected config.proxy.type' to be 'http'`)); } try { - Logging.debug(`\tConnecting to ${config.host}:${config.port} over http proxy at ${config.proxy!.host}:${config.proxy!.port}`); + Logging.debug(`\tConnecting to ${config.host}:${config.port} over http proxy at ${proxy!.host}:${proxy!.port}`); const req = request({ - port: config.proxy!.port, - hostname: config.proxy!.host, + port: proxy!.port, + hostname: proxy!.host, method: 'CONNECT', path: `${config.host}:${config.port}`, }); req.end(); req.on('connect', (res, socket) => { - resolve(socket as NodeJS.ReadableStream); + resolve(socket as NodeJS.ReadWriteStream); }); } catch (e) { reject(new Error(`Error while connecting to the the proxy: ${e.message}`));