mirror of
https://github.com/dndx/phantun.git
synced 2025-09-16 04:04:29 +08:00
Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
b40ca10cc1 | ||
|
30f0a1118b | ||
|
fd607bc72a | ||
|
939e4aa94e | ||
|
7bcfada87b | ||
|
fe18a49d40 | ||
|
b707c5bd12 | ||
|
6af7757456 | ||
|
f374ac8081 | ||
|
50346c1ba0 | ||
|
f649c79656 | ||
|
c91bda7e6a | ||
|
00a308a005 | ||
|
9ff691d063 | ||
|
b5e79653f0 | ||
|
f496a7919b | ||
|
bf6b9bc2ff | ||
|
47b9037968 | ||
|
c2341b6662 | ||
|
a3eff42453 | ||
|
87a42a1e23 | ||
|
851750b13d | ||
|
b89b683bb2 | ||
|
838cfa6738 |
74
README.md
74
README.md
@@ -35,7 +35,7 @@ Table of Contents
|
||||
|
||||
# Latest release
|
||||
|
||||
[v0.3.2](https://github.com/dndx/phantun/releases/tag/v0.3.2)
|
||||
[v0.5.0](https://github.com/dndx/phantun/releases/tag/v0.5.0)
|
||||
|
||||
# Overview
|
||||
|
||||
@@ -72,30 +72,32 @@ It is also assumed that **Phantun Client** listens for incoming UDP packets at
|
||||
`127.0.0.1:1234` (the `--local` option for client) and connects to Phantun Server at `10.0.0.1:4567`
|
||||
(the `--remote` option for client).
|
||||
|
||||
Phantun creates TUN interface for both the Client and Server. For Client, Phantun assigns itself the IP address
|
||||
`192.168.200.2` by default and for Server, it assigns `192.168.201.2` by default. Therefore, your Kernel must have
|
||||
`net.ipv4.ip_forward` enabled and setup appropriate iptables rules for NAT between your physical
|
||||
NIC address and Phantun's TUN interface address.
|
||||
Phantun creates TUN interface for both the Client and Server. For **Client**, Phantun assigns itself the IP address
|
||||
`192.168.200.2` and `fcc8::2` by default.
|
||||
For **Server**, it assigns `192.168.201.2` and `fcc9::2` by default. Therefore, your Kernel must have
|
||||
IPv4/IPv6 forwarding enabled and setup appropriate iptables/nftables rules for NAT between your physical
|
||||
NIC address and Phantun's Tun interface address.
|
||||
|
||||
You may customize the name of Tun interface created by Phantun and the assigned addresses. Please
|
||||
run the executable with `-h` options to see how to change them.
|
||||
|
||||
Another way to help understand this network topology (please see the diagram above for an illustration of this topology):
|
||||
|
||||
Phantun Client is like a machine with private IP address (`192.168.200.2`) behind a router.
|
||||
Phantun Client is like a machine with private IP address (`192.168.200.2`/`fcc8::2`) behind a router.
|
||||
In order for it to reach the Internet, you will need to SNAT the private IP address before it's traffic
|
||||
leaves the NIC.
|
||||
|
||||
Phantun Server is like a server with private IP address (`192.168.201.2`) behind a router.
|
||||
Phantun Server is like a server with private IP address (`192.168.201.2`/`fcc9::2`) behind a router.
|
||||
In order to access it from the Internet, you need to `DNAT` it's listening port on the router
|
||||
and change the destination IP address to where the server is listening for incoming connections.
|
||||
|
||||
In those cases, the machine/iptables running Phantun acts as the "router" that allows Phantun
|
||||
to communicate with outside using it's private IP addresses.
|
||||
|
||||
As of Phantun v0.2.2, IPv6 support for UDP endpoints has been added, however Fake TCP IPv6 support
|
||||
has not been finished yet. To specify an IPv6 address, use the following format: `[::1]:1234` with
|
||||
the command line options.
|
||||
As of Phantun v0.4.1, IPv6 is fully supported for both TCP and UDP sides.
|
||||
To specify an IPv6 address, use the following format: `[::1]:1234` with
|
||||
the command line options. Resolving AAAA record is also supported. Please run the program
|
||||
with `-h` to see detailed options on how to control the IPv6 behavior.
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
@@ -103,6 +105,12 @@ the command line options.
|
||||
|
||||
Edit `/etc/sysctl.conf`, add `net.ipv4.ip_forward=1` and run `sudo sysctl -p /etc/sysctl.conf`.
|
||||
|
||||
<details>
|
||||
<summary>IPv6 specific config</summary>
|
||||
|
||||
`net.ipv6.conf.all.forwarding=1` will need to be set as well.
|
||||
</details>
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
## 2. Add required firewall rules
|
||||
@@ -128,12 +136,16 @@ table inet nat {
|
||||
}
|
||||
```
|
||||
|
||||
Note: The above rule uses `inet` as the table family type, so it is compatible with
|
||||
both IPv4 and IPv6 usage.
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
#### Using iptables
|
||||
|
||||
```
|
||||
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
|
||||
ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
|
||||
```
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
@@ -150,10 +162,11 @@ actual TCP port number used by Phantun server
|
||||
#### Using nftables
|
||||
|
||||
```
|
||||
table ip nat {
|
||||
table inet nat {
|
||||
chain prerouting {
|
||||
type nat hook prerouting priority dstnat; policy accept;
|
||||
iif eth0 tcp dport 4567 dnat to 192.168.201.2
|
||||
iif eth0 tcp dport 4567 dnat ip to 192.168.201.2
|
||||
iif eth0 tcp dport 4567 dnat ip6 to fcc9::2
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -164,6 +177,7 @@ table ip nat {
|
||||
|
||||
```
|
||||
iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 4567 -j DNAT --to-destination 192.168.201.2
|
||||
ip6tables -t nat -A PREROUTING -p tcp -i eth0 --dport 4567 -j DNAT --to-destination fcc9::2
|
||||
```
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
@@ -202,6 +216,10 @@ Or use host name with `--remote`:
|
||||
RUST_LOG=info /usr/local/bin/phantun_server --local 4567 --remote example.com:1234
|
||||
```
|
||||
|
||||
Note: Server by default assigns both IPv4 and IPv6 private address to the Tun interface.
|
||||
If you do not wish to use IPv6, you can simply skip creating the IPv6 DNAT rule above and
|
||||
the presence of IPv6 address on the Tun interface should have no side effect to the server.
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
### Client
|
||||
@@ -219,12 +237,22 @@ Or use host name with `--remote`:
|
||||
RUST_LOG=info /usr/local/bin/phantun_client --local 127.0.0.1:1234 --remote example.com:4567
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>IPv6 specific config</summary>
|
||||
|
||||
```
|
||||
RUST_LOG=info /usr/local/bin/phantun_client --local 127.0.0.1:1234 --remote [fdxx::1234]:4567
|
||||
```
|
||||
|
||||
Domain name with AAAA record is also supported.
|
||||
</details>
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
# MTU overhead
|
||||
|
||||
Phantun aims to keep tunneling overhead to the minimum. The overhead compared to a plain UDP packet
|
||||
is the following:
|
||||
is the following (using IPv4 below as an example):
|
||||
|
||||
**Standard UDP packet:** `20 byte IP header + 8 byte UDP header = 28 bytes`
|
||||
|
||||
@@ -248,18 +276,23 @@ For people who use Phantun to tunnel [WireGuard®](https://www.wireguard.com) UD
|
||||
out the correct MTU to use for your WireGuard interface.
|
||||
|
||||
```
|
||||
WireGuard MTU = Interface MTU - IP header (20 bytes) - TCP header (20 bytes) - WireGuard overhead (32 bytes)
|
||||
WireGuard MTU = Interface MTU - IPv4 header (20 bytes) - TCP header (20 bytes) - WireGuard overhead (32 bytes)
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```
|
||||
WireGuard MTU = Interface MTU - IPv6 header (40 bytes) - TCP header (20 bytes) - WireGuard overhead (32 bytes)
|
||||
```
|
||||
|
||||
For example, for a Ethernet interface with 1500 bytes MTU, the WireGuard interface MTU should be set as:
|
||||
|
||||
```
|
||||
1500 - 20 - 20 - 32 = 1428 bytes
|
||||
```
|
||||
IPv4: `1500 - 20 - 20 - 32 = 1428 bytes`
|
||||
IPv6: `1500 - 40 - 20 - 32 = 1408 bytes`
|
||||
|
||||
The resulted Phantun TCP data packet will be 1500 bytes which does not exceed the
|
||||
interface MTU of 1500. Please note it is strongly recommended to use the same interface
|
||||
MTU for both ends of a WireGuard tunnel, or unexected packet loss may occur and these issues are
|
||||
MTU for both ends of a WireGuard tunnel, or unexpected packet loss may occur and these issues are
|
||||
generally very hard to troubleshoot.
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
@@ -296,11 +329,12 @@ Test command: `iperf3 -c <IP> -p <PORT> -R -u -l 1400 -b 1000m -t 30 -P 5`
|
||||
| Phantun (5 streams) | 5.00 Gbits/sec | 2.38 Gbits/sec | 95% (all cores utilized) |
|
||||
| udp2raw (`cipher-mode=none` `auth-mode=none` `disable-anti-replay`) (5 streams) | 5.00 Gbits/sec | 770 Mbits/sec | 50% (2 cores at 100%) |
|
||||
|
||||
Writeup on some of the techniques used in Phantun to achieve this performance result: [Writing Highly Efficient UDP Server in Rust](https://idndx.com/writing-highly-efficient-udp-server-in-rust/).
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
# Future plans
|
||||
|
||||
* IPv6 support for fake-tcp
|
||||
* Load balancing a single UDP stream into multiple TCP streams
|
||||
* Integration tests
|
||||
* Auto insertion/removal of required firewall rules
|
||||
@@ -328,7 +362,7 @@ Here is a quick overview of comparison between those two to help you choose:
|
||||
| Tunneling MTU overhead | 12 bytes | 44 bytes |
|
||||
| Seprate TCP connections for each UDP connection | Client/Server | Server only |
|
||||
| Anti-replay, encryption | ❌ | ✅ |
|
||||
| IPv6 | UDP only | ✅ |
|
||||
| IPv6 | ✅ | ✅ |
|
||||
|
||||
[Back to TOC](#table-of-contents)
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "fake-tcp"
|
||||
version = "0.4.0"
|
||||
version = "0.5.0"
|
||||
edition = "2021"
|
||||
authors = ["Datong Sun <dndx@idndx.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
@@ -16,10 +16,10 @@ benchmark = []
|
||||
|
||||
[dependencies]
|
||||
bytes = "1"
|
||||
pnet = "0.29"
|
||||
pnet = "0.31"
|
||||
tokio = { version = "1.14", features = ["full"] }
|
||||
rand = { version = "0.8", features = ["small_rng"] }
|
||||
log = "0.4"
|
||||
internet-checksum = "0.2"
|
||||
tokio-tun = "0.5"
|
||||
tokio-tun = "0.7"
|
||||
flume = "0.10"
|
||||
|
@@ -436,8 +436,7 @@ impl Stack {
|
||||
let mut tuples: HashMap<AddrTuple, flume::Sender<Bytes>> = HashMap::new();
|
||||
|
||||
loop {
|
||||
let mut buf = BytesMut::with_capacity(MAX_PACKET_LEN);
|
||||
buf.resize(MAX_PACKET_LEN, 0);
|
||||
let mut buf = BytesMut::zeroed(MAX_PACKET_LEN);
|
||||
|
||||
tokio::select! {
|
||||
size = tun.recv(&mut buf) => {
|
||||
|
@@ -47,8 +47,7 @@ pub fn build_tcp_packet(
|
||||
let tcp_header_len = TCP_HEADER_LEN + if wscale { 4 } else { 0 }; // nop + wscale
|
||||
let tcp_total_len = tcp_header_len + payload.map_or(0, |payload| payload.len());
|
||||
let total_len = ip_header_len + tcp_total_len;
|
||||
let mut buf = BytesMut::with_capacity(total_len);
|
||||
buf.resize(total_len, 0);
|
||||
let mut buf = BytesMut::zeroed(total_len);
|
||||
|
||||
let mut ip_buf = buf.split_to(ip_header_len);
|
||||
let mut tcp_buf = buf.split_to(tcp_total_len);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "phantun"
|
||||
version = "0.4.0"
|
||||
version = "0.6.0"
|
||||
edition = "2021"
|
||||
authors = ["Datong Sun <dndx@idndx.com>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
@@ -11,14 +11,14 @@ Transforms UDP stream into (fake) TCP streams that can go through
|
||||
Layer 3 & Layer 4 (NAPT) firewalls/NATs.
|
||||
"""
|
||||
[dependencies]
|
||||
clap = { version = "3.0", features = ["cargo"] }
|
||||
clap = { version = "4.0", features = ["cargo"] }
|
||||
socket2 = { version = "0.4", features = ["all"] }
|
||||
fake-tcp = { path = "../fake-tcp", version = "0.4" }
|
||||
fake-tcp = { path = "../fake-tcp", version = "0.5" }
|
||||
tokio = { version = "1.14", features = ["full"] }
|
||||
tokio-util = "0.7"
|
||||
log = "0.4"
|
||||
pretty_env_logger = "0.4"
|
||||
tokio-tun = "0.5"
|
||||
tokio-tun = "0.7"
|
||||
num_cpus = "1.13"
|
||||
neli = "0.6"
|
||||
nix = "0.23"
|
||||
nix = "0.25"
|
||||
|
@@ -1,9 +1,10 @@
|
||||
use clap::{crate_version, Arg, Command};
|
||||
use clap::{crate_version, Arg, ArgAction, Command};
|
||||
use fake_tcp::packet::MAX_PACKET_LEN;
|
||||
use fake_tcp::{Socket, Stack};
|
||||
use log::{debug, error, info};
|
||||
use phantun::utils::{assign_ipv6_address, new_udp_reuseport};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
@@ -28,7 +29,6 @@ async fn main() -> io::Result<()> {
|
||||
.required(true)
|
||||
.value_name("IP:PORT")
|
||||
.help("Sets the IP and port where Phantun Client listens for incoming UDP datagrams, IPv6 address need to be specified as: \"[IPv6]:PORT\"")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("remote")
|
||||
@@ -37,7 +37,6 @@ async fn main() -> io::Result<()> {
|
||||
.required(true)
|
||||
.value_name("IP or HOST NAME:PORT")
|
||||
.help("Sets the address or host name and port where Phantun Client connects to Phantun Server, IPv6 address need to be specified as: \"[IPv6]:PORT\"")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun")
|
||||
@@ -46,7 +45,6 @@ async fn main() -> io::Result<()> {
|
||||
.value_name("tunX")
|
||||
.help("Sets the Tun interface name, if absent, pick the next available name")
|
||||
.default_value("")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun_local")
|
||||
@@ -55,7 +53,6 @@ async fn main() -> io::Result<()> {
|
||||
.value_name("IP")
|
||||
.help("Sets the Tun interface IPv4 local address (O/S's end)")
|
||||
.default_value("192.168.200.1")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun_peer")
|
||||
@@ -66,15 +63,14 @@ async fn main() -> io::Result<()> {
|
||||
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),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("ipv4_only")
|
||||
.long("ipv4-only")
|
||||
.short('4')
|
||||
.required(false)
|
||||
.help("Do not assign IPv6 addresses to Tun interface")
|
||||
.takes_value(false)
|
||||
.help("Only use IPv4 address when connecting to remote")
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with_all(&["tun_local6", "tun_peer6"]),
|
||||
)
|
||||
.arg(
|
||||
@@ -83,8 +79,7 @@ async fn main() -> io::Result<()> {
|
||||
.required(false)
|
||||
.value_name("IP")
|
||||
.help("Sets the Tun interface IPv6 local address (O/S's end)")
|
||||
.default_value("fec8::1")
|
||||
.takes_value(true),
|
||||
.default_value("fcc8::1")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun_peer6")
|
||||
@@ -94,20 +89,29 @@ async fn main() -> io::Result<()> {
|
||||
.help("Sets the Tun interface IPv6 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("fec8::2")
|
||||
.takes_value(true),
|
||||
.default_value("fcc8::2")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("handshake_packet")
|
||||
.long("handshake-packet")
|
||||
.required(false)
|
||||
.value_name("PATH")
|
||||
.help("Specify a file, which, after TCP handshake, its content will be sent as the \
|
||||
first data packet to the server.\n\
|
||||
Note: ensure this file's size does not exceed the MTU of the outgoing interface. \
|
||||
The content is always sent out in a single packet and will not be further segmented")
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let local_addr: SocketAddr = matches
|
||||
.value_of("local")
|
||||
.get_one::<String>("local")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("bad local address");
|
||||
|
||||
let ipv4_only = matches.is_present("ipv4_only");
|
||||
let ipv4_only = matches.get_flag("ipv4_only");
|
||||
|
||||
let remote_addr = tokio::net::lookup_host(matches.value_of("remote").unwrap())
|
||||
let remote_addr = tokio::net::lookup_host(matches.get_one::<String>("remote").unwrap())
|
||||
.await
|
||||
.expect("bad remote address or host")
|
||||
.find(|addr| !ipv4_only || addr.is_ipv4())
|
||||
@@ -115,30 +119,34 @@ async fn main() -> io::Result<()> {
|
||||
info!("Remote address is: {}", remote_addr);
|
||||
|
||||
let tun_local: Ipv4Addr = matches
|
||||
.value_of("tun_local")
|
||||
.get_one::<String>("tun_local")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("bad local address for Tun interface");
|
||||
let tun_peer: Ipv4Addr = matches
|
||||
.value_of("tun_peer")
|
||||
.get_one::<String>("tun_peer")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("bad peer address for Tun interface");
|
||||
|
||||
let (tun_local6, tun_peer6) = if ipv4_only {
|
||||
let (tun_local6, tun_peer6) = if matches.get_flag("ipv4_only") {
|
||||
(None, None)
|
||||
} else {
|
||||
(
|
||||
matches
|
||||
.value_of("tun_local6")
|
||||
.get_one::<String>("tun_local6")
|
||||
.map(|v| v.parse().expect("bad local address for Tun interface")),
|
||||
matches
|
||||
.value_of("tun_peer6")
|
||||
.get_one::<String>("tun_peer6")
|
||||
.map(|v| v.parse().expect("bad peer address for Tun interface")),
|
||||
)
|
||||
};
|
||||
|
||||
let tun_name = matches.value_of("tun").unwrap();
|
||||
let tun_name = matches.get_one::<String>("tun").unwrap();
|
||||
let handshake_packet: Option<Vec<u8>> = matches
|
||||
.get_one::<String>("handshake_packet")
|
||||
.map(fs::read)
|
||||
.transpose()?;
|
||||
|
||||
let num_cpus = num_cpus::get();
|
||||
info!("{} cores available", num_cpus);
|
||||
@@ -186,9 +194,17 @@ async fn main() -> io::Result<()> {
|
||||
}
|
||||
|
||||
let sock = Arc::new(sock.unwrap());
|
||||
if let Some(ref p) = handshake_packet {
|
||||
if sock.send(p).await.is_none() {
|
||||
error!("Failed to send handshake packet to remote, closing connection.");
|
||||
continue;
|
||||
}
|
||||
|
||||
debug!("Sent handshake packet to: {}", sock);
|
||||
}
|
||||
|
||||
// send first packet
|
||||
let res = sock.send(&buf_r[..size]).await;
|
||||
if res.is_none() {
|
||||
if sock.send(&buf_r[..size]).await.is_none() {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@@ -1,8 +1,9 @@
|
||||
use clap::{crate_version, Arg, Command};
|
||||
use clap::{crate_version, Arg, ArgAction, Command};
|
||||
use fake_tcp::packet::MAX_PACKET_LEN;
|
||||
use fake_tcp::Stack;
|
||||
use log::{debug, error, info};
|
||||
use phantun::utils::{assign_ipv6_address, new_udp_reuseport};
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::sync::Arc;
|
||||
@@ -28,7 +29,6 @@ async fn main() -> io::Result<()> {
|
||||
.required(true)
|
||||
.value_name("PORT")
|
||||
.help("Sets the port where Phantun Server listens for incoming Phantun Client TCP connections")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("remote")
|
||||
@@ -37,7 +37,6 @@ async fn main() -> io::Result<()> {
|
||||
.required(true)
|
||||
.value_name("IP or HOST NAME: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),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun")
|
||||
@@ -46,7 +45,6 @@ async fn main() -> io::Result<()> {
|
||||
.value_name("tunX")
|
||||
.help("Sets the Tun interface name, if absent, pick the next available name")
|
||||
.default_value("")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun_local")
|
||||
@@ -55,7 +53,6 @@ async fn main() -> io::Result<()> {
|
||||
.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::new("tun_peer")
|
||||
@@ -66,7 +63,6 @@ async fn main() -> io::Result<()> {
|
||||
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),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("ipv4_only")
|
||||
@@ -74,7 +70,7 @@ async fn main() -> io::Result<()> {
|
||||
.short('4')
|
||||
.required(false)
|
||||
.help("Do not assign IPv6 addresses to Tun interface")
|
||||
.takes_value(false)
|
||||
.action(ArgAction::SetTrue)
|
||||
.conflicts_with_all(&["tun_local6", "tun_peer6"]),
|
||||
)
|
||||
.arg(
|
||||
@@ -83,8 +79,7 @@ async fn main() -> io::Result<()> {
|
||||
.required(false)
|
||||
.value_name("IP")
|
||||
.help("Sets the Tun interface IPv6 local address (O/S's end)")
|
||||
.default_value("fec9::1")
|
||||
.takes_value(true),
|
||||
.default_value("fcc9::1")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("tun_peer6")
|
||||
@@ -94,49 +89,63 @@ async fn main() -> io::Result<()> {
|
||||
.help("Sets the Tun interface IPv6 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("fec9::2")
|
||||
.takes_value(true),
|
||||
.default_value("fcc9::2")
|
||||
)
|
||||
.arg(
|
||||
Arg::new("handshake_packet")
|
||||
.long("handshake-packet")
|
||||
.required(false)
|
||||
.value_name("PATH")
|
||||
.help("Specify a file, which, after TCP handshake, its content will be sent as the \
|
||||
first data packet to the client.\n\
|
||||
Note: ensure this file's size does not exceed the MTU of the outgoing interface. \
|
||||
The content is always sent out in a single packet and will not be further segmented")
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let local_port: u16 = matches
|
||||
.value_of("local")
|
||||
.get_one::<String>("local")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("bad local port");
|
||||
|
||||
let remote_addr = tokio::net::lookup_host(matches.value_of("remote").unwrap())
|
||||
let remote_addr = tokio::net::lookup_host(matches.get_one::<String>("remote").unwrap())
|
||||
.await
|
||||
.expect("bad remote address or host")
|
||||
.next()
|
||||
.expect("unable to resolve remote host name");
|
||||
|
||||
info!("Remote address is: {}", remote_addr);
|
||||
|
||||
let tun_local: Ipv4Addr = matches
|
||||
.value_of("tun_local")
|
||||
.get_one::<String>("tun_local")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("bad local address for Tun interface");
|
||||
let tun_peer: Ipv4Addr = matches
|
||||
.value_of("tun_peer")
|
||||
.get_one::<String>("tun_peer")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("bad peer address for Tun interface");
|
||||
|
||||
let (tun_local6, tun_peer6) = if matches.is_present("ipv4_only") {
|
||||
let (tun_local6, tun_peer6) = if matches.get_flag("ipv4_only") {
|
||||
(None, None)
|
||||
} else {
|
||||
(
|
||||
matches
|
||||
.value_of("tun_local6")
|
||||
.get_one::<String>("tun_local6")
|
||||
.map(|v| v.parse().expect("bad local address for Tun interface")),
|
||||
matches
|
||||
.value_of("tun_peer6")
|
||||
.get_one::<String>("tun_peer6")
|
||||
.map(|v| v.parse().expect("bad peer address for Tun interface")),
|
||||
)
|
||||
};
|
||||
|
||||
let tun_name = matches.value_of("tun").unwrap();
|
||||
let tun_name = matches.get_one::<String>("tun").unwrap();
|
||||
let handshake_packet: Option<Vec<u8>> = matches
|
||||
.get_one::<String>("handshake_packet")
|
||||
.map(fs::read)
|
||||
.transpose()?;
|
||||
|
||||
let num_cpus = num_cpus::get();
|
||||
info!("{} cores available", num_cpus);
|
||||
@@ -169,6 +178,14 @@ async fn main() -> io::Result<()> {
|
||||
loop {
|
||||
let sock = Arc::new(stack.accept().await);
|
||||
info!("New connection: {}", sock);
|
||||
if let Some(ref p) = handshake_packet {
|
||||
if sock.send(p).await.is_none() {
|
||||
error!("Failed to send handshake packet to remote, closing connection.");
|
||||
continue;
|
||||
}
|
||||
|
||||
debug!("Sent handshake packet to: {}", sock);
|
||||
}
|
||||
|
||||
let packet_received = Arc::new(Notify::new());
|
||||
let quit = CancellationToken::new();
|
||||
|
Reference in New Issue
Block a user