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