mirror of
https://github.com/Eugeny/tabby-web.git
synced 2025-06-22 12:20:06 +00:00
wip
This commit is contained in:
parent
8bae6e1769
commit
663615fe06
@ -1,5 +1,5 @@
|
|||||||
main(*ngIf='ready && user')
|
main(*ngIf='ready && loggedIn')
|
||||||
.login-view(*ngIf='ready && !user')
|
.login-view(*ngIf='ready && !loggedIn')
|
||||||
.buttons
|
.buttons
|
||||||
a.btn(
|
a.btn(
|
||||||
*ngFor='let provider of providers',
|
*ngFor='let provider of providers',
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Component } from '@angular/core'
|
import { Component } from '@angular/core'
|
||||||
import { HttpClient } from '@angular/common/http'
|
import { LoginService } from '../services/login.service'
|
||||||
|
|
||||||
import { faGithub, faGitlab, faGoogle, faMicrosoft } from '@fortawesome/free-brands-svg-icons'
|
import { faGithub, faGitlab, faGoogle, faMicrosoft } from '@fortawesome/free-brands-svg-icons'
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ import { faGithub, faGitlab, faGoogle, faMicrosoft } from '@fortawesome/free-bra
|
|||||||
styleUrls: ['./app.component.scss'],
|
styleUrls: ['./app.component.scss'],
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
user: any
|
loggedIn: any
|
||||||
ready = false
|
ready = false
|
||||||
|
|
||||||
providers = [
|
providers = [
|
||||||
@ -20,14 +20,12 @@ export class AppComponent {
|
|||||||
]
|
]
|
||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
private http: HttpClient,
|
private loginService: LoginService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async ngOnInit () {
|
async ngOnInit () {
|
||||||
const user = await this.http.get('/api/1/user').toPromise()
|
await this.loginService.ready$.toPromise()
|
||||||
if (user.id) {
|
this.loggedIn = !!this.loginService.user
|
||||||
this.user = user
|
|
||||||
}
|
|
||||||
this.ready = true
|
this.ready = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,15 @@
|
|||||||
fa-icon([icon]='_addIcon', [fixedWidth]='true')
|
fa-icon([icon]='_addIcon', [fixedWidth]='true')
|
||||||
span New config
|
span New config
|
||||||
|
|
||||||
|
div(ngbDropdown, placement='bottom-right')
|
||||||
|
button.btn(ngbDropdownToggle)
|
||||||
|
fa-icon([icon]='_connectionIcon', [fixedWidth]='true')
|
||||||
|
|
||||||
|
div(ngbDropdownMenu)
|
||||||
|
.form-check.form-switch
|
||||||
|
input.form-check-input(type='checkbox', ngModel='!!loginService.user.custom_connection_gateway')
|
||||||
|
label(class='form-check-label') Use custom connection gateway
|
||||||
|
|
||||||
div(ngbDropdown, placement='bottom-right')
|
div(ngbDropdown, placement='bottom-right')
|
||||||
button.btn(ngbDropdownToggle)
|
button.btn(ngbDropdownToggle)
|
||||||
fa-icon([icon]='_userIcon', [fixedWidth]='true')
|
fa-icon([icon]='_userIcon', [fixedWidth]='true')
|
||||||
|
@ -3,7 +3,8 @@ import { Component, ElementRef, ViewChild } from '@angular/core'
|
|||||||
import { HttpClient } from '@angular/common/http'
|
import { HttpClient } from '@angular/common/http'
|
||||||
import { AppConnectorService } from '../services/appConnector.service'
|
import { AppConnectorService } from '../services/appConnector.service'
|
||||||
|
|
||||||
import { faCog, faUser, faCopy, faTrash, faPlus } from '@fortawesome/free-solid-svg-icons'
|
import { faCog, faUser, faCopy, faTrash, faPlus, faPlug } from '@fortawesome/free-solid-svg-icons'
|
||||||
|
import { LoginService } from '../services/login.service'
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'main',
|
selector: 'main',
|
||||||
@ -17,6 +18,7 @@ export class MainComponent {
|
|||||||
_copyIcon = faCopy
|
_copyIcon = faCopy
|
||||||
_addIcon = faPlus
|
_addIcon = faPlus
|
||||||
_deleteIcon = faTrash
|
_deleteIcon = faTrash
|
||||||
|
_connectionIcon = faPlug
|
||||||
|
|
||||||
configs: any[] = []
|
configs: any[] = []
|
||||||
versions: any[] = []
|
versions: any[] = []
|
||||||
@ -26,6 +28,7 @@ export class MainComponent {
|
|||||||
constructor (
|
constructor (
|
||||||
private appConnector: AppConnectorService,
|
private appConnector: AppConnectorService,
|
||||||
private http: HttpClient,
|
private http: HttpClient,
|
||||||
|
public loginService: LoginService,
|
||||||
) {
|
) {
|
||||||
window.addEventListener('message', event => {
|
window.addEventListener('message', event => {
|
||||||
if (event.data === 'request-connector') {
|
if (event.data === 'request-connector') {
|
||||||
|
@ -3,30 +3,67 @@ import { Subject } from 'rxjs'
|
|||||||
import { debounceTime } from 'rxjs/operators'
|
import { debounceTime } from 'rxjs/operators'
|
||||||
import { HttpClient } from '@angular/common/http'
|
import { HttpClient } from '@angular/common/http'
|
||||||
import { Injectable } from '@angular/core'
|
import { Injectable } from '@angular/core'
|
||||||
|
import { LoginService } from '../services/login.service'
|
||||||
|
|
||||||
export class SocketProxy {
|
export class SocketProxy {
|
||||||
connect$ = new Subject<void>()
|
connect$ = new Subject<void>()
|
||||||
data$ = new Subject<Buffer>()
|
data$ = new Subject<Buffer>()
|
||||||
|
error$ = new Subject<Buffer>()
|
||||||
|
|
||||||
webSocket: WebSocket
|
webSocket: WebSocket
|
||||||
initialBuffer: Buffer
|
initialBuffer: Buffer
|
||||||
|
options: {
|
||||||
|
host: string
|
||||||
|
port: number
|
||||||
|
}
|
||||||
|
|
||||||
constructor () {
|
constructor (private appConnector: AppConnectorService) {
|
||||||
this.initialBuffer = Buffer.from('')
|
this.initialBuffer = Buffer.from('')
|
||||||
}
|
}
|
||||||
|
|
||||||
connect (options) {
|
connect (options) {
|
||||||
console.log('ws connect', options)
|
this.options = options
|
||||||
this.webSocket = new WebSocket(`ws://${location.host}/api/1/gateway/tcp/${options.host}:${options.port}`)
|
this.webSocket = new WebSocket(
|
||||||
this.webSocket.onopen = event => {
|
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') {
|
||||||
this.connect$.next()
|
this.connect$.next()
|
||||||
this.connect$.complete()
|
this.connect$.complete()
|
||||||
this.webSocket.send(this.initialBuffer)
|
this.webSocket.send(this.initialBuffer)
|
||||||
this.initialBuffer = Buffer.from('')
|
this.initialBuffer = Buffer.from('')
|
||||||
|
} else if (msg._ === 'error') {
|
||||||
|
console.error('Connection gateway error', msg)
|
||||||
|
this.close(new Error(msg.details))
|
||||||
|
} else {
|
||||||
|
console.warn('Unknown service message', msg)
|
||||||
}
|
}
|
||||||
this.webSocket.onmessage = async event => {
|
}
|
||||||
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
|
|
||||||
}
|
sendServiceMessage (msg) {
|
||||||
|
this.webSocket.send(JSON.stringify(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
write (chunk: Buffer): void {
|
write (chunk: Buffer): void {
|
||||||
@ -38,7 +75,12 @@ export class SocketProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close (error: Error): void {
|
close (error: Error): void {
|
||||||
console.warn('socket destroy', error)
|
if (error) {
|
||||||
|
this.error$.next(error)
|
||||||
|
}
|
||||||
|
this.connect$.complete()
|
||||||
|
this.data$.complete()
|
||||||
|
this.error$.complete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,10 +88,13 @@ export class SocketProxy {
|
|||||||
export class AppConnectorService {
|
export class AppConnectorService {
|
||||||
config: any
|
config: any
|
||||||
version: any
|
version: any
|
||||||
Socket = SocketProxy
|
user: any
|
||||||
private configUpdate = new Subject<string>()
|
private configUpdate = new Subject<string>()
|
||||||
|
|
||||||
constructor (private http: HttpClient) {
|
constructor (
|
||||||
|
private http: HttpClient,
|
||||||
|
public loginService: LoginService,
|
||||||
|
) {
|
||||||
this.configUpdate.pipe(debounceTime(1000)).subscribe(async content => {
|
this.configUpdate.pipe(debounceTime(1000)).subscribe(async content => {
|
||||||
const result = await this.http.patch(`/api/1/configs/${this.config.id}`, { content }).toPromise()
|
const result = await this.http.patch(`/api/1/configs/${this.config.id}`, { content }).toPromise()
|
||||||
Object.assign(this.config, result)
|
Object.assign(this.config, result)
|
||||||
@ -68,4 +113,8 @@ export class AppConnectorService {
|
|||||||
getAppVersion (): string {
|
getAppVersion (): string {
|
||||||
return this.version.version
|
return this.version.version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createSocket () {
|
||||||
|
return new SocketProxy(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
21
src/services/login.service.ts
Normal file
21
src/services/login.service.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { AsyncSubject } from 'rxjs'
|
||||||
|
import { HttpClient } from '@angular/common/http'
|
||||||
|
import { Injectable } from '@angular/core'
|
||||||
|
|
||||||
|
|
||||||
|
@Injectable({ providedIn: 'root' })
|
||||||
|
export class LoginService {
|
||||||
|
user: any
|
||||||
|
ready$ = new AsyncSubject<void>()
|
||||||
|
|
||||||
|
constructor (private http: HttpClient) {
|
||||||
|
this.init()
|
||||||
|
}
|
||||||
|
|
||||||
|
private async init () {
|
||||||
|
const user = await this.http.get('/api/1/user').toPromise()
|
||||||
|
this.user = user
|
||||||
|
this.ready$.next()
|
||||||
|
this.ready$.complete()
|
||||||
|
}
|
||||||
|
}
|
@ -2,5 +2,10 @@ from django.contrib import admin
|
|||||||
from django.contrib.auth.admin import UserAdmin
|
from django.contrib.auth.admin import UserAdmin
|
||||||
from .models import User, Config
|
from .models import User, Config
|
||||||
|
|
||||||
admin.site.register(User, UserAdmin)
|
class CustomUserAdmin(UserAdmin):
|
||||||
|
fieldsets = UserAdmin.fieldsets + (
|
||||||
|
(None, {'fields': ('custom_connection_gateway', 'custom_connection_gateway_token')}),
|
||||||
|
)
|
||||||
|
|
||||||
|
admin.site.register(User, CustomUserAdmin)
|
||||||
admin.site.register(Config)
|
admin.site.register(Config)
|
||||||
|
@ -67,7 +67,7 @@ class UserSerializer(ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ('id', 'username', 'active_config')
|
fields = ('id', 'username', 'active_config', 'custom_connection_gateway', 'custom_connection_gateway_token')
|
||||||
read_only_fields = ('id', 'username')
|
read_only_fields = ('id', 'username')
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 3.2.3 on 2021-06-20 20:44
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('app', '0002_auto_20210605_2137'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='custom_connection_gateway',
|
||||||
|
field=models.CharField(max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 3.2.3 on 2021-06-20 20:45
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('app', '0003_user_custom_connection_gateway'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='user',
|
||||||
|
name='custom_connection_gateway_token',
|
||||||
|
field=models.CharField(max_length=255, null=True),
|
||||||
|
),
|
||||||
|
]
|
@ -16,6 +16,8 @@ class Config(models.Model):
|
|||||||
class User(AbstractUser):
|
class User(AbstractUser):
|
||||||
active_config = models.ForeignKey(Config, null=True, on_delete=models.CASCADE, related_name='+')
|
active_config = models.ForeignKey(Config, null=True, on_delete=models.CASCADE, related_name='+')
|
||||||
active_version = models.CharField(max_length=32, null=True)
|
active_version = models.CharField(max_length=32, null=True)
|
||||||
|
custom_connection_gateway = models.CharField(max_length=255, null=True)
|
||||||
|
custom_connection_gateway_token = models.CharField(max_length=255, null=True)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
modified_at = models.DateTimeField(auto_now=True)
|
modified_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user