From 39860c84e662c8ff71ad07a09675bd9fe51d2c83 Mon Sep 17 00:00:00 2001 From: ACoolName Date: Wed, 22 May 2024 19:56:08 +0300 Subject: [PATCH] continue go stuff --- src/actions.tsx | 1132 ++++++++++++++++++++++++----------------------- src/common.tsx | 6 +- src/users.tsx | 16 +- 3 files changed, 590 insertions(+), 564 deletions(-) diff --git a/src/actions.tsx b/src/actions.tsx index 88d8467..fe16fbb 100644 --- a/src/actions.tsx +++ b/src/actions.tsx @@ -2,289 +2,525 @@ import { ActionInfo } from "./common" export const Permission = { Start: 1, - Stop: 1 << 2, - Browse: 1 << 3, - Create: 1 << 4, - Delete: 1 << 5, - RunCommand: 1 << 6, - Admin: 1 << 7, + Stop: 1 << 1, + Browse: 1 << 2, + Create: 1 << 3, + Delete: 1 << 4, + RunCommand: 1 << 5, + Admin: 1 << 6, } const definitions = { - ChangeUserRequest: { - properties: { - permissions: { - items: { - "$ref": "#/definitions/Permission" + ChangeUserRequest: { + properties: { + permissions: { + items: { + "$ref": "#/definitions/Permission" + }, + type: "array", + title: "Permissions" + } + }, + type: "object", + required: [ + "permissions" + ], + title: "ChangeUserRequest" + }, + CreateUserRequest: { + properties: { + username: { + type: "string", + title: "Username" + }, + password: { + type: "string", + title: "Password" + } + }, + type: "object", + required: [ + "username", + "password" + ], + title: "CreateUserRequest" + }, + FileBrowserInfo: { + properties: { + id_: { + type: "string", + title: "Id " + }, + owner_id: { + type: "string", + title: "Owner Id" + }, + domain: { + type: "string", + title: "Domain" + }, + connected_to: { + "$ref": "#/definitions/ServerInfo" + }, + url: { + type: "string", + title: "Url", + readOnly: true + } + }, + type: "object", + required: [ + "id_", + "owner_id", + "domain", + "connected_to", + "url" + ], + title: "FileBrowserInfo" + }, + HTTPValidationError: { + properties: { + detail: { + items: { + "$ref": "#/definitions/ValidationError" + }, + type: "array", + title: "Detail" + } + }, + type: "object", + title: "HTTPValidationError" + }, + ImageInfo: { + properties: { + name: { + type: "string", + title: "Name" + }, + version: { + type: "string", + title: "Version" + }, + ports: { + items: { + "$ref": "#/definitions/Port-Output" + }, + type: "array", + uniqueItems: true, + title: "Ports" + }, + id_: { + type: "string", + title: "Id ", + readOnly: true + }, + display_name: { + type: "string", + title: "Display Name", + readOnly: true + } + }, + type: "object", + required: [ + "name", + "version", + "id_", + "display_name" + ], + title: "ImageInfo" + }, + InviteUserRequests: { + properties: { + email: { + type: "string", + title: "Email" + }, + permissions: { + items: { + "$ref": "#/definitions/Permission" + }, + type: "array", + title: "Permissions" + } + }, + type: "object", + required: [ + "email", + "permissions" + ], + title: "InviteUserRequests" + }, + PasswordRequestForm: { + properties: { + username: { + type: "string", + title: "Username" + }, + password: { + type: "string", + title: "Password" + }, + remember: { + anyOf: [ + { + type: "boolean" }, - type: "array", - title: "Permissions" - } - }, - type: "object", - required: [ - "permissions" - ], - title: "ChangeUserRequest" + { + type: "string" + } + ], + title: "Remember", + default: false + } }, - CreateServer: { - properties: { - image_id: { - type: "string", - title: "Image Id", - fetch_display_path: "display_name", - fetch_key_path: "id_", - fetch_url: "/images" - } + type: "object", + required: [ + "username", + "password" + ], + title: "PasswordRequestForm" + }, + Permission: { + type: "number", + enum: [ + "create", + "start", + "browse", + "cloud", + "run-command", + "delete", + "stop", + "admin" + ], + title: "Permission" + }, + "Port-Input": { + properties: { + number: { + type: "integer", + exclusiveMaximum: 65535, + exclusiveMinimum: 1, + title: "Number" }, - type: "object", - required: [ - "image_id" - ], - title: "CreateServer" + protocol: { + "$ref": "#/definitions/PortProtocol" + } }, - CreateUserRequest: { - properties: { - username: { - type: "string", - title: "Username" - }, - password: { - type: "string", - title: "Password" - } + type: "object", + required: [ + "number", + "protocol" + ], + title: "Port" + }, + "Port-Output": { + properties: { + number: { + type: "integer", + exclusiveMaximum: 65535, + exclusiveMinimum: 1, + title: "Number" }, - type: "object", - required: [ - "username", - "password" - ], - title: "CreateUserRequest" - }, - FileBrowserInfo: { - properties: { - id_: { - type: "string", - title: "Id " - }, - owner_id: { - type: "string", - title: "Owner Id" - }, - domain: { - type: "string", - title: "Domain" - }, - connected_to: { - "$ref": "#/definitions/ServerInfo" - }, - url: { - type: "string", - title: "Url", - readOnly: true - } + protocol: { + "$ref": "#/definitions/PortProtocol" }, - type: "object", - required: [ - "id_", - "owner_id", - "domain", - "connected_to", - "url" - ], - title: "FileBrowserInfo" + id_: { + type: "string", + title: "Id ", + readOnly: true + } }, - HTTPValidationError: { - properties: { - detail: { - items: { - "$ref": "#/definitions/ValidationError" + type: "object", + required: [ + "number", + "protocol", + "id_" + ], + title: "Port" + }, + PortMapping: { + properties: { + source_port: { + "$ref": "#/definitions/Port-Input" + }, + destination_port: { + anyOf: [ + { + "$ref": "#/definitions/Port-Input" }, - type: "array", - title: "Detail" - } - }, - type: "object", - title: "HTTPValidationError" + { + type: "null" + } + ] + } }, - ImageInfo: { - properties: { - name: { - type: "string", - title: "Name" - }, - version: { - type: "string", - title: "Version" - }, - ports: { - items: { - "$ref": "#/definitions/Port-Output" + type: "object", + required: [ + "source_port" + ], + title: "PortMapping" + }, + PortProtocol: { + type: "string", + enum: [ + "tcp", + "udp" + ], + title: "PortProtocol" + }, + ServerInfo: { + properties: { + id_: { + type: "string", + title: "Id " + }, + user_id: { + type: "string", + title: "User Id" + }, + image: { + "$ref": "#/definitions/ImageInfo" + }, + on: { + type: "boolean", + title: "On" + }, + domain: { + anyOf: [ + { + type: "string" }, - type: "array", - uniqueItems: true, - title: "Ports" - }, - id_: { - type: "string", - title: "Id ", - readOnly: true - }, - display_name: { - type: "string", - title: "Display Name", - readOnly: true - } + { + type: "null" + } + ], + title: "Domain" }, - type: "object", - required: [ - "name", - "version", - "id_", - "display_name" - ], - title: "ImageInfo" - }, - InviteUserRequests: { - properties: { - email: { - type: "string", - title: "Email" - }, - permissions: { - items: { - "$ref": "#/definitions/Permission" - }, - type: "array", - title: "Permissions" - } - }, - type: "object", - required: [ - "email", - "permissions" - ], - title: "InviteUserRequests" - }, - PasswordRequestForm: { - properties: { - username: { - type: "string", - title: "Username" - }, - password: { - type: "string", - title: "Password" - }, - remember: { - anyOf: [ - { - type: "boolean" + ports: { + anyOf: [ + { + items: { + "$ref": "#/definitions/Port-Output" }, + type: "array", + uniqueItems: true + }, + { + type: "null" + } + ], + title: "Ports" + }, + nickname: { + anyOf: [ + { + type: "string" + }, + { + type: "null" + } + ], + title: "Nickname" + } + }, + type: "object", + required: [ + "id_", + "user_id", + "image", + "on" + ], + title: "ServerInfo" + }, + SetServerNicknameRequest: { + properties: { + nickname: { + type: "string", + title: "Nickname" + } + }, + type: "object", + required: [ + "nickname" + ], + title: "SetServerNicknameRequest" + }, + SetServerPermissionsRequest: { + properties: { + username: { + type: "string", + title: "Username", + fetch_display_path: "username", + fetch_key_path: "username", + fetch_url: "/users" + }, + permissions: { + items: { + "$ref": "#/definitions/Permission" + }, + type: "array", + title: "Permissions" + } + }, + type: "object", + required: [ + "username" + ], + title: "SetServerPermissionsRequest" + }, + StartServerRequest: { + properties: { + ports: { + items: { + "$ref": "#/definitions/PortMapping" + }, + type: "array", + title: "Ports", + default: [] + }, + command: { + anyOf: [ + { + type: "string" + }, + { + type: "null" + } + ], + title: "Command" + } + }, + type: "object", + title: "StartServerRequest" + }, + Token: { + properties: { + access_token: { + type: "string", + title: "Access Token" + }, + token_type: { + type: "string", + title: "Token Type" + } + }, + type: "object", + required: [ + "access_token", + "token_type" + ], + title: "Token" + }, + UserView: { + properties: { + username: { + type: "string", + title: "Username" + }, + email: { + type: "string", + title: "Email" + }, + max_owned_servers: { + type: "integer", + title: "Max Owned Servers", + default: 5 + }, + permissions: { + items: { + "$ref": "#/definitions/Permission" + }, + type: "array", + title: "Permissions" + } + }, + type: "object", + required: [ + "username", + "email" + ], + title: "UserView" + }, + ValidationError: { + properties: { + loc: { + items: { + anyOf: [ { type: "string" - } - ], - title: "Remember", - default: false - } - }, - type: "object", - required: [ - "username", - "password" - ], - title: "PasswordRequestForm" - }, - Permission: { - type: "number", - enum: [ - "create", - "start", - "browse", - "cloud", - "run-command", - "delete", - "stop", - "admin" - ], - title: "Permission" - }, - "Port-Input": { - properties: { - number: { - type: "integer", - exclusiveMaximum: 65535, - exclusiveMinimum: 1, - title: "Number" - }, - protocol: { - "$ref": "#/definitions/PortProtocol" - } - }, - type: "object", - required: [ - "number", - "protocol" - ], - title: "Port" - }, - "Port-Output": { - properties: { - number: { - type: "integer", - exclusiveMaximum: 65535, - exclusiveMinimum: 1, - title: "Number" - }, - protocol: { - "$ref": "#/definitions/PortProtocol" - }, - id_: { - type: "string", - title: "Id ", - readOnly: true - } - }, - type: "object", - required: [ - "number", - "protocol", - "id_" - ], - title: "Port" - }, - PortMapping: { - properties: { - source_port: { - "$ref": "#/definitions/Port-Input" - }, - destination_port: { - anyOf: [ - { - "$ref": "#/definitions/Port-Input" }, { - type: "null" + type: "integer" } ] - } + }, + type: "array", + title: "Location" }, + msg: { + type: "string", + title: "Message" + }, + type: { + type: "string", + title: "Error Type" + } + }, + type: "object", + required: [ + "loc", + "msg", + "type" + ], + title: "ValidationError" + } +} + + + +export const SERVER_ACTIONS: ActionInfo[] = [ + { + name: "Start", + args: { + "$ref": "#/definitions/StartServerRequest", + definitions: definitions, + }, + requestType: "post", + endpoint: "/servers/{server_id}/start", + permissions: Permission.Start, + response_action: "Ignore" + }, + { + name: "Stop", + args: { + title: "Stop", type: "object", - required: [ - "source_port" - ], - title: "PortMapping" + required: [], + properties: {}, + default: {} }, - PortProtocol: { - type: "string", - enum: [ - "tcp", - "udp" - ], - title: "PortProtocol" + requestType: "post", + endpoint: "/servers/{server_id}/stop", + permissions: Permission.Stop, + response_action: "Ignore" + }, + { + name: "Delete Server", + args: { + title: "Delete Server", + type: "object", + required: [], + properties: {}, + default: {} }, - RunCommandRequest: { + requestType: "delete", + endpoint: "/servers/{server_id}", + permissions: Permission.Delete, + response_action: "Ignore" + }, + { + name: "Run Command", + args: { properties: { command: { type: "string", @@ -295,312 +531,86 @@ const definitions = { required: [ "command" ], - title: "RunCommandRequest" + title: "Run Command", + definitions: definitions, }, - ServerInfo: { - properties: { - id_: { - type: "string", - title: "Id " - }, - user_id: { - type: "string", - title: "User Id" - }, - image: { - "$ref": "#/definitions/ImageInfo" - }, - on: { - type: "boolean", - title: "On" - }, - domain: { - anyOf: [ - { - type: "string" - }, - { - type: "null" - } - ], - title: "Domain" - }, - ports: { - anyOf: [ - { - items: { - "$ref": "#/definitions/Port-Output" - }, - type: "array", - uniqueItems: true - }, - { - type: "null" - } - ], - title: "Ports" - }, - nickname: { - anyOf: [ - { - type: "string" - }, - { - type: "null" - } - ], - title: "Nickname" - } - }, + requestType: "post", + endpoint: "/servers/{server_id}/run_command", + permissions: Permission.RunCommand, + response_action: "Ignore" + }, + { + name: "Browse", + args: { + title: "Browse", type: "object", - required: [ - "id_", - "user_id", - "image", - "on" - ], - title: "ServerInfo" + required: [], + properties: {}, + default: {} }, - SetServerNicknameRequest: { - properties: { - nickname: { - type: "string", - title: "Nickname" - } - }, - type: "object", - required: [ - "nickname" - ], - title: "SetServerNicknameRequest" + requestType: "post", + endpoint: "/servers/{server_id}/browse", + permissions: Permission.Browse, + response_action: "Browse" + }, + { + name: "Set Nickname", + args: { + "$ref": "#/definitions/SetServerNicknameRequest", + definitions: definitions, + }, - SetServerPermissionsRequest: { - properties: { - username: { - type: "string", - title: "Username", - fetch_display_path: "username", - fetch_key_path: "username", - fetch_url: "/users" - }, - permissions: { - items: { - "$ref": "#/definitions/Permission" - }, - type: "array", - title: "Permissions" - } - }, - type: "object", - required: [ - "username" - ], - title: "SetServerPermissionsRequest" + requestType: "post", + endpoint: "/servers/{server_id}/nickname", + permissions: Permission.Admin, + response_action: "Ignore" + }, + { + name: "Add Permissions", + args: { + "$ref": "#/definitions/SetServerPermissionsRequest", + definitions: definitions, }, - StartServerRequest: { - properties: { - ports: { - items: { - "$ref": "#/definitions/PortMapping" - }, - type: "array", - title: "Ports", - default: [] - }, - command: { - anyOf: [ - { - type: "string" - }, - { - type: "null" - } - ], - title: "Command" - } - }, - type: "object", - title: "StartServerRequest" - }, - Token: { - properties: { - access_token: { - type: "string", - title: "Access Token" - }, - token_type: { - type: "string", - title: "Token Type" - } - }, - type: "object", - required: [ - "access_token", - "token_type" - ], - title: "Token" - }, - UserView: { - properties: { - username: { - type: "string", - title: "Username" - }, - email: { - type: "string", - title: "Email" - }, - max_owned_servers: { - type: "integer", - title: "Max Owned Servers", - default: 5 - }, - permissions: { - items: { - "$ref": "#/definitions/Permission" - }, - type: "array", - title: "Permissions" - } - }, - type: "object", - required: [ - "username", - "email" - ], - title: "UserView" - }, - ValidationError: { - properties: { - loc: { - items: { - anyOf: [ - { - type: "string" - }, - { - type: "integer" - } - ] - }, - type: "array", - title: "Location" - }, - msg: { - type: "string", - title: "Message" - }, - type: { - type: "string", - title: "Error Type" - } - }, - type: "object", - required: [ - "loc", - "msg", - "type" - ], - title: "ValidationError" - } + requestType: "post", + endpoint: "/servers/{server_id}/permissions", + permissions: Permission.Admin, + response_action: "Ignore" } - - - -export const SERVER_ACTIONS: ActionInfo[] = [ - { - name: "Start", - args: { - "$ref": "#/definitions/StartServerRequest", - definitions: definitions, - }, - requestType: "post", - endpoint: "/servers/{server_id}/start", - permissions: Permission.Start, - response_action: "Ignore" - }, - { - name: "Stop", - args: { - title: "Stop", - type: "object", - required: [], - properties: {}, - default: {} - }, - requestType: "post", - endpoint: "/servers/{server_id}/stop", - permissions: Permission.Stop, - response_action: "Ignore" - }, - { - name: "Delete Server", - args: { - title: "Delete Server", - type: "object", - required: [], - properties: {}, - default: {} - }, - requestType: "delete", - endpoint: "/servers/{server_id}", - permissions: Permission.Delete, - response_action: "Ignore" - }, - { - name: "Run Command", - args: { - "$ref": "#/definitions/RunCommandRequest", - definitions: definitions, - }, - requestType: "post", - endpoint: "/servers/{server_id}/command", - permissions: Permission.RunCommand, - response_action: "Ignore" - }, - { - name: "Browse", - args: { - title: "Browse", - type: "object", - required: [], - properties: {}, - default: {} - }, - requestType: "post", - endpoint: "/servers/{server_id}/browse", - permissions: Permission.Browse, - response_action: "Browse" - }, - { - name: "Set Nickname", - args: { - "$ref": "#/definitions/SetServerNicknameRequest", - definitions: definitions, - - }, - requestType: "post", - endpoint: "/servers/{server_id}/nickname", - permissions: Permission.Admin, - response_action: "Ignore" - }, - { - name: "Add Permissions", - args: { - "$ref": "#/definitions/SetServerPermissionsRequest", - definitions: definitions, - }, - requestType: "post", - endpoint: "/servers/{server_id}/permissions", - permissions: Permission.Admin, - response_action: "Ignore" - } - ] +] export const CREATE_SERVER_ACTION: ActionInfo = { name: "Create Server", args: { - "$ref": "#/definitions/CreateServer", + properties: { + ImageId: { + type: "string", + title: "Image Id", + fetch_display_path: "DisplayName", + fetch_key_path: "Id", + fetch_url: "/images" + }, + Nickname: { + type: "string", + title: "Server Nickname" + }, + DefaultCommand: { + type: "string", + title: "Default Command" + }, + DefaultPorts: { + items: { + "$ref": "#/definitions/Port-Input" + }, + type: "array", + title: "Default Ports", + } + }, + type: "object", + required: [ + "ImageId" + ], + title: "Create Server", + definitions: definitions, }, requestType: "post", @@ -610,7 +620,7 @@ export const CREATE_SERVER_ACTION: ActionInfo = { } export const INVITE_USER_ACTION: ActionInfo = { - name: "Create Server", + name: "Invite User", args: { "$ref": "#/definitions/InviteUserRequests", definitions: definitions, @@ -619,4 +629,14 @@ export const INVITE_USER_ACTION: ActionInfo = { endpoint: "/users", permissions: Permission.Admin, response_action: "Ignore" -} \ No newline at end of file +} +export const USERS_ACTIONS: ActionInfo[] = [ + { + name: "Delete User", + args: {}, + requestType: "get", + endpoint: "/users/{user_id}", + permissions: Permission.Admin, + response_action: "Ignore" + } +] \ No newline at end of file diff --git a/src/common.tsx b/src/common.tsx index c76534a..d8aece6 100644 --- a/src/common.tsx +++ b/src/common.tsx @@ -81,14 +81,14 @@ export const api: AxiosInstance = axios.create({ export function ApiWrapper(p: { children: ReactNode}) { const {children} = p - const token = localStorage.getItem('token') + const token = Cookies.get('auth') if (token) { api.defaults.headers.common.Authorization = `Bearer ${token}`; - } + const [apiAuthenticated, setApiAuthenticated] = useState(Boolean(token)) if (!apiAuthenticated) { - localStorage.removeItem('token') + Cookies.remove('auth') } const path = useLocation() return ( diff --git a/src/users.tsx b/src/users.tsx index 4116001..ffdea33 100644 --- a/src/users.tsx +++ b/src/users.tsx @@ -2,7 +2,7 @@ import { TableRow, TableCell, Chip } from "@mui/material" import { useContext, Dispatch, useState, useEffect, Context, createContext } from "react" import { apiAuthenticatedContext, api, ActionInfo, ActionGroup, actionIdentifierContext, DataTable } from "./common" import { User } from './interfaces' -import { INVITE_USER_ACTION, Permission } from "./actions"; +import { INVITE_USER_ACTION, Permission, USERS_ACTIONS } from "./actions"; const UserActionsContext: Context = createContext([] as ActionInfo[]) function getPermissionStrings(permissions: number){ @@ -26,7 +26,7 @@ function getPermissionStrings(permissions: number){ strings.push("Delete") } if (permissions & Permission.RunCommand){ - strings.push("RunCommand") + strings.push("Run Command") } return strings } @@ -48,6 +48,7 @@ function UserItem(p: { user: User }) { export function UsersPage({ }) { const [apiAuthenticated, setApiAuthenticated] = useContext(apiAuthenticatedContext) const [users, setUsers]: [User[], Dispatch] = useState([] as User[]) + console.log({apiAuthenticated: apiAuthenticated, users: users}) const action: ActionInfo|undefined = INVITE_USER_ACTION @@ -55,7 +56,9 @@ export function UsersPage({ }) { if (!apiAuthenticated) { return } - api.get('/users').then((response) => { setUsers(response.data) }).catch( + api.get('/users').then((response) => { + setUsers(response.data) + }).catch( (error) => { console.log('Failed to get servers: ' + error); if (error.response) { @@ -70,7 +73,10 @@ export function UsersPage({ }) { ) const interval = setInterval(() => { - api.get('/users').then((response) => { setUsers(response.data) }).catch( + api.get('/users').then((response) => { + setUsers(response.data) + + }).catch( (error) => { console.log('Failed to get users: ' + error); if (error.response) { @@ -96,7 +102,7 @@ export function UsersPage({ }) { } return - + {userComponents}