base code and functional data api

This commit is contained in:
core 2023-07-20 16:03:10 -04:00
parent 420e85a64d
commit 006b7ff92a
Signed by: core
GPG Key ID: FDBF740DADDCEECF
18 changed files with 7348 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/api/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/azalea-worker/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/common/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/websocket-worker/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/locationoverflow.iml" filepath="$PROJECT_DIR$/.idea/locationoverflow.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

6824
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,5 +2,6 @@
members = [ members = [
"api", "api",
"common", "common",
"websocket-worker" "websocket-worker",
"azalea-worker"
] ]

21
api/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
name = "api"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
simple_logger = "4.1"
actix-web = "4.3"
serde = { version = "1", features = ["derive"] }
toml = "0.7"
url = { version = "2.3", features = ["serde"] }
actix-web-actors = "4.2"
actix = "0.13"
lazy_static = "1.4"
chrono = "0.4"
serde_json = "1"
common = { path = "../common" }

58
api/src/main.rs Normal file
View File

@ -0,0 +1,58 @@
use std::error::Error;
use std::fs;
use std::fs::File;
use std::sync::{Arc, RwLock};
use actix_web::{App, HttpServer, web};
use actix_web::web::Data;
use log::info;
use crate::config::Config;
use crate::gateway::WsClient;
use crate::status::status_endpoint;
pub mod config;
pub mod status;
pub mod gateway;
#[actix_web::main]
async fn main() -> Result<(), Box<dyn Error>> {
simple_logger::init_with_env().unwrap();
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("usage: ./api <config>");
std::process::exit(1);
}
let file = args.nth(1).unwrap();
let config_str = fs::read_to_string(&file)?;
let config: Config = toml::from_str(&config_str)?;
info!("Config loaded from {}", file);
let s_config = config.clone();
info!("Base URL is {}", config.server.base_url);
let log_file = File::options().create(true).append(true).open(config.log.log_file)?;
let log_arc = Arc::new(RwLock::new(log_file));
info!("Starting http server on {}:{}", config.server.bind, config.server.port);
let clients: Arc<RwLock<Vec<WsClient>>> = Arc::new(RwLock::new(Vec::new()));
HttpServer::new(move || {
App::new()
.service(status_endpoint)
.route("/gateway", web::get().to(gateway::gateway))
.app_data(Data::new(s_config.clone()))
.app_data(Data::new(clients.clone()))
.app_data(Data::new(log_arc.clone()))
})
.bind((config.server.bind, config.server.port))?
.run()
.await?;
Ok(())
}

32
azalea-worker/Cargo.toml Normal file
View File

@ -0,0 +1,32 @@
[package]
name = "azalea-worker"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
simple_logger = "4.1"
serde = { version = "1", features = ["derive"] }
toml = "0.7"
url = { version = "2.3", features = ["serde"] }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json"] }
serde_json = "1"
async-trait = "0.1"
websocket = { version = "0.26", features = ["async"] }
common = { path = "../common" }
tokio-threadpool = "0.1"
bevy_ecs = "0.11"
bevy = "0.11"
bevy_app = "0.11"
parking_lot = "0.12"
futures = "0.3"
regex = "1.8.1"
uuid = "1.3"
azalea-protocol = { version = "0.7", git = "https://git.e3t.cc/~core/azalea" }
azalea-crypto = { version = "0.7", git = "https://git.e3t.cc/~core/azalea" }
azalea-core = { version = "0.7", git = "https://git.e3t.cc/~core/azalea" }
azalea-buf = { version = "0.7", git = "https://git.e3t.cc/~core/azalea" }
azalea-client = { version = "0.7", git = "https://git.e3t.cc/~core/azalea" }

17
azalea-worker/config.toml Normal file
View File

@ -0,0 +1,17 @@
api_status_url = "http://localhost:8171/status"
token = "test-rw-token"
[server]
ip = "95.216.24.174"
port = 587
protocol_version = 763
username = "CuberCore"
uuid = "10d7bb90-376a-4b72-9bd3-6b885dfd89e3"
microsoft_email = "coredoescode@outlook.com"
owner_username = "CoreCuber"
owner_uuid_undashed = "555d60bb4b4f470880b231bb1de0c527"
ignored = []
[permissions]
owner = []
admin = []

View File

