-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathiframe.component.ts
More file actions
246 lines (217 loc) · 7.2 KB
/
Copy pathiframe.component.ts
File metadata and controls
246 lines (217 loc) · 7.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
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild
} from '@angular/core';
import { View } from '@app/model';
import {
ConnectTokenService,
HttpService,
I18nService,
LogService,
ViewService,
IframeCommunicationService
} from '@app/services';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { environment } from '@src/environments/environment';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { FaceService } from '@app/services/face';
import { DrawerStateService } from '@app/services/drawer';
import { Protocol } from '@app/model';
@Component({
standalone: false,
selector: 'elements-iframe',
templateUrl: 'iframe.component.html',
styleUrls: ['iframe.component.scss']
})
export class ElementIframeComponent implements OnInit, AfterViewInit, OnDestroy {
@Input() src: any;
@Input() id: string;
@Input() view: View;
@Input() origin: string;
@ViewChild('iFrame', { static: false }) iframeRef: ElementRef;
@Output() onLoad: EventEmitter<Boolean> = new EventEmitter<Boolean>();
@Output() createFileConnectToken: EventEmitter<Boolean> = new EventEmitter<Boolean>();
eventHandler: EventListenerOrEventListenerObject;
private renewalTrigger = new Subject<void>();
private subscription: Subscription;
iframeWindow: Window;
showIframe = false;
showValue: boolean = !window['debugIframe'];
ping: number;
debug = false;
trustedUrl: SafeResourceUrl;
constructor(
private _i18n: I18nService,
private _logger: LogService,
private _connectTokenSvc: ConnectTokenService,
private _http: HttpService,
public _viewSvc: ViewService,
private sanitizer: DomSanitizer,
private faceService: FaceService,
private _iframeSvc: IframeCommunicationService,
private _drawerStateService: DrawerStateService
) {}
ngOnInit() {
this._logger.info(`IFrame URL: ${this.src}`);
if (!environment.production) {
this.debug = true;
setTimeout(() => {
this.debug = false;
}, 5000);
}
this.renewalTrigger.pipe(debounceTime(2000)).subscribe(() => {
this._http.get(`/api/v1/health/`).subscribe();
});
this.id = 'window-' + Math.random().toString(36).substr(2);
this.trustedUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.src);
}
ngAfterViewInit() {
this.iframeWindow = this.iframeRef.nativeElement.contentWindow;
this.view.iframeElement = this.iframeWindow;
this.eventHandler = function (e: any) {
const msg = e.data;
if (msg.id !== this.id) {
return;
}
switch (msg.name) {
case 'PING': {
this.iframeWindow.postMessage({ name: 'PONG', id: this.id }, '*');
break;
}
case 'PONG':
setTimeout(() => {
this.showIframe = this.showValue;
});
if (this.view) {
this.view.termComp = this;
}
clearInterval(this.ping);
break;
case 'CLOSE':
if (this.view && this.view.connected) {
this.view.connected = false;
this._drawerStateService.sendComponentMessage({ name: 'SSH_CLOSE', data: this.view });
}
if (this.view && this.view.connectToken && this.view.connectToken.face_monitor_token) {
this.faceService.removeMonitoringTab(this.view.id);
}
this._iframeSvc.sendMessage({ name: 'CLOSE' });
break;
case 'CONNECTED':
this.view.connected = true;
if (this.view.connectToken.face_monitor_token) {
this.faceService.addMonitoringTab(this.view.id);
}
break;
case 'CLICK':
document.body.click();
document.dispatchEvent(
new MouseEvent('mouseup', {
bubbles: true,
cancelable: true
})
);
break;
case 'KEYEVENT':
window.focus();
setTimeout(() => {
this._viewSvc.keyboardSwitchTab(msg.data);
}, 200);
break;
case 'KEYBOARDEVENT':
case 'MOUSEEVENT':
this.renewalTrigger.next();
break;
case 'CREATE_FILE_CONNECT_TOKEN':
this.createFileConnectToken.emit(true);
break;
case 'SHARE_USER_ADD':
this._drawerStateService.sendComponentMessage({ name: 'SHARE_USER_ADD', data: msg.data });
break;
case 'SHARE_CODE_RESPONSE':
this._iframeSvc.sendMessage({ name: 'SHARE_CODE_RESPONSE', data: msg.data });
break;
case 'TERMINAL_CONTENT_RESPONSE':
this.view.terminalContentData = msg.data;
this._iframeSvc.sendMessage({ name: 'TERMINAL_CONTENT_RESPONSE', data: msg.data });
break;
}
}.bind(this);
this.handleIframeEvent();
}
ngOnDestroy() {
window.removeEventListener('message', this.eventHandler);
this.subscription.unsubscribe();
}
setActive() {
this._logger.debug(`[Luna] Send FOCUS to: ${this.id}`);
this.iframeWindow.postMessage({ name: 'FOCUS' }, '*');
}
handleIframeEvent() {
let disbaleFileManager: boolean = false;
// 对于没有授权 sftp 协议的资产,不会在 koko 中展示文件管理
if (!this.view.asset.permed_protocols.some((protocol: Protocol) => protocol.name === 'sftp')) {
disbaleFileManager = true;
}
// @ts-ignore
this.ping = setInterval(() => {
this._logger.info(`[Luna] Send PING to: ${this.id}`);
this.iframeWindow.postMessage({ name: 'PING', id: this.id, disbaleFileManager }, '*');
}, 500);
// 30s 内未PING通, 则主动关闭
setTimeout(
function () {
clearInterval(this.ping);
this.showIframe = this.showValue;
}.bind(this),
1000 * 30
);
this.subscription = this._iframeSvc.message$.subscribe(message => {
this._logger.info('[Luna] Send msg to iframe: ', message);
this.iframeWindow.postMessage(message, '*');
});
window.addEventListener('message', this.eventHandler);
}
sendCommand(data) {
this._logger.info(`[Luna] Send CMD to: ${this.id}`);
this.iframeWindow.postMessage({ name: 'CMD', data: data.data }, '*');
}
// 没有位置用啊
// messageHandler = (event: MessageEvent) => {
// if (event.data && typeof event.data === 'object') {
// this.termComp = event.data;
// }
// };
async reconnect() {
const oldConnectToken = this.view.connectToken;
const newConnectToken = await this._connectTokenSvc.exchange(oldConnectToken);
if (!newConnectToken) {
return;
}
// 更新当前 view 的 connectToken
this.view.connectToken = newConnectToken;
const url = this.src.replace(oldConnectToken.id, newConnectToken.id);
this.src = 'about:blank';
// 清理旧的事件监听器
window.removeEventListener('message', this.eventHandler);
if (this.ping) {
clearInterval(this.ping);
}
setTimeout(() => {
this.src = url;
this.view.connected = true;
this.view.active = true;
this.iframeWindow = this.iframeRef.nativeElement.contentWindow;
this.view.iframeElement = this.iframeWindow;
this.handleIframeEvent();
}, 100);
}
}