Show IP from JSON

main
Gerard Braad 2023-06-22 15:15:54 +08:00
parent 478b991c0a
commit 54fdb6f0b4
3 changed files with 60 additions and 5 deletions

View File

View File

@ -1,22 +1,28 @@
import React from 'react'; import React from 'react';
import { TailscaleBackendState, TailscaleStatusResponse, TailscaleUpResponse } from './types';
import { Card, CardTitle, CardBody } from '@patternfly/react-core'; import { Card, CardTitle, CardBody } from '@patternfly/react-core';
type ApplicationProps = { type ApplicationProps = {
} }
type ApplicationState = { type ApplicationState = {
response: string Status: TailscaleStatusResponse
} }
export class Application extends React.Component<ApplicationProps, ApplicationState> { export class Application extends React.Component<ApplicationProps, ApplicationState> {
state: ApplicationState = { state: ApplicationState = {
response: "" Status: null
} }
constructor(props: ApplicationProps) { constructor(props: ApplicationProps) {
super(props); super(props);
cockpit.spawn(['tailscale', 'status']).done(content => { cockpit.spawn(['tailscale', 'status', '--json']).done(content => {
this.setState(state => ({response: content.trim()})); const status: TailscaleStatusResponse = JSON.parse(content)
this.setState(state => ({Status: status}));
}); });
} }
@ -26,7 +32,11 @@ export class Application extends React.Component<ApplicationProps, ApplicationSt
<CardTitle>Tailscale</CardTitle> <CardTitle>Tailscale</CardTitle>
<CardBody> <CardBody>
<pre> <pre>
{ this.state.response } {
this.state.Status != null
? this.state.Status.Self.TailscaleIPs[0]
: <p>Loading...</p>
}
</pre> </pre>
</CardBody> </CardBody>
</Card> </Card>

45
src/types.ts Normal file
View File

@ -0,0 +1,45 @@
// BackendState
// Keep in sync with https://github.com/tailscale/tailscale/blob/main/ipn/backend.go
export type TailscaleBackendState =
| 'NoState'
| 'NeedsMachineAuth'
| 'NeedsLogin'
| 'InUseOtherUser'
| 'Stopped'
| 'Starting'
| 'Running';
export type TailscaleStatusResponse = {
BackendState: TailscaleBackendState;
AuthURL: string;
Self: {
ID: string;
UserID: number;
HostName: string;
DNSName: string;
OS: string;
TailscaleIPs: string[];
Capabilities: string[];
Online: boolean
};
User: Record<string, TailscaleUser> | null;
CurrentTailnet: {
Name: string;
MagicDNSSuffix: string;
MagicDNSEnabled: boolean;
} | null;
};
export type TailscaleUser = {
ID: number;
LoginName: string;
DisplayName: string;
ProfilePicURL: string;
Roles: string[];
};
export type TailscaleUpResponse = {
BackendState: TailscaleBackendState;
AuthURL?: string; // e.g. https://login.tailscale.com/a/0123456789abcdef
QR?: string; // a DataURL-encoded QR code PNG of the AuthURL
};