diff --git a/src/manager.ts b/src/manager.ts index cafed9b..2e84daf 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -250,6 +250,19 @@ export class Manager implements vscode.FileSystemProvider, vscode.TreeDataProvid if (config.password) config.agent = undefined; return config; } + public async createSocket(config: FileSystemConfig): Promise { + switch (config.proxy && config.proxy.type) { + case null: + case undefined: + break; + case 'socks4': + case 'socks5': + return await proxy.socks(config); + default: + throw new Error(`Unknown proxy method`); + } + return null; + } public async createFileSystem(name: string, config?: FileSystemConfig): Promise { if (name === '') return this.configFileSystem; const existing = this.fileSystems.find(fs => fs.authority === name); @@ -265,17 +278,7 @@ export class Manager implements vscode.FileSystemProvider, vscode.TreeDataProvid this.registerFileSystem(name, { ...config }); config = (await this.calculateActualConfig(config))!; if (config == null) return reject(null); - switch (config.proxy && config.proxy.type) { - case null: - case undefined: - break; - case 'socks4': - case 'socks5': - config = await proxy.socks(config); - break; - default: - return reject(new Error(`Unknown proxy method`)); - } + const sock = await this.createSocket(config); const client = new Client(); client.on('ready', () => { client.sftp((err, sftp) => { @@ -301,7 +304,7 @@ export class Manager implements vscode.FileSystemProvider, vscode.TreeDataProvid reject(error); }); try { - client.connect(Object.assign(config, { tryKeyboard: false })); + client.connect(Object.assign(config, { sock, tryKeyboard: false } as ConnectConfig)); } catch (e) { reject(e); } diff --git a/src/proxy.ts b/src/proxy.ts index 1ff7427..0840474 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -5,7 +5,7 @@ import { SocksClient } from 'socks'; import { FileSystemConfig } from './manager'; import { toPromise } from './toPromise'; -export async function socks(config: FileSystemConfig): Promise { +export async function socks(config: FileSystemConfig): Promise { if (!config.proxy) throw new Error(`Missing field 'config.proxy'`); if (!config.proxy.host) throw new Error(`Missing field 'config.proxy.host'`); if (!config.proxy.port) throw new Error(`Missing field 'config.proxy.port'`); @@ -28,8 +28,7 @@ export async function socks(config: FileSystemConfig): Promise type: config.proxy.type === 'socks4' ? 4 : 5, }, }); - config.sock = con.socket as NodeJS.ReadableStream; - return config; + return con.socket as NodeJS.ReadableStream; } catch (e) { throw new Error(`Error while connecting to the the proxy: ${e.message}`); }