proxylang 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 +31 -2
- package/dist/cli.js +48 -13
- package/dist/{host-CB08cTs0.js → host-Bea3np6i.js} +438 -22
- package/dist/index.d.ts +360 -203
- package/dist/index.js +165 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { A as slot, C as route, D as selectNodes, E as secret, F as deepFreeze, I as idSchema, L as nodeSchema, M as surge, N as use, O as sequence, P as assertData, S as reject, T as ruleSet, _ as group, a as explain, b as policy, c as nativeNameSchema, d as surgeProxy, f as validateProxyOptions, g as direct, h as defineConfig, i as compile, j as strategy, k as singbox, l as targetCatalog, m as block, n as render, o as compileStrategyGroups, p as StaticRuleSetUnknownError, r as secretReader, s as isReservedNativeName, t as buildToDirectory, u as RenderError, v as match, w as rule, x as proxy, y as mihomo } from "./host-
|
|
1
|
+
import { A as slot, B as defineProxyGroup, C as route, D as selectNodes, E as secret, F as deepFreeze, H as defineSurgeProfile, I as idSchema, L as nodeSchema, M as surge, N as use, O as sequence, P as assertData, S as reject, T as ruleSet, U as outbound, V as defineRoute, W as selectProxies, _ as group, a as explain, b as policy, c as nativeNameSchema, d as surgeProxy, f as validateProxyOptions, g as direct, h as defineConfig, i as compile, j as strategy, k as singbox, l as targetCatalog, m as block, n as render, o as compileStrategyGroups, p as StaticRuleSetUnknownError, r as secretReader, s as isReservedNativeName, t as buildToDirectory, u as RenderError, v as match, w as rule, x as proxy, y as mihomo, z as ProfileDefinitionError } from "./host-Bea3np6i.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { isIP } from "node:net";
|
|
4
4
|
import ipaddr from "ipaddr.js";
|
|
5
|
+
import { lstat, mkdir, readlink, rename, rm, symlink, writeFile } from "node:fs/promises";
|
|
6
|
+
import { join, resolve } from "node:path";
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
5
8
|
//#region src/proxy-fragments.ts
|
|
6
9
|
const inputSchema$1 = z.strictObject({
|
|
7
10
|
target: z.strictObject({
|
|
@@ -581,4 +584,164 @@ function renderSurgeProfiles(compilations, resolveSecret) {
|
|
|
581
584
|
return deepFreeze(compilations.map((c) => renderProfile(c, read)));
|
|
582
585
|
}
|
|
583
586
|
//#endregion
|
|
584
|
-
|
|
587
|
+
//#region src/surge-compose.ts
|
|
588
|
+
/** Bind shared names only for included resources; explicit input mappings remain compiler-checked. */
|
|
589
|
+
function surgeProfile(input, names = {}) {
|
|
590
|
+
const bindings = {};
|
|
591
|
+
for (const node of input.nodes) bindings[node.id] = names[node.id] ?? node.label ?? node.id;
|
|
592
|
+
for (const resource of [...input.tunnels ?? [], ...input.groups]) bindings[resource.id] = names[resource.id] ?? resource.id;
|
|
593
|
+
return {
|
|
594
|
+
...input,
|
|
595
|
+
names: {
|
|
596
|
+
...bindings,
|
|
597
|
+
...input.names
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
/** Stable queries produce native-profile policy references without consumer mapping code. */
|
|
602
|
+
function surgeMembers(nodes, query = {}) {
|
|
603
|
+
return selectNodes(nodes, query).map((ref) => ({ ref }));
|
|
604
|
+
}
|
|
605
|
+
//#endregion
|
|
606
|
+
//#region src/surge-publish.ts
|
|
607
|
+
var SurgePublishError = class extends Error {
|
|
608
|
+
published;
|
|
609
|
+
constructor(message, published) {
|
|
610
|
+
super(message);
|
|
611
|
+
this.published = published;
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
async function publishProfiles(files, surgeDirectory, io = { rename }) {
|
|
615
|
+
if (!files.size || files.size > 16 || [...files.keys()].some((name) => !/^[a-z0-9][a-z0-9-]*\.conf$/.test(name))) throw new Error("INVALID_PROFILE_FILENAMES");
|
|
616
|
+
const root = resolve(surgeDirectory);
|
|
617
|
+
const generations = join(root, ".generations");
|
|
618
|
+
const id = randomUUID();
|
|
619
|
+
const generation = join(generations, id);
|
|
620
|
+
const pointer = join(root, `.out-${id}`);
|
|
621
|
+
const output = join(root, "out");
|
|
622
|
+
const backup = join(generations, `legacy-${id}`);
|
|
623
|
+
const lock = join(root, ".build-lock");
|
|
624
|
+
const remove = io.rm ?? rm;
|
|
625
|
+
let failure;
|
|
626
|
+
let locked = false, created = false, committed = false, movedLegacy = false, recoveryFailed = false;
|
|
627
|
+
async function directory(path) {
|
|
628
|
+
await mkdir(path, {
|
|
629
|
+
recursive: true,
|
|
630
|
+
mode: 448
|
|
631
|
+
});
|
|
632
|
+
const stat = await lstat(path);
|
|
633
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("UNSAFE_OUTPUT_DIRECTORY");
|
|
634
|
+
}
|
|
635
|
+
try {
|
|
636
|
+
await directory(root);
|
|
637
|
+
try {
|
|
638
|
+
await mkdir(lock, { mode: 448 });
|
|
639
|
+
locked = true;
|
|
640
|
+
} catch (error) {
|
|
641
|
+
if (error.code === "EEXIST") throw new Error("OUTPUT_LOCKED");
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
644
|
+
await directory(generations);
|
|
645
|
+
let previous;
|
|
646
|
+
try {
|
|
647
|
+
previous = await lstat(output);
|
|
648
|
+
} catch (error) {
|
|
649
|
+
if (error.code !== "ENOENT") throw error;
|
|
650
|
+
}
|
|
651
|
+
if (previous?.isSymbolicLink()) {
|
|
652
|
+
const target = await readlink(output);
|
|
653
|
+
if (!/^\.generations\/(?:legacy-)?[a-f0-9-]{36}$/.test(target)) throw new Error("UNMANAGED_OUTPUT_LINK");
|
|
654
|
+
const stat = await lstat(join(root, target));
|
|
655
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("UNSAFE_OUTPUT_DIRECTORY");
|
|
656
|
+
} else if (previous && !previous.isDirectory()) throw new Error("UNSAFE_OUTPUT_DIRECTORY");
|
|
657
|
+
await mkdir(generation, { mode: 448 });
|
|
658
|
+
created = true;
|
|
659
|
+
for (const [name, content] of files) await writeFile(join(generation, name), content, {
|
|
660
|
+
mode: 384,
|
|
661
|
+
flag: "wx"
|
|
662
|
+
});
|
|
663
|
+
await symlink(`.generations/${id}`, pointer, "dir");
|
|
664
|
+
if (previous?.isDirectory()) {
|
|
665
|
+
await io.rename(output, backup);
|
|
666
|
+
movedLegacy = true;
|
|
667
|
+
}
|
|
668
|
+
try {
|
|
669
|
+
await io.rename(pointer, output);
|
|
670
|
+
committed = true;
|
|
671
|
+
} catch (error) {
|
|
672
|
+
if (movedLegacy) try {
|
|
673
|
+
await io.rename(backup, output);
|
|
674
|
+
movedLegacy = false;
|
|
675
|
+
} catch {
|
|
676
|
+
recoveryFailed = true;
|
|
677
|
+
throw new Error("PUBLISH_RECOVERY_REQUIRED");
|
|
678
|
+
}
|
|
679
|
+
throw error;
|
|
680
|
+
}
|
|
681
|
+
} catch (error) {
|
|
682
|
+
const code = error instanceof Error && /^(?:INVALID_PROFILE_FILENAMES|OUTPUT_LOCKED|UNMANAGED_OUTPUT_LINK|UNSAFE_OUTPUT_DIRECTORY|PUBLISH_RECOVERY_REQUIRED)$/.test(error.message) ? error.message : "PROFILE_PUBLISH_FAILED";
|
|
683
|
+
failure = new Error(code);
|
|
684
|
+
} finally {
|
|
685
|
+
if (!recoveryFailed) try {
|
|
686
|
+
await remove(pointer, { force: true });
|
|
687
|
+
if (created && !committed) await remove(generation, {
|
|
688
|
+
recursive: true,
|
|
689
|
+
force: true
|
|
690
|
+
});
|
|
691
|
+
if (locked) await remove(lock, {
|
|
692
|
+
recursive: true,
|
|
693
|
+
force: true
|
|
694
|
+
});
|
|
695
|
+
} catch {
|
|
696
|
+
failure ??= /* @__PURE__ */ new Error("PROFILE_PUBLISH_CLEANUP_FAILED");
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
if (failure) throw new SurgePublishError(failure.message === "PROFILE_PUBLISH_CLEANUP_FAILED" ? committed ? "PUBLISHED_CLEANUP_FAILED" : "FAILED_CLEANUP_REQUIRED" : failure.message, committed);
|
|
700
|
+
}
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region src/surge-host.ts
|
|
703
|
+
/** Complete-profile batches use the out symlink protocol; the Program host remains unchanged. */
|
|
704
|
+
async function buildSurgeProfilesToDirectory(profiles, output, resolveSecret = (token) => process.env[token.name]) {
|
|
705
|
+
if (typeof output !== "string" || !output.trim()) return {
|
|
706
|
+
published: false,
|
|
707
|
+
exitCode: 1,
|
|
708
|
+
code: "INVALID_OUTPUT_DIRECTORY"
|
|
709
|
+
};
|
|
710
|
+
try {
|
|
711
|
+
const entries = Object.entries(profiles);
|
|
712
|
+
if (!entries.length || entries.length > 16 || entries.some(([id]) => !/^[a-z0-9][a-z0-9-]*$/.test(id))) return {
|
|
713
|
+
published: false,
|
|
714
|
+
exitCode: 1,
|
|
715
|
+
code: "INVALID_PROFILE_FILENAMES"
|
|
716
|
+
};
|
|
717
|
+
const compiled = entries.map(([, input]) => compileSurgeProfile(input));
|
|
718
|
+
if (compiled.some((profile) => !profile.ok)) return {
|
|
719
|
+
published: false,
|
|
720
|
+
exitCode: 1,
|
|
721
|
+
code: "COMPILATION_FAILED"
|
|
722
|
+
};
|
|
723
|
+
const artifacts = renderSurgeProfiles(compiled, resolveSecret);
|
|
724
|
+
await publishProfiles(new Map(entries.map(([id], i) => [`${id}.conf`, artifacts[i].content])), output);
|
|
725
|
+
return {
|
|
726
|
+
published: true,
|
|
727
|
+
exitCode: 0,
|
|
728
|
+
code: "PUBLISHED",
|
|
729
|
+
active: join(resolve(output), "out")
|
|
730
|
+
};
|
|
731
|
+
} catch (error) {
|
|
732
|
+
if (error instanceof SurgePublishError) return {
|
|
733
|
+
published: error.published,
|
|
734
|
+
exitCode: error.published ? 2 : 1,
|
|
735
|
+
code: error.message,
|
|
736
|
+
...error.published ? { active: join(resolve(output), "out") } : {},
|
|
737
|
+
...error.message === "PUBLISH_RECOVERY_REQUIRED" ? { hint: "out may be absent; preserve the lock, backup, generation and temporary pointer for manual recovery" } : {}
|
|
738
|
+
};
|
|
739
|
+
return {
|
|
740
|
+
published: false,
|
|
741
|
+
exitCode: 1,
|
|
742
|
+
code: error instanceof RenderError ? error.code : "BUILD_FAILED"
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
//#endregion
|
|
747
|
+
export { ProfileDefinitionError, RenderError, StaticRuleSetUnknownError, block, buildSurgeProfilesToDirectory, buildToDirectory, compile, compileProxyNodes, compileStrategyGroups, compileSurgeProfile, defineConfig, defineProxyGroup, defineRoute, defineSurgeProfile, direct, explain, group, match, mihomo, outbound, policy, proxy, reject, render, renderProxyNodes, renderSurgeProfile, renderSurgeProfiles, route, rule, ruleSet, secret, selectNodes, selectProxies, sequence, singbox, slot, strategy, surge, surgeMembers, surgeProfile, targetCatalog, use };
|
package/package.json
CHANGED