codex-endpoint-switcher 1.1.0 → 1.2.0

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/src/web/server.js CHANGED
@@ -11,7 +11,8 @@ function wrapAsync(handler) {
11
11
  const data = await handler(req, res);
12
12
  res.json({ ok: true, data });
13
13
  } catch (error) {
14
- res.status(400).json({
14
+ const statusCode = Number(error?.statusCode || 400);
15
+ res.status(statusCode).json({
15
16
  ok: false,
16
17
  error: error instanceof Error ? error.message : String(error),
17
18
  });
@@ -19,6 +20,19 @@ function wrapAsync(handler) {
19
20
  };
20
21
  }
21
22
 
23
+ async function ensureConsoleAccess() {
24
+ const cloudStatus = await cloudSyncClient.getCloudSyncStatus();
25
+ if (cloudStatus.loggedIn) {
26
+ return cloudStatus;
27
+ }
28
+
29
+ const error = new Error(
30
+ cloudStatus.lastError || "请先登录同步账号,登录通过后才能进入连接控制台。",
31
+ );
32
+ error.statusCode = 401;
33
+ throw error;
34
+ }
35
+
22
36
  function createApp() {
23
37
  const app = express();
24
38
  const rendererRoot = path.join(__dirname, "../renderer");
@@ -33,6 +47,7 @@ function createApp() {
33
47
  app.get(
34
48
  "/api/current",
35
49
  wrapAsync(async () => {
50
+ await ensureConsoleAccess();
36
51
  return profileManager.getCurrentEndpointSummary();
37
52
  }),
38
53
  );
@@ -40,6 +55,7 @@ function createApp() {
40
55
  app.get(
41
56
  "/api/endpoints",
42
57
  wrapAsync(async () => {
58
+ await ensureConsoleAccess();
43
59
  return profileManager.listEndpoints();
44
60
  }),
45
61
  );
@@ -47,6 +63,7 @@ function createApp() {
47
63
  app.post(
48
64
  "/api/endpoints",
49
65
  wrapAsync(async (req) => {
66
+ await ensureConsoleAccess();
50
67
  return profileManager.createEndpoint(req.body);
51
68
  }),
52
69
  );
@@ -54,6 +71,7 @@ function createApp() {
54
71
  app.put(
55
72
  "/api/endpoints/:id",
56
73
  wrapAsync(async (req) => {
74
+ await ensureConsoleAccess();
57
75
  return profileManager.updateEndpoint(req.params.id, req.body);
58
76
  }),
59
77
  );
@@ -61,6 +79,7 @@ function createApp() {
61
79
  app.delete(
62
80
  "/api/endpoints/:id",
63
81
  wrapAsync(async (req) => {
82
+ await ensureConsoleAccess();
64
83
  return profileManager.deleteEndpoint(req.params.id);
65
84
  }),
66
85
  );
@@ -68,6 +87,7 @@ function createApp() {
68
87
  app.post(
69
88
  "/api/endpoints/switch",
70
89
  wrapAsync(async (req) => {
90
+ await ensureConsoleAccess();
71
91
  return profileManager.switchEndpoint(req.body.id);
72
92
  }),
73
93
  );
@@ -75,6 +95,7 @@ function createApp() {
75
95
  app.get(
76
96
  "/api/paths",
77
97
  wrapAsync(async () => {
98
+ await ensureConsoleAccess();
78
99
  return profileManager.getManagedPaths();
79
100
  }),
80
101
  );
@@ -82,6 +103,7 @@ function createApp() {
82
103
  app.post(
83
104
  "/api/proxy/enable",
84
105
  wrapAsync(async () => {
106
+ await ensureConsoleAccess();
85
107
  return profileManager.enableProxyMode();
86
108
  }),
87
109
  );
@@ -89,6 +111,7 @@ function createApp() {
89
111
  app.get(
90
112
  "/api/sync/export",
91
113
  wrapAsync(async () => {
114
+ await ensureConsoleAccess();
92
115
  return profileManager.exportSyncPackage();
93
116
  }),
94
117
  );
@@ -96,6 +119,7 @@ function createApp() {
96
119
  app.post(
97
120
  "/api/sync/import",
98
121
  wrapAsync(async (req) => {
122
+ await ensureConsoleAccess();
99
123
  return profileManager.importSyncPackage(req.body.syncCode, req.body.mode);
100
124
  }),
101
125
  );
@@ -145,6 +169,7 @@ function createApp() {
145
169
  app.post(
146
170
  "/api/open-path",
147
171
  wrapAsync(async (req) => {
172
+ await ensureConsoleAccess();
148
173
  const targetPath = String(req.body.targetPath || "").trim();
149
174
  if (!targetPath) {
150
175
  throw new Error("缺少要打开的路径。");