Skip to content

Commit

Permalink
feat: upload certificate path
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoventura committed Oct 21, 2024
1 parent 2ccdd28 commit cf8d72f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/components/Settings/UpstreamProxySettings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldGroup } from '@/components/Form'
import { FieldGroup, FileUploadInput } from '@/components/Form'
import { AppSettings } from '@/types/settings'
import { TextField, Flex, Checkbox, Text } from '@radix-ui/themes'
import { Controller, useFormContext } from 'react-hook-form'
Expand All @@ -9,10 +9,20 @@ export function UpstreamProxySettings() {
control,
register,
formState: { errors },
setValue,
clearErrors,
} = useFormContext<AppSettings>()

const { proxy } = watch()

const handleSelectFile = async () => {
const result = await window.studio.settings.selectUpstreamCertificate()
const { canceled, filePaths } = result
if (canceled || !filePaths.length) return
setValue('proxy.certificatePath', filePaths[0], { shouldDirty: true })
clearErrors('proxy.certificatePath')
}

return (
<>
<FieldGroup
Expand Down Expand Up @@ -76,6 +86,14 @@ export function UpstreamProxySettings() {
</FieldGroup>
</>
)}

<FileUploadInput
name="proxy.certificatePath"
label="Certificate path (optional)"
onSelectFile={handleSelectFile}
buttonText="Select file"
hint="The location of the certificate file (PEM format) used to establish a trusted connection with the upstream server"
/>
</>
)
}
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ import find from 'find-process'
import { initializeLogger, openLogFolder } from './logger'
import log from 'electron-log/main'
import { AppSettings } from './types/settings'
import { getSettings, saveSettings, selectBrowserExecutable } from './settings'
import {
getSettings,
saveSettings,
selectBrowserExecutable,
selectUpstreamCertificate,
} from './settings'
import { ProxyStatus } from './types'

// handle auto updates
Expand Down Expand Up @@ -549,6 +554,10 @@ ipcMain.handle('settings:select-browser-executable', async () => {
return selectBrowserExecutable()
})

ipcMain.handle('settings:select-upstream-certificate', async () => {
return selectUpstreamCertificate()
})

ipcMain.handle('proxy:status:get', async () => {
console.info('proxy:status:get event received')
return proxyStatus
Expand Down
3 changes: 3 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ const settings = {
selectBrowserExecutable: (): Promise<Electron.OpenDialogReturnValue> => {
return ipcRenderer.invoke('settings:select-browser-executable')
},
selectUpstreamCertificate: (): Promise<Electron.OpenDialogReturnValue> => {
return ipcRenderer.invoke('settings:select-upstream-certificate')
},
}

const studio = {
Expand Down
1 change: 1 addition & 0 deletions src/schemas/appSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const UpstreamProxySettingsSchema = RegularProxySettingsSchema.extend({
requiresAuth: z.boolean(),
username: z.string().optional(),
password: z.string().optional(),
certificatePath: z.string().optional(),
})

export const ProxySettingsSchema = z
Expand Down
8 changes: 8 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ export async function selectBrowserExecutable() {
filters: [{ name: 'Executables', extensions }],
})
}

export async function selectUpstreamCertificate() {
return dialog.showOpenDialog({
title: 'Select certificate',
properties: ['openFile'],
filters: [{ name: 'PEM certificate', extensions: ['pem'] }],
})
}

0 comments on commit cf8d72f

Please sign in to comment.