Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/buttons/ServiceConnectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const ServiceConnectButton: React.FC = () => {
// The app the service belongs to, where it does: one whose service is ssh and that signs people in by certificate
// opens in our web SSH as well, with the person's own account (components/AppTerminal).
const serving = useServiceApp(device, service)
const byCertificate = opensTerminal(serving.app)
const byCertificate = opensTerminal(serving.app, serving.appService)
const gateway = !local && reach.checked && !!reach.name && (!!web || ssh) && !proxy
// A local port of this machine (services/localPorts), where it runs our daemon: asked for here, or open already —
// then it is what the panel shows, until it is disconnected.
Expand Down
18 changes: 11 additions & 7 deletions frontend/src/components/AppTerminal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const { apps, name, openTerminal, env } = vi.hoisted(() => ({
// The release a consent names is read from its catalogue entry; none here: the app's own capabilities.
vi.mock('../services/graphQLAppCatalog', () => ({ graphQLCatalogApp: () => Promise.resolve('UNSERVED') }))
vi.mock('../hooks/useDeviceApps', async () => {
const { isAppService, serviceApp } = await import('../helpers/appsHelper')
const { appServiceOf, isAppService, serviceApp } = await import('../helpers/appsHelper')
return {
useServiceApp: (device?: IDevice, service?: IService) => {
const read = device && isAppService(service) ? apps.value : {}
return { ...read, app: serviceApp(read.apps, service) }
const app = serviceApp(read.apps, service)
return { ...read, app, appService: appServiceOf(app, service) }
},
}
})
Expand Down Expand Up @@ -48,14 +49,16 @@ vi.mock('react-i18next', () => ({
import { AppTerminalAction, AppTerminalButton, AppTerminalOption } from './AppTerminal'
import { useServiceApp } from '../hooks/useDeviceApps'
import type { DeviceApp } from '../services/graphQLDeviceApps'
import { withServices } from '../test/deviceApps'
;(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true

// Any app whose service is ssh and that signs people in by certificate: not the console, nor named anywhere.
const shellService = { id: 'S1', name: 'Shell', typeID: 28, host: 'remoteit-app-acme-shell', port: 2222 } as IService
const sshService = { id: 'S2', name: 'SSH', typeID: 28, host: 'localhost', port: 22 } as IService
const device = (permissions = ['CONNECT', 'MANAGE', 'SCRIPTING']) =>
({ id: 'D1', name: 'Pi', permissions, services: [shellService, sshService] } as unknown as IDevice)
const app = (state: string, extra: Partial<DeviceApp> = {}): DeviceApp => ({
const app = (state: string, extra: Partial<DeviceApp> = {}): DeviceApp =>
withServices({
id: 'acme.shell',
name: 'Shell',
publisher: 'Acme',
Expand All @@ -74,7 +77,7 @@ const app = (state: string, extra: Partial<DeviceApp> = {}): DeviceApp => ({
version: null,
settings: [],
...extra,
})
})
const turn = vi.fn()

let root: Root
Expand Down Expand Up @@ -232,7 +235,8 @@ describe('AppTerminalButton — the service’s row in the device’s services',

describe('AppTerminalAction — Open terminal in the device’s Apps', () => {
it('while it runs, through its service’s port; nothing for an app that opens no terminal', async () => {
let container = await render(<AppTerminalAction device={device()} app={app('running')} />)
const action = (shell: DeviceApp) => <AppTerminalAction device={device()} app={shell} service={shell.services[0]} />
let container = await render(action(app('running')))
await click(container.querySelector('[data-app-terminal]'))
expect(openTerminal).toHaveBeenCalledWith({
name: 'pi-evan.on.remote.it',
Expand All @@ -241,10 +245,10 @@ describe('AppTerminalAction — Open terminal in the device’s Apps', () => {
service: 'S1',
})
act(() => root.unmount())
container = await render(<AppTerminalAction device={device()} app={app('starting')} />)
container = await render(action(app('starting')))
expect(container.innerHTML).toBe('')
act(() => root.unmount())
container = await render(<AppTerminalAction device={device()} app={app('running', { capabilities: [] })} />)
container = await render(action(app('running', { capabilities: [] })))
expect(container.innerHTML).toBe('')
})
})
49 changes: 27 additions & 22 deletions frontend/src/components/AppTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'
import type { TFunction } from 'i18next'
import { Button, Typography } from '@mui/material'
import browser from '../services/browser'
import { DeviceApp } from '../services/graphQLDeviceApps'
import { DeviceApp, DeviceAppService } from '../services/graphQLDeviceApps'
import { ServiceApp, useServiceApp } from '../hooks/useDeviceApps'
import { useAppSwitch } from './AppRootConsent'
import { useSubnetName } from '../hooks/useLocalSubnetName'
Expand All @@ -13,36 +13,40 @@ import { IconButton } from '../buttons/IconButton'
import { Link } from './Link'
import { Icon } from './Icon'

/* An app's service opened in our web SSH (pages/TerminalPage), for any app whose service is ssh and that signs people
in by remote.it certificate (helpers/sshHelper opensTerminal) — from wherever it is picked: the device's Apps
/* An app's service opened in our web SSH (pages/TerminalPage), for any app's service that is ssh where the app signs
people in by remote.it certificate (helpers/sshHelper opensTerminal) — from wherever it is picked: the device's Apps
section and the app's page there, its service's row in the device's services and its connect panel. Always the
same: the device's name in device subnets, the service's port, the person's own account and a certificate for it,
and the host key the device reports for the app, which the terminal checks. An option beside the service's own ways
to connect, which stay as they are. Nothing where the device has no name in device subnets or the API has no apps.
From the desktop and mobile apps it opens in the web portal, in the system browser (helpers/terminalHelper). */

function open(app: DeviceApp, name: string, port: number) {
if (!app.serviceId) return
openTerminal({ name, port, title: app.name, service: app.serviceId })
function open(app: DeviceApp, serviceId: string, name: string, port: number) {
openTerminal({ name, port, title: app.name, service: serviceId })
}

const label = (t: TFunction) =>
browser.isNative
? t('appTerminal.openInBrowser', 'Open in web SSH, in your browser')
: t('appTerminal.open', 'Open in web SSH')

const servicePort = (device: IDevice | undefined, app: DeviceApp) =>
device?.services?.find(service => service.id === app.serviceId)?.port
const servicePort = (device: IDevice | undefined, serviceId: string | null) =>
device?.services?.find(service => service.id === serviceId)?.port

/** Open terminal, in the device's Apps section and on the app's page there: while it runs. */
export const AppTerminalAction: React.FC<{ device: IDevice; app: DeviceApp }> = ({ device, app }) => {
/** Open terminal, in the device's Apps section and on the app's page there, for one of its services: while it runs. */
export const AppTerminalAction: React.FC<{ device: IDevice; app: DeviceApp; service: DeviceAppService }> = ({
device,
app,
service,
}) => {
const { t } = useTranslation()
const terminal = opensTerminal(app)
const terminal = opensTerminal(app, service)
const name = useSubnetName(terminal ? device.id : undefined)
const port = servicePort(device, app)
if (!terminal || app.state !== 'running' || !name || !port || !app.serviceId) return null
const port = servicePort(device, service.serviceId)
if (!terminal || app.state !== 'running' || !name || !port || !service.serviceId) return null
const serviceId = service.serviceId
return (
<Button size="small" variant="contained" onClick={() => open(app, name, port)} data-app-terminal>
<Button size="small" variant="contained" onClick={() => open(app, serviceId, name, port)} data-app-terminal>
{t('deviceApps.openTerminal', 'Open terminal')}
</Button>
)
Expand All @@ -51,11 +55,11 @@ export const AppTerminalAction: React.FC<{ device: IDevice; app: DeviceApp }> =
/** A service's row in its device's services: a terminal button while its app runs. */
export const AppTerminalButton: React.FC<{ device: IDevice; service: IService }> = ({ device, service }) => {
const { t } = useTranslation()
const { app } = useServiceApp(device, service)
const terminal = opensTerminal(app)
const { app, appService } = useServiceApp(device, service)
const terminal = opensTerminal(app, appService)
const name = useSubnetName(terminal ? device.id : undefined)
const port = service.port
if (!app || !terminal || app.state !== 'running' || !app.serviceId || !name || !port) return null
if (!app || !terminal || app.state !== 'running' || appService?.serviceId !== service.id || !name || !port) return null
return (
<IconButton
icon="terminal"
Expand All @@ -65,7 +69,7 @@ export const AppTerminalButton: React.FC<{ device: IDevice; service: IService }>
onClick={event => {
// The row opens the service's page: the button is not the row.
event.stopPropagation()
open(app, name, port)
open(app, service.id, name, port)
}}
/>
)
Expand All @@ -79,10 +83,10 @@ export const AppTerminalButton: React.FC<{ device: IDevice; service: IService }>
export const AppTerminalOption: React.FC<{ device?: IDevice; service?: IService; serving: ServiceApp }> = ({
device,
service,
serving: { app, switching, turn, setRelease },
serving: { app, appService, switching, turn, setRelease },
}) => {
const { t } = useTranslation()
const terminal = opensTerminal(app)
const terminal = opensTerminal(app, appService)
const name = useSubnetName(terminal ? device?.id : undefined)
const { flip, consent } = useAppSwitch({ turn, setRelease }, undefined, undefined, device?.accountId)
if (!app || !terminal || !name) return null
Expand All @@ -92,10 +96,11 @@ export const AppTerminalOption: React.FC<{ device?: IDevice; service?: IService;

const port = service?.port
if (app.state === 'running' && !switching) {
if (!app.serviceId || !port) return null
const serviceId = appService?.serviceId
if (!serviceId || !port) return null
return (
<Typography variant="caption" component="div" sx={{ marginTop: 0.75, textAlign: 'right' }}>
<Link onClick={() => open(app, name, port)}>
<Link onClick={() => open(app, serviceId, name, port)}>
<Icon name="terminal" size="sm" />
{label(t)}
</Link>
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/components/DeviceApps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ vi.mock('react-i18next', () => ({

import { DeviceApps } from './DeviceApps'
import type { DeviceApp, DeviceAppSetting } from '../services/graphQLDeviceApps'
import { withServices } from '../test/deviceApps'
;(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true

const setting = (more: Partial<DeviceAppSetting> = {}): DeviceAppSetting => ({
Expand All @@ -59,7 +60,8 @@ const setting = (more: Partial<DeviceAppSetting> = {}): DeviceAppSetting => ({
...more,
})

const consoleApp = (more: Partial<DeviceApp> = {}): DeviceApp => ({
const consoleApp = (more: Partial<DeviceApp> = {}): DeviceApp =>
withServices({
id: 'remoteit.console',
name: 'Console',
publisher: 'remote.it',
Expand All @@ -77,9 +79,10 @@ const consoleApp = (more: Partial<DeviceApp> = {}): DeviceApp => ({
sessionsOpen: 0,
settings: [setting()],
...more,
})
})

const backup = (more: Partial<DeviceApp> = {}): DeviceApp => ({
const backup = (more: Partial<DeviceApp> = {}): DeviceApp =>
withServices({
id: 'acme.backup',
name: 'Backup',
publisher: 'Acme',
Expand Down Expand Up @@ -111,7 +114,7 @@ const backup = (more: Partial<DeviceApp> = {}): DeviceApp => ({
setting({ name: 'keep', type: 'int', label: 'Keep this many', help: null, default: 7, value: 7 }),
],
...more,
})
})

const device = (permissions = ['VIEW', 'CONNECT', 'MANAGE', 'SCRIPTING']) =>
({
Expand Down
31 changes: 19 additions & 12 deletions frontend/src/components/DeviceApps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Typography,
} from '@mui/material'
import { useHistory } from 'react-router-dom'
import { DeviceApp, SCREENVIEW } from '../services/graphQLDeviceApps'
import { DeviceApp, DeviceAppService, SCREENVIEW } from '../services/graphQLDeviceApps'
import { useDeviceApps } from '../hooks/useDeviceApps'
import { useAppSwitch } from './AppRootConsent'
import { AppTerminalAction } from './AppTerminal'
Expand Down Expand Up @@ -175,7 +175,9 @@ export const DeviceApps: React.FC<{ device: IDevice }> = ({ device }) => {
// The switch and Open terminal act alone: a click on them is not the row's.
const actions = removing ? null : (
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }} onClick={event => event.stopPropagation()}>
<AppTerminalAction device={device} app={app} />
{app.services.map(service => (
<AppTerminalAction key={service.key ?? ''} device={device} app={app} service={service} />
))}
<Switch
checked={enabled}
disabled={
Expand Down Expand Up @@ -213,29 +215,34 @@ export const DeviceApps: React.FC<{ device: IDevice }> = ({ device }) => {
)
}

/* Open, for any app reached as a service (serviceId) while it runs: its service's connection, started and launched as
its application type launches (graphql makes the type from the manifest's protocol: http and https open in the
browser, vnc and rdp in a viewer, udp nothing: a port for the person's own UDP client), as any service's Connect
page does; said by its protocol (Open page, Open screen, Connect). One whose service
opens as a terminal (helpers/sshHelper opensTerminal) has Open terminal instead (components/AppTerminal). */
export const AppOpen: React.FC<{ device: IDevice; app: DeviceApp }> = ({ device, app }) => {
/* Open, for one of an app's services while the app runs and the service is served (serviceId): its connection,
started and launched as its application type launches (graphql makes the type from the manifest's protocol: http and
https open in the browser, vnc and rdp in a viewer, udp nothing: a port for the person's own UDP client), as any
service's Connect page does — a multi-port service's on its first port, its launch port; said by its protocol (Open
page, Open screen, Connect). One that opens as a terminal (helpers/sshHelper opensTerminal) has Open terminal
instead (components/AppTerminal). */
export const AppOpen: React.FC<{ device: IDevice; app: DeviceApp; service: DeviceAppService }> = ({
device,
app,
service,
}) => {
const { t } = useTranslation()
const history = useHistory()
if (!app.enabled || app.state !== 'running' || !app.serviceId) return null
if (!app.enabled || app.state !== 'running' || !service.serviceId) return null
return (
<Button
size="small"
variant="contained"
data-app-open
onClick={() =>
history.push({
pathname: `/devices/${device.id}/${app.serviceId}/connect`,
pathname: `/devices/${device.id}/${service.serviceId}/connect`,
// A udp service's connection is all it has: its port is for the person's own UDP client.
state: app.protocol === 'udp' ? { autoConnect: true } : { autoConnect: true, autoLaunch: true },
state: service.protocol === 'udp' ? { autoConnect: true } : { autoConnect: true, autoLaunch: true },
})
}
>
{openWord(t, app.protocol)}
{openWord(t, service.protocol)}
</Button>
)
}
Expand Down
22 changes: 20 additions & 2 deletions frontend/src/helpers/appsHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
stateLine,
stateWord,
withdrawal,
appServiceOf,
} from './appsHelper'
import type { DeviceApp } from '../services/graphQLDeviceApps'
import type { DeviceApp, DeviceAppService } from '../services/graphQLDeviceApps'
import { withServices } from '../test/deviceApps'

const t = ((_key: string, text: string, values?: any) =>
text.replace(/{{(\w+)}}/g, (_, k) => values?.[k] ?? '')) as unknown as TFunction
Expand Down Expand Up @@ -109,7 +111,7 @@ describe('an app’s icon and service, from what the API says — never its id',
expect(appIcon(null)).toBe('cube')
})

const app = (id: string, serviceId: string | null) => ({ id, serviceId } as DeviceApp)
const app = (id: string, serviceId: string | null) => withServices({ id, name: id, serviceId, protocol: null } as DeviceApp)

it('a service at an app’s host is looked for among its device’s apps; any other is no app’s', () => {
expect(appHost('acme.shell')).toBe('remoteit-app-acme-shell')
Expand All @@ -128,4 +130,20 @@ describe('an app’s icon and service, from what the API says — never its id',
expect(serviceApp([app('acme.shell', 'S5')], { id: 'S5', host: 'localhost' })).toBeUndefined()
expect(serviceApp(null, service)).toBeUndefined()
})

it('an app of several services: any of them is the app’s, and which of its services it is', () => {
const stream: DeviceAppService = {
key: 'rtsp', name: 'Stream', protocol: 'tcp', multiPort: true, port: null, portSetting: null, listeners: [], serviceId: 'S6', ports: ['8554', '5004-5005'],
}
const camera = withServices(app('acme.camera', 'S5'))
const both = { ...camera, services: [...camera.services, stream] }
const other = { id: 'S6', host: 'remoteit-app-acme-camera' }
expect(serviceApp([both], other)?.id).toBe('acme.camera')
expect(appServiceOf(both, other)?.key).toBe('rtsp')
expect(appServiceOf(both, { id: 'S5', host: 'remoteit-app-acme-camera' })?.key).toBeNull()
// Disabled, naming none: the primary, at the app's host.
const disabled = withServices({ ...app('acme.camera', null), services: undefined, protocol: 'http' })
expect(appServiceOf(disabled, { id: 'S5', host: 'remoteit-app-acme-camera' })?.key).toBeNull()
expect(appServiceOf(disabled, { id: 'S5', host: 'remoteit-app-acme-other' })).toBeUndefined()
})
})
Loading
Loading