From 1be9f420014691b9d0024e69f12bab18d2ed9a92 Mon Sep 17 00:00:00 2001 From: EthanShoeDev <13422990+EthanShoeDev@users.noreply.github.com> Date: Mon, 8 Sep 2025 00:15:12 -0400 Subject: [PATCH] probably broken in some ways --- src/app/index.tsx | 31 ++++++++++++++++++++---- src/app/shell.tsx | 22 +++++++++++++++-- src/lib/ssh-connection-manager.ts | 40 +++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/lib/ssh-connection-manager.ts diff --git a/src/app/index.tsx b/src/app/index.tsx index ba54e1b..096d446 100644 --- a/src/app/index.tsx +++ b/src/app/index.tsx @@ -1,8 +1,11 @@ import SSHClient, { PtyType } from '@dylankenneally/react-native-ssh-sftp' +import { useRouter } from 'expo-router' import { StyleSheet, Text, View } from 'react-native' import { useFresshAppForm } from '../lib/app-form' +import { sshConnectionManager } from '../lib/ssh-connection-manager' export default function Index() { + const router = useRouter() const connectionForm = useFresshAppForm({ defaultValues: { // host: '', @@ -30,10 +33,14 @@ export default function Index() { ) console.log('Connected to SSH server') - sshClientConnection.on('Shell', (data) => { - console.log(data) - }) + // sshClientConnection.on('Shell', (data) => { + // console.log(data) + // }) await sshClientConnection.startShell(PtyType.XTERM) + const sshConn = sshConnectionManager.addSession({ + client: sshClientConnection, + }) + router.push(`/shell`) }, }, }) @@ -53,12 +60,22 @@ export default function Index() { field={field} autoCapitalize="none" autoCorrect={false} + value={field.state.value} + onChangeText={field.handleChange} + onBlur={field.handleBlur} /> )} {(field) => ( - + field.handleChange(Number(text))} + onBlur={field.handleBlur} + /> )} @@ -69,6 +86,9 @@ export default function Index() { field={field} autoCapitalize="none" autoCorrect={false} + value={field.state.value} + onChangeText={field.handleChange} + onBlur={field.handleBlur} /> )} @@ -79,6 +99,9 @@ export default function Index() { placeholder="••••••••" field={field} secureTextEntry + value={field.state.value} + onChangeText={field.handleChange} + onBlur={field.handleBlur} /> )} diff --git a/src/app/shell.tsx b/src/app/shell.tsx index 953803d..d2303f7 100644 --- a/src/app/shell.tsx +++ b/src/app/shell.tsx @@ -1,12 +1,30 @@ /** * This is the page that is shown after an ssh connection */ +import { useEffect, useState } from 'react' import { Text, View } from 'react-native' +import { sshConnectionManager } from '../lib/ssh-connection-manager' export default function Shell() { + // https://docs.expo.dev/router/reference/url-parameters/ + // const { session: sessionId } = useLocalSearchParams<{ session: string }>() + const sessionId = '123' + const sshConn = sshConnectionManager.getSession({ sessionId }) // this throws if the session is not found + + const [shellData, setShellData] = useState('') + + useEffect(() => { + sshConn.client.on('Shell', (data) => { + setShellData((prev) => prev + data) + }) + // return () => { + // sshConn.client.off('Shell') + // } + }, [setShellData, sshConn.client]) + return ( - - Shell + + {shellData} ) } diff --git a/src/lib/ssh-connection-manager.ts b/src/lib/ssh-connection-manager.ts new file mode 100644 index 0000000..cb8837a --- /dev/null +++ b/src/lib/ssh-connection-manager.ts @@ -0,0 +1,40 @@ +import SSHClient from '@dylankenneally/react-native-ssh-sftp' + +export type SSHConn = { + client: SSHClient + sessionId: string + createdAt: Date +} + +const sshConnections = new Map() + +function addSession(params: { client: SSHClient }) { + // const sessionId = crypto.randomUUID() + const sessionId = '123' + const createdAt = new Date() + const sshConn: SSHConn = { + client: params.client, + sessionId, + createdAt, + } + sshConnections.set(sessionId, sshConn) + return sshConn +} + +function getSession(params: { sessionId: string }) { + const sshConn = sshConnections.get(params.sessionId) + if (!sshConn) throw new Error('Session not found') + return sshConn +} + +function removeAndDisconnectSession(params: { sessionId: string }) { + const sshConn = getSession(params) + sshConn.client.disconnect() + sshConnections.delete(params.sessionId) +} + +export const sshConnectionManager = { + addSession, + getSession, + removeAndDisconnectSession, +}