Move (potential) socket creation to a separate method

pull/64/head
Kelvin Schoofs 7 years ago
parent 69b16b9d8b
commit afc0079f9c

@ -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<NodeJS.ReadableStream | null> {
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<SSHFileSystem> {
if (name === '<config>') 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);
}

@ -5,7 +5,7 @@ import { SocksClient } from 'socks';
import { FileSystemConfig } from './manager';
import { toPromise } from './toPromise';
export async function socks(config: FileSystemConfig): Promise<FileSystemConfig> {
export async function socks(config: FileSystemConfig): Promise<NodeJS.ReadableStream> {
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<FileSystemConfig>
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}`);
}

Loading…
Cancel
Save