feat: 初始化proxy代理请求
This commit is contained in:
167
src/module/index.ts
Normal file
167
src/module/index.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import { getDNS, isLocalhost } from '@/utils/dns.ts';
|
||||
import http from 'http';
|
||||
import { UserApp } from './get-user-app.ts';
|
||||
import { useFileStore } from '@abearxiong/use-file-store';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
import { redis } from './redis/redis.ts';
|
||||
import { getContentType } from './get-content-type.ts';
|
||||
const { api, domain } = useConfig<{
|
||||
api: {
|
||||
host: string;
|
||||
};
|
||||
domain: string;
|
||||
}>();
|
||||
|
||||
const fileStore = useFileStore('upload');
|
||||
console.log('filePath', fileStore);
|
||||
const noProxyUrl = ['/', '/favicon.ico'];
|
||||
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
const dns = getDNS(req);
|
||||
|
||||
let user, app;
|
||||
let domainApp = false;
|
||||
if (isLocalhost(dns.hostName)) {
|
||||
// 本地开发环境 测试
|
||||
// user = 'root';
|
||||
// app = 'codeflow';
|
||||
// domainApp = true;
|
||||
} else {
|
||||
// 生产环境
|
||||
// 验证域名
|
||||
if (dns.hostName !== domain) {
|
||||
// redis获取域名对应的用户和应用
|
||||
domainApp = true;
|
||||
const key = 'domain:' + dns.hostName;
|
||||
const value = await redis.get(key);
|
||||
if (!value) {
|
||||
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
||||
res.write('Invalid domain\n');
|
||||
return res.end();
|
||||
}
|
||||
const [_user, _app] = value.split(':');
|
||||
if (!_user || !_app) {
|
||||
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
||||
res.write('Invalid domain, Config error\n');
|
||||
return res.end();
|
||||
}
|
||||
user = _user;
|
||||
app = _app;
|
||||
}
|
||||
}
|
||||
const url = req.url;
|
||||
if (!domainApp && noProxyUrl.includes(req.url)) {
|
||||
res.write('No proxy for this URL\n');
|
||||
return res.end();
|
||||
}
|
||||
if (!domainApp) {
|
||||
// 原始url地址
|
||||
const urls = url.split('/');
|
||||
if (urls.length < 3) {
|
||||
console.log('urls errpr', urls);
|
||||
res.writeHead(404, { 'Content-Type': 'text/html' });
|
||||
res.write('Invalid Proxy URL\n');
|
||||
return res.end();
|
||||
}
|
||||
const [_, _user, _app] = urls;
|
||||
if (!_user || !_app) {
|
||||
res.write('Invalid URL\n');
|
||||
return res.end();
|
||||
}
|
||||
user = _user;
|
||||
app = _app;
|
||||
}
|
||||
const [_, _api] = req.url.split('/');
|
||||
if (_api === 'api') {
|
||||
// 代理到 http://codeflow.xiongxiao.me/api
|
||||
// 设置代理请求的目标 URL 和请求头
|
||||
const options = {
|
||||
host: api.host,
|
||||
path: req.url,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': req.headers['content-type'],
|
||||
Authroization: req.headers?.['authorization'] || '',
|
||||
},
|
||||
};
|
||||
// 创建代理请求
|
||||
const proxyReq = http.request(options, (proxyRes) => {
|
||||
// 将代理服务器的响应头和状态码返回给客户端
|
||||
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
||||
// 将代理响应流写入客户端响应
|
||||
proxyRes.pipe(res, { end: true });
|
||||
});
|
||||
// 处理代理请求的错误事件
|
||||
proxyReq.on('error', (err) => {
|
||||
console.error(`Proxy request error: ${err.message}`);
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
||||
res.write(`Proxy request error: ${err.message}`);
|
||||
});
|
||||
// 处理 POST 请求的请求体(传递数据到目标服务器)
|
||||
req.pipe(proxyReq, { end: true });
|
||||
return;
|
||||
}
|
||||
const userApp = new UserApp({ user, app });
|
||||
let isExist = await userApp.getExist();
|
||||
if (!isExist) {
|
||||
try {
|
||||
const hasApp = await userApp.setCacheData();
|
||||
if (!hasApp) {
|
||||
res.writeHead(404, { 'Content-Type': 'text/html' });
|
||||
res.write('Not Found App\n');
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('setCacheData error', error);
|
||||
res.writeHead(500, { 'Content-Type': 'text/html' });
|
||||
res.write('Server Error\n');
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
isExist = await userApp.getExist();
|
||||
if (!isExist) {
|
||||
res.writeHead(404, { 'Content-Type': 'text/html' });
|
||||
res.write('Not Found App Index Page\n');
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
}
|
||||
const indexFile = isExist;
|
||||
let appFileUrl: string;
|
||||
if (domainApp) {
|
||||
appFileUrl = (url + '').replace(`/`, '');
|
||||
} else {
|
||||
appFileUrl = (url + '').replace(`/${user}/${app}/`, '');
|
||||
}
|
||||
const appFile = await userApp.getFile(appFileUrl);
|
||||
if (!appFile) {
|
||||
const [indexFilePath, etag] = indexFile.split('||');
|
||||
// 不存在的文件,返回indexFile的文件
|
||||
res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' });
|
||||
const filePath = path.join(fileStore, indexFilePath);
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
return;
|
||||
} else {
|
||||
const [appFilePath, eTag] = appFile.split('||');
|
||||
// 检查 If-None-Match 头判断缓存是否有效
|
||||
if (req.headers['if-none-match'] === eTag) {
|
||||
res.statusCode = 304; // 内容未修改
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
const filePath = path.join(fileStore, appFilePath);
|
||||
let contentType = getContentType(filePath);
|
||||
res.writeHead(200, {
|
||||
'Content-Type': contentType,
|
||||
'Cache-Control': 'public, max-age=3600', // 设置缓存时间为 1 小时
|
||||
ETag: eTag,
|
||||
});
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
return;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user