Skip to content

Commit

Permalink
Merge pull request #72 from ItsRiprod/v0.9.3
Browse files Browse the repository at this point in the history
V0.9.3 update :P
  • Loading branch information
ItsRiprod authored Nov 17, 2024
2 parents 4cb08ac + 3c4d401 commit 892a35c
Show file tree
Hide file tree
Showing 66 changed files with 2,252 additions and 1,241 deletions.
362 changes: 314 additions & 48 deletions DeskThingServer/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion DeskThingServer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deskthing",
"version": "0.9.2",
"version": "0.9.3",
"description": "A DeskThing server UI to interface with the DeskThing car thing app",
"main": "./out/main/index.js",
"author": "Riprod",
Expand Down Expand Up @@ -35,6 +35,7 @@
"react-qr-code": "^2.0.15",
"react-rewards": "^2.0.4",
"react-router-dom": "^6.26.2",
"react-select": "^5.8.3",
"uuid": "^10.0.0",
"ws": "^8.17.1",
"zustand": "^5.0.0-rc.2"
Expand Down
34 changes: 11 additions & 23 deletions DeskThingServer/src/main/handlers/adbHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path'
import { execFile } from 'child_process'
import getPlatform from '../utils/get-platform'
import dataListener, { MESSAGE_TYPES } from '../utils/events'
import loggingStore from '../stores/loggingStore'
import settingsStore from '../stores/settingsStore'
import { LoggingData } from '@shared/types'
import { ReplyFn, MESSAGE_TYPES } from '@shared/types'

const isDevelopment = process.env.NODE_ENV === 'development'
const execPath = isDevelopment
Expand All @@ -25,45 +25,33 @@ const splitArgs = (str: string): string[] => {
return matches
}

