mirror of
				https://github.com/dndx/phantun.git
				synced 2025-10-31 01:45:35 +08:00 
			
		
		
		
	style(phantun) refactor out common functions and constants
This commit is contained in:
		| @@ -2,38 +2,16 @@ use clap::{crate_version, Arg, Command}; | |||||||
| 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 phantun::utils::new_udp_reuseport; | ||||||
| use std::collections::HashMap; | use std::collections::HashMap; | ||||||
| use std::convert::TryInto; |  | ||||||
| use std::net::{Ipv4Addr, SocketAddr}; | use std::net::{Ipv4Addr, SocketAddr}; | ||||||
| use std::sync::Arc; | use std::sync::Arc; | ||||||
| use std::time::Duration; |  | ||||||
| use tokio::net::UdpSocket; |  | ||||||
| use tokio::sync::{Notify, RwLock}; | use tokio::sync::{Notify, RwLock}; | ||||||
| use tokio::time; | use tokio::time; | ||||||
| use tokio_tun::TunBuilder; | use tokio_tun::TunBuilder; | ||||||
| use tokio_util::sync::CancellationToken; | use tokio_util::sync::CancellationToken; | ||||||
|  |  | ||||||
| const UDP_TTL: Duration = Duration::from_secs(180); | use phantun::UDP_TTL; | ||||||
|  |  | ||||||
| fn new_udp_reuseport(addr: SocketAddr) -> UdpSocket { |  | ||||||
|     let udp_sock = socket2::Socket::new( |  | ||||||
|         if addr.is_ipv4() { |  | ||||||
|             socket2::Domain::IPV4 |  | ||||||
|         } else { |  | ||||||
|             socket2::Domain::IPV6 |  | ||||||
|         }, |  | ||||||
|         socket2::Type::DGRAM, |  | ||||||
|         None, |  | ||||||
|     ) |  | ||||||
|     .unwrap(); |  | ||||||
|     udp_sock.set_reuse_port(true).unwrap(); |  | ||||||
|     // from tokio-rs/mio/blob/master/src/sys/unix/net.rs |  | ||||||
|     udp_sock.set_cloexec(true).unwrap(); |  | ||||||
|     udp_sock.set_nonblocking(true).unwrap(); |  | ||||||
|     udp_sock.bind(&socket2::SockAddr::from(addr)).unwrap(); |  | ||||||
|     let udp_sock: std::net::UdpSocket = udp_sock.into(); |  | ||||||
|     udp_sock.try_into().unwrap() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| #[tokio::main] | #[tokio::main] | ||||||
| async fn main() { | async fn main() { | ||||||
|   | |||||||
| @@ -2,34 +2,16 @@ use clap::{crate_version, Arg, Command}; | |||||||
| use fake_tcp::packet::MAX_PACKET_LEN; | use fake_tcp::packet::MAX_PACKET_LEN; | ||||||
| use fake_tcp::Stack; | use fake_tcp::Stack; | ||||||
| use log::{debug, error, info}; | use log::{debug, error, info}; | ||||||
| use std::net::{Ipv4Addr, SocketAddr}; | use phantun::utils::new_udp_reuseport; | ||||||
|  | use std::net::Ipv4Addr; | ||||||
| use std::sync::Arc; | use std::sync::Arc; | ||||||
| use tokio::net::UdpSocket; | use tokio::net::UdpSocket; | ||||||
| use tokio::sync::Notify; | use tokio::sync::Notify; | ||||||
| use tokio::time::{self, Duration}; | use tokio::time; | ||||||
| use tokio_tun::TunBuilder; | use tokio_tun::TunBuilder; | ||||||
| use tokio_util::sync::CancellationToken; | use tokio_util::sync::CancellationToken; | ||||||
| const UDP_TTL: Duration = Duration::from_secs(180); |  | ||||||
|  |  | ||||||
| fn new_udp_reuseport(addr: SocketAddr) -> UdpSocket { | use phantun::UDP_TTL; | ||||||
|     let udp_sock = socket2::Socket::new( |  | ||||||
|         if addr.is_ipv4() { |  | ||||||
|             socket2::Domain::IPV4 |  | ||||||
|         } else { |  | ||||||
|             socket2::Domain::IPV6 |  | ||||||
|         }, |  | ||||||
|         socket2::Type::DGRAM, |  | ||||||
|         None, |  | ||||||
|     ) |  | ||||||
|     .unwrap(); |  | ||||||
|     udp_sock.set_reuse_port(true).unwrap(); |  | ||||||
|     // from tokio-rs/mio/blob/master/src/sys/unix/net.rs |  | ||||||
|     udp_sock.set_cloexec(true).unwrap(); |  | ||||||
|     udp_sock.set_nonblocking(true).unwrap(); |  | ||||||
|     udp_sock.bind(&socket2::SockAddr::from(addr)).unwrap(); |  | ||||||
|     let udp_sock: std::net::UdpSocket = udp_sock.into(); |  | ||||||
|     udp_sock.try_into().unwrap() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| #[tokio::main] | #[tokio::main] | ||||||
| async fn main() { | async fn main() { | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								phantun/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								phantun/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | use std::time::Duration; | ||||||
|  |  | ||||||
|  | pub mod utils; | ||||||
|  |  | ||||||
|  | pub const UDP_TTL: Duration = Duration::from_secs(180); | ||||||
							
								
								
									
										22
									
								
								phantun/src/utils.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								phantun/src/utils.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | use std::net::SocketAddr; | ||||||
|  | use tokio::net::UdpSocket; | ||||||
|  |  | ||||||
|  | pub fn new_udp_reuseport(local_addr: SocketAddr) -> UdpSocket { | ||||||
|  |     let udp_sock = socket2::Socket::new( | ||||||
|  |         if local_addr.is_ipv4() { | ||||||
|  |             socket2::Domain::IPV4 | ||||||
|  |         } else { | ||||||
|  |             socket2::Domain::IPV6 | ||||||
|  |         }, | ||||||
|  |         socket2::Type::DGRAM, | ||||||
|  |         None, | ||||||
|  |     ) | ||||||
|  |     .unwrap(); | ||||||
|  |     udp_sock.set_reuse_port(true).unwrap(); | ||||||
|  |     // from tokio-rs/mio/blob/master/src/sys/unix/net.rs | ||||||
|  |     udp_sock.set_cloexec(true).unwrap(); | ||||||
|  |     udp_sock.set_nonblocking(true).unwrap(); | ||||||
|  |     udp_sock.bind(&socket2::SockAddr::from(local_addr)).unwrap(); | ||||||
|  |     let udp_sock: std::net::UdpSocket = udp_sock.into(); | ||||||
|  |     udp_sock.try_into().unwrap() | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user