Add ngrok tunnel
This commit is contained in:
@@ -2,6 +2,7 @@ import express from "express";
|
|||||||
import { createEvent, getGitRevision } from "./utils.mjs";
|
import { createEvent, getGitRevision } from "./utils.mjs";
|
||||||
import YouProvider from "./provider.mjs";
|
import YouProvider from "./provider.mjs";
|
||||||
import localtunnel from "localtunnel";
|
import localtunnel from "localtunnel";
|
||||||
|
import ngrok from 'ngrok';
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 8080;
|
const port = process.env.PORT || 8080;
|
||||||
@@ -438,28 +439,55 @@ app.listen(port, async () => {
|
|||||||
}
|
}
|
||||||
console.log(`Custom mode: ${process.env.USE_CUSTOM_MODE == "true" ? "enabled" : "disabled"}`);
|
console.log(`Custom mode: ${process.env.USE_CUSTOM_MODE == "true" ? "enabled" : "disabled"}`);
|
||||||
|
|
||||||
// 检查是否启用隧道
|
// 检查是否启用隧道
|
||||||
if (process.env.ENABLE_TUNNEL === "true") {
|
if (process.env.ENABLE_TUNNEL === "true") {
|
||||||
// 输出等待创建隧道的提示
|
const tunnelType = process.env.TUNNEL_TYPE || "localtunnel";
|
||||||
console.log("创建隧道中...");
|
console.log(`创建${tunnelType}隧道中...`);
|
||||||
|
|
||||||
// 设置隧道配置
|
if (tunnelType === "localtunnel") {
|
||||||
const tunnelOptions = { port: port };
|
// localtunnel
|
||||||
if (process.env.SUBDOMAIN) {
|
const tunnelOptions = { port: port };
|
||||||
tunnelOptions.subdomain = process.env.SUBDOMAIN;
|
if (process.env.SUBDOMAIN) {
|
||||||
}
|
tunnelOptions.subdomain = process.env.SUBDOMAIN;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const tunnel = await localtunnel(tunnelOptions);
|
const tunnel = await localtunnel(tunnelOptions);
|
||||||
console.log(`隧道已成功创建,可通过以下URL访问: ${tunnel.url}/v1`);
|
console.log(`隧道已成功创建,可通过以下URL访问: ${tunnel.url}/v1`);
|
||||||
|
|
||||||
tunnel.on("close", () => {
|
tunnel.on("close", () => {
|
||||||
console.log("已关闭隧道");
|
console.log("已关闭隧道");
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("创建隧道失败:", error);
|
console.error("创建隧道失败:", error);
|
||||||
}
|
}
|
||||||
}
|
} else if (tunnelType === "ngrok") {
|
||||||
|
// ngrok
|
||||||
|
try {
|
||||||
|
const ngrokOptions = {
|
||||||
|
addr: port,
|
||||||
|
authtoken: process.env.NGROK_AUTH_TOKEN
|
||||||
|
};
|
||||||
|
|
||||||
|
// 添加自定义域名
|
||||||
|
if (process.env.NGROK_CUSTOM_DOMAIN) {
|
||||||
|
ngrokOptions.hostname = process.env.NGROK_CUSTOM_DOMAIN;
|
||||||
|
} else if (process.env.SUBDOMAIN) {
|
||||||
|
ngrokOptions.subdomain = process.env.SUBDOMAIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = await ngrok.connect(ngrokOptions);
|
||||||
|
console.log(`隧道已成功创建,可通过以下URL访问: ${url}/v1`);
|
||||||
|
|
||||||
|
process.on('SIGTERM', async () => {
|
||||||
|
await ngrok.kill();
|
||||||
|
console.log("已关闭隧道");
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("创建隧道失败:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function AnthropicApiKeyAuth(req, res, next) {
|
function AnthropicApiKeyAuth(req, res, next) {
|
||||||
|
|||||||
Reference in New Issue
Block a user