Websocket Support (#159)

This commit is contained in:
Gregory Schier
2025-01-31 09:00:11 -08:00
committed by GitHub
parent d411713502
commit c8be8082c5
122 changed files with 5090 additions and 616 deletions

View File

@@ -0,0 +1,26 @@
[package]
name = "yaak-ws"
links = "yaak-ws"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
futures-util = "0.3.31"
log = "0.4.20"
md5 = "0.7.0"
rustls = { version = "0.23.21", default-features = false, features = ["custom-provider", "ring"] }
rustls-platform-verifier = "0.5.0"
serde = { version = "1.0.217", features = ["derive"] }
tauri = { workspace = true }
thiserror = "2.0.11"
tokio = { version = "1.0", default-features = false, features = ["macros", "time", "test-util"] }
tokio-tungstenite = { version = "0.26.1", default-features = false, features = ["rustls-tls-native-roots", "connect"] }
yaak-models = { workspace = true }
yaak-plugins = { workspace = true }
yaak-templates = { workspace = true }
serde_json = "1.0.132"
chrono = "0.4.38"
[build-dependencies]
tauri-plugin = { workspace = true, features = ["build"] }

View File

@@ -0,0 +1,17 @@
use tauri_plugin;
const COMMANDS: &[&str] = &[
"close",
"connect",
"delete_connection",
"delete_connections",
"delete_request",
"list_connections",
"list_events",
"list_requests",
"send",
"upsert_request",
];
fn main() {
tauri_plugin::Builder::new(COMMANDS).build();
}

View File

@@ -0,0 +1,77 @@
import { invoke } from '@tauri-apps/api/core';
import { WebsocketConnection, WebsocketEvent, WebsocketRequest } from '@yaakapp-internal/models';
export function upsertWebsocketRequest(
request: WebsocketRequest | Partial<Omit<WebsocketRequest, 'id'>>,
) {
return invoke('plugin:yaak-ws|upsert_request', {
request,
}) as Promise<WebsocketRequest>;
}
export function deleteWebsocketRequest(requestId: string) {
return invoke('plugin:yaak-ws|delete_request', {
requestId,
});
}
export function deleteWebsocketConnection(connectionId: string) {
return invoke('plugin:yaak-ws|delete_connection', {
connectionId,
});
}
export function deleteWebsocketConnections(requestId: string) {
return invoke('plugin:yaak-ws|delete_connections', {
requestId,
});
}
export function listWebsocketRequests({ workspaceId }: { workspaceId: string }) {
return invoke('plugin:yaak-ws|list_requests', { workspaceId }) as Promise<WebsocketRequest[]>;
}
export function listWebsocketEvents({ connectionId }: { connectionId: string }) {
return invoke('plugin:yaak-ws|list_events', { connectionId }) as Promise<WebsocketEvent[]>;
}
export function listWebsocketConnections({ workspaceId }: { workspaceId: string }) {
return invoke('plugin:yaak-ws|list_connections', { workspaceId }) as Promise<
WebsocketConnection[]
>;
}
export function connectWebsocket({
requestId,
environmentId,
cookieJarId,
}: {
requestId: string;
environmentId: string | null;
cookieJarId: string | null;
}) {
return invoke('plugin:yaak-ws|connect', {
requestId,
environmentId,
cookieJarId,
}) as Promise<WebsocketConnection>;
}
export function closeWebsocket({ connectionId }: { connectionId: string }) {
return invoke('plugin:yaak-ws|close', {
connectionId,
});
}
export function sendWebsocket({
connectionId,
environmentId,
}: {
connectionId: string;
environmentId: string | null;
}) {
return invoke('plugin:yaak-ws|send', {
connectionId,
environmentId,
});
}

View File

@@ -0,0 +1,6 @@
{
"name": "@yaakapp-internal/ws",
"private": true,
"version": "1.0.0",
"main": "index.ts"
}

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-cancel"
description = "Enables the cancel command without any pre-configured scope."
commands.allow = ["cancel"]
[[permission]]
identifier = "deny-cancel"
description = "Denies the cancel command without any pre-configured scope."
commands.deny = ["cancel"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-close"
description = "Enables the close command without any pre-configured scope."
commands.allow = ["close"]
[[permission]]
identifier = "deny-close"
description = "Denies the close command without any pre-configured scope."
commands.deny = ["close"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-connect"
description = "Enables the connect command without any pre-configured scope."
commands.allow = ["connect"]
[[permission]]
identifier = "deny-connect"
description = "Denies the connect command without any pre-configured scope."
commands.deny = ["connect"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-delete-connection"
description = "Enables the delete_connection command without any pre-configured scope."
commands.allow = ["delete_connection"]
[[permission]]
identifier = "deny-delete-connection"
description = "Denies the delete_connection command without any pre-configured scope."
commands.deny = ["delete_connection"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-delete-connections"
description = "Enables the delete_connections command without any pre-configured scope."
commands.allow = ["delete_connections"]
[[permission]]
identifier = "deny-delete-connections"
description = "Denies the delete_connections command without any pre-configured scope."
commands.deny = ["delete_connections"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-delete-request"
description = "Enables the delete_request command without any pre-configured scope."
commands.allow = ["delete_request"]
[[permission]]
identifier = "deny-delete-request"
description = "Denies the delete_request command without any pre-configured scope."
commands.deny = ["delete_request"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-list-connections"
description = "Enables the list_connections command without any pre-configured scope."
commands.allow = ["list_connections"]
[[permission]]
identifier = "deny-list-connections"
description = "Denies the list_connections command without any pre-configured scope."
commands.deny = ["list_connections"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-list-events"
description = "Enables the list_events command without any pre-configured scope."
commands.allow = ["list_events"]
[[permission]]
identifier = "deny-list-events"
description = "Denies the list_events command without any pre-configured scope."
commands.deny = ["list_events"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-list-requests"
description = "Enables the list_requests command without any pre-configured scope."
commands.allow = ["list_requests"]
[[permission]]
identifier = "deny-list-requests"
description = "Denies the list_requests command without any pre-configured scope."
commands.deny = ["list_requests"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-list-websocket-connections"
description = "Enables the list_websocket_connections command without any pre-configured scope."
commands.allow = ["list_websocket_connections"]
[[permission]]
identifier = "deny-list-websocket-connections"
description = "Denies the list_websocket_connections command without any pre-configured scope."
commands.deny = ["list_websocket_connections"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-list-websocket-requests"
description = "Enables the list_websocket_requests command without any pre-configured scope."
commands.allow = ["list_websocket_requests"]
[[permission]]
identifier = "deny-list-websocket-requests"
description = "Denies the list_websocket_requests command without any pre-configured scope."
commands.deny = ["list_websocket_requests"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-send"
description = "Enables the send command without any pre-configured scope."
commands.allow = ["send"]
[[permission]]
identifier = "deny-send"
description = "Denies the send command without any pre-configured scope."
commands.deny = ["send"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-upsert-request"
description = "Enables the upsert_request command without any pre-configured scope."
commands.allow = ["upsert_request"]
[[permission]]
identifier = "deny-upsert-request"
description = "Denies the upsert_request command without any pre-configured scope."
commands.deny = ["upsert_request"]

View File

@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-upsert-websocket-request"
description = "Enables the upsert_websocket_request command without any pre-configured scope."
commands.allow = ["upsert_websocket_request"]
[[permission]]
identifier = "deny-upsert-websocket-request"
description = "Denies the upsert_websocket_request command without any pre-configured scope."
commands.deny = ["upsert_websocket_request"]

View File

@@ -0,0 +1,388 @@
## Default Permission
Default permissions for the plugin
- `allow-close`
- `allow-connect`
- `allow-delete-connection`
- `allow-delete-connections`
- `allow-delete-request`
- `allow-list-connections`
- `allow-list-events`
- `allow-list-requests`
- `allow-send`
- `allow-upsert-request`
## Permission Table
<table>
<tr>
<th>Identifier</th>
<th>Description</th>
</tr>
<tr>
<td>
`yaak-ws:allow-cancel`
</td>
<td>
Enables the cancel command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-cancel`
</td>
<td>
Denies the cancel command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-close`
</td>
<td>
Enables the close command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-close`
</td>
<td>
Denies the close command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-connect`
</td>
<td>
Enables the connect command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-connect`
</td>
<td>
Denies the connect command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-delete-connection`
</td>
<td>
Enables the delete_connection command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-delete-connection`
</td>
<td>
Denies the delete_connection command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-delete-connections`
</td>
<td>
Enables the delete_connections command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-delete-connections`
</td>
<td>
Denies the delete_connections command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-delete-request`
</td>
<td>
Enables the delete_request command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-delete-request`
</td>
<td>
Denies the delete_request command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-list-connections`
</td>
<td>
Enables the list_connections command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-list-connections`
</td>
<td>
Denies the list_connections command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-list-events`
</td>
<td>
Enables the list_events command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-list-events`
</td>
<td>
Denies the list_events command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-list-requests`
</td>
<td>
Enables the list_requests command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-list-requests`
</td>
<td>
Denies the list_requests command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-list-websocket-connections`
</td>
<td>
Enables the list_websocket_connections command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-list-websocket-connections`
</td>
<td>
Denies the list_websocket_connections command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-list-websocket-requests`
</td>
<td>
Enables the list_websocket_requests command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-list-websocket-requests`
</td>
<td>
Denies the list_websocket_requests command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-send`
</td>
<td>
Enables the send command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-send`
</td>
<td>
Denies the send command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-upsert-request`
</td>
<td>
Enables the upsert_request command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-upsert-request`
</td>
<td>
Denies the upsert_request command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:allow-upsert-websocket-request`
</td>
<td>
Enables the upsert_websocket_request command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`yaak-ws:deny-upsert-websocket-request`
</td>
<td>
Denies the upsert_websocket_request command without any pre-configured scope.
</td>
</tr>
</table>

View File

@@ -0,0 +1,14 @@
[default]
description = "Default permissions for the plugin"
permissions = [
"allow-close",
"allow-connect",
"allow-delete-connection",
"allow-delete-connections",
"allow-delete-request",
"allow-list-connections",
"allow-list-events",
"allow-list-requests",
"allow-send",
"allow-upsert-request",
]

View File

@@ -0,0 +1,445 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "PermissionFile",
"description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.",
"type": "object",
"properties": {
"default": {
"description": "The default permission set for the plugin",
"anyOf": [
{
"$ref": "#/definitions/DefaultPermission"
},
{
"type": "null"
}
]
},
"set": {
"description": "A list of permissions sets defined",
"type": "array",
"items": {
"$ref": "#/definitions/PermissionSet"
}
},
"permission": {
"description": "A list of inlined permissions",
"default": [],
"type": "array",
"items": {
"$ref": "#/definitions/Permission"
}
}
},
"definitions": {
"DefaultPermission": {
"description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.",
"type": "object",
"required": [
"permissions"
],
"properties": {
"version": {
"description": "The version of the permission.",
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 1.0
},
"description": {
"description": "Human-readable description of what the permission does. Tauri convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
"type": [
"string",
"null"
]
},
"permissions": {
"description": "All permissions this set contains.",
"type": "array",
"items": {
"type": "string"
}
}
}
},
"PermissionSet": {
"description": "A set of direct permissions grouped together under a new name.",
"type": "object",
"required": [
"description",
"identifier",
"permissions"
],
"properties": {
"identifier": {
"description": "A unique identifier for the permission.",
"type": "string"
},
"description": {
"description": "Human-readable description of what the permission does.",
"type": "string"
},
"permissions": {
"description": "All permissions this set contains.",
"type": "array",
"items": {
"$ref": "#/definitions/PermissionKind"
}
}
}
},
"Permission": {
"description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.",
"type": "object",
"required": [
"identifier"
],
"properties": {
"version": {
"description": "The version of the permission.",
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 1.0
},
"identifier": {
"description": "A unique identifier for the permission.",
"type": "string"
},
"description": {
"description": "Human-readable description of what the permission does. Tauri internal convention is to use <h4> headings in markdown content for Tauri documentation generation purposes.",
"type": [
"string",
"null"
]
},
"commands": {
"description": "Allowed or denied commands when using this permission.",
"default": {
"allow": [],
"deny": []
},
"allOf": [
{
"$ref": "#/definitions/Commands"
}
]
},
"scope": {
"description": "Allowed or denied scoped when using this permission.",
"allOf": [
{
"$ref": "#/definitions/Scopes"
}
]
},
"platforms": {
"description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Target"
}
}
}
},
"Commands": {
"description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.",
"type": "object",
"properties": {
"allow": {
"description": "Allowed command.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"deny": {
"description": "Denied command, which takes priority.",
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Scopes": {
"description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```",
"type": "object",
"properties": {
"allow": {
"description": "Data that defines what is allowed by the scope.",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Value"
}
},
"deny": {
"description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Value"
}
}
}
},
"Value": {
"description": "All supported ACL values.",
"anyOf": [
{
"description": "Represents a null JSON value.",
"type": "null"
},
{
"description": "Represents a [`bool`].",
"type": "boolean"
},
{
"description": "Represents a valid ACL [`Number`].",
"allOf": [
{
"$ref": "#/definitions/Number"
}
]
},
{
"description": "Represents a [`String`].",
"type": "string"
},
{
"description": "Represents a list of other [`Value`]s.",
"type": "array",
"items": {
"$ref": "#/definitions/Value"
}
},
{
"description": "Represents a map of [`String`] keys to [`Value`]s.",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Value"
}
}
]
},
"Number": {
"description": "A valid ACL number.",
"anyOf": [
{
"description": "Represents an [`i64`].",
"type": "integer",
"format": "int64"
},
{
"description": "Represents a [`f64`].",
"type": "number",
"format": "double"
}
]
},
"Target": {
"description": "Platform target.",
"oneOf": [
{
"description": "MacOS.",
"type": "string",
"enum": [
"macOS"
]
},
{
"description": "Windows.",
"type": "string",
"enum": [
"windows"
]
},
{
"description": "Linux.",
"type": "string",
"enum": [
"linux"
]
},
{
"description": "Android.",
"type": "string",
"enum": [
"android"
]
},
{
"description": "iOS.",
"type": "string",
"enum": [
"iOS"
]
}
]
},
"PermissionKind": {
"type": "string",
"oneOf": [
{
"description": "Enables the cancel command without any pre-configured scope.",
"type": "string",
"const": "allow-cancel"
},
{
"description": "Denies the cancel command without any pre-configured scope.",
"type": "string",
"const": "deny-cancel"
},
{
"description": "Enables the close command without any pre-configured scope.",
"type": "string",
"const": "allow-close"
},
{
"description": "Denies the close command without any pre-configured scope.",
"type": "string",
"const": "deny-close"
},
{
"description": "Enables the connect command without any pre-configured scope.",
"type": "string",
"const": "allow-connect"
},
{
"description": "Denies the connect command without any pre-configured scope.",
"type": "string",
"const": "deny-connect"
},
{
"description": "Enables the delete_connection command without any pre-configured scope.",
"type": "string",
"const": "allow-delete-connection"
},
{
"description": "Denies the delete_connection command without any pre-configured scope.",
"type": "string",
"const": "deny-delete-connection"
},
{
"description": "Enables the delete_connections command without any pre-configured scope.",
"type": "string",
"const": "allow-delete-connections"
},
{
"description": "Denies the delete_connections command without any pre-configured scope.",
"type": "string",
"const": "deny-delete-connections"
},
{
"description": "Enables the delete_request command without any pre-configured scope.",
"type": "string",
"const": "allow-delete-request"
},
{
"description": "Denies the delete_request command without any pre-configured scope.",
"type": "string",
"const": "deny-delete-request"
},
{
"description": "Enables the list_connections command without any pre-configured scope.",
"type": "string",
"const": "allow-list-connections"
},
{
"description": "Denies the list_connections command without any pre-configured scope.",
"type": "string",
"const": "deny-list-connections"
},
{
"description": "Enables the list_events command without any pre-configured scope.",
"type": "string",
"const": "allow-list-events"
},
{
"description": "Denies the list_events command without any pre-configured scope.",
"type": "string",
"const": "deny-list-events"
},
{
"description": "Enables the list_requests command without any pre-configured scope.",
"type": "string",
"const": "allow-list-requests"
},
{
"description": "Denies the list_requests command without any pre-configured scope.",
"type": "string",
"const": "deny-list-requests"
},
{
"description": "Enables the list_websocket_connections command without any pre-configured scope.",
"type": "string",
"const": "allow-list-websocket-connections"
},
{
"description": "Denies the list_websocket_connections command without any pre-configured scope.",
"type": "string",
"const": "deny-list-websocket-connections"
},
{
"description": "Enables the list_websocket_requests command without any pre-configured scope.",
"type": "string",
"const": "allow-list-websocket-requests"
},
{
"description": "Denies the list_websocket_requests command without any pre-configured scope.",
"type": "string",
"const": "deny-list-websocket-requests"
},
{
"description": "Enables the send command without any pre-configured scope.",
"type": "string",
"const": "allow-send"
},
{
"description": "Denies the send command without any pre-configured scope.",
"type": "string",
"const": "deny-send"
},
{
"description": "Enables the upsert_request command without any pre-configured scope.",
"type": "string",
"const": "allow-upsert-request"
},
{
"description": "Denies the upsert_request command without any pre-configured scope.",
"type": "string",
"const": "deny-upsert-request"
},
{
"description": "Enables the upsert_websocket_request command without any pre-configured scope.",
"type": "string",
"const": "allow-upsert-websocket-request"
},
{
"description": "Denies the upsert_websocket_request command without any pre-configured scope.",
"type": "string",
"const": "deny-upsert-websocket-request"
},
{
"description": "Default permissions for the plugin",
"type": "string",
"const": "default"
}
]
}
}
}

View File

@@ -0,0 +1,330 @@
use crate::error::Error::GenericError;
use crate::error::Result;
use crate::manager::WebsocketManager;
use crate::render::render_request;
use chrono::Utc;
use log::info;
use std::str::FromStr;
use tauri::http::{HeaderMap, HeaderName};
use tauri::{AppHandle, Manager, Runtime, State, WebviewWindow};
use tokio::sync::{mpsc, Mutex};
use tokio_tungstenite::tungstenite::http::HeaderValue;
use tokio_tungstenite::tungstenite::Message;
use yaak_models::models::{
HttpResponseHeader, WebsocketConnection, WebsocketConnectionState, WebsocketEvent,
WebsocketEventType, WebsocketRequest,
};
use yaak_models::queries;
use yaak_models::queries::{
get_base_environment, get_cookie_jar, get_environment, get_websocket_connection,
get_websocket_request, upsert_websocket_connection, upsert_websocket_event, UpdateSource,
};
use yaak_plugins::events::{
CallHttpAuthenticationRequest, HttpHeader, RenderPurpose, WindowContext,
};
use yaak_plugins::manager::PluginManager;
use yaak_plugins::template_callback::PluginTemplateCallback;
#[tauri::command]
pub(crate) async fn upsert_request<R: Runtime>(
request: WebsocketRequest,
w: WebviewWindow<R>,
) -> Result<WebsocketRequest> {
Ok(queries::upsert_websocket_request(&w, request, &UpdateSource::Window).await?)
}
#[tauri::command]
pub(crate) async fn delete_request<R: Runtime>(
request_id: &str,
w: WebviewWindow<R>,
) -> Result<WebsocketRequest> {
Ok(queries::delete_websocket_request(&w, request_id, &UpdateSource::Window).await?)
}
#[tauri::command]
pub(crate) async fn delete_connection<R: Runtime>(
connection_id: &str,
w: WebviewWindow<R>,
) -> Result<WebsocketConnection> {
Ok(queries::delete_websocket_connection(&w, connection_id, &UpdateSource::Window).await?)
}
#[tauri::command]
pub(crate) async fn delete_connections<R: Runtime>(
request_id: &str,
w: WebviewWindow<R>,
) -> Result<()> {
Ok(queries::delete_all_websocket_connections(&w, request_id, &UpdateSource::Window).await?)
}
#[tauri::command]
pub(crate) async fn list_events<R: Runtime>(
connection_id: &str,
app_handle: AppHandle<R>,
) -> Result<Vec<WebsocketEvent>> {
Ok(queries::list_websocket_events(&app_handle, connection_id).await?)
}
#[tauri::command]
pub(crate) async fn list_requests<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> Result<Vec<WebsocketRequest>> {
Ok(queries::list_websocket_requests(&app_handle, workspace_id).await?)
}
#[tauri::command]
pub(crate) async fn list_connections<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> Result<Vec<WebsocketConnection>> {
Ok(queries::list_websocket_connections_for_workspace(&app_handle, workspace_id).await?)
}
#[tauri::command]
pub(crate) async fn send<R: Runtime>(
connection_id: &str,
environment_id: Option<&str>,
window: WebviewWindow<R>,
ws_manager: State<'_, Mutex<WebsocketManager>>,
) -> Result<WebsocketConnection> {
let connection = get_websocket_connection(&window, connection_id).await?;
let unrendered_request = get_websocket_request(&window, &connection.request_id)
.await?
.ok_or(GenericError("WebSocket Request not found".to_string()))?;
let environment = match environment_id {
Some(id) => Some(get_environment(&window, id).await?),
None => None,
};
let base_environment = get_base_environment(&window, &unrendered_request.workspace_id).await?;
let request = render_request(
&unrendered_request,
&base_environment,
environment.as_ref(),
&PluginTemplateCallback::new(
window.app_handle(),
&WindowContext::from_window(&window),
RenderPurpose::Send,
),
)
.await;
let mut ws_manager = ws_manager.lock().await;
ws_manager.send(&connection.id, Message::Text(request.message.clone().into())).await?;
upsert_websocket_event(
&window,
WebsocketEvent {
connection_id: connection.id.clone(),
request_id: request.id.clone(),
workspace_id: connection.workspace_id.clone(),
is_server: false,
message_type: WebsocketEventType::Text,
message: request.message.into(),
..Default::default()
},
&UpdateSource::Window,
)
.await
.unwrap();
Ok(connection)
}
#[tauri::command]
pub(crate) async fn close<R: Runtime>(
connection_id: &str,
window: WebviewWindow<R>,
ws_manager: State<'_, Mutex<WebsocketManager>>,
) -> Result<WebsocketConnection> {
let connection = get_websocket_connection(&window, connection_id).await?;
let request = get_websocket_request(&window, &connection.request_id)
.await?
.ok_or(GenericError("WebSocket Request not found".to_string()))?;
let mut ws_manager = ws_manager.lock().await;
ws_manager.send(&connection.id, Message::Close(None)).await?;
upsert_websocket_event(
&window,
WebsocketEvent {
connection_id: connection.id.clone(),
request_id: request.id.clone(),
workspace_id: request.workspace_id.clone(),
is_server: false,
message_type: WebsocketEventType::Close,
..Default::default()
},
&UpdateSource::Window,
)
.await
.unwrap();
let connection = upsert_websocket_connection(
&window,
&WebsocketConnection {
state: WebsocketConnectionState::Closed,
elapsed: Utc::now()
.naive_utc()
.signed_duration_since(connection.created_at)
.num_milliseconds() as i32,
..connection.clone()
},
&UpdateSource::Window,
)
.await?;
Ok(connection)
}
#[tauri::command]
pub(crate) async fn connect<R: Runtime>(
request_id: &str,
environment_id: Option<&str>,
cookie_jar_id: Option<&str>,
window: WebviewWindow<R>,
plugin_manager: State<'_, PluginManager>,
ws_manager: State<'_, Mutex<WebsocketManager>>,
) -> Result<WebsocketConnection> {
let unrendered_request = get_websocket_request(&window, request_id)
.await?
.ok_or(GenericError("Failed to find GRPC request".to_string()))?;
let environment = match environment_id {
Some(id) => Some(get_environment(&window, id).await?),
None => None,
};
let base_environment = get_base_environment(&window, &unrendered_request.workspace_id).await?;
let request = render_request(
&unrendered_request,
&base_environment,
environment.as_ref(),
&PluginTemplateCallback::new(
window.app_handle(),
&WindowContext::from_window(&window),
RenderPurpose::Send,
),
)
.await;
let mut headers = HeaderMap::new();
if let Some(auth_name) = request.authentication_type.clone() {
let auth = request.authentication.clone();
let plugin_req = CallHttpAuthenticationRequest {
context_id: format!("{:x}", md5::compute(request_id.to_string())),
values: serde_json::from_value(serde_json::to_value(&auth).unwrap()).unwrap(),
method: "POST".to_string(),
url: request.url.clone(),
headers: request
.headers
.clone()
.into_iter()
.map(|h| HttpHeader {
name: h.name,
value: h.value,
})
.collect(),
};
let plugin_result =
plugin_manager.call_http_authentication(&window, &auth_name, plugin_req).await?;
for header in plugin_result.set_headers {
headers.insert(
HeaderName::from_str(&header.name).unwrap(),
HeaderValue::from_str(&header.value).unwrap(),
);
}
}
// TODO: Handle cookies
let _cookie_jar = match cookie_jar_id {
Some(id) => Some(get_cookie_jar(&window, id).await?),
None => None,
};
let connection = upsert_websocket_connection(
&window,
&WebsocketConnection {
workspace_id: request.workspace_id.clone(),
request_id: request_id.to_string(),
..Default::default()
},
&UpdateSource::Window,
)
.await?;
let (receive_tx, mut receive_rx) = mpsc::channel::<Message>(128);
let mut ws_manager = ws_manager.lock().await;
{
let connection_id = connection.id.clone();
let request_id = request.id.to_string();
let workspace_id = request.workspace_id.clone();
let window = window.clone();
tokio::spawn(async move {
while let Some(message) = receive_rx.recv().await {
upsert_websocket_event(
&window,
WebsocketEvent {
connection_id: connection_id.clone(),
request_id: request_id.clone(),
workspace_id: workspace_id.clone(),
is_server: true,
message_type: match message {
Message::Text(_) => WebsocketEventType::Text,
Message::Binary(_) => WebsocketEventType::Binary,
Message::Ping(_) => WebsocketEventType::Ping,
Message::Pong(_) => WebsocketEventType::Pong,
Message::Close(_) => WebsocketEventType::Close,
// Raw frame will never happen during a read
Message::Frame(_) => WebsocketEventType::Frame,
},
message: message.into_data().into(),
..Default::default()
},
&UpdateSource::Window,
)
.await
.unwrap();
}
info!("Websocket connection closed");
});
}
let response = match ws_manager.connect(&connection.id, &request.url, headers, receive_tx).await
{
Ok(r) => r,
Err(e) => {
return Ok(upsert_websocket_connection(
&window,
&WebsocketConnection {
error: Some(format!("{e:?}")),
..connection
},
&UpdateSource::Window,
)
.await?);
}
};
let response_headers = response
.headers()
.into_iter()
.map(|(name, value)| HttpResponseHeader {
name: name.to_string(),
value: value.to_str().unwrap().to_string(),
})
.collect::<Vec<HttpResponseHeader>>();
let connection = upsert_websocket_connection(
&window,
&WebsocketConnection {
state: WebsocketConnectionState::Connected,
headers: response_headers,
status: response.status().as_u16() as i32,
url: request.url.clone(),
..connection
},
&UpdateSource::Window,
)
.await?;
Ok(connection)
}

View File

@@ -0,0 +1,80 @@
use log::info;
use rustls::crypto::ring;
use rustls::ClientConfig;
use rustls_platform_verifier::BuilderVerifierExt;
use std::sync::Arc;
use tauri::http::HeaderMap;
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::handshake::client::Response;
use tokio_tungstenite::tungstenite::http::HeaderValue;
use tokio_tungstenite::tungstenite::protocol::WebSocketConfig;
use tokio_tungstenite::{
connect_async_tls_with_config, Connector, MaybeTlsStream, WebSocketStream,
};
pub(crate) async fn ws_connect(
url: &str,
headers: HeaderMap<HeaderValue>,
) -> crate::error::Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Response)> {
info!("Connecting to WS {url}");
let arc_crypto_provider = Arc::new(ring::default_provider());
let config = ClientConfig::builder_with_provider(arc_crypto_provider)
.with_safe_default_protocol_versions()
.unwrap()
.with_platform_verifier()
.with_no_client_auth();
let mut req = url.into_client_request()?;
let req_headers = req.headers_mut();
for (name, value) in headers {
if let Some(name) = name {
req_headers.insert(name, value);
}
}
let (stream, response) = connect_async_tls_with_config(
req,
Some(WebSocketConfig::default()),
false,
Some(Connector::Rustls(Arc::new(config))),
)
.await?;
Ok((stream, response))
}
#[cfg(test)]
mod tests {
use crate::connect::ws_connect;
use crate::error::Result;
use futures_util::{SinkExt, StreamExt};
use std::time::Duration;
use tokio::time::timeout;
use tokio_tungstenite::tungstenite::Message;
#[tokio::test]
async fn test_connection() -> Result<()> {
let (stream, response) = ws_connect("wss://echo.websocket.org/", Default::default()).await?;
assert_eq!(response.status(), 101);
let (mut write, mut read) = stream.split();
let task = tokio::spawn(async move {
while let Some(Ok(message)) = read.next().await {
if message.is_text() && message.to_text().unwrap() == "Hello" {
return message;
}
}
panic!("Didn't receive text message");
});
write.send(Message::Text("Hello".into())).await?;
let task = timeout(Duration::from_secs(3), task);
let message = task.await.unwrap().unwrap();
assert_eq!(message.into_text().unwrap(), "Hello");
Ok(())
}
}

View File

@@ -0,0 +1,29 @@
use serde::{Serialize, Serializer};
use thiserror::Error;
use tokio_tungstenite::tungstenite;
#[derive(Error, Debug)]
pub enum Error {
#[error("WebSocket error: {0}")]
WebSocketErr(#[from] tungstenite::Error),
#[error("Model error: {0}")]
ModelError(#[from] yaak_models::error::Error),
#[error("Plugin error: {0}")]
PluginError(#[from] yaak_plugins::error::Error),
#[error("WebSocket error: {0}")]
GenericError(String),
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
pub type Result<T> = std::result::Result<T, Error>;

View File

@@ -0,0 +1,37 @@
mod cmd;
mod connect;
mod error;
mod manager;
mod render;
use crate::cmd::{
close, connect, delete_connection, delete_connections, delete_request, list_connections,
list_events, list_requests, send, upsert_request,
};
use crate::manager::WebsocketManager;
use tauri::plugin::{Builder, TauriPlugin};
use tauri::{generate_handler, Manager, Runtime};
use tokio::sync::Mutex;
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("yaak-ws")
.invoke_handler(generate_handler![
close,
connect,
delete_connection,
delete_connections,
delete_request,
list_connections,
list_events,
list_requests,
send,
upsert_request,
])
.setup(|app, _api| {
let manager = WebsocketManager::new();
app.manage(Mutex::new(manager));
Ok(())
})
.build()
}

View File

@@ -0,0 +1,62 @@
use crate::connect::ws_connect;
use crate::error::Result;
use futures_util::stream::SplitSink;
use futures_util::{SinkExt, StreamExt};
use log::debug;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio::sync::{mpsc, Mutex};
use tokio_tungstenite::tungstenite::handshake::client::Response;
use tokio_tungstenite::tungstenite::http::{HeaderMap, HeaderValue};
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
#[derive(Clone)]
pub struct WebsocketManager {
connections:
Arc<Mutex<HashMap<String, SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>>>,
}
impl WebsocketManager {
pub fn new() -> Self {
WebsocketManager {
connections: Default::default(),
}
}
pub async fn connect(
&mut self,
id: &str,
url: &str,
headers: HeaderMap<HeaderValue>,
receive_tx: mpsc::Sender<Message>,
) -> Result<Response> {
let (stream, response) = ws_connect(url, headers).await?;
let (write, mut read) = stream.split();
self.connections.lock().await.insert(id.to_string(), write);
let tx = receive_tx.clone();
tauri::async_runtime::spawn(async move {
while let Some(Ok(message)) = read.next().await {
debug!("Received websocket message {message:?}");
if message.is_close() {
return;
}
tx.send(message).await.unwrap();
}
});
Ok(response)
}
pub async fn send(&mut self, id: &str, msg: Message) -> Result<()> {
debug!("Send websocket message {msg:?}");
let mut connections = self.connections.lock().await;
let connection = match connections.get_mut(id) {
None => return Ok(()),
Some(c) => c,
};
connection.send(msg).await?;
Ok(())
}
}

View File

@@ -0,0 +1,40 @@
use std::collections::BTreeMap;
use yaak_models::models::{Environment, HttpRequestHeader, WebsocketRequest};
use yaak_models::render::make_vars_hashmap;
use yaak_templates::{parse_and_render, render_json_value_raw, TemplateCallback};
pub async fn render_request<T: TemplateCallback>(
r: &WebsocketRequest,
base_environment: &Environment,
environment: Option<&Environment>,
cb: &T,
) -> WebsocketRequest {
let vars = &make_vars_hashmap(base_environment, environment);
let mut headers = Vec::new();
for p in r.headers.clone() {
headers.push(HttpRequestHeader {
enabled: p.enabled,
name: parse_and_render(&p.name, vars, cb).await,
value: parse_and_render(&p.value, vars, cb).await,
id: p.id,
})
}
let mut authentication = BTreeMap::new();
for (k, v) in r.authentication.clone() {
authentication.insert(k, render_json_value_raw(v, vars, cb).await);
}
let url = parse_and_render(r.url.as_str(), vars, cb).await;
let message = parse_and_render(&r.message.clone(), vars, cb).await;
WebsocketRequest {
url,
headers,
authentication,
message,
..r.to_owned()
}
}