2023-02-12 12:56:29 +01:00
|
|
|
import fs from 'unstorage/drivers/fs'
|
|
|
|
import memory from 'unstorage/drivers/memory'
|
2023-02-12 12:57:56 +01:00
|
|
|
import kv from 'unstorage/drivers/cloudflare-kv-http'
|
2022-12-22 03:12:37 +01:00
|
|
|
|
2022-12-19 00:13:01 +01:00
|
|
|
import { $fetch } from 'ofetch'
|
2022-11-24 00:12:25 +01:00
|
|
|
import type { Storage } from 'unstorage'
|
|
|
|
|
2023-02-08 21:00:43 +01:00
|
|
|
import cached from '../cache-driver'
|
2022-11-24 00:12:25 +01:00
|
|
|
|
2023-01-29 15:18:27 +01:00
|
|
|
// @ts-expect-error virtual import
|
|
|
|
import { env } from '#build-info'
|
2023-05-02 12:03:49 +02:00
|
|
|
|
2023-01-29 16:52:01 +01:00
|
|
|
// @ts-expect-error virtual import
|
|
|
|
import { driver } from '#storage-config'
|
2023-01-29 15:18:27 +01:00
|
|
|
|
2022-11-15 16:48:23 +01:00
|
|
|
import type { AppInfo } from '~/types'
|
2022-11-24 00:12:25 +01:00
|
|
|
import { APP_NAME } from '~/constants'
|
2022-11-15 15:29:46 +01:00
|
|
|
|
2022-11-24 00:12:25 +01:00
|
|
|
const storage = useStorage() as Storage
|
|
|
|
|
2023-01-29 16:52:01 +01:00
|
|
|
if (driver === 'fs') {
|
|
|
|
const config = useRuntimeConfig()
|
2022-11-30 22:17:20 +01:00
|
|
|
storage.mount('servers', fs({ base: config.storage.fsBase }))
|
2022-11-24 00:12:25 +01:00
|
|
|
}
|
2023-01-29 16:52:01 +01:00
|
|
|
else if (driver === 'cloudflare') {
|
|
|
|
const config = useRuntimeConfig()
|
2022-11-24 00:12:25 +01:00
|
|
|
storage.mount('servers', cached(kv({
|
|
|
|
accountId: config.cloudflare.accountId,
|
|
|
|
namespaceId: config.cloudflare.namespaceId,
|
|
|
|
apiToken: config.cloudflare.apiToken,
|
|
|
|
})))
|
|
|
|
}
|
2023-01-29 16:52:01 +01:00
|
|
|
else if (driver === 'memory') {
|
2023-01-16 22:52:50 +01:00
|
|
|
storage.mount('servers', memory())
|
|
|
|
}
|
2022-11-30 22:17:20 +01:00
|
|
|
|
2023-01-01 20:30:39 +01:00
|
|
|
export function getRedirectURI(origin: string, server: string) {
|
2023-02-12 13:18:02 +01:00
|
|
|
origin = origin.replace(/\?.*$/, '')
|
|
|
|
return `${origin}/api/${server}/oauth/${encodeURIComponent(origin)}`
|
2022-11-29 23:55:53 +01:00
|
|
|
}
|
2022-11-24 00:12:25 +01:00
|
|
|
|
2023-01-01 20:30:39 +01:00
|
|
|
async function fetchAppInfo(origin: string, server: string) {
|
2022-11-24 00:12:25 +01:00
|
|
|
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2023-01-29 15:18:27 +01:00
|
|
|
client_name: APP_NAME + (env !== 'release' ? ` (${env})` : ''),
|
2022-11-29 23:55:53 +01:00
|
|
|
website: 'https://elk.zone',
|
2023-01-01 20:30:39 +01:00
|
|
|
redirect_uris: getRedirectURI(origin, server),
|
2022-11-24 00:12:25 +01:00
|
|
|
scopes: 'read write follow push',
|
|
|
|
},
|
2022-11-15 16:48:23 +01:00
|
|
|
})
|
2022-11-24 00:12:25 +01:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:30:39 +01:00
|
|
|
export async function getApp(origin: string, server: string) {
|
2023-02-12 13:18:02 +01:00
|
|
|
const host = origin.replace(/^https?:\/\//, '').replace(/[^\w\d]/g, '-').replace(/\?.*$/, '')
|
|
|
|
const key = `servers:v3:${server}:${host}.json`.toLowerCase()
|
2022-11-24 00:12:25 +01:00
|
|
|
|
|
|
|
try {
|
2022-11-27 19:22:39 +01:00
|
|
|
if (await storage.hasItem(key))
|
|
|
|
return await storage.getItem(key) as Promise<AppInfo>
|
2023-01-01 20:30:39 +01:00
|
|
|
const appInfo = await fetchAppInfo(origin, server)
|
2022-11-24 00:12:25 +01:00
|
|
|
await storage.setItem(key, appInfo)
|
|
|
|
return appInfo
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return null
|
|
|
|
}
|
2022-11-15 15:29:46 +01:00
|
|
|
}
|
2022-12-24 01:07:38 +01:00
|
|
|
|
2023-01-15 10:34:17 +01:00
|
|
|
export async function deleteApp(server: string) {
|
2023-02-12 13:18:02 +01:00
|
|
|
const keys = (await storage.getKeys(`servers:v3:${server}:`))
|
2023-01-15 10:34:17 +01:00
|
|
|
for (const key of keys)
|
|
|
|
await storage.removeItem(key)
|
|
|
|
}
|
|
|
|
|
2022-12-24 01:07:38 +01:00
|
|
|
export async function listServers() {
|
2023-02-12 13:18:02 +01:00
|
|
|
const keys = await storage.getKeys('servers:v3:')
|
2022-12-24 01:07:38 +01:00
|
|
|
const servers = new Set<string>()
|
|
|
|
for await (const key of keys) {
|
2023-01-17 11:44:15 +01:00
|
|
|
const id = key.split(':')[2]
|
2022-12-24 01:07:38 +01:00
|
|
|
if (id)
|
2022-12-24 01:51:45 +01:00
|
|
|
servers.add(id.toLocaleLowerCase())
|
2022-12-24 01:07:38 +01:00
|
|
|
}
|
|
|
|
return Array.from(servers).sort()
|
|
|
|
}
|