@ -0,0 +1,31 @@
use std::net::Ipv4Addr;
use serde::{Deserialize, Serialize};
use url::Url;
use uuid::Uuid;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
pub api_status_url: Url,
pub token: String,
pub server: ServerConfig,
pub permissions: PermissionsConfig
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ServerConfig {
pub ip: Ipv4Addr,
pub port: u16,
pub protocol_version: u64,
pub username: String,
pub uuid: Uuid,
pub microsoft_email: String,
pub owner_username: String,
pub owner_uuid_undashed: String,
pub ignored: Vec<String>
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PermissionsConfig {
pub owner: Vec<Uuid>,
pub admin: Vec<Uuid>
}

166
azalea-worker/src/main.rs Normal file
View File

@ -0,0 +1,166 @@
use std::error::Error;
use std::fs;
use std::net::SocketAddr;
use azalea_client::{Account, Client, DefaultPlugins, start_ecs};
use azalea_client::packet_handling::DeathEvent;
use azalea_client::respawn::{perform_respawn, PerformRespawnEvent};
use azalea_protocol::packets::game::serverbound_client_information_packet::{ChatVisibility, ServerboundClientInformationPacket};
use azalea_protocol::ServerAddress;
use bevy_app::{App, Plugin, Update};
use bevy_ecs::event::{EventReader, EventWriter};
use bevy_ecs::prelude::IntoSystemConfigs;
use log::{debug, error, info};
use tokio::sync::mpsc;
use tokio_threadpool::ThreadPool;
use url::Url;
use websocket::{ClientBuilder, Message, OwnedMessage};
use websocket::websocket_base::result::WebSocketResult;
use common::message::{GatewayChatMessage, GatewayPacketC2S, GatewayPacketS2C};
use common::status::{DATA_API_VERSION, Status};
use crate::config::Config;
pub mod config;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
simple_logger::init_with_env().unwrap();
info!("Loading config");
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("usage: ./azalea-worker <config>");
std::process::exit(1);
}
let file = args.nth(1).unwrap();
let config_str = fs::read_to_string(&file)?;
let config: Config = toml::from_str(&config_str)?;
info!("Config loaded from {}", file);
info!("Loading status from the API ({})...", config.api_status_url);
let status: Status = reqwest::get(config.api_status_url).await?.json().await?;
debug!("{:?}", status);
if status.data_api_version != DATA_API_VERSION {
error!("Data API is incompatible. This version of the websocket worker was compiled with Data API v{}, but your Data API is Data API v{}", DATA_API_VERSION, status.data_api_version);
std::process::exit(1);
}
info!("Connecting to gateway uri {}", status.gateway_url.to_string());
let mut client = ClientBuilder::new(status.gateway_url.as_str())?.connect(None)?;
// send the authentication packet, and then start listen
let message = Message::text(serde_json::to_string(&GatewayPacketC2S::Authenticate {
token: config.token.clone(),
request_write_perms: true,
})?);
client.send_message(&message)?;
//let (mut tx, mut rx) = client.split().unwrap();
info!("Connecting to the Minecraft server...");
let account = Account::microsoft(&config.server.microsoft_email).await?;
let (run_schedule_sender, run_schedule_receiver) = mpsc::unbounded_channel();
let mut app = App::new();
app.add_plugins((DefaultPlugins, AutoRespawnPlugin));
let ecs_lock = start_ecs(app, run_schedule_receiver, run_schedule_sender.clone());
let (mut client, mut rx) = Client::start_client(
ecs_lock,
&account,
&ServerAddress::from(SocketAddr::new(config.server.ip.into(), config.server.port)),
&SocketAddr::new(config.server.ip.into(), config.server.port),
run_schedule_sender
).await?;
client.chat("Hello!");
client.set_client_information(ServerboundClientInformationPacket {
language: "en_US".to_string(),
view_distance: 16,
chat_visibility: ChatVisibility::Full,
chat_colors: true,
model_customization: Default::default(),
main_hand: Default::default(),
text_filtering_enabled: false,
allows_listing: true,
}).await?;
/*
for msg in client.incoming_messages() {
match msg {
Ok(msg) => {
match msg {
OwnedMessage::Text(txt) => {
// decode
debug!("{}", txt);
let packet: GatewayPacketS2C = serde_json::from_str(&txt)?;
match packet {
GatewayPacketS2C::AuthenticationAccepted { .. } => {
info!("auth accepted by server");
}
GatewayPacketS2C::Disconnect { reason } => {
info!("disconnected by server: {:?}", reason);
return Err("Disconnected by server")?;
}
GatewayPacketS2C::Relayed { msg } => {
info!("msg: {:?}", msg);
}
}
}
OwnedMessage::Close(_) => {
info!("closing connection");
return Ok(());
}
_ => {
debug!("ignoring unknown type");
}
}
}
Err(e) => {
error!("rx error: {}", e);
return Err(e.into());
}
}
}
*/
Ok(())
}
#[derive(Clone, Default)]
pub struct AutoRespawnPlugin;
impl Plugin for AutoRespawnPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, auto_respawn.before(perform_respawn));
}
}
fn auto_respawn(
mut events: EventReader<DeathEvent>,
mut perform_respawn_events: EventWriter<PerformRespawnEvent>,
) {
for event in events.iter() {
perform_respawn_events.send(PerformRespawnEvent {
entity: event.entity,
});
}
}

