2025-06-12 00:13:57 +08:00
|
|
|
import {Context, Hono} from 'hono'
|
|
|
|
import {KVNamespace} from '@cloudflare/workers-types';
|
|
|
|
import {serveStatic} from 'hono/cloudflare-workers' // @ts-ignore
|
|
|
|
import manifest from '__STATIC_CONTENT_MANIFEST'
|
2025-06-12 13:01:25 +08:00
|
|
|
import * as local from "hono/cookie";
|
2025-06-12 00:13:57 +08:00
|
|
|
|
|
|
|
export type Bindings = {
|
2025-06-12 13:01:25 +08:00
|
|
|
MAIN_URLS: string
|
2025-06-12 00:13:57 +08:00
|
|
|
}
|
|
|
|
const app = new Hono<{ Bindings: Bindings }>()
|
|
|
|
app.use("*", serveStatic({manifest: manifest, root: "./"}));
|
2025-06-12 15:08:01 +08:00
|
|
|
const driver_map: Record<string, string[]> = {
|
|
|
|
"onedrive_go": [
|
|
|
|
'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
|
|
|
|
'https://login.microsoftonline.com/common/oauth2/v2.0/token'
|
|
|
|
],
|
|
|
|
"onedrive_cn": [
|
|
|
|
'https://login.chinacloudapi.cn/common/oauth2/v2.0/authorize',
|
|
|
|
'https://microsoftgraph.chinacloudapi.cn/common/oauth2/v2.0/token'
|
|
|
|
],
|
|
|
|
"onedrive_de": [
|
|
|
|
'https://login.microsoftonline.de/common/oauth2/v2.0/authorize',
|
|
|
|
'https://graph.microsoft.de/common/oauth2/v2.0/token'
|
|
|
|
],
|
|
|
|
"onedrive_us": [
|
|
|
|
'https://login.microsoftonline.us/common/oauth2/v2.0/authorize',
|
|
|
|
'https://graph.microsoft.us/common/oauth2/v2.0/token'
|
|
|
|
],
|
|
|
|
}
|
2025-06-12 13:01:25 +08:00
|
|
|
// 登录申请 ##############################################################################
|
2025-06-12 00:13:57 +08:00
|
|
|
app.get('/onedrive/requests', async (c) => {
|
|
|
|
const client_uid = <string>c.req.query('client_uid');
|
|
|
|
const client_key = <string>c.req.query('client_key');
|
2025-06-12 15:08:01 +08:00
|
|
|
const driver_txt = <string>c.req.query('apps_type');
|
2025-06-12 00:13:57 +08:00
|
|
|
const scopes_all = 'offline_access Files.ReadWrite.All';
|
2025-06-12 15:08:01 +08:00
|
|
|
const client_url: string = driver_map[driver_txt][0];
|
2025-06-12 13:01:25 +08:00
|
|
|
// 请求参数 ==========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
const params_all: Record<string, any> = {
|
|
|
|
client_id: client_uid,
|
|
|
|
scope: scopes_all,
|
|
|
|
response_type: 'code',
|
|
|
|
redirect_uri: 'https://' + c.env.MAIN_URLS + '/onedrive/callback'
|
|
|
|
};
|
|
|
|
const urlWithParams = new URL(client_url);
|
|
|
|
Object.keys(params_all).forEach(key => {
|
|
|
|
urlWithParams.searchParams.append(key, params_all[key]);
|
|
|
|
});
|
2025-06-12 13:01:25 +08:00
|
|
|
// 执行请求 ===========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
try {
|
|
|
|
const response = await fetch(urlWithParams.href, {
|
|
|
|
method: 'GET',
|
|
|
|
});
|
2025-06-12 13:01:25 +08:00
|
|
|
local.setCookie(c, 'client_uid', client_uid);
|
|
|
|
local.setCookie(c, 'client_key', client_key);
|
2025-06-12 15:08:01 +08:00
|
|
|
local.setCookie(c, 'apps_types', driver_txt);
|
2025-06-12 00:13:57 +08:00
|
|
|
return c.json({text: response.url}, 200);
|
|
|
|
} catch (error) {
|
|
|
|
return c.json({text: error}, 500);
|
|
|
|
}
|
|
|
|
})
|
2025-06-12 13:01:25 +08:00
|
|
|
// 令牌申请 ##############################################################################
|
|
|
|
app.get('/onedrive/callback', async (c) => {
|
2025-06-12 00:13:57 +08:00
|
|
|
const login_data = <string>c.req.query('code');
|
2025-06-12 15:08:01 +08:00
|
|
|
const client_uid: string = <string>local.getCookie(c, 'client_uid')
|
|
|
|
const client_key: string = <string>local.getCookie(c, 'client_key')
|
|
|
|
const driver_txt: string = <string>local.getCookie(c, 'apps_types')
|
|
|
|
const client_url: string = driver_map[driver_txt][1];
|
2025-06-12 13:01:25 +08:00
|
|
|
console.log(login_data);
|
|
|
|
// 请求参数 ==========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
const params_all = {
|
|
|
|
client_id: client_uid,
|
|
|
|
client_secret: client_key,
|
|
|
|
redirect_uri: 'https://' + c.env.MAIN_URLS + '/onedrive/callback',
|
|
|
|
code: login_data,
|
|
|
|
grant_type: 'authorization_code'
|
|
|
|
};
|
2025-06-12 13:01:25 +08:00
|
|
|
// 执行请求 ===========================================================================
|
2025-06-12 00:13:57 +08:00
|
|
|
try {
|
2025-06-12 13:01:25 +08:00
|
|
|
const paramsString = new URLSearchParams(params_all).toString();
|
|
|
|
const response: Response = await fetch(client_url, {
|
2025-06-12 00:13:57 +08:00
|
|
|
method: 'POST',
|
2025-06-12 13:01:25 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
},
|
|
|
|
body: paramsString,
|
2025-06-12 00:13:57 +08:00
|
|
|
});
|
2025-06-12 13:01:25 +08:00
|
|
|
console.log(response);
|
|
|
|
if (!response.ok)
|
|
|
|
return c.json({text: response.text()}, 403);
|
|
|
|
const json: Record<string, any> = await response.json();
|
2025-06-12 00:13:57 +08:00
|
|
|
if (json.token_type === 'Bearer') {
|
2025-06-12 13:01:25 +08:00
|
|
|
return c.redirect(
|
|
|
|
`/?access_token=${json.access_token}`
|
|
|
|
+ `&refresh_token=${json.refresh_token}`
|
|
|
|
+ `&client_uid=${client_uid}`
|
|
|
|
+ `&client_key=${client_key}`);
|
2025-06-12 00:13:57 +08:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-06-12 13:01:25 +08:00
|
|
|
console.error(error);
|
|
|
|
return c.json({text: error}, 500);
|
2025-06-12 00:13:57 +08:00
|
|
|
}
|
2025-06-12 13:01:25 +08:00
|
|
|
})
|
|
|
|
|
2025-06-12 00:13:57 +08:00
|
|
|
|
|
|
|
export default app
|