Files
tabby-web/src/services/appConnector.service.ts

141 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-06-19 23:01:22 +02:00
import { Buffer } from 'buffer'
2021-06-14 23:00:05 +02:00
import { Subject } from 'rxjs'
import { debounceTime } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
2021-06-21 09:22:13 +02:00
import { LoginService } from '../services/login.service'
2021-06-25 21:55:40 +02:00
import { Config, Version } from '../api'
2021-06-14 23:00:05 +02:00
2021-06-19 23:01:22 +02:00
export class SocketProxy {
connect$ = new Subject<void>()
data$ = new Subject<Buffer>()
2021-06-21 09:22:13 +02:00
error$ = new Subject<Buffer>()
2021-06-19 23:01:22 +02:00
webSocket: WebSocket
initialBuffer: Buffer
2021-06-21 09:22:13 +02:00
options: {
host: string
port: number
}
2021-06-19 23:01:22 +02:00
2021-06-21 09:22:13 +02:00
constructor (private appConnector: AppConnectorService) {
2021-06-19 23:01:22 +02:00
this.initialBuffer = Buffer.from('')
}
2021-06-20 22:42:58 +02:00
connect (options) {
2021-06-21 09:22:13 +02:00
this.options = options
this.webSocket = new WebSocket(
this.appConnector.loginService.user.custom_connection_gateway ||
`ws://${location.host}/api/1/gateway/tcp`
)
this.webSocket.onmessage = async event => {
if (typeof(event.data) === 'string') {
this.handleServiceMessage(JSON.parse(event.data))
} else {
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
}
}
}
handleServiceMessage (msg) {
if (msg._ === 'hello') {
this.sendServiceMessage({
_: 'hello',
version: 1,
auth_token: this.appConnector.loginService.user.custom_connection_gateway_token,
})
} else if (msg._ === 'ready') {
this.sendServiceMessage({
_: 'connect',
host: this.options.host,
port: this.options.port,
})
} else if (msg._ === 'connected') {
2021-06-19 23:01:22 +02:00
this.connect$.next()
this.connect$.complete()
this.webSocket.send(this.initialBuffer)
this.initialBuffer = Buffer.from('')
2021-06-21 09:22:13 +02:00
} else if (msg._ === 'error') {
console.error('Connection gateway error', msg)
this.close(new Error(msg.details))
} else {
console.warn('Unknown service message', msg)
2021-06-19 23:01:22 +02:00
}
2021-06-21 09:22:13 +02:00
}
sendServiceMessage (msg) {
this.webSocket.send(JSON.stringify(msg))
2021-06-19 23:01:22 +02:00
}
write (chunk: Buffer): void {
if (!this.webSocket.readyState) {
this.initialBuffer = Buffer.concat([this.initialBuffer, chunk])
} else {
this.webSocket.send(chunk)
}
}
close (error: Error): void {
2021-06-21 09:22:13 +02:00
if (error) {
this.error$.next(error)
}
this.connect$.complete()
this.data$.complete()
this.error$.complete()
2021-06-19 23:01:22 +02:00
}
}
2021-06-14 23:00:05 +02:00
@Injectable({ providedIn: 'root' })
export class AppConnectorService {
private configUpdate = new Subject<string>()
2021-06-25 21:55:40 +02:00
private config: Config
private version: Version
2021-06-14 23:00:05 +02:00
2021-06-21 09:22:13 +02:00
constructor (
private http: HttpClient,
public loginService: LoginService,
) {
2021-06-15 23:43:23 +02:00
this.configUpdate.pipe(debounceTime(1000)).subscribe(async content => {
const result = await this.http.patch(`/api/1/configs/${this.config.id}`, { content }).toPromise()
Object.assign(this.config, result)
2021-06-14 23:00:05 +02:00
})
}
2021-06-25 21:55:40 +02:00
setState (config: Config, version: Version) {
this.config = config
this.version = version
}
2021-06-14 23:00:05 +02:00
async loadConfig (): Promise<string> {
return this.config.content
}
async saveConfig (content: string): Promise<void> {
this.configUpdate.next(content)
2021-06-15 23:43:23 +02:00
this.config.content = content
2021-06-14 23:00:05 +02:00
}
2021-06-19 23:01:22 +02:00
getAppVersion (): string {
return this.version.version
}
2021-06-21 09:22:13 +02:00
2021-06-27 16:28:49 +02:00
getDistURL (): string {
return '../app-dist'
}
getPluginsToLoad (): string[] {
return [
'terminus-core',
'terminus-settings',
'terminus-terminal',
'terminus-ssh',
'terminus-community-color-schemes',
'terminus-web',
]
}
2021-06-21 09:22:13 +02:00
createSocket () {
return new SocketProxy(this)
}
2021-06-14 23:00:05 +02:00
}