const http = require('http');
const https = require('https');
const url = require('url');
const querystring = require('querystring');
const fs = require('fs');
const pathModule = require('path');
const crypto = require('crypto');

const requestTimeout = 10000; // 10 seconds
const cacheDir = pathModule.join(__dirname, '.cache');
const args = process.argv.slice(2);
const pathIndex = {};

let port = 9001;
let apiEndpoint = 'https://oss.x-php.com/alist/link';

// 解析命令行参数
args.forEach(arg => {
    // 去掉--
    if (arg.startsWith('--')) {
        arg = arg.substring(2);
    }
    const [key, value] = arg.split('=');
    if (key === 'port') {
        port = parseInt(value, 10);
    } else if (key === 'api') {
        apiEndpoint = value;
    }
});

// 确保缓存目录存在
if (!fs.existsSync(cacheDir)) {
    fs.mkdirSync(cacheDir);
}

// 定时清理过期缓存数据
setInterval(() => {
    const currentTime = Date.now();
    for (const key in pathIndex) {
        if (currentTime - pathIndex[key].timestamp > 24 * 60 * 60 * 1000) {
            delete pathIndex[key];
        }
    }
}, 60 * 60 * 1000); // 每隔 1 小时执行一次

// 处理请求并返回数据
const server = http.createServer(async (req, res) => {

    req.url = req.url.replace(/\/{2,}/g, '/');


    if (req.url === '/favicon.ico') {
        res.writeHead(204);
        res.end();
        return;
    }

    // 返回 endpoint, 缓存目录, 缓存数量, 用于监听服务是否正常运行
    if (req.url === '/endpoint') {
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
            code: 200,
            data: {
                api: apiEndpoint,
                port: port,
                cacheDir: cacheDir,
                pathIndexCount: Object.keys(pathIndex).length,
            }
        }));
        return;
    }

    const parsedUrl = url.parse(req.url, true);
    const reqPath = parsedUrl.pathname;
    const sign = parsedUrl.query.sign || '';

    if (!sign || reqPath === '/') {
        res.writeHead(400, { 'Content-Type': 'text/plain' });
        res.end('Bad Request: Missing sign or path (' + req.url + ')');
        return;
    }

    const uniqidhex = crypto.createHash('md5').update(reqPath + sign).digest('hex');

    let cacheMetaFile = '';
    let cacheContentFile = '';
    let tempCacheContentFile = '';

    if (pathIndex[uniqidhex]) {
        cacheMetaFile = pathModule.join(cacheDir, `${pathIndex[uniqidhex].uniqid}.meta`);
        cacheContentFile = pathModule.join(cacheDir, `${pathIndex[uniqidhex].uniqid}.content`);
    }

    if (pathIndex[uniqidhex] && isCacheValid(cacheMetaFile, cacheContentFile)) {
        serveFromCache(cacheMetaFile, cacheContentFile, res);
    } else {
        try {
            const apiData = await fetchApiData(reqPath, sign);
            if (apiData.code === 200 && apiData.data && apiData.data.url) {
                const { url: realUrl, cloudtype, expiration, path, headers, uniqid } = apiData.data;
                const data = { realUrl, cloudtype, expiration: expiration * 1000, path, headers, uniqid };

                // 修改 pathIndex 记录时,添加时间戳
                pathIndex[uniqidhex] = { uniqid: data.uniqid, timestamp: Date.now() };

                cacheMetaFile = pathModule.join(cacheDir, `${data.uniqid}.meta`);
                cacheContentFile = pathModule.join(cacheDir, `${data.uniqid}.content`);
                tempCacheContentFile = pathModule.join(cacheDir, `${data.uniqid}_${crypto.randomBytes(16).toString('hex')}.temp`);

                // 重新写入 meta 缓存
                fs.writeFileSync(cacheMetaFile, JSON.stringify(data));

                // 如果内容缓存存在, 则直接调用
                if (fs.existsSync(cacheContentFile)) {
                    serveFromCache(cacheMetaFile, cacheContentFile, res);
                } else {
                    fetchAndServe(data, tempCacheContentFile, cacheContentFile, res);
                }
            } else {
                res.writeHead(502, { 'Content-Type': 'text/plain' });
                res.end(apiData.message || 'Bad Gateway');
            }
        } catch (error) {
            res.writeHead(502, { 'Content-Type': 'text/plain' });
            res.end('Bad Gateway: Failed to decode JSON ' + error);
        }
    }
});

// 检查缓存是否有效
const isCacheValid = (cacheMetaFile, cacheContentFile) => {
    if (!fs.existsSync(cacheMetaFile) || !fs.existsSync(cacheContentFile)) return false;

    const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
    return cacheData.expiration > Date.now();
};

