-
Notifications
You must be signed in to change notification settings - Fork 285
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
Accept hostnames for tcp:, tcp-listen:, udp:, udp-listen: #56
Open
andersk
wants to merge
1
commit into
vi:master
Choose a base branch
from
andersk:net-hostnames
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
extern crate net2; | ||
|
||
use futures; | ||
use futures::future::Future; | ||
use futures::future::{Future, IntoFuture}; | ||
use futures::stream::Stream; | ||
use futures::unsync::oneshot::{channel, Receiver, Sender}; | ||
use std; | ||
use std::io::Result as IoResult; | ||
use std::io::{Read, Write}; | ||
use std::net::SocketAddr; | ||
use std::net::{SocketAddr, ToSocketAddrs}; | ||
use tokio_io::{AsyncRead, AsyncWrite}; | ||
|
||
use std::cell::RefCell; | ||
|
@@ -17,11 +17,11 @@ use tokio_tcp::{TcpListener, TcpStream}; | |
use tokio_udp::UdpSocket; | ||
|
||
use super::L2rUser; | ||
use super::{box_up_err, peer_err_s, wouldblock, BoxedNewPeerFuture, BoxedNewPeerStream, Peer}; | ||
use super::{box_up_err, wouldblock, BoxedNewPeerFuture, BoxedNewPeerStream, Peer}; | ||
use super::{multi, once, ConstructParams, Options, PeerConstructor, Specifier}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct TcpConnect(pub SocketAddr); | ||
pub struct TcpConnect(pub String); | ||
impl Specifier for TcpConnect { | ||
fn construct(&self, _: ConstructParams) -> PeerConstructor { | ||
once(tcp_connect_peer(&self.0)) | ||
|
@@ -50,7 +50,7 @@ Example: redirect websocket connections to local SSH server over IPv6 | |
); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct TcpListen(pub SocketAddr); | ||
pub struct TcpListen(pub String); | ||
impl Specifier for TcpListen { | ||
fn construct(&self, p: ConstructParams) -> PeerConstructor { | ||
multi(tcp_listen_peer(&self.0, p.left_to_right)) | ||
|
@@ -79,10 +79,10 @@ Example: redirect TCP to a websocket | |
); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct UdpConnect(pub SocketAddr); | ||
pub struct UdpConnect(pub String); | ||
impl Specifier for UdpConnect { | ||
fn construct(&self, p: ConstructParams) -> PeerConstructor { | ||
once(udp_connect_peer(&self.0, &p.program_options)) | ||
once(udp_connect_peer(&self.0, p.program_options)) | ||
} | ||
specifier_boilerplate!(noglobalstate singleconnect no_subspec ); | ||
} | ||
|
@@ -100,10 +100,10 @@ Send and receive packets to specified UDP socket, from random UDP port | |
); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct UdpListen(pub SocketAddr); | ||
pub struct UdpListen(pub String); | ||
impl Specifier for UdpListen { | ||
fn construct(&self, p: ConstructParams) -> PeerConstructor { | ||
once(udp_listen_peer(&self.0, &p.program_options)) | ||
once(udp_listen_peer(&self.0, p.program_options)) | ||
} | ||
specifier_boilerplate!(noglobalstate singleconnect no_subspec ); | ||
} | ||
|
@@ -192,25 +192,39 @@ impl Drop for MyTcpStream { | |
} | ||
} | ||
|
||
pub fn tcp_connect_peer(addr: &SocketAddr) -> BoxedNewPeerFuture { | ||
Box::new( | ||
pub fn resolve_addr( | ||
addr_str: &str, | ||
) -> impl Future<Item = SocketAddr, Error = Box<dyn std::error::Error + 'static>> { | ||
// TODO: resolve asynchronously | ||
addr_str | ||
.to_socket_addrs() | ||
.and_then(|mut addrs| { | ||
addrs | ||
.next() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No happy eyeballs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be a welcome extension. |
||
.ok_or_else(|| ::simple_err(format!("No address for {}", addr_str))) | ||
}) | ||
.map_err(box_up_err) | ||
.into_future() | ||
} | ||
|
||
pub fn tcp_connect_peer(addr_str: &str) -> BoxedNewPeerFuture { | ||
Box::new(resolve_addr(addr_str).and_then(|addr| { | ||
TcpStream::connect(&addr) | ||
.map(|x| { | ||
info!("Connected to TCP"); | ||
let x = Rc::new(x); | ||
Peer::new(MyTcpStream(x.clone(), true), MyTcpStream(x.clone(), false)) | ||
}) | ||
.map_err(box_up_err), | ||
) as BoxedNewPeerFuture | ||
.map_err(box_up_err) | ||
})) as BoxedNewPeerFuture | ||
} | ||
|
||
pub fn tcp_listen_peer(addr: &SocketAddr, l2r: L2rUser) -> BoxedNewPeerStream { | ||
let bound = match TcpListener::bind(&addr) { | ||
Ok(x) => x, | ||
Err(e) => return peer_err_s(e), | ||
}; | ||
pub fn tcp_listen_peer(addr_str: &str, l2r: L2rUser) -> BoxedNewPeerStream { | ||
use tk_listen::ListenExt; | ||
Box::new( | ||
Box::new(resolve_addr(addr_str).and_then(|addr| { | ||
TcpListener::bind(&addr).map_err(box_up_err) | ||
}).into_stream().map(move |bound| { | ||
let l2r = l2r.clone(); | ||
bound | ||
.incoming() | ||
.sleep_on_error(::std::time::Duration::from_millis(500)) | ||
|
@@ -229,8 +243,8 @@ pub fn tcp_listen_peer(addr: &SocketAddr, l2r: L2rUser) -> BoxedNewPeerStream { | |
let x = Rc::new(x); | ||
Peer::new(MyTcpStream(x.clone(), true), MyTcpStream(x.clone(), false)) | ||
}) | ||
.map_err(|()| ::simple_err2("unreachable error?")), | ||
) as BoxedNewPeerStream | ||
.map_err(|()| ::simple_err2("unreachable error?")) | ||
}).flatten()) as BoxedNewPeerStream | ||
} | ||
|
||
#[derive(Debug)] | ||
|
@@ -323,14 +337,14 @@ pub fn get_udp(addr: &SocketAddr, opts: &Rc<Options>) -> IoResult<UdpSocket> { | |
UdpSocket::from_std(u, &tokio_reactor::Handle::default()) | ||
} | ||
|
||
pub fn udp_connect_peer(addr: &SocketAddr, opts: &Rc<Options>) -> BoxedNewPeerFuture { | ||
let za = get_zero_address(addr); | ||
pub fn udp_connect_peer(addr_str: &str, opts: Rc<Options>) -> BoxedNewPeerFuture { | ||
Box::new(resolve_addr(addr_str).and_then(move |addr| { | ||
let za = get_zero_address(&addr); | ||
|
||
Box::new(futures::future::result( | ||
get_udp(&za, opts) | ||
get_udp(&za, &opts) | ||
.and_then(|x| { | ||
x.connect(addr)?; | ||
apply_udp_options(&x, opts)?; | ||
x.connect(&addr)?; | ||
apply_udp_options(&x, &opts)?; | ||
|
||
let h1 = UdpPeerHandle(Rc::new(RefCell::new(UdpPeer { | ||
s: x, | ||
|
@@ -340,15 +354,15 @@ pub fn udp_connect_peer(addr: &SocketAddr, opts: &Rc<Options>) -> BoxedNewPeerFu | |
let h2 = h1.clone(); | ||
Ok(Peer::new(h1, h2)) | ||
}) | ||
.map_err(box_up_err), | ||
)) as BoxedNewPeerFuture | ||
.map_err(box_up_err) | ||
})) as BoxedNewPeerFuture | ||
} | ||
|
||
pub fn udp_listen_peer(addr: &SocketAddr, opts: &Rc<Options>) -> BoxedNewPeerFuture { | ||
Box::new(futures::future::result( | ||
get_udp(addr, opts) | ||
pub fn udp_listen_peer(addr_str: &str, opts: Rc<Options>) -> BoxedNewPeerFuture { | ||
Box::new(resolve_addr(addr_str).and_then(move |addr| { | ||
get_udp(&addr, &opts) | ||
.and_then(|x| { | ||
apply_udp_options(&x, opts)?; | ||
apply_udp_options(&x, &opts)?; | ||
let h1 = UdpPeerHandle(Rc::new(RefCell::new(UdpPeer { | ||
s: x, | ||
state: Some(UdpPeerState::WaitingForAddress(channel())), | ||
|
@@ -357,8 +371,8 @@ pub fn udp_listen_peer(addr: &SocketAddr, opts: &Rc<Options>) -> BoxedNewPeerFut | |
let h2 = h1.clone(); | ||
Ok(Peer::new(h1, h2)) | ||
}) | ||
.map_err(box_up_err), | ||
)) as BoxedNewPeerFuture | ||
.map_err(box_up_err) | ||
})) as BoxedNewPeerFuture | ||
} | ||
|
||
impl Read for UdpPeerHandle { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really needed to be resolved at connecting time, not just once at argument parsing time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is: when running websocat as a daemon, it shouldn’t need to be restarted just because a remote DNS name changed. Additionally, one might want to point websocat at a service using DNS load balancing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tardiness in DNS reply may not only delay establishing a new connection, but also suspend all unrelated neighbour connections - websocat is mostly single-threaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, until we get async DNS, that’s a tradeoff. But this design allows the user to make that tradeoff. They can always choose to provide an IP address, or to install a local caching resolver (which will do the job much better than we would—sharing the cache between all apps on the system, respecting TTLs, and so on).