@bitmacro/relay-agent 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -5
- package/dist/bin/relay-agent.mjs +37 -3
- package/dist/bin/relay-agent.mjs.map +1 -1
- package/dist/index.mjs +34 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2981,6 +2981,25 @@ async function readWhitelist() {
|
|
|
2981
2981
|
const content = await readFile(WHITELIST_PATH, "utf-8");
|
|
2982
2982
|
return content.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
2983
2983
|
}
|
|
2984
|
+
var PUBKEY_HEX_REGEX = /^[0-9a-f]{64}$/;
|
|
2985
|
+
function isValidPubkey(s) {
|
|
2986
|
+
return PUBKEY_HEX_REGEX.test(s.toLowerCase());
|
|
2987
|
+
}
|
|
2988
|
+
async function getPolicyEntries() {
|
|
2989
|
+
const lines = await readWhitelist();
|
|
2990
|
+
const entries = [];
|
|
2991
|
+
for (const line of lines) {
|
|
2992
|
+
if (line.startsWith("#") || !line) continue;
|
|
2993
|
+
if (line.startsWith("!")) {
|
|
2994
|
+
const pubkey2 = line.slice(1).toLowerCase();
|
|
2995
|
+
if (isValidPubkey(pubkey2)) entries.push({ pubkey: pubkey2, status: "blocked" });
|
|
2996
|
+
continue;
|
|
2997
|
+
}
|
|
2998
|
+
const pubkey = line.toLowerCase();
|
|
2999
|
+
if (isValidPubkey(pubkey)) entries.push({ pubkey, status: "allowed" });
|
|
3000
|
+
}
|
|
3001
|
+
return entries;
|
|
3002
|
+
}
|
|
2984
3003
|
async function writeWhitelist(lines) {
|
|
2985
3004
|
const dir = dirname(WHITELIST_PATH);
|
|
2986
3005
|
if (!existsSync(dir)) {
|
|
@@ -3079,6 +3098,14 @@ statsRoutes.get("/stats", async (c) => {
|
|
|
3079
3098
|
// src/routes/policy.ts
|
|
3080
3099
|
var PUBKEY_REGEX = /^[0-9a-f]{64}$/;
|
|
3081
3100
|
var policyRoutes = new Hono2();
|
|
3101
|
+
policyRoutes.get("/policy", async (c) => {
|
|
3102
|
+
try {
|
|
3103
|
+
const entries = await getPolicyEntries();
|
|
3104
|
+
return c.json({ entries });
|
|
3105
|
+
} catch {
|
|
3106
|
+
return c.json({ error: "relay unavailable" }, 503);
|
|
3107
|
+
}
|
|
3108
|
+
});
|
|
3082
3109
|
policyRoutes.post("/policy/block", async (c) => {
|
|
3083
3110
|
try {
|
|
3084
3111
|
const body = await c.req.json();
|
|
@@ -3132,12 +3159,18 @@ usersRoutes.get("/users", async (c) => {
|
|
|
3132
3159
|
|
|
3133
3160
|
// src/index.ts
|
|
3134
3161
|
var DEFAULT_ORIGINS = [
|
|
3135
|
-
"https://
|
|
3162
|
+
"https://relay-panel.bitmacro.cloud",
|
|
3163
|
+
"https://relay-panel.bitmacro.pro",
|
|
3136
3164
|
"http://localhost:3000"
|
|
3137
3165
|
];
|
|
3138
3166
|
var EXTRA_ORIGINS = (process.env.ALLOWED_ORIGINS ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
3139
3167
|
var ALLOWED_ORIGINS = [...DEFAULT_ORIGINS, ...EXTRA_ORIGINS];
|
|
3140
3168
|
var app = new Hono2();
|
|
3169
|
+
app.use("*", async (c, next) => {
|
|
3170
|
+
const start = Date.now();
|
|
3171
|
+
await next();
|
|
3172
|
+
console.log(`[relay-agent] ${c.req.method} ${c.req.path} ${c.res.status} ${Date.now() - start}ms`);
|
|
3173
|
+
});
|
|
3141
3174
|
app.use("*", cors({ origin: ALLOWED_ORIGINS }));
|
|
3142
3175
|
app.use("*", async (c, next) => {
|
|
3143
3176
|
if (c.req.path === "/health") return next();
|