// 从 API 获取数据
const fetchApiData = (reqPath, sign) => {
    return new Promise((resolve, reject) => {
        const postData = querystring.stringify({ path: reqPath, sign });

        const apiReq = https.request(apiEndpoint, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Accept': 'application/json',
                'Content-Length': Buffer.byteLength(postData),
                'sign': sign
            },
            timeout: requestTimeout
        }, (apiRes) => {
            let data = '';
            apiRes.on('data', chunk => data += chunk);
            apiRes.on('end', () => {
                try {
                    resolve(JSON.parse(data));
                } catch (error) {
                    reject(error);
                }
            });
        });

        apiReq.on('error', reject);
        apiReq.write(postData);
        apiReq.end();
    });
};

// 从真实 URL 获取数据并写入缓存
const fetchAndServe = (data, tempCacheContentFile, cacheContentFile, res) => {
    https.get(data.realUrl, { timeout: requestTimeout * 10 }, (realRes) => {
        const cacheStream = fs.createWriteStream(tempCacheContentFile, { flags: 'w' });

        let isVideo = data.path && typeof data.path === 'string' && data.path.includes('.mp4');
        data.headers['content-length'] = realRes.headers['content-length'];
        res.writeHead(realRes.statusCode, {
            ...data.headers,
            'Cloud-Type': data.cloudtype,
            'Cloud-Expiration': data.expiration,
            'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
            'ETag': data.uniqid || '',
            'Cache-Control': 'public, max-age=31536000',
            'Expires': new Date(Date.now() + 31536000000).toUTCString(),
            'Accept-Ranges': 'bytes',
            'Connection': 'keep-alive',
            'Date': new Date().toUTCString(),
            'Last-Modified': new Date().toUTCString(),
        });

        realRes.pipe(cacheStream);
        realRes.pipe(res);

        realRes.on('end', () => {
            cacheStream.end();
            if (fs.existsSync(tempCacheContentFile)) {
                try {
                    fs.renameSync(tempCacheContentFile, cacheContentFile);
                } catch (err) {
                    console.error(`Error renaming file: ${err}`);
                }
            }
        });

        realRes.on('error', (e) => {
            handleResponseError(res, tempCacheContentFile, data.realUrl);
        });
    }).on('error', (e) => {
        handleResponseError(res, tempCacheContentFile, data.realUrl);
    });
};

// 从缓存中读取数据并返回
const serveFromCache = (cacheMetaFile, cacheContentFile, res) => {
    const cacheData = JSON.parse(fs.readFileSync(cacheMetaFile, 'utf8'));
    const readStream = fs.createReadStream(cacheContentFile);

    let isVideo = cacheData.path && typeof cacheData.path === 'string' && cacheData.path.includes('.mp4');
    cacheData.headers['content-length'] = fs.statSync(cacheContentFile).size;

    readStream.on('open', () => {
        res.writeHead(200, {
            ...cacheData.headers,
            'Cloud-Type': cacheData.cloudtype,
            'Cloud-Expiration': cacheData.expiration,
            'Content-Type': isVideo ? 'video/mp4' : 'application/octet-stream',
            'ETag': cacheData.uniqid || '',
            'Cache-Control': 'public, max-age=31536000',
            'Expires': new Date(Date.now() + 31536000000).toUTCString(),
            'Accept-Ranges': 'bytes',
            'Connection': 'keep-alive',
            'Date': new Date().toUTCString(),
            'Last-Modified': new Date().toUTCString(),
        });
        readStream.pipe(res);
    });

    readStream.on('error', (err) => {
        handleCacheReadError(res);
    });
};

// 处理响应错误
const handleResponseError = (res, tempCacheContentFile, realUrl) => {
    if (!res.headersSent) {
        res.writeHead(502, { 'Content-Type': 'text/plain' });
        res.end(`Bad Gateway: ${realUrl}`);
    }
    if (fs.existsSync(tempCacheContentFile)) {
        fs.unlinkSync(tempCacheContentFile);
    }
};

// 处理缓存读取错误
const handleCacheReadError = (res) => {
    if (!res.headersSent) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Internal Server Error: Unable to read cache content file');
    }
};

// 启动服务器
server.listen(port, () => {
    console.log(`Proxy server is running on http://localhost:${port}`);
});

// 处理 SIGINT 信号(Ctrl+C)
process.on('SIGINT', () => {
    console.log('Received SIGINT. Shutting down gracefully...');
    server.close(() => {
        console.log('Server closed.');
        process.exit(0);
    });

    setTimeout(() => {
        console.error('Forcing shutdown...');
        process.exit(1);
    }, 10000);
});