Provisiona RustDesk automaticamente
This commit is contained in:
parent
967d4bf1c6
commit
ef1db284fa
6 changed files with 565 additions and 9 deletions
|
|
@ -2261,6 +2261,87 @@ export const updateRemoteAccess = mutation({
|
|||
},
|
||||
})
|
||||
|
||||
export const upsertRemoteAccessViaToken = mutation({
|
||||
args: {
|
||||
machineToken: v.string(),
|
||||
provider: v.string(),
|
||||
identifier: v.string(),
|
||||
url: v.optional(v.string()),
|
||||
username: v.optional(v.string()),
|
||||
password: v.optional(v.string()),
|
||||
notes: v.optional(v.string()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const { machine } = await getActiveToken(ctx, args.machineToken)
|
||||
const trimmedProvider = args.provider.trim()
|
||||
const trimmedIdentifier = args.identifier.trim()
|
||||
if (!trimmedProvider || !trimmedIdentifier) {
|
||||
throw new ConvexError("Informe provedor e identificador do acesso remoto.")
|
||||
}
|
||||
|
||||
let normalizedUrl: string | null = null
|
||||
if (args.url) {
|
||||
const trimmedUrl = args.url.trim()
|
||||
if (trimmedUrl) {
|
||||
const isValidScheme = /^https?:\/\//i.test(trimmedUrl) || /^rustdesk:\/\//i.test(trimmedUrl)
|
||||
if (!isValidScheme) {
|
||||
throw new ConvexError("Informe uma URL iniciando com http://, https:// ou rustdesk://.")
|
||||
}
|
||||
try {
|
||||
new URL(trimmedUrl.replace(/^rustdesk:\/\//i, "https://"))
|
||||
} catch {
|
||||
throw new ConvexError("Informe uma URL válida para o acesso remoto.")
|
||||
}
|
||||
normalizedUrl = trimmedUrl
|
||||
}
|
||||
}
|
||||
|
||||
const cleanedUsername = args.username?.trim() ? args.username.trim() : null
|
||||
const cleanedPassword = args.password?.trim() ? args.password.trim() : null
|
||||
const cleanedNotes = args.notes?.trim() ? args.notes.trim() : null
|
||||
const timestamp = Date.now()
|
||||
const existingEntries = normalizeRemoteAccessList(machine.remoteAccess)
|
||||
const existingIndex = existingEntries.findIndex(
|
||||
(entry) =>
|
||||
entry.provider.toLowerCase() === trimmedProvider.toLowerCase() &&
|
||||
entry.identifier.toLowerCase() === trimmedIdentifier.toLowerCase()
|
||||
)
|
||||
|
||||
const entryId = existingIndex >= 0 ? existingEntries[existingIndex].id : createRemoteAccessId()
|
||||
const updatedEntry: RemoteAccessEntry = {
|
||||
id: entryId,
|
||||
provider: trimmedProvider,
|
||||
identifier: trimmedIdentifier,
|
||||
url: normalizedUrl,
|
||||
username: cleanedUsername,
|
||||
password: cleanedPassword,
|
||||
notes: cleanedNotes,
|
||||
lastVerifiedAt: timestamp,
|
||||
metadata: {
|
||||
provider: trimmedProvider,
|
||||
identifier: trimmedIdentifier,
|
||||
url: normalizedUrl,
|
||||
username: cleanedUsername,
|
||||
password: cleanedPassword,
|
||||
notes: cleanedNotes,
|
||||
lastVerifiedAt: timestamp,
|
||||
},
|
||||
}
|
||||
|
||||
const nextEntries =
|
||||
existingIndex >= 0
|
||||
? existingEntries.map((entry, index) => (index === existingIndex ? updatedEntry : entry))
|
||||
: [...existingEntries, updatedEntry]
|
||||
|
||||
await ctx.db.patch(machine._id, {
|
||||
remoteAccess: nextEntries,
|
||||
updatedAt: timestamp,
|
||||
})
|
||||
|
||||
return { remoteAccess: nextEntries }
|
||||
},
|
||||
})
|
||||
|
||||
export const remove = mutation({
|
||||
args: {
|
||||
machineId: v.id("machines"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue