Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ssh url handling #7614

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions .github/workflows/nodejs.yml
Expand Up @@ -8,30 +8,40 @@ on:
defaults:
run:
shell: bash
env:
NODE_VERSION: 18.x
jobs:
build:
runs-on: ${{matrix.os}}
strategy:
matrix:
node-version: [16.x]
os:
- macos-12
- ubuntu-18.04
- ubuntu-latest
- windows-latest
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/checkout@v4
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
node-version: ${{ env.NODE_VERSION }}
cache: yarn
cache-dependency-path: |
yarn.lock
app/yarn.lock
- name: Fix node-gyp and Python
run: python3 -m pip install packaging setuptools
- name: Install
run: yarn install
env:
npm_config_node_gyp: ${{ github.workspace }}${{ runner.os == 'Windows' && '\node_modules\node-gyp\bin\node-gyp.js' || '/node_modules/node-gyp/bin/node-gyp.js' }}
- name: Install libarchive-tools
if: runner.os == 'Linux'
run: |
sudo apt update
sudo apt install libarchive-tools
- name: Lint and Run Unit Tests
run: yarn run test
- name: Getting Build Icon
Expand Down
8 changes: 7 additions & 1 deletion app/index.ts
Expand Up @@ -25,11 +25,13 @@ import * as config from './config';
// set up config
config.setup();

import notify from './notify';
import * as plugins from './plugins';
import {installCLI} from './utils/cli-install';
import * as AppMenu from './menus/menu';
import {newWindow} from './ui/window';
import * as windowUtils from './utils/window-utils';
import parseUrl from 'parse-url';

const windowSet = new Set<BrowserWindow>([]);

Expand Down Expand Up @@ -233,6 +235,10 @@ app.on('open-file', (_event, path) => {

app.on('open-url', (_event, sshUrl) => {
GetWindow((win: BrowserWindow) => {
win.rpc.emit('open ssh', sshUrl);
try {
win.rpc.emit('open ssh', parseUrl(sshUrl));
} catch (e) {
notify('Invalid ssh url', 'Please check your ssh url and try again.', {error: e});
}
});
});
27 changes: 19 additions & 8 deletions app/ui/window.ts
@@ -1,4 +1,4 @@
import {app, BrowserWindow, shell, Menu, BrowserWindowConstructorOptions, Event} from 'electron';

Check warning on line 1 in app/ui/window.ts

View workflow job for this annotation

GitHub Actions / build (macos-12)

'Event' is defined but never used

Check warning on line 1 in app/ui/window.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'Event' is defined but never used

Check warning on line 1 in app/ui/window.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

'Event' is defined but never used
import {isAbsolute, normalize, sep} from 'path';
import {URL, fileURLToPath} from 'url';
import {v4 as uuidv4} from 'uuid';
Expand Down Expand Up @@ -275,22 +275,33 @@
}
});

const handleDrop = (event: Event, url: string) => {
const handleDroppedURL = (url: string) => {
const protocol = typeof url === 'string' && new URL(url).protocol;
if (protocol === 'file:') {
event.preventDefault();
const path = fileURLToPath(url);
rpc.emit('session data send', {data: path, escaped: true});
return {data: path, escaped: true};
} else if (protocol === 'http:' || protocol === 'https:') {
event.preventDefault();
rpc.emit('session data send', {data: url});
return {data: url};
}
};

// If file is dropped onto the terminal window, navigate and new-window events are prevented
// and his path is added to active session.
window.webContents.on('will-navigate', handleDrop);
window.webContents.on('new-window', handleDrop);
// and it's path is added to active session.
window.webContents.on('will-navigate', (event, url) => {
const data = handleDroppedURL(url);
if (data) {
event.preventDefault();
rpc.emit('session data send', data);
}
});
window.webContents.setWindowOpenHandler(({url}) => {
const data = handleDroppedURL(url);
if (data) {
rpc.emit('session data send', data);
return {action: 'deny'};
}
return {action: 'allow'};
});

// expose internals to extension authors
window.rpc = rpc;
Expand Down
9 changes: 7 additions & 2 deletions lib/actions/ui.ts
Expand Up @@ -292,12 +292,17 @@ export function leaveFullScreen(): HyperActions {
};
}

export function openSSH(url: string) {
export function openSSH(parsedUrl: ReturnType<typeof parseUrl>) {
return (dispatch: HyperDispatch) => {
dispatch({
type: UI_OPEN_SSH_URL,
effect() {
const parsedUrl = parseUrl(url, true);
const resourseIsValid = /^[a-zA-Z0-9.-]+$/.test(parsedUrl.resource);
if (!resourseIsValid) {
notify('Invalid ssh url', 'Please check your ssh url and try again.');
return;
}

let command = `${parsedUrl.protocol} ${parsedUrl.user ? `${parsedUrl.user}@` : ''}${parsedUrl.resource}`;

if (parsedUrl.port) command += ` -p ${parsedUrl.port}`;
Expand Down
4 changes: 0 additions & 4 deletions lib/ext-modules.d.ts
Expand Up @@ -2,10 +2,6 @@ declare module 'php-escape-shell' {
export function php_escapeshellcmd(path: string): string;
}

declare module 'parse-url' {
export default function (...args: any[]): any;
}

declare module 'react-deep-force-update' {
export default function (...args: any[]): any;
}
4 changes: 2 additions & 2 deletions lib/index.tsx
Expand Up @@ -204,8 +204,8 @@ rpc.on('open file', ({path}) => {
store_.dispatch(uiActions.openFile(path));
});

rpc.on('open ssh', (url) => {
store_.dispatch(uiActions.openSSH(url));
rpc.on('open ssh', (parsedUrl) => {
store_.dispatch(uiActions.openSSH(parsedUrl));
});

rpc.on('update available', ({releaseName, releaseNotes, releaseUrl, canInstall}) => {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -39,7 +39,6 @@
"ms": "2.1.3",
"open": "8.4.0",
"ora": "5.4.1",
"parse-url": "8.1.0",
"php-escape-shell": "1.0.0",
"react": "17.0.2",
"react-deep-force-update": "2.1.3",
Expand Down Expand Up @@ -103,7 +102,7 @@
"copy-webpack-plugin": "11.0.0",
"cpy-cli": "^3.1.1",
"cross-env": "7.0.3",
"electron": "20.3.6",
"electron": "22.3.25",
"electron-builder": "^23.3.3",
"electron-devtools-installer": "3.2.0",
"electron-link": "^0.6.0",
Expand All @@ -119,8 +118,8 @@
"husky": "8.0.2",
"inquirer": "9.1.4",
"node-addon-api": "5.0.0",
"node-gyp": "9.3.0",
"null-loader": "4.0.1",
"parse-url": "8.1.0",
"playwright": "1.27.1",
"plist": "3.0.6",
"prettier": "2.8.1",
Expand Down