@tuya-sat/micro-dev-proxy 3.2.22 → 3.2.24
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/dist/appProxy.d.ts +3 -0
- package/dist/appProxy.js +35 -6
- package/package.json +1 -1
package/dist/appProxy.d.ts
CHANGED
|
@@ -35,6 +35,9 @@ export default class AppProxy {
|
|
|
35
35
|
spinnies: Spinnies;
|
|
36
36
|
constructor({ target, username, password, additionHeaders, isMainApp, isThirdLogin, loginApi, csrf, logSign, }: AppProxyConfig);
|
|
37
37
|
init(): Promise<void>;
|
|
38
|
+
needManualLogin(): boolean;
|
|
39
|
+
isLoginApi(url: string): boolean;
|
|
40
|
+
isApiBelongMainApp(url: string): boolean;
|
|
38
41
|
getCsrf(): Promise<void>;
|
|
39
42
|
getLoginCookie(): Promise<void>;
|
|
40
43
|
getCookie(prevCookie?: string): string;
|
package/dist/appProxy.js
CHANGED
|
@@ -70,7 +70,9 @@ class AppProxy {
|
|
|
70
70
|
this.target = target;
|
|
71
71
|
if (!isMainApp) {
|
|
72
72
|
this.username = username;
|
|
73
|
-
|
|
73
|
+
if (password) {
|
|
74
|
+
this.password = (0, js_sha256_1.sha256)(password);
|
|
75
|
+
}
|
|
74
76
|
}
|
|
75
77
|
this.isMainApp = isMainApp;
|
|
76
78
|
this.loginApi = loginApi;
|
|
@@ -99,6 +101,26 @@ class AppProxy {
|
|
|
99
101
|
}
|
|
100
102
|
});
|
|
101
103
|
}
|
|
104
|
+
needManualLogin() {
|
|
105
|
+
return !this.username || !this.password;
|
|
106
|
+
}
|
|
107
|
+
isLoginApi(url) {
|
|
108
|
+
return [
|
|
109
|
+
this.loginApi,
|
|
110
|
+
'/api/login',
|
|
111
|
+
'/api/login/qrcode'
|
|
112
|
+
].includes(url.replace(/\/+$/, ''));
|
|
113
|
+
}
|
|
114
|
+
isApiBelongMainApp(url) {
|
|
115
|
+
return [
|
|
116
|
+
this.loginApi,
|
|
117
|
+
'/api/login',
|
|
118
|
+
'/api/login/qrcode',
|
|
119
|
+
'/open-api/v1.0/current/user',
|
|
120
|
+
'/open-api/v1.0/m/life/web/user/info',
|
|
121
|
+
'/open-api/v1.0/m/life/web/mqtt/connection/config',
|
|
122
|
+
].includes(url.replace(/\/+$/, ''));
|
|
123
|
+
}
|
|
102
124
|
//获取csrf
|
|
103
125
|
getCsrf() {
|
|
104
126
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -122,6 +144,12 @@ class AppProxy {
|
|
|
122
144
|
if (!this.spinnies.pick('login')) {
|
|
123
145
|
this.spinnies.add('login', { text: SPIN_LABEL_NAME.login });
|
|
124
146
|
}
|
|
147
|
+
if (this.needManualLogin()) {
|
|
148
|
+
this.spinnies.fail('login', {
|
|
149
|
+
text: '账密未配置,需要手动登录'
|
|
150
|
+
});
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
125
153
|
const { href: loginUrl } = new url_1.URL(this.loginApi, this.target);
|
|
126
154
|
yield (0, initLogin_1.default)(loginUrl, { username: this.username, password: this.password }, {
|
|
127
155
|
headers: this.combinedHeader({ cookie: this.csrfCookie.value }),
|
|
@@ -180,12 +208,12 @@ class AppProxy {
|
|
|
180
208
|
return proccessedHeader;
|
|
181
209
|
}
|
|
182
210
|
processRes(responseBuffer, proxyRes, req, res) {
|
|
183
|
-
var _a, _b;
|
|
211
|
+
var _a, _b, _c;
|
|
184
212
|
return __awaiter(this, void 0, void 0, function* () {
|
|
185
213
|
let response;
|
|
186
214
|
try {
|
|
187
215
|
response = JSON.parse(responseBuffer.toString('utf8'));
|
|
188
|
-
if (req.url
|
|
216
|
+
if (this.isLoginApi(req.url)) {
|
|
189
217
|
const token = (0, util_1.parseSetCookie)(proxyRes.headers['set-cookie']);
|
|
190
218
|
if (token) {
|
|
191
219
|
this.loginCookie.setValue(token);
|
|
@@ -196,14 +224,14 @@ class AppProxy {
|
|
|
196
224
|
this.log('----json parse responseBuffer string error-----', ((_a = err.response) === null || _a === void 0 ? void 0 : _a.data) || err.message);
|
|
197
225
|
return responseBuffer;
|
|
198
226
|
}
|
|
199
|
-
const
|
|
200
|
-
if (!this.isMainApp && !isApiBelongMainApp
|
|
227
|
+
const isMicroAppPage = (_b = req.referer) === null || _b === void 0 ? void 0 : _b.includes('/apps');
|
|
228
|
+
if (!this.isMainApp && isMicroAppPage && !this.isApiBelongMainApp(req.url)) {
|
|
201
229
|
const apiPathMatchReg = /\/(:[^/]+|\$?{[^/]+})/g;
|
|
202
230
|
const apis = mf.apis || [];
|
|
203
231
|
const microAppId = req.headers['micro-app-id'];
|
|
204
232
|
if (microAppId && !microAppId.includes('main-app')) {
|
|
205
233
|
const method = (req === null || req === void 0 ? void 0 : req.method) || 'GET';
|
|
206
|
-
const urlPath = ((
|
|
234
|
+
const urlPath = ((_c = req === null || req === void 0 ? void 0 : req.path) === null || _c === void 0 ? void 0 : _c.replace(/^\s*\/(custom-api|open-api)/, '')) || '';
|
|
207
235
|
let isExists = false;
|
|
208
236
|
apis.forEach((api) => {
|
|
209
237
|
const isLegal = new RegExp(`^${api.path.replace(apiPathMatchReg, '/[^/]+')}/?$`).test(urlPath);
|
|
@@ -215,6 +243,7 @@ class AppProxy {
|
|
|
215
243
|
return JSON.stringify({
|
|
216
244
|
msg: `${method}:${urlPath} 未在manifest.json中注册`,
|
|
217
245
|
code: '888888',
|
|
246
|
+
success: false,
|
|
218
247
|
});
|
|
219
248
|
}
|
|
220
249
|
}
|