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

feat: add unsafe mode flag #7

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ The Relay comes with pre-set TLS certificates configured during build time, so y
The default port for the Middleware Endpoint is 3001, but you can change this by setting the downlink_port in your (configuration file)[#configuration-file-and-environment-variables].
Repl
3. **Local Testing**:
For local testing, you can use tools like ngrok or tailscale to expose your local server to the internet securely.
For local testing, you can use tools like ngrok or tailscale to expose your local server to the internet securely. Ensure to run the relay with the `--unsafe-mode` flag.

```sh
tagoio-relay start --unsafe-mode
```

**Using Ngrok:**
```bash
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ enum Commands {
/// Path to the configuration file
#[arg(short, long)]
config_path: Option<String>,

/// Unsafe mode: disable SSL verification
#[arg(long)]
unsafe_mode: bool,
},
}

Expand Down Expand Up @@ -97,6 +101,7 @@ async fn main() {
Commands::Start {
verbose: _,
config_path,
unsafe_mode,
} => {
let config = utils::fetch_config_file(config_path.clone());
if let Some(config) = config {
Expand All @@ -106,7 +111,7 @@ async fn main() {
std::process::exit(1);
}

if let Err(e) = relay::start_relay().await {
if let Err(e) = relay::start_relay(*unsafe_mode).await {
log::error!("Error starting relay: {}", e);
}
}
Expand Down
30 changes: 17 additions & 13 deletions src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const HOST_ADDRESS: &str = "127.0.0.1";
#[cfg(not(debug_assertions))]
const HOST_ADDRESS: &str = "::"; // ? External IPv4/IPv6 support

fn create_ssl_acceptor() -> Result<Arc<SslAcceptor>, openssl::error::ErrorStack> {
fn create_ssl_acceptor(unsafe_mode: bool) -> Result<Arc<SslAcceptor>, openssl::error::ErrorStack> {
// Certificates contents are stored in the environment variables
let cert = dotenv!("CARGO_SERVER_SSL_CERT").as_bytes();
let key = dotenv!("CARGO_SERVER_SSL_KEY").as_bytes();
Expand All @@ -53,28 +53,32 @@ fn create_ssl_acceptor() -> Result<Arc<SslAcceptor>, openssl::error::ErrorStack>
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;
acceptor.set_private_key(&key)?;
acceptor.set_certificate(&cert)?;
// acceptor.add_client_ca(&ca)?;
acceptor.check_private_key()?;

// Create a new X509Store and add the CA certificate to it
let mut store_builder = X509StoreBuilder::new()?;
store_builder.add_cert(ca.clone())?;
let store = store_builder.build();
if !unsafe_mode {
// Create a new X509Store and add the CA certificate to it
let mut store_builder = X509StoreBuilder::new()?;
store_builder.add_cert(ca.clone())?;
let store = store_builder.build();

// Set the CA store for the acceptor
acceptor.set_cert_store(store);
// Set the CA store for the acceptor
acceptor.set_cert_store(store);

// Add the CA certificate as a client CA
acceptor.add_client_ca(&ca)?;
// Add the CA certificate as a client CA
acceptor.add_client_ca(&ca)?;

acceptor.set_verify(SslVerifyMode::PEER | SslVerifyMode::FAIL_IF_NO_PEER_CERT);
acceptor.set_verify(SslVerifyMode::PEER | SslVerifyMode::FAIL_IF_NO_PEER_CERT);
} else {
log::warn!(target: "security", "Running in unsafe mode: SSL Certificates verification disabled");
acceptor.set_verify(SslVerifyMode::NONE);
}
Ok(Arc::new(acceptor.build()))
}

/**
* Start the MQTT Relay service
*/
pub async fn start_relay() -> Result<()> {
pub async fn start_relay(unsafe_mode: bool) -> Result<()> {
// Simulate fetching relay configurations
let relay_list = get_relay_list().await?;
let relay_list = Arc::new(RwLock::new(relay_list));
Expand All @@ -99,7 +103,7 @@ pub async fn start_relay() -> Result<()> {
config_file.as_ref().unwrap().downlink_port.unwrap_or(3000)
};

let test = create_ssl_acceptor().unwrap();
let test = create_ssl_acceptor(unsafe_mode).unwrap();
let acceptor = OpenSSLConfig::from_acceptor(test);

// let listener = match tokio::net::TcpListener::bind(format!("{}:{}", HOST_ADDRESS, api_port)).await {
Expand Down