From 839e451fba0e4a04dbaeda4b0e47722c835ba562 Mon Sep 17 00:00:00 2001 From: Alexander Baryshnikov Date: Tue, 5 May 2020 19:25:00 +0800 Subject: [PATCH] expose configuration to UI --- cmd/tinc-web-boot/main.go | 1 + docs/tinc_web_ui.md | 26 ++++++++++++++++++++++- support/go/tincwebui/client.go | 11 ++++++++++ support/js/tincwebui.js | 12 +++++++++++ support/postman/tincwebui.json | 39 ++++++++++++++++++++++++++++++++++ support/python/tincwebui.py | 32 ++++++++++++++++++++++++++++ support/ts/tincweb.ts | 2 +- support/ts/tincwebui.ts | 16 ++++++++++++++ web/internal/tinc_web_ui.go | 6 +++++- web/routes.go | 2 ++ web/routes_ui.go | 5 +++++ web/shared/interface.go | 6 ++++++ 12 files changed, 155 insertions(+), 3 deletions(-) diff --git a/cmd/tinc-web-boot/main.go b/cmd/tinc-web-boot/main.go index 7a95efa..4b9ba56 100644 --- a/cmd/tinc-web-boot/main.go +++ b/cmd/tinc-web-boot/main.go @@ -170,6 +170,7 @@ func (m *Root) Run(global *globalContext) error { AuthKey: m.AuthKey, LocalUIPort: uint16(port), PublicAddresses: m.UIPublicAddress, + Binding: m.Bind, } webApi, uiApp := apiCfg.New(pool) if !m.Headless { diff --git a/docs/tinc_web_ui.md b/docs/tinc_web_ui.md index 9261bde..eb6c880 100755 --- a/docs/tinc_web_ui.md +++ b/docs/tinc_web_ui.md @@ -6,6 +6,7 @@ Operations with tinc-web-boot related to UI * [TincWebUI.IssueAccessToken](#tincwebuiissueaccesstoken) - Issue and sign token * [TincWebUI.Notify](#tincwebuinotify) - Make desktop notification if system supports it * [TincWebUI.Endpoints](#tincwebuiendpoints) - Endpoints list to access web UI +* [TincWebUI.Configuration](#tincwebuiconfiguration) - Configuration defined for the instance @@ -81,4 +82,27 @@ EOF |------|------|---------| | host | `string` | | | port | `uint16` | | -| kind | `EndpointKind` | | \ No newline at end of file +| kind | `EndpointKind` | | + +## TincWebUI.Configuration + +Configuration defined for the instance + +* Method: `TincWebUI.Configuration` +* Returns: `*Config` + +```bash +curl -H 'Content-Type: application/json' --data-binary @- "http://127.0.0.1:8686/api/" < 'Endpoint': ) +@dataclass +class Config: + binding: 'str' + + def to_json(self) -> dict: + return { + "binding": self.binding, + } + + @staticmethod + def from_json(payload: dict) -> 'Config': + return Config( + binding=payload['binding'], + ) + + class TincWebUIError(RuntimeError): def __init__(self, method: str, code: int, message: str, data: Any): super().__init__('{}: {}: {} - {}'.format(method, code, message, data)) @@ -119,3 +135,19 @@ async def endpoints(self) -> List[Endpoint]: if 'error' in payload: raise TincWebUIError.from_json('endpoints', payload['error']) return [Endpoint.from_json(x) for x in (payload['result'] or [])] + + async def configuration(self) -> Config: + """ + Configuration defined for the instance + """ + response = await self.__request('POST', self.__url, json={ + "jsonrpc": "2.0", + "method": "TincWebUI.Configuration", + "id": self.__next_id(), + "params": [] + }) + assert response.status // 100 == 2, str(response.status) + " " + str(response.reason) + payload = await response.json() + if 'error' in payload: + raise TincWebUIError.from_json('configuration', payload['error']) + return Config.from_json(payload['result']) diff --git a/support/ts/tincweb.ts b/support/ts/tincweb.ts index 1af60ed..90970dd 100755 --- a/support/ts/tincweb.ts +++ b/support/ts/tincweb.ts @@ -70,7 +70,7 @@ export interface Upgrade { -export type Duration = string; // suffixes: ns, us, ms, s, m, h (!!!!) +export type Duration = string; // suffixes: ns, us, ms, s, m, h // support stuff diff --git a/support/ts/tincwebui.ts b/support/ts/tincwebui.ts index 85d5e14..8f1fcc2 100755 --- a/support/ts/tincwebui.ts +++ b/support/ts/tincwebui.ts @@ -16,6 +16,10 @@ export interface Endpoint { kind: EndpointKind } +export interface Config { + binding: string +} + export enum EndpointKind { @@ -220,6 +224,18 @@ export class TincWebUI { })) as Array; } + /** + Configuration defined for the instance + **/ + async configuration(): Promise { + return (await this.__call({ + "jsonrpc" : "2.0", + "method" : "TincWebUI.Configuration", + "id" : this.__next_id(), + "params" : [] + })) as Config; + } + private __next_id() { this.__id += 1; diff --git a/web/internal/tinc_web_ui.go b/web/internal/tinc_web_ui.go index 27f19aa..848334c 100644 --- a/web/internal/tinc_web_ui.go +++ b/web/internal/tinc_web_ui.go @@ -45,5 +45,9 @@ func RegisterTincWebUI(router *jsonrpc2.Router, wrap shared.TincWebUI) []string return wrap.Endpoints() }) - return []string{"TincWebUI.IssueAccessToken", "TincWebUI.Notify", "TincWebUI.Endpoints"} + router.RegisterFunc("TincWebUI.Configuration", func(params json.RawMessage, positional bool) (interface{}, error) { + return wrap.Configuration() + }) + + return []string{"TincWebUI.IssueAccessToken", "TincWebUI.Notify", "TincWebUI.Endpoints", "TincWebUI.Configuration"} } diff --git a/web/routes.go b/web/routes.go index f92c36c..2ab0aab 100644 --- a/web/routes.go +++ b/web/routes.go @@ -22,6 +22,7 @@ type Config struct { AuthKey string LocalUIPort uint16 PublicAddresses []string + Binding string } //go:generate go-bindata -pkg web -prefix ui/build/ -fs ui/build/... @@ -60,6 +61,7 @@ func (cfg Config) New(pool *tincd.Tincd) (*gin.Engine, *uiRoutes) { port: cfg.LocalUIPort, publicAddress: cfg.PublicAddresses, pool: pool, + config: shared.Config{Binding: cfg.Binding}, } internal.RegisterTincWeb(&jsonRouter, &api{pool: pool, publicAddress: cfg.PublicAddresses, key: cfg.AuthKey}) diff --git a/web/routes_ui.go b/web/routes_ui.go index cb9c20b..5e70f0f 100644 --- a/web/routes_ui.go +++ b/web/routes_ui.go @@ -15,6 +15,7 @@ type uiRoutes struct { key string port uint16 publicAddress []string + config shared.Config pool *tincd.Tincd } @@ -78,3 +79,7 @@ func (srv *uiRoutes) Endpoints() ([]shared.Endpoint, error) { } return ans, nil } + +func (srv *uiRoutes) Configuration() (*shared.Config, error) { + return &srv.config, nil +} diff --git a/web/shared/interface.go b/web/shared/interface.go index 1a8bf8b..81356aa 100644 --- a/web/shared/interface.go +++ b/web/shared/interface.go @@ -71,6 +71,10 @@ type Endpoint struct { Kind EndpointKind `json:"kind"` } +type Config struct { + Binding string `json:"binding"` +} + // Operations with tinc-web-boot related to UI type TincWebUI interface { // Issue and sign token @@ -79,6 +83,8 @@ type TincWebUI interface { Notify(title, message string) (bool, error) // Endpoints list to access web UI Endpoints() ([]Endpoint, error) + // Configuration defined for the instance + Configuration() (*Config, error) } // Operations for joining public network