mirror of
				https://github.com/dndx/phantun.git
				synced 2025-10-31 09:55:35 +08:00 
			
		
		
		
	feat(phantun) make Tun interface name and address configurable. Improved
documentations of Clap CLI help Closes #8
This commit is contained in:
		| @@ -1,12 +1,12 @@ | |||||||
| extern crate dndx_fork_tokio_tun as tokio_tun; | extern crate dndx_fork_tokio_tun as tokio_tun; | ||||||
|  |  | ||||||
| use clap::{App, Arg}; | use clap::{crate_version, App, Arg}; | ||||||
| use fake_tcp::packet::MAX_PACKET_LEN; | use fake_tcp::packet::MAX_PACKET_LEN; | ||||||
| use fake_tcp::{Socket, Stack}; | 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::{SocketAddr, SocketAddrV4}; | use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; | ||||||
| use std::sync::Arc; | use std::sync::Arc; | ||||||
| use std::time::Duration; | use std::time::Duration; | ||||||
| use tokio::net::UdpSocket; | use tokio::net::UdpSocket; | ||||||
| @@ -32,15 +32,15 @@ async fn main() { | |||||||
|     pretty_env_logger::init(); |     pretty_env_logger::init(); | ||||||
|  |  | ||||||
|     let matches = App::new("Phantun Client") |     let matches = App::new("Phantun Client") | ||||||
|         .version("1.0") |         .version(crate_version!()) | ||||||
|         .author("dndx@GitHub") |         .author("Datong Sun (github.com/dndx)") | ||||||
|         .arg( |         .arg( | ||||||
|             Arg::with_name("local") |             Arg::with_name("local") | ||||||
|                 .short("l") |                 .short("l") | ||||||
|                 .long("local") |                 .long("local") | ||||||
|                 .required(true) |                 .required(true) | ||||||
|                 .value_name("IP:PORT") |                 .value_name("IP:PORT") | ||||||
|                 .help("Sets the listening socket address") |                 .help("Sets the IP and port where Phantun Client listens for incoming UDP datagrams") | ||||||
|                 .takes_value(true), |                 .takes_value(true), | ||||||
|         ) |         ) | ||||||
|         .arg( |         .arg( | ||||||
| @@ -49,7 +49,36 @@ async fn main() { | |||||||
|                 .long("remote") |                 .long("remote") | ||||||
|                 .required(true) |                 .required(true) | ||||||
|                 .value_name("IP:PORT") |                 .value_name("IP:PORT") | ||||||
|                 .help("Sets the connecting socket address") |                 .help("Sets the address and port where Phantun Client connects to Phantun Server") | ||||||
|  |                 .takes_value(true), | ||||||
|  |         ) | ||||||
|  |         .arg( | ||||||
|  |             Arg::with_name("tun") | ||||||
|  |                 .long("tun") | ||||||
|  |                 .required(false) | ||||||
|  |                 .value_name("tunX") | ||||||
|  |                 .help("Sets the Tun interface name, if absent, pick the next available name") | ||||||
|  |                 .default_value("") | ||||||
|  |                 .takes_value(true), | ||||||
|  |         ) | ||||||
|  |         .arg( | ||||||
|  |             Arg::with_name("tun_local") | ||||||
|  |                 .long("tun-local") | ||||||
|  |                 .required(false) | ||||||
|  |                 .value_name("IP") | ||||||
|  |                 .help("Sets the Tun interface local address (O/S's end)") | ||||||
|  |                 .default_value("192.168.200.1") | ||||||
|  |                 .takes_value(true), | ||||||
|  |         ) | ||||||
|  |         .arg( | ||||||
|  |             Arg::with_name("tun_peer") | ||||||
|  |                 .long("tun-peer") | ||||||
|  |                 .required(false) | ||||||
|  |                 .value_name("IP") | ||||||
|  |                 .help("Sets the Tun interface destination (peer) address (Phantun Client's end). \ | ||||||
|  |                        You will need to setup SNAT/MASQUERADE rules on your Internet facing interface \ | ||||||
|  |                        in order for Phantun Client to connect to Phantun Server") | ||||||
|  |                 .default_value("192.168.200.2") | ||||||
|                 .takes_value(true), |                 .takes_value(true), | ||||||
|         ) |         ) | ||||||
|         .get_matches(); |         .get_matches(); | ||||||
| @@ -64,14 +93,24 @@ async fn main() { | |||||||
|         .unwrap() |         .unwrap() | ||||||
|         .parse() |         .parse() | ||||||
|         .expect("bad remote address"); |         .expect("bad remote address"); | ||||||
|  |     let tun_local: Ipv4Addr = matches | ||||||
|  |         .value_of("tun_local") | ||||||
|  |         .unwrap() | ||||||
|  |         .parse() | ||||||
|  |         .expect("bad local address for Tun interface"); | ||||||
|  |     let tun_peer: Ipv4Addr = matches | ||||||
|  |         .value_of("tun_peer") | ||||||
|  |         .unwrap() | ||||||
|  |         .parse() | ||||||
|  |         .expect("bad peer address for Tun interface"); | ||||||
|  |  | ||||||
|     let tun = TunBuilder::new() |     let tun = TunBuilder::new() | ||||||
|         .name("") // if name is empty, then it is set by kernel. |         .name(matches.value_of("tun").unwrap()) // if name is empty, then it is set by kernel. | ||||||
|         .tap(false) // false (default): TUN, true: TAP. |         .tap(false) // false (default): TUN, true: TAP. | ||||||
|         .packet_info(false) // false: IFF_NO_PI, default is true. |         .packet_info(false) // false: IFF_NO_PI, default is true. | ||||||
|         .up() // or set it up manually using `sudo ip link set <tun-name> up`. |         .up() // or set it up manually using `sudo ip link set <tun-name> up`. | ||||||
|         .address("192.168.200.1".parse().unwrap()) |         .address(tun_local) | ||||||
|         .destination("192.168.200.2".parse().unwrap()) |         .destination(tun_peer) | ||||||
|         .try_build_mq(num_cpus::get()) |         .try_build_mq(num_cpus::get()) | ||||||
|         .unwrap(); |         .unwrap(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,10 +1,10 @@ | |||||||
| extern crate dndx_fork_tokio_tun as tokio_tun; | extern crate dndx_fork_tokio_tun as tokio_tun; | ||||||
|  |  | ||||||
| use clap::{App, Arg}; | 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::SocketAddrV4; | use std::net::{Ipv4Addr, SocketAddrV4}; | ||||||
| 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; | ||||||
| @@ -15,15 +15,15 @@ async fn main() { | |||||||
|     pretty_env_logger::init(); |     pretty_env_logger::init(); | ||||||
|  |  | ||||||
|     let matches = App::new("Phantun Server") |     let matches = App::new("Phantun Server") | ||||||
|         .version("1.0") |         .version(crate_version!()) | ||||||
|         .author("dndx@GitHub") |         .author("Datong Sun (github.com/dndx)") | ||||||
|         .arg( |         .arg( | ||||||
|             Arg::with_name("local") |             Arg::with_name("local") | ||||||
|                 .short("l") |                 .short("l") | ||||||
|                 .long("local") |                 .long("local") | ||||||
|                 .required(true) |                 .required(true) | ||||||
|                 .value_name("PORT") |                 .value_name("PORT") | ||||||
|                 .help("Sets the listening port") |                 .help("Sets the port where Phantun Server listens for incoming Phantun Client TCP connections") | ||||||
|                 .takes_value(true), |                 .takes_value(true), | ||||||
|         ) |         ) | ||||||
|         .arg( |         .arg( | ||||||
| @@ -32,7 +32,36 @@ async fn main() { | |||||||
|                 .long("remote") |                 .long("remote") | ||||||
|                 .required(true) |                 .required(true) | ||||||
|                 .value_name("IP:PORT") |                 .value_name("IP:PORT") | ||||||
|                 .help("Sets the connecting socket address") |                 .help("Sets the address and port where Phantun Server forwards UDP packets to") | ||||||
|  |                 .takes_value(true), | ||||||
|  |         ) | ||||||
|  |         .arg( | ||||||
|  |             Arg::with_name("tun") | ||||||
|  |                 .long("tun") | ||||||
|  |                 .required(false) | ||||||
|  |                 .value_name("tunX") | ||||||
|  |                 .help("Sets the Tun interface name, if absent, pick the next available name") | ||||||
|  |                 .default_value("") | ||||||
|  |                 .takes_value(true), | ||||||
|  |         ) | ||||||
|  |         .arg( | ||||||
|  |             Arg::with_name("tun_local") | ||||||
|  |                 .long("tun-local") | ||||||
|  |                 .required(false) | ||||||
|  |                 .value_name("IP") | ||||||
|  |                 .help("Sets the Tun interface local address (O/S's end)") | ||||||
|  |                 .default_value("192.168.201.1") | ||||||
|  |                 .takes_value(true), | ||||||
|  |         ) | ||||||
|  |         .arg( | ||||||
|  |             Arg::with_name("tun_peer") | ||||||
|  |                 .long("tun-peer") | ||||||
|  |                 .required(false) | ||||||
|  |                 .value_name("IP") | ||||||
|  |                 .help("Sets the Tun interface destination (peer) address (Phantun Server's end). \ | ||||||
|  |                        You will need to setup DNAT rules to this address in order for Phantun Server \ | ||||||
|  |                        to accept TCP traffic from Phantun Client") | ||||||
|  |                 .default_value("192.168.201.2") | ||||||
|                 .takes_value(true), |                 .takes_value(true), | ||||||
|         ) |         ) | ||||||
|         .get_matches(); |         .get_matches(); | ||||||
| @@ -47,14 +76,24 @@ async fn main() { | |||||||
|         .unwrap() |         .unwrap() | ||||||
|         .parse() |         .parse() | ||||||
|         .expect("bad remote address"); |         .expect("bad remote address"); | ||||||
|  |     let tun_local: Ipv4Addr = matches | ||||||
|  |         .value_of("tun_local") | ||||||
|  |         .unwrap() | ||||||
|  |         .parse() | ||||||
|  |         .expect("bad local address for Tun interface"); | ||||||
|  |     let tun_peer: Ipv4Addr = matches | ||||||
|  |         .value_of("tun_peer") | ||||||
|  |         .unwrap() | ||||||
|  |         .parse() | ||||||
|  |         .expect("bad peer address for Tun interface"); | ||||||
|  |  | ||||||
|     let tun = TunBuilder::new() |     let tun = TunBuilder::new() | ||||||
|         .name("") // if name is empty, then it is set by kernel. |         .name(matches.value_of("tun").unwrap()) // if name is empty, then it is set by kernel. | ||||||
|         .tap(false) // false (default): TUN, true: TAP. |         .tap(false) // false (default): TUN, true: TAP. | ||||||
|         .packet_info(false) // false: IFF_NO_PI, default is true. |         .packet_info(false) // false: IFF_NO_PI, default is true. | ||||||
|         .up() // or set it up manually using `sudo ip link set <tun-name> up`. |         .up() // or set it up manually using `sudo ip link set <tun-name> up`. | ||||||
|         .address("192.168.201.1".parse().unwrap()) |         .address(tun_local) | ||||||
|         .destination("192.168.201.2".parse().unwrap()) |         .destination(tun_peer) | ||||||
|         .try_build_mq(num_cpus::get()) |         .try_build_mq(num_cpus::get()) | ||||||
|         .unwrap(); |         .unwrap(); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user