Skip to content

Commit 8453189

Browse files
committed
port verification
1 parent bb38182 commit 8453189

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

shinkai-bin/shinkai-node/src/runner.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use shinkai_message_primitives::shinkai_utils::signatures::{
1919
use std::collections::HashMap;
2020
use std::error::Error as StdError;
2121
use std::fmt;
22+
use std::net::TcpListener;
2223
use std::path::Path;
2324
use std::sync::{Arc, Weak};
2425
use std::{env, fs};
@@ -49,6 +50,14 @@ impl From<Box<dyn StdError + Send + Sync>> for NodeRunnerError {
4950
}
5051
}
5152

53+
/// Checks if a port is available for binding
54+
fn port_is_available(port: u16) -> bool {
55+
match TcpListener::bind(("127.0.0.1", port)) {
56+
Ok(_) => true,
57+
Err(_) => false,
58+
}
59+
}
60+
5261
pub async fn initialize_node() -> Result<
5362
(Sender<NodeCommand>, JoinHandle<()>, JoinHandle<()>, Weak<Mutex<Node>>),
5463
Box<dyn std::error::Error + Send + Sync>,
@@ -61,6 +70,42 @@ pub async fn initialize_node() -> Result<
6170
let args = parse_args();
6271
let node_env = fetch_node_environment();
6372

73+
// Check if required ports are available
74+
let api_port = node_env.api_listen_address.port();
75+
let node_port = node_env.listen_address.port();
76+
let ws_port = node_env.ws_address.map(|addr| addr.port());
77+
let https_port = node_env.api_https_listen_address.port();
78+
79+
if !port_is_available(api_port) {
80+
return Err(Box::new(std::io::Error::new(
81+
std::io::ErrorKind::AddrInUse,
82+
format!("API port {} is already in use", api_port),
83+
)));
84+
}
85+
86+
if !port_is_available(node_port) {
87+
return Err(Box::new(std::io::Error::new(
88+
std::io::ErrorKind::AddrInUse,
89+
format!("Node port {} is already in use", node_port),
90+
)));
91+
}
92+
93+
if let Some(port) = ws_port {
94+
if !port_is_available(port) {
95+
return Err(Box::new(std::io::Error::new(
96+
std::io::ErrorKind::AddrInUse,
97+
format!("WebSocket port {} is already in use", port),
98+
)));
99+
}
100+
}
101+
102+
if !port_is_available(https_port) {
103+
return Err(Box::new(std::io::Error::new(
104+
std::io::ErrorKind::AddrInUse,
105+
format!("HTTPS port {} is already in use", https_port),
106+
)));
107+
}
108+
64109
// TODO:
65110
// Read file encryption key from ENV variable and decrypt the secrets file
66111
// Store in memory this file encryption key, which is used to encrypt / decrypt other information

0 commit comments

Comments
 (0)