-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathapp.ts
More file actions
384 lines (340 loc) · 11.2 KB
/
Copy pathapp.ts
File metadata and controls
384 lines (340 loc) · 11.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { environment } from '@src/environments/environment';
import { DataStore, User } from '@app/globals';
import { HttpService } from './http';
import { LocalStorageService, LogService } from './share';
import { SettingService } from '@app/services/setting';
import { Account, Asset, AuthInfo, ConnectData, Endpoint, Organization, View } from '@app/model';
import * as CryptoJS from 'crypto-js';
import { OrganizationService } from './organization';
import { I18nService } from '@app/services/i18n';
declare function unescape(s: string): string;
function gotoLogin() {
const currentPath = encodeURI(document.location.pathname + document.location.search);
setTimeout(() => {
window.location.href = document.location.origin + '/core/auth/login/?next=' + currentPath;
}, 500);
}
@Injectable()
export class AppService {
// user:User = user ;
public lang: string;
public connectDialogShown = false;
private protocolPreferConnectTypes: object = {};
private assetPreferAccount: object = {};
private protocolPreferKey = 'ProtocolPreferLoginType';
private accountPreferKey = 'PreferAccount';
private protocolConnectTypesMap: object = {};
private checkIntervalId: number;
private newLoginHasOpen = false; // 避免多次打开新登录页
private checkSecond = 120;
constructor(
private _http: HttpService,
private _router: Router,
private _cookie: CookieService,
private _i18n: I18nService,
private _logger: LogService,
private _settingSvc: SettingService,
private _localStorage: LocalStorageService,
private _orgSvc: OrganizationService
) {
this.setLogLevel();
this.setOrgFromQueryString();
this.checkLogin();
this.loadPreferData();
this.loadOriManualAuthInfo();
this.getConnectMethods();
}
setLogLevel() {
// 设置logger level
let logLevel = this._cookie.get('logLevel');
if (!logLevel) {
logLevel = environment.production ? '1' : '5';
}
this._logger.level = parseInt(logLevel, 10);
}
setOrgFromQueryString() {
const oid = this.getQueryString('oid');
if (oid) {
const currentOrg: Organization = { id: oid, name: '' };
this._orgSvc.switchOrg(currentOrg);
}
}
async getProfileStatus(recheck = false) {
let status = '';
let statusTime = '';
// From local storage
if (!recheck) {
statusTime = localStorage.getItem('checkProfile');
if (statusTime && statusTime.split(' ').length === 2) {
const time = statusTime.split(' ')[1];
const expired = new Date().getTime() - parseInt(time, 10) > 1000 * this.checkSecond;
if (!expired) {
status = statusTime.split(' ')[0];
}
}
}
if (!status) {
User.logined = false;
try {
await this._http.get(`/api/v1/users/profile/?fields_size=mini`).toPromise();
status = 'ok';
User.logined = true;
} catch (err) {
status = 'error'; // 默认错误状态
if (err.status === 401) {
status = 'unauthorized';
} else if (err.status === 400) {
status = 'badrequest';
}
} finally {
localStorage.setItem('checkProfile', status + ' ' + new Date().getTime());
}
} else {
this._logger.debug('Found cache using: ', statusTime);
}
return status;
}
async doCheckProfile(recheck = false) {
const status = await this.getProfileStatus(recheck);
if (['unauthorized', 'badrequest', 'error'].includes(status)) {
clearInterval(this.checkIntervalId);
const ok = confirm(this._i18n.instant(this.getErrorMsg(status)));
if (ok && !this.newLoginHasOpen) {
window.open('/core/auth/login/?next=/luna/', '_blank');
this.newLoginHasOpen = true;
}
setTimeout(() => this.doCheckProfile(true), 5000);
this._logger.debug(`${status}, redirect to login`);
} else if (status === 'ok') {
this.newLoginHasOpen = false;
if (recheck) {
this.intervalCheckLogin().then();
}
this._logger.debug('Profile is ok');
}
return status;
}
getErrorMsg(status: string) {
const messages = {
unauthorized: 'LoginExpireMsg',
badrequest: 'Bad request. The server does not understand the syntax of the request',
error: 'The server encountered an error while trying to process the request'
};
return messages[status];
}
async intervalCheckLogin(second = null, clear: boolean = false) {
if (second == null) {
second = this.checkSecond;
}
if (this.checkIntervalId) {
clearInterval(this.checkIntervalId);
}
// @ts-ignore
this.checkIntervalId = setInterval(() => {
this.doCheckProfile();
}, second * 1000);
}
checkLogin() {
this._logger.debug('Check user auth');
if (!DataStore.Path) {
this._router.navigate(['FOF']);
}
if (User.logined) {
if (document.location.pathname === '/login/') {
this._router.navigate(['']);
} else {
this._router.navigate([document.location.pathname]);
}
return;
}
// Connection connectToken 方式不用检查过期了
const token = this.getQueryString('token');
this._http.getUserProfile().then(
user => {
this._orgSvc.setWorkbenchOrgs(user['workbench_orgs']);
Object.assign(User, user);
User.logined = true;
this._localStorage.set('user', user.id);
this.intervalCheckLogin();
},
err => {
// this._logger.error(err);
User.logined = false;
if (!token) {
gotoLogin();
}
// this._router.navigate(['login']);
}
);
}
browser() {
this._http.reportBrowser();
}
getQueryString(name: string) {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
const r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
getConnectMethods() {
const url = '/api/v1/terminal/components/connect-methods/';
this._http.get(url).subscribe(response => {
this.protocolConnectTypesMap = response;
});
}
getProtocolConnectMethods(protocol: string) {
return this.protocolConnectTypesMap[protocol] || [];
}
loadPreferData() {
const protocolPreferData = this._localStorage.get(this.protocolPreferKey);
if (protocolPreferData && typeof protocolPreferData === 'object') {
this.protocolPreferConnectTypes = protocolPreferData;
}
const accountPreferData = this._localStorage.get(this.accountPreferKey);
if (accountPreferData && typeof accountPreferData === 'object') {
this.assetPreferAccount = accountPreferData;
}
}
loadOriManualAuthInfo() {
const manualAuthInfoKey = 'ManualAuthInfo';
const authInfos = this._localStorage.get(manualAuthInfoKey);
if (!authInfos) {
return;
}
if (authInfos && typeof authInfos === 'object') {
for (const [key, auths] of Object.entries(authInfos)) {
const newKey = `JMS_MA_${key}`;
this._localStorage.set(newKey, auths);
}
}
this._localStorage.delete(manualAuthInfoKey);
}
setPreConnectData(asset: Asset, connectData: ConnectData) {
const { account, protocol, connectMethod, manualAuthInfo, connectOption } = connectData;
const key = `JMS_PRE_${asset.id}`;
const saveData = {
account: { alias: account.alias, username: account.username, has_secret: account.has_secret },
connectMethod: { value: connectMethod.value },
protocol: { name: protocol.name },
downloadRDP: connectData.downloadRDP,
autoLogin: connectData.autoLogin,
connectOption,
direct: connectData.direct
};
this.setAccountLocalAuth(asset, account, manualAuthInfo);
this._localStorage.set(key, saveData);
}
getPreConnectData(asset: Asset): ConnectData {
const key = `JMS_PRE_${asset.id}`;
const connectData = this._localStorage.get(key) as ConnectData;
if (!connectData) {
return null;
}
connectData.manualAuthInfo = new AuthInfo();
if (connectData.account.has_secret) {
return connectData;
}
if (connectData.account) {
const auths = this.getAccountLocalAuth(asset.id);
const matched = auths.find(item => item.alias === connectData.account.alias);
if (matched) {
connectData.manualAuthInfo = matched;
}
}
return connectData;
}
encrypt(s) {
const secretKey = `${User.id}_${User.username}`;
try {
return CryptoJS.AES.encrypt(s, secretKey).toString();
} catch (err) {
return '';
}
}
decrypt(secret) {
const secretKey = `${User.id}_${User.username}`;
try {
const bytes = CryptoJS.AES.decrypt(secret, secretKey);
return bytes.toString(CryptoJS.enc.Utf8);
} catch (err) {
return '';
}
}
disableAutoConnect(assetId) {
const key = 'JMS_PRE_' + assetId;
const connectData = this._localStorage.get(key);
if (connectData) {
connectData.autoLogin = false;
this._localStorage.set(key, connectData);
}
}
getAccountLocalAuth(assetId: string, decrypt = true): AuthInfo[] {
const localKey = `JMS_MA_${assetId}`;
const auths = this._localStorage.get(localKey);
if (!auths || !Array.isArray(auths)) {
return [];
}
if (!decrypt) {
return auths;
}
const newAuths: AuthInfo[] = [];
for (const auth of auths) {
const newAuth = Object.assign({}, auth);
newAuth.secret = this.decrypt(newAuth.secret);
newAuths.push(newAuth);
}
return newAuths;
}
setAccountLocalAuth(asset: Asset, account: Account, auth: AuthInfo) {
const assetId = asset.id;
const newAuth = Object.assign({ alias: account.alias, username: account.username }, auth);
// 如果 auth.alias 是 undefined,保持使用 account.alias
if (auth.alias === undefined && account.alias !== undefined) {
newAuth.alias = account.alias;
}
if (!auth.secret || !auth.rememberAuth) {
newAuth.secret = '';
} else {
newAuth.secret = this.encrypt(auth.secret);
}
let auths = this.getAccountLocalAuth(assetId, false);
const localKey = `JMS_MA_${assetId}`;
auths = auths.filter(item => item.username !== newAuth.username);
auths.splice(0, 0, newAuth);
this._localStorage.set(localKey, auths);
}
getSmartEndpoint(view: View): Promise<Endpoint> {
let protocol = '';
if (view && view.connectMethod && view.connectMethod.endpoint_protocol) {
protocol = view.connectMethod.endpoint_protocol;
} else if (view && view.protocol) {
protocol = view.protocol;
}
if (protocol === 'http') {
protocol = window.location.protocol.replace(':', '');
}
const data = { assetId: '', sessionId: '', token: '' };
if (view.connectToken) {
data['token'] = view.connectToken.id;
} else {
data['assetId'] = view.asset.id;
}
if (view.connectMethod && view.connectMethod.type === 'applet') {
if (view.connectOption && view.connectOption['appletConnectMethod'] === 'client') {
protocol = 'rdp';
} else {
protocol = 'http';
}
}
const res = this._http.getSmartEndpoint(data, protocol);
res.catch(err => {
alert(err.error.detail);
});
return res;
}
}