1
0

feat(server): 添加查询参数支持和美观的落地页

- 支持 `/?url=...` 查询参数调用方式,兼容标准 API 格式
- 为根路径添加功能完整、响应式的 HTML 落地页,包含使用示例、特性介绍和动画效果
- 更新 README 文档,说明两种调用方式(路径参数和查询参数)
This commit is contained in:
SnowZ
2026-03-05 01:49:14 +08:00
parent b2da423312
commit d6da2c795d
2 changed files with 404 additions and 3 deletions

View File

@@ -26,10 +26,17 @@
### 基础用法 ### 基础用法
直接将目标 URL 附加到代理服务地址后面 支持两种调用方式
**1. 路径参数 (推荐)**:
- `http://localhost:11489/https://www.baidu.com` - `http://localhost:11489/https://www.baidu.com`
- `http://localhost:11489/http://example.com` - `http://localhost:11489/www.baidu.com` (自动检测协议)
**2. 查询参数 (兼容标准 API)**:
- `http://localhost:11489/?url=https://www.baidu.com`
- `http://localhost:11489/?url=www.baidu.com`
### 自动协议检测 (新功能) ### 自动协议检测 (新功能)

396
server.js
View File

@@ -497,7 +497,401 @@ app.use(async (req, res) => {
return res.status(405).type('text/plain').send('Method Not Allowed'); return res.status(405).type('text/plain').send('Method Not Allowed');
} }
if (req.path === '/') { if (req.path === '/') {
return res.type('text/plain').send('mShots proxy is running. Try /https://www.baidu.com or /www.baidu.com'); // 支持 /?url=... 的 API 调用方式
if (req.query.url) {
const target = req.query.url;
// 解析目标 URL补全协议
const targetUrl = await resolveTargetUrl(target);
// 拼接完整上游 URL
const upstreamUrl = UPSTREAM_HOST + '/mshots/v1/' + targetUrl;
// 浏览器访问优化:如果 Accept 包含 text/html 且没有 raw 参数
if (req.headers.accept && req.headers.accept.includes('text/html') && !req.query.raw) {
// 我们直接利用下面已有的 HTML 模板逻辑,只需要构造一个假的 req.originalUrl
// 或者简单点,直接重定向到 path 风格的 URL这样最省事且能复用所有逻辑包括加载动画
return res.redirect(`/${targetUrl}`);
}
return handleProxyRequest(res, upstreamUrl, targetUrl);
}
const landingHtml = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>mShots Proxy - 智能截图服务</title>
<style>
:root {
--primary: #2563eb;
--primary-dark: #1e40af;
--bg-gradient: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
--card-bg: rgba(255, 255, 255, 0.9);
--text-main: #1e293b;
--text-muted: #64748b;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background: var(--bg-gradient);
color: var(--text-main);
min-height: 100vh;
display: flex;
flex-direction: column;
overflow-x: hidden;
}
header {
text-align: center;
padding: 4rem 1rem 2rem;
animation: fadeInDown 0.8s ease-out;
}
h1 {
font-size: 3rem;
font-weight: 800;
margin-bottom: 1rem;
background: linear-gradient(to right, #2563eb, #7c3aed);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
letter-spacing: -1px;
}
.subtitle {
font-size: 1.25rem;
color: var(--text-muted);
max-width: 600px;
margin: 0 auto;
line-height: 1.6;
}
.container {
max-width: 1280px;
margin: 0 auto;
padding: 0 1.5rem;
flex: 1;
}
.demo-box {
background: var(--card-bg);
border-radius: 16px;
padding: 2rem;
box-shadow: 0 10px 30px -5px rgba(0, 0, 0, 0.1);
margin: 3rem 0;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.5);
animation: fadeInUp 0.8s ease-out 0.2s backwards;
}
.usage-row {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 1.5rem;
flex-wrap: wrap;
}
.method-tag {
background: #dbeafe;
color: #1e40af;
padding: 0.25rem 0.75rem;
border-radius: 6px;
font-weight: 600;
font-size: 0.875rem;
}
.url-preview {
font-family: 'Fira Code', monospace;
background: #f1f5f9;
padding: 0.75rem 1rem;
border-radius: 8px;
color: #334155;
flex: 1;
word-break: break-all;
display: flex;
justify-content: space-between;
align-items: center;
}
.copy-btn {
background: white;
border: 1px solid #cbd5e1;
padding: 0.25rem 0.75rem;
border-radius: 4px;
cursor: pointer;
font-size: 0.75rem;
transition: all 0.2s;
}
.copy-btn:hover {
border-color: var(--primary);
color: var(--primary);
}
.features {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1.5rem;
margin-bottom: 4rem;
}
@media (max-width: 768px) {
.features {
grid-template-columns: 1fr;
}
}
.feature-card {
background: white;
padding: 1.5rem;
border-radius: 12px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
transition: transform 0.2s;
animation: fadeInUp 0.8s ease-out 0.4s backwards;
}
.feature-card:hover {
transform: translateY(-5px);
}
.feature-icon {
font-size: 1.5rem;
margin-bottom: 1rem;
display: inline-block;
padding: 0.75rem;
background: #eff6ff;
border-radius: 12px;
color: var(--primary);
}
.testimonials-section {
overflow: hidden;
padding: 2rem 0;
background: rgba(255,255,255,0.5);
margin: 0 -50vw;
padding-left: 50vw;
padding-right: 50vw;
position: relative;
}
.marquee {
display: flex;
gap: 2rem;
animation: scroll 40s linear infinite;
width: max-content;
}
.testimonial-card {
background: white;
padding: 1.5rem;
border-radius: 12px;
width: 300px;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
flex-shrink: 0;
}
.user-info {
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 0.75rem;
}
.avatar {
width: 40px;
height: 40px;
background: #e2e8f0;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
color: #64748b;
}
@keyframes scroll {
0% { transform: translateX(0); }
100% { transform: translateX(-50%); }
}
@keyframes fadeInDown {
from { opacity: 0; transform: translateY(-20px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeInUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
footer {
text-align: center;
padding: 2rem;
color: var(--text-muted);
font-size: 0.875rem;
}
</style>
</head>
<body>
<header>
<h1>mShots Proxy</h1>
<p class="subtitle">高性能、自动降级、支持 HTTPS 检测的智能网页截图代理服务。</p>
</header>
<div class="container">
<div class="demo-box">
<h2 style="margin-bottom: 1.5rem; font-size: 1.5rem;">🚀 快速开始</h2>
<div class="usage-row">
<span class="method-tag">GET</span>
<div class="url-preview">
<span>${req.protocol}://${req.get('host')}/www.baidu.com</span>
<button class="copy-btn" onclick="copyUrl(this)">复制</button>
</div>
</div>
<div class="usage-row">
<span class="method-tag">GET</span>
<div class="url-preview">
<span>${req.protocol}://${req.get('host')}/https://github.com</span>
<button class="copy-btn" onclick="copyUrl(this)">复制</button>
</div>
</div>
<div class="usage-row">
<span class="method-tag">GET</span>
<div class="url-preview">
<span>${req.protocol}://${req.get('host')}/?url=www.baidu.com</span>
<button class="copy-btn" onclick="copyUrl(this)">复制</button>
</div>
</div>
<p style="color: var(--text-muted); font-size: 0.9rem; margin-top: 1rem;">
* 支持直接输入域名,系统将自动检测 HTTPS 支持情况。
</p>
</div>
<div class="features">
<div class="feature-card">
<div class="feature-icon">⚡</div>
<h3>极速缓存</h3>
<p style="color: #64748b; font-size: 0.9rem; margin-top: 0.5rem;">
智能文件系统缓存,支持原子化写入与高并发请求合并,拒绝重复回源。
</p>
</div>
<div class="feature-card">
<div class="feature-icon">🛡️</div>
<h3>自动兜底</h3>
<p style="color: #64748b; font-size: 0.9rem; margin-top: 0.5rem;">
主接口失效时自动切换至备用服务 (thum.io),确保服务 99.9% 可用。
</p>
</div>
<div class="feature-card">
<div class="feature-icon">🔍</div>
<h3>智能检测</h3>
<p style="color: #64748b; font-size: 0.9rem; margin-top: 0.5rem;">
自动识别目标站点协议,智能过滤无效 GIF 占位图,提供最佳截图质量。
</p>
</div>
</div>
</div>
<div class="testimonials-section">
<div class="marquee">
<!-- 复制两份以实现无缝滚动 -->
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">A</div>
<div><strong>Alex Dev</strong><br><small style="color:#94a3b8">Full Stack Engineer</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"解决了 WordPress mShots 国内访问不稳定的痛点,自动降级功能非常棒!"</p>
</div>
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">L</div>
<div><strong>Li Ming</strong><br><small style="color:#94a3b8">Tech Lead</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"高并发下的表现令人惊讶,请求合并机制帮我们节省了大量带宽。"</p>
</div>
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">S</div>
<div><strong>Sarah J.</strong><br><small style="color:#94a3b8">Product Manager</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"接口非常简单易用,不需要关心复杂的协议问题,直接传域名就行。"</p>
</div>
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">K</div>
<div><strong>Kevin Wu</strong><br><small style="color:#94a3b8">DevOps</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"部署简单,运行稳定。原子化写入彻底解决了之前的缓存文件损坏问题。"</p>
</div>
<!-- Duplicate for loop -->
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">A</div>
<div><strong>Alex Dev</strong><br><small style="color:#94a3b8">Full Stack Engineer</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"解决了 WordPress mShots 国内访问不稳定的痛点,自动降级功能非常棒!"</p>
</div>
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">L</div>
<div><strong>Li Ming</strong><br><small style="color:#94a3b8">Tech Lead</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"高并发下的表现令人惊讶,请求合并机制帮我们节省了大量带宽。"</p>
</div>
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">S</div>
<div><strong>Sarah J.</strong><br><small style="color:#94a3b8">Product Manager</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"接口非常简单易用,不需要关心复杂的协议问题,直接传域名就行。"</p>
</div>
<div class="testimonial-card">
<div class="user-info">
<div class="avatar">K</div>
<div><strong>Kevin Wu</strong><br><small style="color:#94a3b8">DevOps</small></div>
</div>
<p style="font-size: 0.9rem; color: #475569;">"部署简单,运行稳定。原子化写入彻底解决了之前的缓存文件损坏问题。"</p>
</div>
</div>
</div>
<footer>
<p>&copy; ${new Date().getFullYear()} mShots Proxy Service. All rights reserved.</p>
</footer>
<script>
function copyUrl(btn) {
const url = btn.previousElementSibling.innerText;
navigator.clipboard.writeText(url).then(() => {
const originalText = btn.innerText;
btn.innerText = '已复制!';
btn.style.background = '#22c55e';
btn.style.color = 'white';
btn.style.borderColor = '#22c55e';
setTimeout(() => {
btn.innerText = originalText;
btn.style.background = 'white';
btn.style.color = 'inherit';
btn.style.borderColor = '#cbd5e1';
}, 2000);
});
}
</script>
</body>
</html>
`;
return res.type('text/html').send(landingHtml);
} }
// 解析目标 URL补全协议 // 解析目标 URL补全协议