5
chat_log.log Normal file
View File

@ -0,0 +1,5 @@
{"source":"Discord","username":"@realcore","message":"ello","timestamp":"2023-07-20T01:58:36Z"}
{"source":"Discord","username":"@realcore","message":"ello","timestamp":"2023-07-20T01:58:36Z"}
{"source":"Discord","username":"@realcore","message":"ello","timestamp":"2023-07-20T01:58:36Z"}
{"source":"Discord","username":"@realcore","message":"ello","timestamp":"2023-07-20T01:58:36Z"}
{"source":"Discord","username":"@realcore","message":"ello","timestamp":"2023-07-20T01:58:36Z"}

11
common/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "common"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1", features = ["derive"] }
url = { version = "2.3", features = ["serde"] }
chrono = { version = "0.4", features = ["serde"] }

2
common/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod status;
pub mod message;

View File

@ -0,0 +1,21 @@
[package]
name = "websocket-worker"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4"
simple_logger = "4.1"
serde = { version = "1", features = ["derive"] }
toml = "0.7"
url = { version = "2.3", features = ["serde"] }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11", features = ["json"] }
serde_json = "1"
async-trait = "0.1"
websocket = { version = "0.26", features = ["async"] }
common = { path = "../common" }
serenity = "0.11"
tokio-threadpool = "0.1"

View File

@ -0,0 +1,121 @@
use std::error::Error;
use std::fs;
use log::{debug, error, info};
use serenity::builder::ParseValue;
use serenity::http::Http;
use serenity::model::webhook::Webhook;
use tokio_threadpool::ThreadPool;
use url::Url;
use websocket::{ClientBuilder, Message, OwnedMessage};
use websocket::websocket_base::result::WebSocketResult;
use common::message::{GatewayChatMessage, GatewayPacketC2S, GatewayPacketS2C};
use common::status::{DATA_API_VERSION, Status};
use crate::config::Config;
pub mod config;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
simple_logger::init_with_env().unwrap();
info!("Loading config");
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("usage: ./websocket-worker <config>");
std::process::exit(1);
}
let file = args.nth(1).unwrap();
let config_str = fs::read_to_string(&file)?;
let config: Config = toml::from_str(&config_str)?;
info!("Config loaded from {}", file);
info!("Loading status from the API ({})...", config.api_status_url);
let status: Status = reqwest::get(config.api_status_url).await?.json().await?;
debug!("{:?}", status);
if status.data_api_version != DATA_API_VERSION {
error!("Data API is incompatible. This version of the websocket worker was compiled with Data API v{}, but your Data API is Data API v{}", DATA_API_VERSION, status.data_api_version);
std::process::exit(1);
}
info!("Starting the threadpool");
info!("Connecting to gateway uri {}", status.gateway_url.to_string());
let mut client = ClientBuilder::new(status.gateway_url.as_str())?.connect(None)?;
// send the authentication packet, and then start listen
let message = Message::text(serde_json::to_string(&GatewayPacketC2S::Authenticate {
token: config.token.clone(),
request_write_perms: false, // websocket-worker *only* reads, we do not need write perms
})?);
client.send_message(&message)?;
for msg in client.incoming_messages() {
match msg {
Ok(msg) => {
match msg {
OwnedMessage::Text(txt) => {
// decode
debug!("{}", txt);
let packet: GatewayPacketS2C = serde_json::from_str(&txt)?;
match packet {
GatewayPacketS2C::AuthenticationAccepted { .. } => {
info!("auth accepted by server");
}
GatewayPacketS2C::Disconnect { reason } => {
info!("disconnected by server: {:?}", reason);
return Err("Disconnected by server")?;
}
GatewayPacketS2C::Relayed { msg } => {
info!("msg: {:?}", msg);
for wh in &config.websockets {
tokio::spawn({
let wh_clone = wh.clone();
send_webhook(msg.clone(), wh_clone)
});
}
}
}
}
OwnedMessage::Close(_) => {
info!("closing connection");
return Ok(());
}
_ => {
debug!("ignoring unknown type");
}
}
}
Err(e) => {
error!("rx error: {}", e);
return Err(e.into());
}
}
}
Ok(())
}
async fn send_webhook(msg: GatewayChatMessage, webhook: String) {
let http = Http::new("");
let wh = Webhook::from_url(&http, &webhook).await.unwrap();
wh.execute(&http, false, |w| {
w.content(msg.message)
.username(&msg.username)
.allowed_mentions(|am| am.parse(ParseValue::Users).parse(ParseValue::Roles))
.avatar_url(&format!("https://mc-heads.net/head/{}", msg.username))
}).await.unwrap();
debug!("send job complete");
}