54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
"use strict";
|
|
|
|
const path = require("path");
|
|
const express = require("express");
|
|
const { convertTextToClashMihomoYAML } = require("./src/convert");
|
|
|
|
const app = express();
|
|
app.use(express.json({ limit: "2mb" }));
|
|
app.use(
|
|
express.text({ type: ["text/*", "application/octet-stream"], limit: "2mb" })
|
|
);
|
|
app.use(express.static(path.join(__dirname, "public")));
|
|
|
|
app.get("/", (req, res) => {
|
|
res.sendFile(path.join(__dirname, "public", "index.html"));
|
|
});
|
|
|
|
app.get("/convert/clash", async (req, res) => {
|
|
try {
|
|
const input = (req.query.uri || "").toString();
|
|
if (!input) return res.status(400).json({ error: "missing uri" });
|
|
const yamlText = await convertTextToClashMihomoYAML(input);
|
|
res.setHeader("Content-Type", "application/yaml; charset=utf-8");
|
|
res.send(yamlText);
|
|
} catch (err) {
|
|
res
|
|
.status(400)
|
|
.json({ error: err && err.message ? err.message : String(err) });
|
|
}
|
|
});
|
|
|
|
app.post("/convert/clash", async (req, res) => {
|
|
try {
|
|
const input =
|
|
typeof req.body === "string"
|
|
? req.body
|
|
: (req.body && req.body.input) || "";
|
|
if (!input) return res.status(400).json({ error: "missing input" });
|
|
const yamlText = await convertTextToClashMihomoYAML(input);
|
|
res.setHeader("Content-Type", "application/yaml; charset=utf-8");
|
|
res.send(yamlText);
|
|
} catch (err) {
|
|
res
|
|
.status(400)
|
|
.json({ error: err && err.message ? err.message : String(err) });
|
|
}
|
|
});
|
|
|
|
const port = process.env.PORT || 8080;
|
|
app.listen(port, () => {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`listening on http://127.0.0.1:${port}`);
|
|
});
|