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

13 connection use ssh tunnel #20

Merged
merged 7 commits into from
Oct 30, 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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ tree-sitter-highlight = "0.23"
tree-sitter-json = "0.23"
tree-sitter-html = "0.23"
tree-sitter-ron = { path = "ratisui-tree-sitter-ron" }

russh = { version = "0.45.0", default-features = false }
async-trait = "0.1.83"
# git crates

[build-dependencies]
Expand Down
44 changes: 38 additions & 6 deletions examples/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,51 @@ use redis::ConnectionAddr::Tcp;
use redis::{Cmd, ConnectionInfo, ProtocolVersion, RedisConnectionInfo};

pub fn dead_pool() -> Result<deadpool_redis::Pool> {
build_pool(Config {
host: "redis-16430.c1.asia-northeast1-1.gce.redns.redis-cloud.com".to_string(),
port: 16430,
username: Some(String::from("default")),
password: Some("9JRCAjglNSTc4pXWOggLT7BKljwuoSSy".to_string()),
db: 0,
protocol: ProtocolVersion::RESP3,
})
}

pub fn build_pool(config: Config) -> Result<deadpool_redis::Pool> {
let config = deadpool_redis::Config::from_connection_info(ConnectionInfo {
addr: Tcp("redis-16430.c1.asia-northeast1-1.gce.redns.redis-cloud.com".to_string(), 16430),
addr: Tcp(config.host, config.port),
redis: RedisConnectionInfo {
db: 0,
username: Some(String::from("default")),
password: Some("9JRCAjglNSTc4pXWOggLT7BKljwuoSSy".to_string()),
protocol: ProtocolVersion::RESP3,
db: config.db as i64,
username: config.username,
password: config.password,
protocol: config.protocol,
},
});

config.create_pool(Some(Runtime::Tokio1)).context("Failed to create pool")
}

pub struct Config {
pub host: String,
pub port: u16,
pub username: Option<String>,
pub password: Option<String>,
pub db: u8,
pub protocol: ProtocolVersion,
}

impl Default for Config {
fn default() -> Self {
Self {
host: "127.0.0.1".to_string(),
port: 6379,
username: None,
password: None,
db: 0,
protocol: ProtocolVersion::RESP3,
}
}
}

#[macro_export]
macro_rules! str_cmd {
($cmd:expr) => {{
Expand Down
6 changes: 6 additions & 0 deletions examples/redis_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ async fn main() -> Result<()> {
let pool = dead_pool()?;
let client = &pool.manager().client;

let mut connection = pool.get().await?;
let x: Value = cmd("ping").query_async(&mut connection).await?;
dbg!(x);
let mut connection = pool.get().await?;
let x: Value = cmd("ping").query_async(&mut connection).await?;
dbg!(x);
let mut connection = pool.get().await?;
let x: Value = cmd("ping").query_async(&mut connection).await?;
dbg!(x);
Expand Down
73 changes: 73 additions & 0 deletions examples/ssh_tunnel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#[path = "common/lib.rs"]
mod common;

use crate::common::client::build_pool;
use anyhow::Error;
use anyhow::Result;
use async_trait::async_trait;
use ratisui::ssh_tunnel::SshTunnel;
use redis::cmd;
use russh::client::Handler;
use russh::keys::key;
use std::ops::Deref;

const SSH_HOST: &str = "10.37.1.133";
const SSH_PORT: u16 = 22;
const SSH_USER: &str = "guest";
const SSH_PASSWORD: &str = "123";

const REDIS_HOST: &str = "redis-16430.c1.asia-northeast1-1.gce.redns.redis-cloud.com";
const REDIS_PORT: u16 = 16430;
const REDIS_USER: Some(String) = Some(String::from("default"));
const REDIS_PASSWORD: Some(String) = Some(String::from("9JRCAjglNSTc4pXWOggLT7BKljwuoSSy"));

const LOCAL_HOST: &str = "127.0.0.1";

struct IHandler;

#[async_trait]
impl Handler for IHandler {
type Error = Error;
async fn check_server_key(&mut self, _: &key::PublicKey) -> Result<bool, Self::Error> {
Ok(true)
}
}

#[tokio::main]
async fn main() -> Result<()> {
let mut ssh_tunnel = SshTunnel::new(
SSH_HOST.to_string(),
SSH_PORT,
SSH_USER.to_string(),
SSH_PASSWORD.to_string(),
REDIS_HOST.to_string(),
REDIS_PORT,
);

let addr = ssh_tunnel.open().await?;
println!("{}", addr);

let pool = build_pool(common::client::Config {
host: addr.ip().to_string(),
port: addr.port(),
username: REDIS_USER.deref().clone(),
password: REDIS_PASSWORD.deref().clone(),
..Default::default()
})?;
let mut connection = pool.get().await?;
let pong: String = cmd("PING").query_async(&mut connection).await?;
assert_eq!(pool.status().size, 1);
assert!("PONG".eq_ignore_ascii_case(pong.as_str()));
let mut connection = pool.get().await?;
let pong: String = cmd("PING").query_async(&mut connection).await?;
assert_eq!(pool.status().size, 3);
assert!("PONG".eq_ignore_ascii_case(pong.as_str()));
let mut connection = pool.get().await?;
let pong: String = cmd("PING").query_async(&mut connection).await?;
assert_eq!(pool.status().size, 3);
assert!("PONG".eq_ignore_ascii_case(pong.as_str()));
ssh_tunnel.close().await?;
assert!(!ssh_tunnel.is_connected());

Ok(())
}
Loading