-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add local and dynamic port forward methods
- Loading branch information
1 parent
48c7c43
commit edf3d3a
Showing
10 changed files
with
1,267 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
"main": "./out/main/index.js", | ||
"author": "ShellHub <[email protected]>(https://shellhub.io)", | ||
"homepage": "https://github.com/shellhub-io/desktop", | ||
"engines": { | ||
"node": "^20" | ||
}, | ||
"scripts": { | ||
"format": "prettier --write .", | ||
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts,.vue --fix", | ||
|
@@ -17,7 +20,8 @@ | |
"postinstall": "electron-builder install-app-deps", | ||
"build:win": "npm run build && electron-builder --win --config", | ||
"build:mac": "npm run build && electron-builder --mac --config", | ||
"build:linux": "npm run build && electron-builder --linux --config" | ||
"build:linux": "npm run build && electron-builder --linux --config", | ||
"test": "vitest" | ||
}, | ||
"dependencies": { | ||
"@electron-toolkit/preload": "^2.0.0", | ||
|
@@ -31,6 +35,8 @@ | |
"electron-updater": "^6.1.1", | ||
"pinia": "^2.1.7", | ||
"sass": "^1.69.5", | ||
"socksv5": "^0.0.6", | ||
"ssh2": "^1.16.0", | ||
"vee-validate": "^4.12.7", | ||
"vue-router": "^4.2.5", | ||
"vuetify": "^3.4.6", | ||
|
@@ -42,7 +48,7 @@ | |
"@electron-toolkit/eslint-config-ts": "^1.0.0", | ||
"@electron-toolkit/tsconfig": "^1.0.1", | ||
"@rushstack/eslint-patch": "^1.3.3", | ||
"@types/node": "^18.17.5", | ||
"@types/node": "^22.7.4", | ||
"@vitejs/plugin-vue": "^4.3.1", | ||
"@vue/eslint-config-prettier": "^8.0.0", | ||
"@vue/eslint-config-typescript": "^11.0.3", | ||
|
@@ -56,6 +62,7 @@ | |
"typescript": "^5.1.6", | ||
"vite": "^4.4.9", | ||
"vite-plugin-vuetify": "^2.0.1", | ||
"vitest": "^2.1.2", | ||
"vue": "^3.3.4", | ||
"vue-tsc": "^1.8.8" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import EventEmitter from 'node:events' | ||
import ssh2 from 'ssh2' | ||
|
||
export enum SSHEvent { | ||
Auth = 'auth', | ||
Connect = 'connect', | ||
Error = 'error', | ||
Disconnect = 'disconnect' | ||
} | ||
|
||
export class SSHEmitter extends EventEmitter {} | ||
|
||
export type SSHConnectionAuth = { | ||
host: string | ||
username: string | ||
password: string | ||
namespace: string | ||
device: string | ||
} | ||
|
||
export type SSHLocalPortForwardSettings = { | ||
sourceAddr: string | ||
sourcePort: number | ||
destinationAddr: string | ||
destinationPort: number | ||
} | ||
|
||
export type SSHDynamicPortForwardSettings = { | ||
destinationAddr: string | ||
destinationPort: number | ||
} | ||
|
||
export interface SSHConnection { | ||
events: SSHEmitter | ||
client: ssh2.Client | ||
connect(auth: SSHConnectionAuth): void | ||
disconnect(): void | ||
onAuth(callback: any): void | ||
onConnect(callback: any): void | ||
onError(callback: any): void | ||
onDisconnect(callback: any): void | ||
} | ||
|
||
export * from './ssh' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { describe, it, beforeEach, vi, expect } from 'vitest' | ||
import { | ||
SSHConnectionPortForward, | ||
SSHConnectionLocalPortForward, | ||
SSHConnectionDynamicPortForward | ||
} from './ssh' | ||
import { SSHEmitter, SSHEvent, SSHConnectionAuth } from './index.d' | ||
Check failure on line 7 in src/preload/ssh/ssh.test.ts
|
||
import net from 'node:net' | ||
import socks from 'socksv5' | ||
import ssh2 from 'ssh2' | ||
|
||
vi.mock('node:net') | ||
vi.mock('socksv5') | ||
vi.mock('ssh2') | ||
|
||
let sshClientMock: any | ||
let sshEmitterMock: any | ||
Check failure on line 17 in src/preload/ssh/ssh.test.ts
|
||
let serverMock: any | ||
|
||
beforeEach(() => { | ||
sshClientMock = { | ||
connect: vi.fn(), | ||
end: vi.fn(), | ||
forwardOut: vi.fn(), | ||
on: vi.fn() | ||
} | ||
|
||
sshEmitterMock = new SSHEmitter() | ||
|
||
serverMock = { | ||
listen: vi.fn(), | ||
on: vi.fn(), | ||
close: vi.fn(), | ||
useAuth: vi.fn() | ||
} | ||
|
||
vi.spyOn(ssh2, 'Client').mockImplementation(() => sshClientMock) | ||
vi.spyOn(net, 'createServer').mockReturnValue(serverMock as any) | ||
vi.spyOn(socks, 'createServer').mockReturnValue(serverMock as any) | ||
}) | ||
|
||
describe('SSHConnectionPortForward', () => { | ||
it('should connect using provided auth', () => { | ||
const sshConnection = new SSHConnectionPortForward({}) | ||
const auth: SSHConnectionAuth = { | ||
host: 'localhost', | ||
username: 'user', | ||
password: 'pass', | ||
namespace: 'ns', | ||
device: 'dev' | ||
} | ||
|
||
sshConnection.connect(auth) | ||
expect(sshClientMock.connect).toHaveBeenCalledWith({ | ||
host: 'localhost', | ||
username: '[email protected]', | ||
password: 'pass' | ||
}) | ||
}) | ||
|
||
it('should emit error event on connection error', () => { | ||
const sshConnection = new SSHConnectionPortForward({}) | ||
const errorCallback = vi.fn() | ||
sshConnection.onError(errorCallback) | ||
|
||
const error = new Error('Connection failed') | ||
sshClientMock.connect.mockImplementation(() => { | ||
throw error | ||
}) | ||
|
||
sshConnection.connect({ | ||
host: 'localhost', | ||
username: 'user', | ||
password: 'pass', | ||
namespace: 'ns', | ||
device: 'dev' | ||
}) | ||
|
||
expect(errorCallback).toHaveBeenCalledWith(error) | ||
}) | ||
}) | ||
|
||
describe('SSHConnectionLocalPortForward', () => { | ||
it('should start local port forwarding', () => { | ||
const settings = { | ||
sourceAddr: '127.0.0.1', | ||
sourcePort: 8000, | ||
destinationAddr: '192.168.1.10', | ||
destinationPort: 8080 | ||
} | ||
|
||
const sshConnection = new SSHConnectionLocalPortForward(settings) | ||
Check failure on line 92 in src/preload/ssh/ssh.test.ts
|
||
sshClientMock.on.mock.calls.find((call) => call[0] === 'ready')[1]() | ||
|
||
expect(serverMock.listen).toHaveBeenCalledWith( | ||
settings.sourcePort, | ||
settings.sourceAddr, | ||
expect.any(Function) | ||
) | ||
}) | ||
}) | ||
|
||
describe('SSHConnectionDynamicPortForward', () => { | ||
it('should start dynamic port forwarding', () => { | ||
const settings = { | ||
destinationAddr: '127.0.0.1', | ||
destinationPort: 1080 | ||
} | ||
|
||
const sshConnection = new SSHConnectionDynamicPortForward(settings) | ||
Check failure on line 110 in src/preload/ssh/ssh.test.ts
|
||
sshClientMock.on.mock.calls.find((call) => call[0] === 'ready')[1]() | ||
|
||
expect(serverMock.listen).toHaveBeenCalledWith( | ||
settings.destinationPort, | ||
settings.destinationAddr, | ||
expect.any(Function) | ||
) | ||
}) | ||
|
||
it('should handle socks authentication', () => { | ||
const settings = { | ||
destinationAddr: '127.0.0.1', | ||
destinationPort: 1080 | ||
} | ||
|
||
new SSHConnectionDynamicPortForward(settings) | ||
|
||
expect(serverMock.useAuth).toHaveBeenCalledWith(socks.auth.None()) | ||
}) | ||
}) |
Oops, something went wrong.