probably broken in some ways

This commit is contained in:
EthanShoeDev
2025-09-08 00:15:12 -04:00
parent efebb08ce4
commit 1be9f42001
3 changed files with 87 additions and 6 deletions

View File

@@ -1,8 +1,11 @@
import SSHClient, { PtyType } from '@dylankenneally/react-native-ssh-sftp' import SSHClient, { PtyType } from '@dylankenneally/react-native-ssh-sftp'
import { useRouter } from 'expo-router'
import { StyleSheet, Text, View } from 'react-native' import { StyleSheet, Text, View } from 'react-native'
import { useFresshAppForm } from '../lib/app-form' import { useFresshAppForm } from '../lib/app-form'
import { sshConnectionManager } from '../lib/ssh-connection-manager'
export default function Index() { export default function Index() {
const router = useRouter()
const connectionForm = useFresshAppForm({ const connectionForm = useFresshAppForm({
defaultValues: { defaultValues: {
// host: '', // host: '',
@@ -30,10 +33,14 @@ export default function Index() {
) )
console.log('Connected to SSH server') console.log('Connected to SSH server')
sshClientConnection.on('Shell', (data) => { // sshClientConnection.on('Shell', (data) => {
console.log(data) // console.log(data)
}) // })
await sshClientConnection.startShell(PtyType.XTERM) await sshClientConnection.startShell(PtyType.XTERM)
const sshConn = sshConnectionManager.addSession({
client: sshClientConnection,
})
router.push(`/shell`)
}, },
}, },
}) })
@@ -53,12 +60,22 @@ export default function Index() {
field={field} field={field}
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}
value={field.state.value}
onChangeText={field.handleChange}
onBlur={field.handleBlur}
/> />
)} )}
</connectionForm.AppField> </connectionForm.AppField>
<connectionForm.AppField name="port"> <connectionForm.AppField name="port">
{(field) => ( {(field) => (
<field.NumberField label="Port" placeholder="22" field={field} /> <field.NumberField
label="Port"
placeholder="22"
field={field}
value={field.state.value.toString()}
onChangeText={(text) => field.handleChange(Number(text))}
onBlur={field.handleBlur}
/>
)} )}
</connectionForm.AppField> </connectionForm.AppField>
<connectionForm.AppField name="username"> <connectionForm.AppField name="username">
@@ -69,6 +86,9 @@ export default function Index() {
field={field} field={field}
autoCapitalize="none" autoCapitalize="none"
autoCorrect={false} autoCorrect={false}
value={field.state.value}
onChangeText={field.handleChange}
onBlur={field.handleBlur}
/> />
)} )}
</connectionForm.AppField> </connectionForm.AppField>
@@ -79,6 +99,9 @@ export default function Index() {
placeholder="••••••••" placeholder="••••••••"
field={field} field={field}
secureTextEntry secureTextEntry
value={field.state.value}
onChangeText={field.handleChange}
onBlur={field.handleBlur}
/> />
)} )}
</connectionForm.AppField> </connectionForm.AppField>

View File

@@ -1,12 +1,30 @@
/** /**
* This is the page that is shown after an ssh connection * This is the page that is shown after an ssh connection
*/ */
import { useEffect, useState } from 'react'
import { Text, View } from 'react-native' import { Text, View } from 'react-native'
import { sshConnectionManager } from '../lib/ssh-connection-manager'
export default function Shell() { 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 ( return (
<View> <View style={{ flex: 1, margin: 16 }}>
<Text>Shell</Text> <Text>{shellData}</Text>
</View> </View>
) )
} }

View File

@@ -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<string, SSHConn>()
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,
}