File size: 4,048 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { apiFetch } from "../api.mjs";
import { emit } from "../output.mjs";
import { t } from "../i18n.mjs";

function fmtTs(v) {
  if (!v) return "-";
  return new Date(typeof v === "number" ? v * 1000 : v).toLocaleString();
}

async function mcpCall(name, args) {
  const res = await apiFetch("/api/mcp/tools/call", {
    method: "POST",
    body: { name, arguments: args },
  });
  if (!res.ok) {
    process.stderr.write(`MCP error: ${res.status}\n`);
    process.exit(1);
  }
  return res.json();
}

const proxySchema = [
  { key: "host", header: "Host", width: 35 },
  { key: "type", header: "Type", width: 8 },
  { key: "region", header: "Region" },
  { key: "latencyMs", header: "Latency", formatter: (v) => (v ? `${v}ms` : "-") },
  {
    key: "successRate",
    header: "Success%",
    formatter: (v) => (v ? `${(v * 100).toFixed(0)}%` : "-"),
  },
  { key: "lastUsed", header: "Last Used", formatter: fmtTs },
  { key: "state", header: "State" },
];

export function registerOneProxy(program) {
  const op = program.command("oneproxy").description(t("oneproxy.description"));

  op.command("status").action(async (opts, cmd) => {
    const data = await mcpCall("omniroute_oneproxy_stats", {});
    emit(data, cmd.optsWithGlobals());
  });

  op.command("stats")
    .option("--provider <p>", t("oneproxy.stats.provider"))
    .option("--period <p>", t("oneproxy.stats.period"), "24h")
    .action(async (opts, cmd) => {
      const data = await mcpCall("omniroute_oneproxy_stats", {
        provider: opts.provider,
        period: opts.period,
      });
      emit(data, cmd.optsWithGlobals());
    });

  op.command("fetch")
    .description(t("oneproxy.fetch.description"))
    .option("--count <n>", t("oneproxy.fetch.count"), parseInt, 1)
    .option("--type <t>", t("oneproxy.fetch.type"), "http")
    .action(async (opts, cmd) => {
      const data = await mcpCall("omniroute_oneproxy_fetch", {
        count: opts.count,
        type: opts.type,
      });
      emit(data.proxies ?? data, cmd.optsWithGlobals(), proxySchema);
    });

  op.command("rotate")
    .description(t("oneproxy.rotate.description"))
    .option("--provider <p>", t("oneproxy.rotate.provider"))
    .option("--connection-id <id>", t("oneproxy.rotate.connectionId"))
    .action(async (opts, cmd) => {
      const data = await mcpCall("omniroute_oneproxy_rotate", {
        provider: opts.provider,
        connectionId: opts.connectionId,
      });
      emit(data, cmd.optsWithGlobals());
    });

  const config = op.command("config").description(t("oneproxy.config.description"));

  config.command("show").action(async (opts, cmd) => {
    const res = await apiFetch("/api/settings/oneproxy");
    if (!res.ok) {
      process.stderr.write(`Error: ${res.status}\n`);
      process.exit(1);
    }
    emit(await res.json(), cmd.optsWithGlobals());
  });

  config
    .command("set")
    .option("--enabled <b>", t("oneproxy.config.enabled"), (v) => v === "true")
    .option("--pool-size <n>", t("oneproxy.config.poolSize"), parseInt)
    .option("--provider-source <url>", t("oneproxy.config.providerSource"))
    .option("--rotation-policy <p>", t("oneproxy.config.rotationPolicy"))
    .action(async (opts, cmd) => {
      const body = {};
      for (const k of ["enabled", "poolSize", "providerSource", "rotationPolicy"]) {
        if (opts[k] !== undefined) body[k] = opts[k];
      }
      const res = await apiFetch("/api/settings/oneproxy", { method: "PUT", body });
      if (!res.ok) {
        process.stderr.write(`Error: ${res.status}\n`);
        process.exit(1);
      }
      emit(await res.json(), cmd.optsWithGlobals());
    });

  op.command("pool")
    .description(t("oneproxy.pool.description"))
    .action(async (opts, cmd) => {
      const res = await apiFetch("/api/settings/oneproxy?include=pool");
      if (!res.ok) {
        process.stderr.write(`Error: ${res.status}\n`);
        process.exit(1);
      }
      const data = await res.json();
      emit(data.pool ?? data, cmd.optsWithGlobals(), proxySchema);
    });
}