mirror of
https://github.com/wez/wezterm.git
synced 2024-09-20 03:09:06 +03:00
ssh: split SessionWrap out
This commit is contained in:
parent
65eaa44b0d
commit
75f946c22c
@ -5,6 +5,7 @@ mod filewrap;
|
||||
mod host;
|
||||
mod pty;
|
||||
mod session;
|
||||
mod sessionwrap;
|
||||
mod sftp;
|
||||
|
||||
pub use auth::*;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::session::{
|
||||
ChannelId, ChannelInfo, DescriptorState, SessionRequest, SessionSender, SessionWrap,
|
||||
SignalChannel,
|
||||
ChannelId, ChannelInfo, DescriptorState, SessionRequest, SessionSender, SignalChannel,
|
||||
};
|
||||
use crate::sessionwrap::SessionWrap;
|
||||
use filedescriptor::{socketpair, FileDescriptor};
|
||||
use portable_pty::{ExitStatus, PtySize};
|
||||
use smol::channel::{bounded, Receiver, Sender, TryRecvError};
|
||||
|
@ -1,9 +1,8 @@
|
||||
use crate::config::ConfigMap;
|
||||
use crate::filewrap::FileWrap;
|
||||
use crate::pty::*;
|
||||
use crate::session::{
|
||||
ChannelWrap, Exec, ExecResult, SessionEvent, SessionRequest, SessionWrap, SignalChannel,
|
||||
};
|
||||
use crate::session::{ChannelWrap, Exec, ExecResult, SessionEvent, SessionRequest, SignalChannel};
|
||||
use crate::sessionwrap::SessionWrap;
|
||||
use crate::sftp::{
|
||||
self, File, FileId, FileRequest, Metadata, SftpChannelError, SftpChannelResult, SftpRequest,
|
||||
};
|
||||
|
@ -5,13 +5,9 @@ use crate::host::*;
|
||||
use crate::pty::*;
|
||||
pub(crate) use crate::session::inner::*;
|
||||
use crate::sftp::{Sftp, SftpRequest};
|
||||
use filedescriptor::{
|
||||
socketpair, AsRawSocketDescriptor, FileDescriptor, SocketDescriptor, POLLIN, POLLOUT,
|
||||
};
|
||||
use libssh_rs as libssh;
|
||||
use filedescriptor::{socketpair, FileDescriptor};
|
||||
use portable_pty::PtySize;
|
||||
use smol::channel::{bounded, Receiver, Sender};
|
||||
use ssh2::BlockDirections;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
use std::sync::{Arc, Mutex};
|
||||
@ -74,74 +70,6 @@ pub(crate) struct Exec {
|
||||
pub reply: Sender<ExecResult>,
|
||||
}
|
||||
|
||||
pub(crate) struct Ssh2Session {
|
||||
sess: ssh2::Session,
|
||||
sftp: Option<ssh2::Sftp>,
|
||||
}
|
||||
|
||||
pub(crate) enum SessionWrap {
|
||||
Ssh2(Ssh2Session),
|
||||
LibSsh(libssh::Session),
|
||||
}
|
||||
|
||||
impl SessionWrap {
|
||||
pub fn with_ssh2(sess: ssh2::Session) -> Self {
|
||||
Self::Ssh2(Ssh2Session { sess, sftp: None })
|
||||
}
|
||||
|
||||
pub fn with_libssh(sess: libssh::Session) -> Self {
|
||||
Self::LibSsh(sess)
|
||||
}
|
||||
|
||||
pub fn set_blocking(&mut self, blocking: bool) {
|
||||
match self {
|
||||
Self::Ssh2(sess) => sess.sess.set_blocking(blocking),
|
||||
Self::LibSsh(sess) => sess.set_blocking(blocking),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_poll_flags(&self) -> i16 {
|
||||
match self {
|
||||
Self::Ssh2(sess) => match sess.sess.block_directions() {
|
||||
BlockDirections::None => 0,
|
||||
BlockDirections::Inbound => POLLIN,
|
||||
BlockDirections::Outbound => POLLOUT,
|
||||
BlockDirections::Both => POLLIN | POLLOUT,
|
||||
},
|
||||
Self::LibSsh(sess) => {
|
||||
let (read, write) = sess.get_poll_state();
|
||||
match (read, write) {
|
||||
(false, false) => 0,
|
||||
(true, false) => POLLIN,
|
||||
(false, true) => POLLOUT,
|
||||
(true, true) => POLLIN | POLLOUT,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_socket_descriptor(&self) -> SocketDescriptor {
|
||||
match self {
|
||||
Self::Ssh2(sess) => sess.sess.as_socket_descriptor(),
|
||||
Self::LibSsh(sess) => sess.as_socket_descriptor(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_session(&self) -> anyhow::Result<ChannelWrap> {
|
||||
match self {
|
||||
Self::Ssh2(sess) => {
|
||||
let channel = sess.sess.channel_session()?;
|
||||
Ok(ChannelWrap::Ssh2(channel))
|
||||
}
|
||||
Self::LibSsh(sess) => {
|
||||
let channel = sess.new_channel()?;
|
||||
channel.open_session()?;
|
||||
Ok(ChannelWrap::LibSsh(channel))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Session {
|
||||
tx: SessionSender,
|
||||
|
72
wezterm-ssh/src/sessionwrap.rs
Normal file
72
wezterm-ssh/src/sessionwrap.rs
Normal file
@ -0,0 +1,72 @@
|
||||
use crate::channelwrap::ChannelWrap;
|
||||
use filedescriptor::{AsRawSocketDescriptor, SocketDescriptor, POLLIN, POLLOUT};
|
||||
use libssh_rs as libssh;
|
||||
use ssh2::BlockDirections;
|
||||
|
||||
pub(crate) struct Ssh2Session {
|
||||
pub sess: ssh2::Session,
|
||||
pub sftp: Option<ssh2::Sftp>,
|
||||
}
|
||||
|
||||
pub(crate) enum SessionWrap {
|
||||
Ssh2(Ssh2Session),
|
||||
LibSsh(libssh::Session),
|
||||
}
|
||||
|
||||
impl SessionWrap {
|
||||
pub fn with_ssh2(sess: ssh2::Session) -> Self {
|
||||
Self::Ssh2(Ssh2Session { sess, sftp: None })
|
||||
}
|
||||
|
||||
pub fn with_libssh(sess: libssh::Session) -> Self {
|
||||
Self::LibSsh(sess)
|
||||
}
|
||||
|
||||
pub fn set_blocking(&mut self, blocking: bool) {
|
||||
match self {
|
||||
Self::Ssh2(sess) => sess.sess.set_blocking(blocking),
|
||||
Self::LibSsh(sess) => sess.set_blocking(blocking),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_poll_flags(&self) -> i16 {
|
||||
match self {
|
||||
Self::Ssh2(sess) => match sess.sess.block_directions() {
|
||||
BlockDirections::None => 0,
|
||||
BlockDirections::Inbound => POLLIN,
|
||||
BlockDirections::Outbound => POLLOUT,
|
||||
BlockDirections::Both => POLLIN | POLLOUT,
|
||||
},
|
||||
Self::LibSsh(sess) => {
|
||||
let (read, write) = sess.get_poll_state();
|
||||
match (read, write) {
|
||||
(false, false) => 0,
|
||||
(true, false) => POLLIN,
|
||||
(false, true) => POLLOUT,
|
||||
(true, true) => POLLIN | POLLOUT,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_socket_descriptor(&self) -> SocketDescriptor {
|
||||
match self {
|
||||
Self::Ssh2(sess) => sess.sess.as_socket_descriptor(),
|
||||
Self::LibSsh(sess) => sess.as_socket_descriptor(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_session(&self) -> anyhow::Result<ChannelWrap> {
|
||||
match self {
|
||||
Self::Ssh2(sess) => {
|
||||
let channel = sess.sess.channel_session()?;
|
||||
Ok(ChannelWrap::Ssh2(channel))
|
||||
}
|
||||
Self::LibSsh(sess) => {
|
||||
let channel = sess.new_channel()?;
|
||||
channel.open_session()?;
|
||||
Ok(ChannelWrap::LibSsh(channel))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user