Spaces:
Running
Running
File size: 6,324 Bytes
9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 105e682 9ed87a2 720fe41 430c991 9ed87a2 720fe41 9ed87a2 430c991 105e682 9ed87a2 73cb3e1 430c991 9ed87a2 105e682 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 720fe41 9ed87a2 73cb3e1 9ed87a2 73cb3e1 9ed87a2 |
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 |
// main.ts - Deno 版本的 ish2api 服务 (v1.0.2 - With Sponsor Adblock)
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
// 加载环境变量
const env = await load();
// --- 类型定义 ---
interface ChatMessage {
role: string;
content: string;
}
interface OpenAIChatRequest {
model: string;
messages: ChatMessage[];
max_tokens?: number;
temperature?: number;
stream?: boolean;
}
// --- 配置 ---
const POLLINATIONS_HEADERS = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Content-Type': 'application/json',
'Origin': 'https://ish.junioralive.in',
'Referer': 'https://ish.junioralive.in/',
'Sec-Ch-Ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'cross-site',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
};
const TARGET_URL = env.TARGET_URL || Deno.env.get("TARGET_URL") || "https://text.pollinations.ai/openai";
// --- 核心:流式代理函数 ---
async function* streamProxy(requestBody: OpenAIChatRequest): AsyncGenerator<Uint8Array> {
try {
const response = await fetch(TARGET_URL, {
method: "POST",
headers: POLLINATIONS_HEADERS,
body: JSON.stringify(requestBody),
});
if (!response.ok) {
const errorText = await response.text();
const errorDetails = {
error: {
message: `Upstream API error: ${response.status}`,
type: "upstream_error",
details: errorText
}
};
const errorMessage = `data: ${JSON.stringify(errorDetails)}\n\n`;
yield new TextEncoder().encode(errorMessage);
console.error(`Error from upstream API: ${response.status} - ${errorText}`);
return;
}
if (!response.body) {
throw new Error("Response body is null");
}
const reader = response.body.getReader();
const textDecoder = new TextDecoder();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
// =================== 广告过滤逻辑开始 ===================
// 将二进制块解码为字符串以便检查内容
const chunkStr = textDecoder.decode(value, { stream: true });
// 检查解码后的字符串是否包含 "Sponsor" 关键词
if (chunkStr.includes("Sponsor")) {
console.log("Sponsor content detected. Stopping the stream to the client.");
// 发现广告,立即中断循环,不再向客户端发送任何数据
break;
}
// 如果没有广告,将原始的二进制块转发给客户端
yield value;
// =================== 广告过滤逻辑结束 ===================
}
} finally {
reader.releaseLock();
}
} catch (error) {
const errorDetails = {
error: {
message: `An unexpected error occurred: ${error.message}`,
type: "proxy_error"
}
};
const errorMessage = `data: ${JSON.stringify(errorDetails)}\n\n`;
yield new TextEncoder().encode(errorMessage);
console.error(`An unexpected error occurred: ${error}`);
}
}
// --- 路由处理 ---
async function handleChatCompletions(request: Request): Promise<Response> {
try {
const payload = await request.json() as OpenAIChatRequest;
// 强制启用流式传输
const requestBody = {
...payload,
stream: true
};
console.log(`Forwarding request for model '${payload.model}' to ${TARGET_URL}`);
const stream = new ReadableStream({
async start(controller) {
try {
for await (const chunk of streamProxy(requestBody)) {
controller.enqueue(chunk);
}
controller.close();
} catch (error) {
controller.error(error);
}
}
});
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
});
} catch (error) {
console.error('Error parsing request:', error);
return new Response(JSON.stringify({
error: {
message: 'Invalid request format',
type: 'request_error'
}
}), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
}
// --- 主处理函数 ---
async function handler(request: Request): Promise<Response> {
const url = new URL(request.url);
const { pathname, method } = { pathname: url.pathname, method: request.method };
// 处理 CORS 预检请求
if (method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
});
}
// 路由分发
if (pathname === '/v1/chat/completions' && method === 'POST') {
return handleChatCompletions(request);
}
if (pathname === '/' && method === 'GET') {
return new Response(JSON.stringify({
message: "Pollinations OpenAI-Compatible Proxy is running. Use the /v1/chat/completions endpoint.",
version: "1.0.2",
target_url: TARGET_URL
}), {
headers: { 'Content-Type': 'application/json' }
});
}
// 404 处理
return new Response(JSON.stringify({
error: {
message: 'Not Found',
type: 'not_found'
}
}), {
status: 404,
headers: { 'Content-Type': 'application/json' }
});
}
// --- 启动服务器 ---
const port = parseInt(Deno.env.get("PORT") || "7860");
console.log("Starting Deno server...");
console.log(`Forwarding requests to: ${TARGET_URL}`);
console.log(`OpenAI compatible endpoint available at: http://127.0.0.1:${port}/v1/chat/completions`);
await serve(handler, { port }); |