export const handleAdbCommands = async (
command: string,
send?: (channel: string, ...args: LoggingData[]) => void
): Promise<string> => {
export const handleAdbCommands = async (command: string, replyFn?: ReplyFn): Promise<string> => {
const settings = await settingsStore.getSettings()
const useGlobalADB = settings.globalADB === true
dataListener.asyncEmit(
MESSAGE_TYPES.LOGGING,
useGlobalADB ? 'Using Global ADB' : 'Using Local ADB'
)
loggingStore.log(MESSAGE_TYPES.LOGGING, useGlobalADB ? 'Using Global ADB' : 'Using Local ADB')
return new Promise((resolve, reject) => {
execFile(
useGlobalADB ? 'adb' : adbPath,
splitArgs(command),
{ cwd: execPath },
(error, stdout, stderr) => {
console.log(error, stdout, stderr)
if (error) {
if (send) {
send('logging', {
replyFn &&
replyFn('logging', {
status: false,
data: 'Error Encountered!',
final: true,
final: false,
error: stderr
})
}
dataListener.asyncEmit(
MESSAGE_TYPES.ERROR,
`ADB Error: ${stderr}, ${command}, ${adbPath}`
)
loggingStore.log(MESSAGE_TYPES.ERROR, `ADB Error: ${stderr}, ${command}, ${adbPath}`)
reject(`ADB Error: ${stderr}, ${command}, ${adbPath}`)
} else {
if (send) {
send('logging', {
replyFn &&
replyFn('logging', {
status: true,
data: 'ADB Success!',
final: true
final: false
})
}
resolve(stdout)
}
}
Expand Down
23 changes: 13 additions & 10 deletions DeskThingServer/src/main/handlers/appHandler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import path from 'path'
import { AppIPCData, ReplyFn } from '@shared/types/ipcTypes'
import dataListener, { MESSAGE_TYPES } from '../utils/events'
import {
App,
AppDataInterface,
AppReturnData,
AppIPCData,
ReplyFn,
MESSAGE_TYPES
} from '@shared/types'
import loggingStore from '../stores/loggingStore'
import { getData, setData } from './dataHandler'
import { dialog, BrowserWindow } from 'electron'
import { sendMessageToApp, AppHandler } from '../services/apps'
import { App, AppDataInterface, AppReturnData } from '@shared/types'
const appStore = AppHandler.getInstance()

export const appHandler: Record<
Expand Down Expand Up @@ -109,7 +115,7 @@ export const appHandler: Record<
return { path: filePath, name: path.basename(filePath) }
},
'dev-add-app': async (data, replyFn) => {
dataListener.asyncEmit(
loggingStore.log(
MESSAGE_TYPES.ERROR,
'Developer App Not implemented Yet ',
data.payload.appPath
Expand All @@ -118,7 +124,6 @@ export const appHandler: Record<
replyFn('logging', { status: true, data: 'Finished', final: true })
},
'send-to-app': async (data, replyFn) => {
console.log('sending data to app: ', data.payload.app, data)
await sendMessageToApp(data.payload.app, data.payload)
replyFn('logging', { status: true, data: 'Finished', final: true })
},
Expand All @@ -130,16 +135,14 @@ export const appHandler: Record<

const getApps = (replyFn: ReplyFn): App[] => {
replyFn('logging', { status: true, data: 'Getting data', final: false })
console.log('Getting app data')
const data = appStore.getAllBase()
replyFn('logging', { status: true, data: 'Finished', final: true })
replyFn('app-data', { status: true, data: data, final: true })
return data
}

const setAppData = async (replyFn, id, data: AppDataInterface): Promise<void> => {
console.log('Saving app data: ', data)
dataListener.asyncEmit(MESSAGE_TYPES.LOGGING, 'SERVER: Saving ' + id + "'s data " + data)
const setAppData = async (replyFn: ReplyFn, id, data: AppDataInterface): Promise<void> => {
loggingStore.log(MESSAGE_TYPES.LOGGING, 'SERVER: Saving ' + id + "'s data " + data)
await setData(id, data)
replyFn('logging', { status: true, data: 'Finished', final: true })
}
Expand All @@ -150,7 +153,7 @@ const getAppData = async (replyFn, payload): Promise<AppDataInterface | null> =>
replyFn('logging', { status: true, data: 'Finished', final: true })
return data
} catch (error) {
dataListener.asyncEmit(MESSAGE_TYPES.ERROR, 'SERVER: Error saving manifest' + error)
loggingStore.log(MESSAGE_TYPES.ERROR, 'SERVER: Error saving manifest' + error)
console.error('Error setting client manifest:', error)
replyFn('logging', { status: false, data: 'Unfinished', error: error, final: true })
return null
Expand Down
22 changes: 10 additions & 12 deletions DeskThingServer/src/main/handlers/authHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { sendMessageToApp } from '../services/apps' // Assuming you have an app
import http from 'http'
import url from 'url'
import settingsStore from '../stores/settingsStore'
import dataListener, { MESSAGE_TYPES } from '../utils/events'
import { Settings } from '@shared/types'
import loggingStore from '../stores/loggingStore'
import { Settings, MESSAGE_TYPES } from '@shared/types'

const successView = '<h1>Success</h1><p>You can now close this window.</p>'

Expand All @@ -14,7 +14,7 @@ let callBackPort: number
function handleCallback(req: http.IncomingMessage, res: http.ServerResponse): void {
const parsedUrl = url.parse(req.url || '', true)

dataListener.asyncEmit(
loggingStore.log(
MESSAGE_TYPES.LOGGING,
`AUTH: Received callback request for ${parsedUrl.pathname}`
)
Expand All @@ -29,15 +29,13 @@ function handleCallback(req: http.IncomingMessage, res: http.ServerResponse): vo

const appName = urlParts[1] // The app name should be the third part after '/callback/'
const config = getAppData() // Assuming getConfig() returns an object with active apps
console.log('AUTH DATA: ', config)
if (!config.apps || !config.apps.some((app) => app.name === appName && app.enabled)) {
res.writeHead(404, { 'Content-Type': 'text/html' })
res.end(`<h1>App Not Found</h1><p>App '${appName}' not found or not active.</p>`)
return
}

const code = parsedUrl.query.code as string
console.log('AUTH CODE: ', code)
sendMessageToApp(appName, { type: 'callback-data', payload: code })

res.writeHead(200, { 'Content-Type': 'text/html' })
Expand All @@ -47,11 +45,10 @@ function handleCallback(req: http.IncomingMessage, res: http.ServerResponse): vo
const startServer = async (): Promise<void> => {
if (server) {
await server.close(() => {
console.log('CALLBACK: Previous server closed.')
loggingStore.log(MESSAGE_TYPES.LOGGING, 'CALLBACK: Shutting down the server...')
})
}

console.log('CALLBACK: Starting server...')
server = http.createServer((req, res) => {
const parsedUrl = new URL(`http://${req.headers.host}${req.url}`)
const pathname = parsedUrl.pathname
Expand All @@ -63,9 +60,8 @@ const startServer = async (): Promise<void> => {
}
})

console.log('CALLBACK: Listening...')
server.listen(callBackPort, () => {
dataListener.asyncEmit(
loggingStore.log(
MESSAGE_TYPES.MESSAGE,
`CALLBACK: running at http://localhost:${callBackPort}/`
)
Expand All @@ -82,16 +78,18 @@ const initializeServer = async (): Promise<void> => {
}
}

dataListener.on(MESSAGE_TYPES.SETTINGS, (newSettings) => {
settingsStore.addListener((newSettings) => {
try {
if (newSettings.callbackPort != callBackPort) {
callBackPort = newSettings.callbackPort
startServer()
} else {
dataListener.asyncEmit(MESSAGE_TYPES.LOGGING, 'CALLBACK: Not starting - port is not changed')
loggingStore.log(MESSAGE_TYPES.LOGGING, 'CALLBACK: Not starting - port is not changed')
}
} catch (error) {
dataListener.asyncEmit(MESSAGE_TYPES.ERROR, 'CALLBACK: Error updating with settings', error)
if (error instanceof Error) {
loggingStore.log(MESSAGE_TYPES.ERROR, 'CALLBACK: Error updating with settings' + error)
}
}
})

Expand Down
79 changes: 49 additions & 30 deletions DeskThingServer/src/main/handlers/clientHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientIPCData, ReplyFn } from '@shared/types/ipcTypes'
import dataListener, { MESSAGE_TYPES } from '../utils/events'
import { ClientIPCData, ClientManifest, SocketData, ReplyFn, MESSAGE_TYPES } from '@shared/types'
import loggingStore from '../stores/loggingStore'
import { handleAdbCommands } from './adbHandler'
import {
configureDevice,
Expand All @@ -9,7 +9,6 @@ import {
HandleWebappZipFromUrl,
SetupProxy
} from './deviceHandler'
import { ClientManifest, SocketData } from '@shared/types'
import { sendMessageToClient, sendMessageToClients } from '../services/client/clientCom'

export const clientHandler: Record<
Expand All @@ -28,7 +27,11 @@ export const clientHandler: Record<
return `Pinging ${data.payload}...`
} catch (error) {
console.error('Error pinging client:', error)
dataListener.asyncEmit(MESSAGE_TYPES.ERROR, error)
if (error instanceof Error) {
loggingStore.log(MESSAGE_TYPES.ERROR, error.message)
} else {
loggingStore.log(MESSAGE_TYPES.ERROR, String(error))
}
return 'Error pinging' + data.payload
}
},
Expand All @@ -45,35 +48,31 @@ export const clientHandler: Record<
return handleUrl(data, replyFn)
},
adb: async (data, replyFn) => {
console.log('Running ADB command:', data.payload)
replyFn('logging', { status: true, data: 'Working', final: false })

const reply = async (status, data, final, error): Promise<void> => {
replyFn('logging', {
status: status,
data: data,
final: final,
error: error
})
}

return await handleAdbCommands(data.payload, reply)
const response = await handleAdbCommands(data.payload, replyFn)
replyFn('logging', { status: true, data: response, final: true })
return response
},
configure: async (data, replyFn) => {
replyFn('logging', { status: true, data: 'Configuring Device', final: false })
return await configureDevice(data.payload, replyFn)
const response = await configureDevice(data.payload, replyFn)
replyFn('logging', { status: true, data: 'Device Configured!', final: true })
return response
},
'client-manifest': async (data, replyFn) => {
if (data.request === 'get') {
return await getClientManifest(false, replyFn)
const response = await getClientManifest(replyFn)
replyFn('logging', { status: true, data: response, final: true })
return response
} else if (data.request === 'set') {
return await handleClientManifestUpdate(data.payload, replyFn)
}
return
},
'push-staged': async (data, replyFn) => {
try {
console.log('Pushing staged webapp...')
loggingStore.log(MESSAGE_TYPES.LOGGING, 'Pushing staged app...')
HandlePushWebApp(data.payload, replyFn)
} catch (error) {
replyFn('logging', {
Expand All @@ -82,14 +81,22 @@ export const clientHandler: Record<
error: 'Failed to push staged app!',
final: true
})
console.error('Error extracting zip file:', error)
dataListener.asyncEmit(MESSAGE_TYPES.ERROR, error)
if (error instanceof Error) {
loggingStore.log(MESSAGE_TYPES.ERROR, error.message)
} else {
loggingStore.log(MESSAGE_TYPES.ERROR, String(error))
}
}
},
'push-proxy-script': async (data, replyFn) => {
try {
console.log('Pushing proxy script...')
loggingStore.log(MESSAGE_TYPES.LOGGING, 'Pushing proxy script...')
SetupProxy(replyFn, data.payload)
replyFn('logging', {
status: true,
data: 'Proxy script pushed!',
final: true
})
} catch (error) {
replyFn('logging', {
status: false,
Expand All @@ -98,7 +105,11 @@ export const clientHandler: Record<
final: true
})
console.error('Error pushing proxy script:', error)
dataListener.asyncEmit(MESSAGE_TYPES.ERROR, error)
if (error instanceof Error) {
loggingStore.log(MESSAGE_TYPES.ERROR, error.message)
} else {
loggingStore.log(MESSAGE_TYPES.ERROR, String(error))
}
}
},
'run-device-command': async (data, replyFn) => {
Expand All @@ -110,29 +121,37 @@ export const clientHandler: Record<
request: data.payload.request,
payload: !payload.includes('{') ? data.payload.payload : JSON.parse(data.payload.payload)
}
console.log('Sending data', data)
replyFn('logging', { status: true, data: 'Finished', final: true })
return await sendMessageToClients(message)
}
}

const handleUrl = (data, replyFn: ReplyFn): void => {
const handleUrl = async (data, replyFn: ReplyFn): Promise<void> => {
try {
replyFn('logging', {
status: true,
data: 'Handling web app from URL',
final: false
})

const reply = async (channel: string, data): Promise<void> => {
replyFn(channel, data)
}

HandleWebappZipFromUrl(reply, data.payload)
await HandleWebappZipFromUrl(replyFn, data.payload)
replyFn('logging', { status: true, data: 'Successfully downloaded client', final: true })
} catch (error) {
console.error('Error extracting zip file:', error)
if (error instanceof Error) {
replyFn('zip-extracted', { status: false, error: error.message, data: null, final: true })
replyFn('logging', {
status: false,
error: error.message,
data: 'Error handling URL',
final: true
})
} else {
replyFn('logging', {
status: false,
error: 'Unable to download CLIENT',
data: 'Error handling URL',
final: true
})
}
}
}
Loading

0 comments on commit 892a35c

Please sign in to comment.