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

Add bio support to decouple the ssh client from std::net::TcpStream #29

Merged
merged 6 commits into from Nov 4, 2022

Conversation

HsuJv
Copy link
Collaborator

@HsuJv HsuJv commented Nov 4, 2022

The main commit is the first one
It adds a generic type to the ssh client, decoupling the structure from TcpStream to IO: Read + Write.
This allows the user to connect to the ssh server using different methods (e.g. WebSocket or some other proxies).
POC at here which works fine.

In order not to break the compatibility of the existing APIs, a new API connect_bio has been added which asks the user to provide its buffered input/output objects. Whereas the type-specific bindings for TcpStream were always established in the old connect API.

impl Session<TcpStream> {
    pub fn connect<A>(&mut self, addr: A) -> SshResult<()>
    where
        A: ToSocketAddrs,
    {
        if self.user_info.is_none() {
            return Err(SshError::from("user info is none."));
        }
        // 建立通道
        let tcp = TcpStream::connect(addr)?;
        // default nonblocking
        tcp.set_nonblocking(true).unwrap();

        log::info!("session opened.");
        self.connect_bio(tcp)
    }
}


impl<IO> Session<IO>
where
    IO: Read + Write,
{
    pub fn connect_bio(&mut self, stream: IO) -> SshResult<()> {
        if self.user_info.is_none() {
            return Err(SshError::from("user info is none."));
        }
        // 建立通道
        self.client = Some(Client::<IO>::connect(
            stream,
            self.timeout_sec,
            self.user_info.clone().unwrap(),
        )?);
        log::info!("session opened.");
        self.post_connect()
    }
}

All the old examples work fine.
Add a new example to show how bio works.

@1148118271 1148118271 merged commit 57fc54b into 1148118271:main Nov 4, 2022
@1148118271
Copy link
Owner

I'll test out the old examples locally and have a new 0.2.2 release soon.

@HsuJv HsuJv deleted the generic_world branch November 5, 2022 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants