net: split pasted sftp:// and ssh:// addresses into the connection fields
Also parses a bracketed IPv6 host without a port. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
b7d46fe2b8
commit
f77ecda1a8
4 changed files with 145 additions and 9 deletions
|
|
@ -130,20 +130,63 @@ impl SftpLocation {
|
|||
}
|
||||
}
|
||||
|
||||
/// `[user@]host[:port]`.
|
||||
fn parse_authority(a: &str) -> Option<(Option<&str>, &str, Option<u16>)> {
|
||||
/// `[user@]host[:port]`, an IPv6 address in brackets (`[::1]:2222`). The host comes back
|
||||
/// without the brackets.
|
||||
pub fn parse_authority(a: &str) -> Option<(Option<&str>, &str, Option<u16>)> {
|
||||
let (user, hostport) = match a.rsplit_once('@') {
|
||||
Some((u, h)) => (Some(u).filter(|u| !u.is_empty()), h),
|
||||
None => (None, a),
|
||||
};
|
||||
let (host, port) = match hostport.rsplit_once(':') {
|
||||
Some((h, p)) if !h.ends_with(']') || h.starts_with('[') => (h, Some(p.parse().ok()?)),
|
||||
_ => (hostport, None),
|
||||
let (host, port) = match hostport.strip_prefix('[') {
|
||||
Some(rest) => {
|
||||
let (host, after) = rest.split_once(']')?;
|
||||
match after {
|
||||
"" => (host, None),
|
||||
_ => (host, Some(after.strip_prefix(':')?)),
|
||||
}
|
||||
}
|
||||
None => match hostport.split_once(':') {
|
||||
Some((h, p)) => (h, Some(p)),
|
||||
None => (hostport, None),
|
||||
},
|
||||
};
|
||||
let port = match port {
|
||||
Some(p) => Some(p.parse().ok().filter(|&p| p > 0)?),
|
||||
None => None,
|
||||
};
|
||||
let host = host.trim_start_matches('[').trim_end_matches(']');
|
||||
(!host.is_empty()).then_some((user, host, port))
|
||||
}
|
||||
|
||||
/// An address typed or pasted into the connection editor, split into its parts.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct Address {
|
||||
pub user: Option<String>,
|
||||
pub host: String,
|
||||
pub port: Option<u16>,
|
||||
/// The folder after the host; `None` when the address ends at the host or `/`.
|
||||
pub folder: Option<String>,
|
||||
}
|
||||
|
||||
/// Splits `sftp://` or `ssh://` `[user@]host[:port][/path]` (scheme in any case, `%XX`
|
||||
/// escapes in the path). `None` for anything else.
|
||||
pub fn split_address(text: &str) -> Option<Address> {
|
||||
let text = text.trim();
|
||||
let (scheme, _) = text.split_once("://")?;
|
||||
if !["sftp", "ssh"].contains(&scheme.to_ascii_lowercase().as_str()) {
|
||||
return None;
|
||||
}
|
||||
let uri = Uri::parse(text)?;
|
||||
let (user, host, port) = parse_authority(uri.authority())?;
|
||||
let folder = String::from_utf8(uri.path().to_vec()).ok()?;
|
||||
let folder = folder.trim_end_matches('/');
|
||||
Some(Address {
|
||||
user: user.map(str::to_owned),
|
||||
host: host.to_owned(),
|
||||
port,
|
||||
folder: (!folder.is_empty()).then(|| folder.to_owned()),
|
||||
})
|
||||
}
|
||||
|
||||
fn remote_path(uri: &Uri) -> Result<String> {
|
||||
String::from_utf8(uri.path().to_vec()).map_err(|_| Error::Unsupported)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,9 +57,14 @@ impl Connection {
|
|||
} else {
|
||||
format!("{}@", self.user)
|
||||
};
|
||||
let host = if self.host.contains(':') {
|
||||
format!("[{}]", self.host)
|
||||
} else {
|
||||
self.host.clone()
|
||||
};
|
||||
match self.port {
|
||||
Some(port) => format!("{user}{}:{port}", self.host),
|
||||
None => format!("{user}{}", self.host),
|
||||
Some(port) => format!("{user}{host}:{port}"),
|
||||
None => format!("{user}{host}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -89,6 +94,23 @@ fn expand(path: &str) -> std::path::PathBuf {
|
|||
}
|
||||
}
|
||||
|
||||
/// A `sftp://` or `ssh://` address in the host field, spread over the host, user, port and
|
||||
/// start folder fields; the parts it lacks keep what the fields had. `None` for a plain host.
|
||||
fn split(mut data: ConnectionData) -> Option<ConnectionData> {
|
||||
let a = sftp::split_address(&data.host)?;
|
||||
data.host = a.host.into();
|
||||
if let Some(user) = a.user {
|
||||
data.user = user.into();
|
||||
}
|
||||
if let Some(port) = a.port {
|
||||
data.port = port.to_string().into();
|
||||
}
|
||||
if let Some(folder) = a.folder {
|
||||
data.folder = folder.into();
|
||||
}
|
||||
Some(data)
|
||||
}
|
||||
|
||||
/// `connections.toml`.
|
||||
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
|
|
@ -340,6 +362,7 @@ pub fn attach(ui: &MainWindow, shell: &Rc<Shell>, sftp: Arc<SftpLocation>) {
|
|||
weak.upgrade()
|
||||
.map_or_else(Default::default, |net| net.save(&data).into())
|
||||
});
|
||||
ui.on_connection_host_edited(|data| split(data.clone()).unwrap_or(data));
|
||||
let weak = Rc::downgrade(&net);
|
||||
ui.on_connection_removed(move || {
|
||||
if let Some(net) = weak.upgrade()
|
||||
|
|
@ -565,6 +588,7 @@ impl Net {
|
|||
|
||||
/// Validates and stores the dialog's connection; returns why it can't, or "".
|
||||
fn save(&self, data: &ConnectionData) -> String {
|
||||
let data = &split(data.clone()).unwrap_or_else(|| data.clone());
|
||||
let host = data.host.trim();
|
||||
if host.is_empty() || host.contains(char::is_whitespace) || host.contains('/') {
|
||||
return "Enter the name or address of the server".into();
|
||||
|
|
|
|||
65
crates/tunal/tests/net_address.rs
Normal file
65
crates/tunal/tests/net_address.rs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
//! Addresses pasted into the connection editor.
|
||||
|
||||
use tunal_net::sftp::{Address, parse_authority, split_address};
|
||||
|
||||
fn address(user: Option<&str>, host: &str, port: Option<u16>, folder: Option<&str>) -> Address {
|
||||
Address {
|
||||
user: user.map(str::to_owned),
|
||||
host: host.to_owned(),
|
||||
port,
|
||||
folder: folder.map(str::to_owned),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn splits_sftp_and_ssh_addresses() {
|
||||
assert_eq!(
|
||||
split_address("sftp://[email protected]:2222/srv/www"),
|
||||
Some(address(
|
||||
Some("me"),
|
||||
"example.org",
|
||||
Some(2222),
|
||||
Some("/srv/www")
|
||||
))
|
||||
);
|
||||
assert_eq!(
|
||||
split_address(" SSH://example.org "),
|
||||
Some(address(None, "example.org", None, None))
|
||||
);
|
||||
assert_eq!(
|
||||
split_address("ssh://box/"),
|
||||
Some(address(None, "box", None, None))
|
||||
);
|
||||
assert_eq!(
|
||||
split_address("sftp://box/My%20Files/"),
|
||||
Some(address(None, "box", None, Some("/My Files")))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ipv6_in_brackets() {
|
||||
assert_eq!(
|
||||
split_address("sftp://root@[fe80::1]:22/etc"),
|
||||
Some(address(Some("root"), "fe80::1", Some(22), Some("/etc")))
|
||||
);
|
||||
assert_eq!(
|
||||
split_address("ssh://[::1]"),
|
||||
Some(address(None, "::1", None, None))
|
||||
);
|
||||
assert_eq!(parse_authority("[::1]"), Some((None, "::1", None)));
|
||||
assert_eq!(
|
||||
parse_authority("u@[::1]:2200"),
|
||||
Some((Some("u"), "::1", Some(2200)))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_other_text() {
|
||||
assert_eq!(split_address("example.org"), None);
|
||||
assert_eq!(split_address("ftp://example.org"), None);
|
||||
assert_eq!(split_address("sftp://"), None);
|
||||
assert_eq!(split_address("sftp://host:port"), None);
|
||||
assert_eq!(split_address("sftp://host:0"), None);
|
||||
assert_eq!(split_address("sftp://[::1"), None);
|
||||
assert_eq!(split_address("sftp://[::1]x"), None);
|
||||
}
|
||||
|
|
@ -26,6 +26,9 @@ export component ConnectionDialog inherits Dialog {
|
|||
in-out property <ConnectionData> data;
|
||||
callback saved(ConnectionData) -> string;
|
||||
callback removed();
|
||||
// The host changed: returns the data with a pasted `sftp://` or `ssh://` address split
|
||||
// into the host, user, port and folder fields, or the data as it was.
|
||||
callback host-edited(ConnectionData) -> ConnectionData;
|
||||
|
||||
glyph: Icons.cloud;
|
||||
title: root.data.editing ? "Edit connection" : "New connection";
|
||||
|
|
@ -67,11 +70,12 @@ export component ConnectionDialog inherits Dialog {
|
|||
TextField {
|
||||
horizontal-stretch: 1;
|
||||
label: "Host";
|
||||
placeholder: "name or address";
|
||||
placeholder: "name, address or sftp:// address";
|
||||
error: root.data.error;
|
||||
text <=> root.data.host;
|
||||
edited => {
|
||||
root.data.error = "";
|
||||
root.data = root.host-edited(root.data);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue