| Crates.io | sal-net |
| lib.rs | sal-net |
| version | 0.1.0 |
| created_at | 2025-06-30 17:35:21.193837+00 |
| updated_at | 2025-06-30 17:35:21.193837+00 |
| description | SAL Network - Network connectivity utilities for TCP, HTTP, and SSH |
| homepage | |
| repository | https://git.threefold.info/herocode/sal |
| max_upload_size | |
| id | 1732122 |
| size | 129,112 |
Network connectivity utilities for TCP, HTTP, and SSH operations.
The sal-net package provides a comprehensive set of network connectivity tools for the SAL (System Abstraction Layer) ecosystem. It includes utilities for TCP port checking, HTTP/HTTPS connectivity testing, and SSH command execution.
use sal_net::TcpConnector;
use std::time::Duration;
// Create a TCP connector
let connector = TcpConnector::new();
// Check if a port is open
let is_open = connector.check_port("127.0.0.1".parse().unwrap(), 80).await?;
// Check multiple ports
let ports = vec![22, 80, 443];
let results = connector.check_ports("example.com".parse().unwrap(), &ports).await?;
// Ping a host
let is_reachable = connector.ping("google.com").await?;
use sal_net::HttpConnector;
// Create an HTTP connector
let connector = HttpConnector::new()?;
// Check if a URL is reachable
let is_reachable = connector.check_url("https://example.com").await?;
// Get status code
let status = connector.check_status("https://example.com").await?;
// Fetch content
let content = connector.get_content("https://api.example.com/data").await?;
// Verify specific status
let matches = connector.verify_status("https://example.com", reqwest::StatusCode::OK).await?;
use sal_net::SshConnectionBuilder;
use std::time::Duration;
// Build an SSH connection
let connection = SshConnectionBuilder::new()
.host("example.com")
.port(22)
.user("username")
.timeout(Duration::from_secs(30))
.build();
// Execute a command
let (exit_code, output) = connection.execute("ls -la").await?;
// Test connectivity
let is_connected = connection.ping().await?;
The package provides Rhai scripting integration for network operations:
// Check if a TCP port is open
let is_open = tcp_check("127.0.0.1", 80);
print(`Port 80 is ${is_open ? "open" : "closed"}`);
// Ping a host (cross-platform)
let can_ping = tcp_ping("google.com");
print(`Can ping Google: ${can_ping}`);
// Check if an HTTP URL is reachable
let is_reachable = http_check("https://example.com");
print(`URL is ${is_reachable ? "reachable" : "unreachable"}`);
// Get HTTP status code
let status = http_status("https://example.com");
print(`HTTP status: ${status}`);
// Execute SSH command and get exit code
let exit_code = ssh_execute("example.com", "user", "ls -la");
print(`SSH command exit code: ${exit_code}`);
// Execute SSH command and get output
let output = ssh_execute_output("example.com", "user", "whoami");
print(`SSH output: ${output}`);
// Test SSH connectivity
let can_connect = ssh_ping("example.com", "user");
print(`SSH connection: ${can_connect ? "success" : "failed"}`);
// Network connectivity test script
print("=== Network Connectivity Test ===");
// Test TCP connectivity
let ports = [22, 80, 443];
for port in ports {
let is_open = tcp_check("example.com", port);
print(`Port ${port}: ${is_open ? "OPEN" : "CLOSED"}`);
}
// Test ping connectivity
let hosts = ["google.com", "github.com", "stackoverflow.com"];
for host in hosts {
let can_ping = tcp_ping(host);
print(`${host}: ${can_ping ? "REACHABLE" : "UNREACHABLE"}`);
}
// Test HTTP connectivity
let urls = ["https://google.com", "https://github.com", "https://httpbin.org/status/200"];
for url in urls {
let is_reachable = http_check(url);
let status = http_status(url);
print(`${url}: ${is_reachable ? "REACHABLE" : "UNREACHABLE"} (Status: ${status})`);
}
// Test SSH connectivity (requires SSH access)
let ssh_hosts = ["example.com"];
for host in ssh_hosts {
let can_connect = ssh_ping(host, "user");
print(`SSH ${host}: ${can_connect ? "CONNECTED" : "FAILED"}`);
}
The package includes comprehensive tests:
# Run all tests
cargo test
# Run specific test suites
cargo test --test tcp_tests
cargo test --test http_tests
cargo test --test ssh_tests
cargo test --test rhai_integration_tests
# Run Rhai script tests
cargo test --test rhai_integration_tests
tokio: Async runtime for network operationsreqwest: HTTP client functionalityanyhow: Error handlingrhai: Scripting integrationAll network operations return Result types with meaningful error messages. Operations gracefully handle: