Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playitgg integration #336

Merged
merged 32 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7138941
start working on playitgg integration
seliayeu Jun 21, 2023
072674b
finish mvp for playitgg (without key creation)
seliayeu Jun 25, 2023
8bf81f7
write skeleton code for playitgg signup support
seliayeu Jul 2, 2023
347491e
finish necessary features for signup on the backend
seliayeu Jul 2, 2023
caed6d1
make basic frontend (untested)
seliayeu Jul 3, 2023
af96f10
make bindings, change some types to support bindings
seliayeu Jul 3, 2023
a4c5e5f
bug fixes
seliayeu Jul 5, 2023
5997b66
display ip on tunnel start
seliayeu Jul 7, 2023
3802604
Merge branch 'dev' into playitgg_integration
Ynng Jul 17, 2023
9d4c197
Merge branch 'dev' into playitgg_integration
Ynng Jul 17, 2023
56d6f9f
merge
seliayeu Jul 18, 2023
aa04552
merge with monorepo
seliayeu Jul 18, 2023
f8624b5
Merge remote-tracking branch 'monorepo/playitgg_integration' into pla…
seliayeu Jul 18, 2023
4f4e204
bump tauri version
CheatCod Jul 20, 2023
acf83e1
add endpoint to launch cli
seliayeu Jul 21, 2023
ae80cdc
get cli starting, drop on exit working
seliayeu Jul 23, 2023
706bb0b
implement mvp using cli
seliayeu Jul 23, 2023
75f4913
change subprocess implementation to a playitgg run function
seliayeu Jul 23, 2023
1e93d6e
add ability to turn break playit run loop
seliayeu Jul 23, 2023
0aaf15a
make frontend nicer
seliayeu Aug 2, 2023
067ce2f
finish important features with bad design
seliayeu Aug 11, 2023
8901cc5
fix no key prop
seliayeu Aug 23, 2023
be75ee4
fix wrong tailwind classes
seliayeu Aug 27, 2023
66cd711
make playit integration look nicer
seliayeu Aug 27, 2023
ea4f7fd
update overview ui
seliayeu Nov 3, 2023
6712c69
update to new api
seliayeu Jan 1, 2024
c249958
add reload
seliayeu Jan 7, 2024
831d2a6
add ability to disable playit
seliayeu Jan 7, 2024
40fe2f1
make small change
seliayeu Jan 14, 2024
50c471d
fix clippy errors
seliayeu Jan 15, 2024
bd46700
bug fixes and some qol improvements
seliayeu Jan 22, 2024
76adede
remove unused file and add copyright notice
seliayeu Jan 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 193 additions & 39 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ clap = { version = "4.3.0", features = ["derive"] }
once_cell = "1.17.1"
fs3 = "0.5.0"
import_map = "0.15.0"
playit-agent-common = {git = "https://github.com/playit-cloud/playit-agent/", branch = "v0.9"}
playit-agent-core-v09 = {package = "playit-agent-core", git = "https://github.com/playit-cloud/playit-agent/", branch = "v0.9"}
playit-agent-core = {package = "playit-agent-core", git = "https://github.com/playit-cloud/playit-agent/", branch = "master"}
playit-agent-proto = {package = "playit-agent-proto", git = "https://github.com/playit-cloud/playit-agent/", branch = "master"}
hex = "0.4.3"
toml = "0.7.4"
[dependencies.uuid]
version = "1.1.2"
features = [
Expand Down
3 changes: 2 additions & 1 deletion core/bindings/EventInner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import type { FSEvent } from "./FSEvent";
import type { InstanceEvent } from "./InstanceEvent";
import type { MacroEvent } from "./MacroEvent";
import type { PlayitggRunnerEvent } from "./PlayitggRunnerEvent";
import type { ProgressionEvent } from "./ProgressionEvent";
import type { UserEvent } from "./UserEvent";

export type EventInner = { type: "InstanceEvent" } & InstanceEvent | { type: "UserEvent" } & UserEvent | { type: "MacroEvent" } & MacroEvent | { type: "FSEvent" } & FSEvent | { type: "ProgressionEvent" } & ProgressionEvent;
export type EventInner = { type: "InstanceEvent" } & InstanceEvent | { type: "UserEvent" } & UserEvent | { type: "MacroEvent" } & MacroEvent | { type: "FSEvent" } & FSEvent | { type: "ProgressionEvent" } & ProgressionEvent | { type: "PlayitggRunnerEvent" } & PlayitggRunnerEvent;
2 changes: 1 addition & 1 deletion core/bindings/EventType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type EventType = "InstanceEvent" | "UserEvent" | "MacroEvent" | "FSEvent" | "ProgressionEvent";
export type EventType = "InstanceEvent" | "UserEvent" | "MacroEvent" | "FSEvent" | "ProgressionEvent" | "PlayitggRunnerEvent";
2 changes: 1 addition & 1 deletion core/bindings/GlobalSettingsData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface GlobalSettingsData { core_name: string, safe_mode: boolean, domain: string | null, }
export interface GlobalSettingsData { core_name: string, safe_mode: boolean, domain: string | null, playit_enabled: boolean, }
3 changes: 3 additions & 0 deletions core/bindings/PlayitSignupData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export interface PlayitSignupData { url: string, claim_code: string, }
4 changes: 4 additions & 0 deletions core/bindings/PlayitTunnelInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { TunnelUuid } from "./TunnelUuid";

export interface PlayitTunnelInfo { local_ip: string, local_port: number, tunnel_id: TunnelUuid, name: string, server_address: string, active: boolean, }
4 changes: 4 additions & 0 deletions core/bindings/PlayitTunnelParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { PortType } from "./PortType";

export interface PlayitTunnelParams { local_port: number, port_type: PortType, }
4 changes: 4 additions & 0 deletions core/bindings/PlayitggRunnerEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { PlayitggRunnerEventInner } from "./PlayitggRunnerEventInner";

export interface PlayitggRunnerEvent { playitgg_runner_event_inner: PlayitggRunnerEventInner, }
3 changes: 3 additions & 0 deletions core/bindings/PlayitggRunnerEventInner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type PlayitggRunnerEventInner = { type: "RunnerStarted" } | { type: "RunnerLoading" } | { type: "RunnerStopped" };
3 changes: 3 additions & 0 deletions core/bindings/PortType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type PortType = "tcp" | "udp" | "both";
2 changes: 1 addition & 1 deletion core/bindings/ProgressionStartValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { InstanceUuid } from "./InstanceUuid";

export type ProgressionStartValue = { type: "InstanceCreation", instance_uuid: InstanceUuid } | { type: "InstanceDelete", instance_uuid: InstanceUuid, };
export type ProgressionStartValue = { type: "InstanceCreation", instance_uuid: InstanceUuid, } | { type: "InstanceDelete", instance_uuid: InstanceUuid, };
3 changes: 3 additions & 0 deletions core/bindings/SignupStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type SignupStatus = "CodeNotFound" | "CodeExpired" | "UserRejected" | "NotAccepted" | "NotSetup" | "Completed";
3 changes: 3 additions & 0 deletions core/bindings/TunnelUuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type TunnelUuid = string;
1 change: 1 addition & 0 deletions core/src/auth/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl User {
}
// TODO!,
EventInner::ProgressionEvent(_progression_event) => true,
EventInner::PlayitggRunnerEvent(_playitgg_runner_event) => true,
}
}

Expand Down
25 changes: 19 additions & 6 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,8 @@ pub enum ProgressionEndValue {
#[ts(export)]
#[serde(tag = "type")]
pub enum ProgressionStartValue {
InstanceCreation {
instance_uuid: InstanceUuid,
},
InstanceDelete {
instance_uuid: InstanceUuid,
},
InstanceCreation { instance_uuid: InstanceUuid },
InstanceDelete { instance_uuid: InstanceUuid },
}

// the backend will keep exactly 1 copy of ProgressionStart, and 1 copy of ProgressionUpdate OR ProgressionEnd
Expand All @@ -245,6 +241,16 @@ pub enum ProgressionEventInner {
inner: Option<ProgressionEndValue>,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, TS, PartialEq)]
#[ts(export)]
#[serde(tag = "type")]
pub enum PlayitggRunnerEventInner {
RunnerStarted,
RunnerLoading,
RunnerStopped,
}

#[derive(Serialize, Deserialize, Clone, Debug, TS, PartialEq)]
#[ts(export)]
pub enum FSOperation {
Expand Down Expand Up @@ -292,6 +298,12 @@ pub struct ProgressionEvent {
progression_event_inner: ProgressionEventInner,
}

#[derive(Serialize, Deserialize, Clone, Debug, TS, PartialEq)]
#[ts(export)]
pub struct PlayitggRunnerEvent {
pub playitgg_runner_event_inner: PlayitggRunnerEventInner,
}

impl ProgressionEvent {
pub fn event_id(&self) -> Snowflake {
self.event_id
Expand All @@ -312,6 +324,7 @@ pub enum EventInner {
MacroEvent(MacroEvent),
FSEvent(FSEvent),
ProgressionEvent(ProgressionEvent),
PlayitggRunnerEvent(PlayitggRunnerEvent),
}

impl AsRef<EventInner> for EventInner {
Expand Down
19 changes: 19 additions & 0 deletions core/src/global_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct GlobalSettingsData {
pub core_name: String,
pub safe_mode: bool,
pub domain: Option<String>,
pub playit_enabled: bool,
}

impl Default for GlobalSettingsData {
Expand All @@ -21,6 +22,7 @@ impl Default for GlobalSettingsData {
core_name: format!("{}'s Lodestone Core", whoami::realname()),
safe_mode: true,
domain: None,
playit_enabled: true,
}
}
}
Expand Down Expand Up @@ -146,6 +148,23 @@ impl GlobalSettings {
pub fn domain(&self) -> Option<String> {
self.global_settings_data.domain.clone()
}

pub async fn set_playit_enabled(&mut self, playit_enabled: bool) -> Result<(), Error> {
let old_playit_enabled = self.global_settings_data.playit_enabled;
self.global_settings_data.playit_enabled = playit_enabled;
match self.write_to_file().await {
Ok(_) => Ok(()),
Err(e) => {
self.global_settings_data.playit_enabled = old_playit_enabled;
Err(e)
}
}
}

pub fn playit_enabled(&self) -> bool {
self.global_settings_data.playit_enabled
}

}

impl AsRef<GlobalSettingsData> for GlobalSettings {
Expand Down
1 change: 1 addition & 0 deletions core/src/handlers/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ async fn console_stream_ws(
EventInner::MacroEvent(_) => continue,
EventInner::ProgressionEvent(_) => continue,
EventInner::FSEvent(_) => continue,
EventInner::PlayitggRunnerEvent(_) => continue,
}
}
Some(Ok(ws_msg)) = receiver.next() => {
Expand Down
24 changes: 24 additions & 0 deletions core/src/handlers/global_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,35 @@ pub async fn change_domain(
Ok(())
}

pub async fn change_core_playit_enabled(
axum::extract::State(state): axum::extract::State<AppState>,
AuthBearer(token): AuthBearer,
Json(playit_enabled): Json<bool>,
) -> Result<(), Error> {
let requester = state.users_manager.read().await.try_auth_or_err(&token)?;

if !requester.is_owner {
return Err(Error {
kind: ErrorKind::PermissionDenied,
source: eyre!("Not authorized to change Playit Enabled status."),
});
}

state
.global_settings
.lock()
.await
.set_playit_enabled(playit_enabled)
.await?;
Ok(())
}

pub fn get_global_settings_routes(state: AppState) -> Router {
Router::new()
.route("/global_settings", get(get_core_settings))
.route("/global_settings/name", put(change_core_name))
.route("/global_settings/safe_mode", put(change_core_safe_mode))
.route("/global_settings/domain", put(change_domain))
.route("/global_settings/playit_enabled", put(change_core_playit_enabled))
.with_state(state)
}
1 change: 1 addition & 0 deletions core/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pub mod monitor;
pub mod setup;
pub mod system;
pub mod users;
pub mod playitgg;
mod util;
15 changes: 15 additions & 0 deletions core/src/handlers/playitgg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use axum::{routing::{post, get}, Router};
use crate::playitgg::{stop_cli, generate_signup_link, start_cli, verify_key, cli_is_running, get_tunnels};

use crate::AppState;

pub fn get_playitgg_routes(state: AppState) -> Router {
Router::new()
.route("/playitgg/generate_signup_link", get(generate_signup_link))
.route("/playitgg/start_cli", post(start_cli))
.route("/playitgg/stop_cli", post(stop_cli))
.route("/playitgg/verify_key", post(verify_key))
.route("/playitgg/cli_is_running", get(cli_is_running))
.route("/playitgg/get_tunnels", get(get_tunnels))
.with_state(state)
}
31 changes: 30 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
instance_macro::get_instance_macro_routes, instance_players::get_instance_players_routes,
instance_server::get_instance_server_routes,
instance_setup_configs::get_instance_setup_config_routes, monitor::get_monitor_routes,
setup::get_setup_route, system::get_system_routes, users::get_user_routes,
playitgg::get_playitgg_routes, setup::get_setup_route, system::get_system_routes,
users::get_user_routes,
},
util::rand_alphanumeric,
};
Expand All @@ -35,9 +36,11 @@
use error::Error;
use events::{CausedBy, Event};
use futures::Future;
use futures_util::TryFutureExt;
use global_settings::GlobalSettings;

Check warning on line 40 in core/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `futures_util::TryFutureExt`

warning: unused import: `futures_util::TryFutureExt` --> core/src/lib.rs:40:5 | 40 | use futures_util::TryFutureExt; | ^^^^^^^^^^^^^^^^^^^^^^^^^^
use implementations::{generic, minecraft};
use macro_executor::MacroExecutor;
use playitgg::utils::is_valid_secret_key;
use port_manager::PortManager;
use prelude::GameInstance;
use reqwest::{header, Method};
Expand All @@ -46,6 +49,7 @@
use fs3::FileExt;
use semver::Version;
use sqlx::{sqlite::SqliteConnectOptions, Pool};
use std::sync::atomic::AtomicBool;
use std::{
collections::{HashMap, HashSet},
net::SocketAddr,
Expand Down Expand Up @@ -82,6 +86,7 @@
pub mod macro_executor;
mod migration;
mod output_types;
pub mod playitgg;
mod port_manager;
pub mod prelude;
pub mod tauri_export;
Expand All @@ -104,9 +109,11 @@
system: Arc<Mutex<sysinfo::System>>,
port_manager: Arc<Mutex<PortManager>>,
first_time_setup_key: Arc<Mutex<Option<String>>>,
playitgg_key: Arc<Mutex<Option<String>>>,
download_urls: Arc<Mutex<HashMap<String, DownloadableFile>>>,
macro_executor: MacroExecutor,
sqlite_pool: sqlx::SqlitePool,
playit_keep_running: Arc<Mutex<Option<Arc<AtomicBool>>>>,
}

impl AppState {
Expand Down Expand Up @@ -453,6 +460,25 @@
} else {
None
};

let playitgg_key = if let Ok(playitgg_file) =
tokio::fs::read_to_string(lodestone_path.join("playit.toml")).await
{
let toml_data: toml::Table = toml::from_str(&playitgg_file).unwrap();
if let Some(res) = toml_data["secret_key"].as_str() {
if is_valid_secret_key(res.to_string()).await {
println!("Validated playitgg key...");
Some(res.to_string())
} else {
None
}
} else {
None
}
} else {
None
};

let macro_executor = MacroExecutor::new(tx.clone(), tokio::runtime::Handle::current());
let instances = restore_instances(&path_to_instances, tx.clone(), macro_executor.clone())
.await
Expand All @@ -479,8 +505,10 @@
up_since: chrono::Utc::now().timestamp(),
port_manager: Arc::new(Mutex::new(PortManager::new(allocated_ports))),
first_time_setup_key: Arc::new(Mutex::new(first_time_setup_key)),
playitgg_key: Arc::new(Mutex::new(playitgg_key)),
system: Arc::new(Mutex::new(sysinfo::System::new_all())),
download_urls: Arc::new(Mutex::new(HashMap::new())),
playit_keep_running: Arc::new(Mutex::new(None)),
global_settings: Arc::new(Mutex::new(global_settings)),
macro_executor,
sqlite_pool: Pool::connect_with(
Expand Down Expand Up @@ -610,6 +638,7 @@
.merge(get_global_fs_routes(shared_state.clone()))
.merge(get_global_settings_routes(shared_state.clone()))
.merge(get_gateway_routes(shared_state.clone()))
.merge(get_playitgg_routes(shared_state.clone()))
.layer(cors)
.layer(trace);
let app = Router::new().nest("/api/v1", api_routes);
Expand Down
1 change: 1 addition & 0 deletions core/src/output_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl From<&Event> for ClientEvent {
}
},
EventInner::FSEvent(_) => EventLevel::Info,
EventInner::PlayitggRunnerEvent(_) => EventLevel::Info,
};
ClientEvent {
event_inner: event.event_inner.clone(),
Expand Down
Loading
Loading