feat(*) add DNS name support for --remote argument in both Client and

Server
This commit is contained in:
Datong Sun 2021-11-18 04:46:18 -08:00
parent 49cc6a6865
commit 91988520e5
3 changed files with 28 additions and 20 deletions

View File

@ -396,11 +396,10 @@ impl Stack {
} else { } else {
trace!("Cache miss, checking the shared tuples table for connection"); trace!("Cache miss, checking the shared tuples table for connection");
let sender; let sender = {
{
let tuples = shared.tuples.read().unwrap(); let tuples = shared.tuples.read().unwrap();
sender = tuples.get(&tuple).cloned(); tuples.get(&tuple).cloned()
} };
if let Some(c) = sender { if let Some(c) = sender {
trace!("Storing connection information into local tuples"); trace!("Storing connection information into local tuples");

View File

@ -6,7 +6,7 @@ use fake_tcp::{Socket, Stack};
use log::{debug, error, info}; use log::{debug, error, info};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto; use std::convert::TryInto;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddr};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
@ -57,8 +57,8 @@ async fn main() {
.short("r") .short("r")
.long("remote") .long("remote")
.required(true) .required(true)
.value_name("IP:PORT") .value_name("IP or HOST NAME:PORT")
.help("Sets the address and port where Phantun Client connects to Phantun Server") .help("Sets the address or host name and port where Phantun Client connects to Phantun Server")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
@ -97,11 +97,18 @@ async fn main() {
.unwrap() .unwrap()
.parse() .parse()
.expect("bad local address"); .expect("bad local address");
let remote_addr: SocketAddrV4 = matches
.value_of("remote") let remote_addr = tokio::net::lookup_host(matches.value_of("remote").unwrap())
.unwrap() .await
.parse() .expect("bad remote address or host")
.expect("bad remote address"); .next()
.expect("unable to resolve remote host name");
let remote_addr = if let SocketAddr::V4(addr) = remote_addr {
addr
} else {
panic!("only IPv4 remote address is supported");
};
let tun_local: Ipv4Addr = matches let tun_local: Ipv4Addr = matches
.value_of("tun_local") .value_of("tun_local")
.unwrap() .unwrap()

View File

@ -4,7 +4,7 @@ use clap::{crate_version, App, Arg};
use fake_tcp::packet::MAX_PACKET_LEN; use fake_tcp::packet::MAX_PACKET_LEN;
use fake_tcp::Stack; use fake_tcp::Stack;
use log::{error, info}; use log::{error, info};
use std::net::{Ipv4Addr, SocketAddr}; use std::net::Ipv4Addr;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
use tokio::time::{self, Duration}; use tokio::time::{self, Duration};
use tokio_tun::TunBuilder; use tokio_tun::TunBuilder;
@ -31,8 +31,8 @@ async fn main() {
.short("r") .short("r")
.long("remote") .long("remote")
.required(true) .required(true)
.value_name("IP:PORT") .value_name("IP or HOST NAME:PORT")
.help("Sets the address and port where Phantun Server forwards UDP packets to, IPv6 address need to be specified as: \"[IPv6]:PORT\"") .help("Sets the address or host name and port where Phantun Server forwards UDP packets to, IPv6 address need to be specified as: \"[IPv6]:PORT\"")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
@ -71,11 +71,13 @@ async fn main() {
.unwrap() .unwrap()
.parse() .parse()
.expect("bad local port"); .expect("bad local port");
let remote_addr: SocketAddr = matches
.value_of("remote") let remote_addr = tokio::net::lookup_host(matches.value_of("remote").unwrap())
.unwrap() .await
.parse() .expect("bad remote address or host")
.expect("bad remote address"); .next()
.expect("unable to resolve remote host name");
let tun_local: Ipv4Addr = matches let tun_local: Ipv4Addr = matches
.value_of("tun_local") .value_of("tun_local")
.unwrap() .unwrap()