File size: 13,353 Bytes
7fc5208
 
 
 
 
 
 
 
 
 
 
 
 
 
025b1cc
 
8314b51
7fc5208
 
 
 
 
f83effb
8314b51
7fc5208
 
 
 
 
6c6d16c
 
8314b51
11a8eb2
 
 
7fc5208
 
 
 
 
 
 
 
 
 
6d5559e
 
7fc5208
 
 
 
 
 
 
 
 
 
 
 
025b1cc
7fc5208
 
 
 
 
 
 
6c6d16c
7fc5208
11a8eb2
 
 
 
 
 
 
7fc5208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f83effb
 
 
8314b51
 
 
7fc5208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c6d16c
 
 
 
 
 
8314b51
 
 
11a8eb2
 
 
 
 
 
7fc5208
11a8eb2
 
 
 
 
7fc5208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
025b1cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8314b51
3ed7406
 
 
8314b51
025b1cc
8314b51
025b1cc
 
 
 
 
 
 
 
 
f83effb
025b1cc
 
 
 
9dbf793
025b1cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fc5208
 
 
 
 
 
 
025b1cc
 
 
 
 
1877b95
7fc5208
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { Context, Hono } from 'hono'
import * as dotenv from 'dotenv'
import { cors } from "hono/cors";
import { compress } from "hono/compress";
import { prettyJSON } from "hono/pretty-json";
import { trimTrailingSlash } from "hono/trailing-slash";
import { serve } from '@hono/node-server'
import { createStorage } from "unstorage";
import cloudflareKVHTTPDriver from "unstorage/drivers/cloudflare-kv-http";
import { serveStatic } from '@hono/node-server/serve-static'

import path from 'path'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import { getTenantAccessToken, sendFeishuMessageText } from './functions/utils/feishu.js'
import { get_access_token, getEmails } from './functions/utils/mail.js'
import { activateMailboxSmart } from './functions/utils/mailboxActivator.js'

// 导入所有路由处理函数
import { onRequest as handleAccount } from './functions/api/account.js'
import { onRequest as handleLogin } from './functions/api/login.js'
import { onRequest as handleSetting } from './functions/api/setting.js'
import { onRequest as handleIP } from './functions/api/ip.js'
import { onRequest as handleMailLogin } from './functions/api/mail/login.js'
import { onRequest as handleMailAuth } from './functions/api/mail/auth.js'
import { onRequest as handleMailCallback } from './functions/api/mail/callback.js'
import { onRequest as handleMailAll } from './functions/api/mail/all.js'
import { onRequest as handleMailNew } from './functions/api/mail/new.js'
import { onRequest as handleMailSend } from './functions/api/mail/send.js'
import { onRequest as handleBatch } from './functions/api/mail/batch.js'
import { onRequest as handleMailStatus } from './functions/api/mail/status.js'
import { onRequest as handleMailActivate } from './functions/api/mail/activate.js'
import { onRequestGET as handleDebugList } from './functions/api/debug/list.js'
import { onRequestGET as handleDebugScreenshot } from './functions/api/debug/screenshot.js'
import { onRequestPOST as handleDebugCleanup } from './functions/api/debug/cleanup.js'
dotenv.config({ path: ['.env', '.env.local'], override: true });
const isDev = process.env.NODE_ENV === 'development'

const app = new Hono<{ Bindings: Env }>()
app.use(compress());
app.use(prettyJSON());
app.use(trimTrailingSlash());
app.use('*', cors({
    origin: '*',
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    allowHeaders: ['*'],  // Allow all headers
    exposeHeaders: ['*'], // Expose all headers
    credentials: true,
}));


const storage = createStorage({
    driver: cloudflareKVHTTPDriver({
        accountId: process.env.CLOUDFLARE_ACCOUNT_ID || "",
        namespaceId: process.env.CLOUDFLARE_NAMESPACE_ID || "",
        apiToken: process.env.CLOUDFLARE_API_TOKEN || "",
    }),
});

const kv: KVNamespace = {
    get: async (key: string) => {
        const value = await storage.getItemRaw(key);
        return value as string;
    },
    put: async (key: string, value: string) => {
        await storage.setItem(key, value);
    },
    delete: async (key: string) => {
        await storage.removeItem(key);
    },
    list: async (options?: { prefix?: string }) => {
        const keys = await storage.getKeys(options?.prefix);
        console.log(options?.prefix,keys)
        return {
            keys: keys.map(key => ({ name: key }))
        };
    }
};

app.use('*', async (c, next) => {
    c.env.KV = kv;
    await next()
})


const scriptPath = fileURLToPath(import.meta.url)
const scriptDir = dirname(scriptPath)
const rootDir = isDev ? dirname(scriptPath) : dirname(scriptDir)
const currentDir = process.cwd();
let staticPath = path.relative(currentDir, rootDir);
if (!isDev) {
    staticPath = path.relative(currentDir, path.join(rootDir, "dist"))
}
console.log('Script dir:', scriptDir)
console.log('Root dir:', rootDir)
console.log('Current dir:', currentDir);
console.log('Relative path for static files:', staticPath || '.');



const createContext = (c: Context) => {
    const eventContext: RouteContext = {
        request: c.req.raw,
        functionPath: c.req.path,
        waitUntil: (promise: Promise<any>) => {
            if (c.executionCtx?.waitUntil) {
                c.executionCtx.waitUntil(promise);
            }
        },
        passThroughOnException: () => {
            if (c.executionCtx?.passThroughOnException) {
                c.executionCtx.passThroughOnException();
            }
        },
        next: async (input?: Request | string, init?: RequestInit) => {
            if (typeof input === 'string') {
                return fetch(input, init);
            } else if (input instanceof Request) {
                return fetch(input);
            }
            return new Response('Not Found', { status: 404 });
        },
        env: {
            ...c.env,
            ASSETS: {
                fetch: fetch.bind(globalThis)
            }
        },
        params: c.req.param(),
        // 可以从 c.get() 获取数据,或者传入空对象
        data: c.get('data') || {}
    };
    return eventContext;
}

app.all('/api/*', async (c) => {
    try {
        const context = createContext(c);
        const path = c.req.path;
        // 根据路径匹配对应的处理函数
        let response: Response;
        switch (path) {
            case '/api/account':
                response = await handleAccount(context);
                break;
            case '/api/login':
                response = await handleLogin(context);
                break;
            case '/api/setting':
                response = await handleSetting(context);
                break;
            case '/api/ip':
                response = await handleIP(context);
                break;
            case '/api/mail/login':
                response = await handleMailLogin(context);
                break;
            case '/api/mail/auth':
                response = await handleMailAuth(context);
                break;
            case '/api/mail/callback':
                response = await handleMailCallback(context);
                break;
            case '/api/mail/all':
                response = await handleMailAll(context);
                break;
            case '/api/mail/new':
                response = await handleMailNew(context);
                break;
            case '/api/mail/send':
                response = await handleMailSend(context);
                break;
            case '/api/mail/status':
                response = await handleMailStatus(context);
                break;
            case '/api/mail/batch':
                response = await handleBatch(context);
                break;
            case '/api/mail/activate':
                response = await handleMailActivate(context);
                break;
            case '/api/debug/list':
                response = await handleDebugList(context);
                break;
            case '/api/debug/cleanup':
                response = await handleDebugCleanup(context);
                break;
            default:
                // 处理带参数的路由
                if (path.startsWith('/api/debug/screenshot')) {
                    response = await handleDebugScreenshot(context);
                    break;
                }
                return c.json({ error: 'Route not found' }, 404);
        }
        return response;
    } catch (error) {
        return c.json({ error: (error as Error).message }, 500);
    }
})


// 中间件配置
app.get('/*', serveStatic({
    root: staticPath,
    rewriteRequestPath: (path) => {
        return path === '/' ? '/index.html' : path;
    },
    onFound: async (path, c) => {
        console.log('Found:', path)
    },
    onNotFound: async (path, c) => {
        console.log('Not Found:', path)
    }
}))


