pub mod context;
use context::CommonContext;
use hightower_client::HightowerConnection;
use tracing::{debug, error};
pub async fn run(context: &CommonContext) -> Result<HightowerConnection, String> {
let gateway_url = context
.kv
.get_bytes(context::GATEWAY_URL_KEY)
.ok()
.flatten()
.and_then(|bytes| String::from_utf8(bytes).ok())
.unwrap_or_else(|| "http://127.0.0.1:8008".to_string());
let auth_token = context
.kv
.get_bytes(context::HT_AUTH_KEY)
.map_err(|e| format!("Failed to get auth token: {:?}", e))?
.ok_or_else(|| "Auth token not found in context".to_string())?;
let auth_token = String::from_utf8(auth_token)
.map_err(|e| format!("Invalid auth token encoding: {:?}", e))?;
debug!(gateway_url = %gateway_url, "Connecting to gateway");
let connection = HightowerConnection::connect(&gateway_url, &auth_token)
.await
.map_err(|e| {
error!(error = ?e, "Failed to connect to gateway");
format!("Failed to connect to gateway: {:?}", e)
})?;
debug!(
node_id = %connection.node_id(),
assigned_ip = %connection.assigned_ip(),
"Connected to gateway"
);
if let Err(e) = connection.ping_gateway().await {
error!(error = ?e, "Failed to ping gateway over WireGuard");
} else {
debug!("Successfully pinged gateway over WireGuard");
}
Ok(connection)
}
pub async fn deregister(connection: HightowerConnection) -> Result<(), String> {
connection
.disconnect()
.await
.map_err(|e| format!("Failed to disconnect: {:?}", e))
}