// 邮箱监控功能
async function checkEmailsForNewMessages(env: Env) {
    try {
        // 获取设置
        const KV_KEY = "settings";
        const settingsStr = await env.KV.get(KV_KEY);
        if (!settingsStr) {
            console.log('未找到设置信息');
            return;
        }

        const settings = JSON.parse(settingsStr);
        if (!settings.emailMonitor || !Array.isArray(settings.emailMonitor) || settings.emailMonitor.length === 0) {
            console.log('未配置监控邮箱');
            return;
        }

        // 获取飞书访问令牌
        const feishuConfig = settings.feishu;
        if (!feishuConfig || !feishuConfig.app_id || !feishuConfig.app_secret || !feishuConfig.receive_id) {
            console.log('飞书配置不完整');
            return;
        }

        const tokenResponse = await getTenantAccessToken(feishuConfig.app_id, feishuConfig.app_secret);
        if (tokenResponse.code !== 0) {
            console.log('获取飞书访问令牌失败:', tokenResponse.msg);
            return;
        }

        const feishuToken = tokenResponse.tenant_access_token;
        const now = new Date();
        const currentTime = now.toISOString();

        // 遍历所有监控邮箱
        for (const monitor of settings.emailMonitor) {
            if (!monitor.email) continue;

            // 检查是否到了检查间隔
            let shouldCheck = false;
            if (monitor.lastCheckTime) {
                const lastCheck = new Date(monitor.lastCheckTime);
                const diffMinutes = Math.floor((now.getTime() - lastCheck.getTime()) / (1000 * 60));
                shouldCheck = diffMinutes >= (monitor.checkInterval || 60);
            } else {
                shouldCheck = true; // 如果从未检查过,则立即检查
            }

            if (shouldCheck) {
                try {
                    // 获取邮箱的访问令牌
                    const tokenInfoStr = await env.KV.get(`refresh_token_${monitor.email}`);
                    if (!tokenInfoStr) {
                        console.log(`邮箱 ${monitor.email} 未授权`);
                        continue;
                    }

                    const tokenInfo = JSON.parse(tokenInfoStr);
                    const access_token = await get_access_token(tokenInfo, env.ENTRA_CLIENT_ID, env.ENTRA_CLIENT_SECRET);

                    // 使用智能激活器激活邮箱
                    // console.log(`开始智能激活邮箱: ${monitor.email}`);
                    // const activationResult = await activateMailboxSmart(access_token);
                    // console.log(`邮箱 ${monitor.email} 激活结果:`, activationResult);

                    // 获取最新邮件
                    const emails = await getEmails(access_token, 1, false);
                    if (emails.length === 0) {
                        console.log(`邮箱 ${monitor.email} 没有邮件`);
                        continue;
                    }

                    const latestEmail = emails[0];

                    // 更新检查时间
                    monitor.lastCheckTime = currentTime;


                    // 获取最新邮件的时间戳
                    const latestEmailTime = new Date(latestEmail.date.received || latestEmail.date.created || latestEmail.date.sent || latestEmail.date.modified).getTime();
                    const lastEmailTime = monitor.lastEmailTimestamp ? new Date(monitor.lastEmailTimestamp).getTime() : 0;
                    //console.log(JSON.stringify(latestEmail),latestEmailTime,lastEmailTime)
                    // 如果最新邮件的时间戳大于用户设置的时间戳,且ID不同,则认为有新邮件
                    if (latestEmailTime > lastEmailTime && (!monitor.lastEmailId || monitor.lastEmailId !== latestEmail.id)) {
                        // 只更新最新邮件ID,不更新lastEmailTimestamp(该值由用户手动设置)
                        monitor.lastEmailId = latestEmail.id;

                        // 发送飞书通知
                        const notificationContent = `邮箱 ${monitor.email} 收到新邮件\n主题: ${latestEmail.subject}\n发件人: ${latestEmail.from}\n时间: ${latestEmail.date.received}\n预览: ${latestEmail.preview || '无预览内容'}`;

                        await sendFeishuMessageText(feishuToken, feishuConfig.receive_id, notificationContent);
                        console.log(`已发送邮箱 ${monitor.email} 的新邮件通知`);
                    } else {
                        console.log(`邮箱 ${monitor.email} 没有新邮件`);
                    }
                } catch (error) {
                    console.error(`检查邮箱 ${monitor.email} 失败:`, error);
                }
            }
        }

        // 保存更新后的设置
        await env.KV.put(KV_KEY, JSON.stringify(settings));
        console.log('邮箱监控完成');
    } catch (error) {
        console.error('邮箱监控出错:', error);
    }
}

// 启动服务器
const port = parseInt(process.env.PORT || '8788')
serve({
    fetch: (request: Request, env) => app.fetch(request, { ...env, ...process.env }),
    port
}, () => {
    console.log(`Server running at http://localhost:${port}`)

    // 启动定时监控邮箱
    setInterval(() => {
        const envWithKV = { ...process.env, KV: kv };
        checkEmailsForNewMessages(envWithKV as unknown as Env);
    }, 600000); // 每10分钟检查一次
})

export default app