Initial commit, forwarding WireGuard works

This commit is contained in:
Linus Färnstrand 2020-08-14 01:45:53 +02:00
commit 5b577e2c5b
6 changed files with 938 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

637
Cargo.lock generated Normal file
View File

@ -0,0 +1,637 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi 0.3.9",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bytes"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "clap"
version = "2.33.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10040cdf04294b565d9e0319955430099ec3813a64c952b86a41200ad714ae48"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "err-context"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "449aad22b1364e927ff3bf50f55404efd705c40065fb47f73f28704de707c89e"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
dependencies = [
"bitflags",
"fuchsia-zircon-sys",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
name = "futures"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e05b85ec287aac0dc34db7d4a569323df697f9c55b99b15d6b4ef8cde49f613"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399"
[[package]]
name = "futures-executor"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10d6bb888be1153d3abeb9006b11b02cf5e9b209fda28693c31ae1e4e012e314"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789"
[[package]]
name = "futures-macro"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39"
dependencies = [
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f2032893cb734c7a05d85ce0cc8b8c4075278e93b24b66f9de99d6eb0fa8acc"
[[package]]
name = "futures-task"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626"
dependencies = [
"once_cell",
]
[[package]]
name = "futures-util"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project",
"pin-utils",
"proc-macro-hack",
"proc-macro-nested",
"slab",
]
[[package]]
name = "heck"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9"
dependencies = [
"libc",
]
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "iovec"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
dependencies = [
"libc",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10"
[[package]]
name = "log"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
[[package]]
name = "mio"
version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
dependencies = [
"cfg-if",
"fuchsia-zircon",
"fuchsia-zircon-sys",
"iovec",
"kernel32-sys",
"libc",
"log",
"miow",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]]
name = "miow"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
dependencies = [
"kernel32-sys",
"net2",
"winapi 0.2.8",
"ws2_32-sys",
]
[[package]]
name = "net2"
version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
dependencies = [
"cfg-if",
"libc",
"winapi 0.3.9",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d"
[[package]]
name = "pin-project"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598"
[[package]]
name = "proc-macro-nested"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a"
[[package]]
name = "proc-macro2"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
[[package]]
name = "slab"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de5472fb24d7e80ae84a7801b7978f95a19ec32cb1876faea59ab711eb901976"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0eb37335aeeebe51be42e2dc07f031163fbabfa6ac67d7ea68b5c2f68d5f99"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e69abc24912995b3038597a7a593be5053eb0fb44f3cc5beec0deb421790c1f4"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[[package]]
name = "tokio"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d34ca54d84bf2b5b4d7d31e901a8464f7b60ac145a284fba25ceb801f2ddccd"
dependencies = [
"bytes",
"iovec",
"lazy_static",
"memchr",
"mio",
"num_cpus",
"pin-project-lite",
"tokio-macros",
]
[[package]]
name = "tokio-macros"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "triggered"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3aa15a5f3ffd17f7c101155c6e7cf9c4060ee767730eb53c0580e452c3725af1"
[[package]]
name = "udp2tcp"
version = "0.1.0"
dependencies = [
"env_logger",
"err-context",
"futures",
"log",
"structopt",
"tokio",
"triggered",
]
[[package]]
name = "unicode-segmentation"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "ws2_32-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]

24
Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "udp2tcp"
version = "0.1.0"
authors = ["Linus Färnstrand <linus@mullvad.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
name = "udp2tcp"
path = "src/udp2tcp.rs"
[[bin]]
name = "tcp2udp"
path = "src/tcp2udp.rs"
[dependencies]
tokio = { version = "0.2.22", features = ["rt-threaded", "macros", "tcp", "udp", "io-util"] }
err-context = "0.1.0"
log = "0.4.11"
env_logger = "0.7.1"
triggered = "0.1.1"
futures = "0.3.5"
structopt = "0.3.16"

57
src/shared.rs Normal file
View File

@ -0,0 +1,57 @@
use err_context::ResultExt as _;
use std::convert::TryFrom;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::tcp::{OwnedReadHalf as TcpReadHalf, OwnedWriteHalf as TcpWriteHalf};
use tokio::net::udp::{RecvHalf as UdpRecvHalf, SendHalf as UdpSendHalf};
pub async fn process_tcp2udp(
mut tcp_in: TcpReadHalf,
mut udp_out: UdpSendHalf,
) -> Result<(), Box<dyn std::error::Error>> {
let mut buffer = [0u8; 1024 * 64];
loop {
let datagram_len = tcp_in.read_u16().await? as usize;
let tcp_read_len = tcp_in
.read_exact(&mut buffer[..datagram_len])
.await
.context("Failed reading from TCP")?;
assert_eq!(datagram_len, tcp_read_len);
let udp_write_len = udp_out
.send(&buffer[..datagram_len])
.await
.context("Failed writing to UDP")?;
if tcp_read_len != udp_write_len {
log::warn!(
"Read {} bytes from TCP but wrote only {} to UDP",
tcp_read_len,
udp_write_len
);
} else {
log::trace!("Forwarded {} bytes TCP->UDP", tcp_read_len);
}
}
}
pub async fn process_udp2tcp(
mut udp_in: UdpRecvHalf,
mut tcp_out: TcpWriteHalf,
) -> Result<(), Box<dyn std::error::Error>> {
let mut buffer = [0u8; 2 + 1024 * 64];
loop {
let udp_read_len = udp_in
.recv(&mut buffer[2..])
.await
.context("Failed reading from UDP")?;
if udp_read_len == 0 {
break;
}
let datagram_len = u16::try_from(udp_read_len).unwrap();
buffer[..2].copy_from_slice(&datagram_len.to_be_bytes()[..]);
tcp_out
.write_all(&buffer[..2 + udp_read_len])
.await
.context("Failed writing to TCP")?;
log::trace!("Forwarded {} bytes UDP->TCP", udp_read_len);
}
Ok(())
}

118
src/tcp2udp.rs Normal file
View File

@ -0,0 +1,118 @@
use err_context::{BoxedErrorExt as _, ResultExt as _};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use structopt::StructOpt;
use tokio::net::{TcpListener, TcpStream, UdpSocket};
mod shared;
use shared::{process_tcp2udp, process_udp2tcp};
#[derive(Debug, StructOpt)]
#[structopt(name = "tcp2udp", about = "Listen for incoming TCP and forward to UDP")]
struct Options {
tcp_listen_addr: SocketAddrV4,
udp_forward_addr: SocketAddrV4,
#[structopt(long = "udp-bind", default_value = "0.0.0.0")]
udp_bind_ip: Ipv4Addr,
}
#[tokio::main]
async fn main() {
env_logger::init();
let options = Options::from_args();
if let Err(error) = run(options).await {
log::error!("Error: {}", error.display("\nCaused by: "));
std::process::exit(1);
}
}
async fn run(options: Options) -> Result<(), Box<dyn std::error::Error>> {
let mut tcp_listener = TcpListener::bind(options.tcp_listen_addr)
.await
.with_context(|_| {
format!(
"Failed to bind a TCP listener to {}",
options.tcp_listen_addr
)
})?;
log::info!("Listening on {}/TCP", tcp_listener.local_addr().unwrap());
loop {
match tcp_listener.accept().await {
Ok((socket, tcp_peer_addr)) => {
log::debug!("Incoming connection from {}/TCP", tcp_peer_addr);
let udp_bind_ip = options.udp_bind_ip;
let udp_forward_addr = options.udp_forward_addr;
tokio::spawn(async move {
if let Err(error) =
process_socket(socket, tcp_peer_addr, udp_bind_ip, udp_forward_addr).await
{
log::error!("Error: {}", error.display("\nCaused by: "));
}
});
}
Err(error) => log::error!("Error when accepting incoming TCP connection: {}", error),
}
}
}
async fn process_socket(
socket: TcpStream,
tcp_peer_addr: SocketAddr,
udp_bind_ip: Ipv4Addr,
udp_peer_addr: SocketAddrV4,
) -> Result<(), Box<dyn std::error::Error>> {
let (tcp_in, tcp_out) = socket.into_split();
let udp_bind_addr = SocketAddrV4::new(udp_bind_ip, 0);
let udp_socket = UdpSocket::bind(udp_bind_addr)
.await
.with_context(|_| format!("Failed to bind UDP socket to {}", udp_bind_addr))?;
udp_socket
.connect(udp_peer_addr)
.await
.with_context(|_| format!("Failed to connect UDP socket to {}", udp_peer_addr))?;
log::debug!(
"UDP socket bound to {} and connected to {}",
udp_socket.local_addr()?,
udp_peer_addr
);
let (udp_in, udp_out) = udp_socket.split();
let tcp2udp_future = async move {
if let Err(error) = process_tcp2udp(tcp_in, udp_out).await {
log::error!("Error: {}", error.display("\nCaused by: "));
}
};
let udp2tcp_future = async move {
if let Err(error) = process_udp2tcp(udp_in, tcp_out).await {
log::error!("Error: {}", error.display("\nCaused by: "));
}
};
tokio::select! {
_ = tcp2udp_future => {
log::trace!(
"Closing TCP->UDP end for {}->{}",
tcp_peer_addr,
udp_peer_addr
);
},
_ = udp2tcp_future => {
log::trace!(
"Closing UDP->TCP end for {}->{}",
udp_peer_addr,
tcp_peer_addr
);
}
}
log::trace!(
"Closing forwarding for {}/TCP <-> {}/UDP",
tcp_peer_addr,
udp_peer_addr
);
Ok(())
}

101
src/udp2tcp.rs Normal file
View File

@ -0,0 +1,101 @@
use err_context::{BoxedErrorExt as _, ResultExt as _};
use std::convert::TryFrom;
use std::net::SocketAddrV4;
use structopt::StructOpt;
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpStream, UdpSocket};
mod shared;
use shared::{process_tcp2udp, process_udp2tcp};
#[derive(Debug, StructOpt)]
#[structopt(name = "tcp2udp", about = "Listen for incoming TCP and forward to UDP")]
struct Options {
#[structopt(long = "udp-listen", default_value = "0.0.0.0:51820")]
udp_listen_addr: SocketAddrV4,
tcp_forward_addr: SocketAddrV4,
}
#[tokio::main]
async fn main() {
env_logger::init();
let options = Options::from_args();
if let Err(error) = run(options).await {
log::error!("Error: {}", error.display("\nCaused by: "));
std::process::exit(1);
}
}
async fn run(options: Options) -> Result<(), Box<dyn std::error::Error>> {
let mut udp_socket = UdpSocket::bind(options.udp_listen_addr)
.await
.with_context(|_| format!("Failed to bind UDP socket to {}", options.udp_listen_addr))?;
log::info!("Listening on {}/UDP", udp_socket.local_addr().unwrap());
let mut buffer = [0u8; 1024 * 64];
let (udp_read_len, udp_peer_addr) = udp_socket
.recv_from(&mut buffer)
.await
.context("Failed receiving the first packet")?;
log::info!(
"Incoming connection from {}/UDP, forwarding to {}/TCP",
udp_peer_addr,
options.tcp_forward_addr
);
udp_socket
.connect(udp_peer_addr)
.await
.with_context(|_| format!("Failed to connect UDP socket to {}", udp_peer_addr))?;
let (udp_in, udp_out) = udp_socket.split();
let (tcp_in, mut tcp_out) = TcpStream::connect(options.tcp_forward_addr)
.await
.with_context(|_| format!("Failed to connect to {}/TCP", options.tcp_forward_addr))?
.into_split();
let datagram_len = u16::try_from(udp_read_len).unwrap();
log::trace!("Read {} byte UDP datagram", datagram_len);
tcp_out.write_u16(datagram_len).await?;
tcp_out
.write_all(&buffer[..udp_read_len])
.await
.context("Failed writing to TCP")?;
log::trace!("Forwarded {} bytes UDP->TCP", udp_read_len);
let tcp2udp_future = async move {
if let Err(error) = process_tcp2udp(tcp_in, udp_out).await {
log::error!("Error: {}", error.display("\nCaused by: "));
}
};
let udp2tcp_future = async move {
if let Err(error) = process_udp2tcp(udp_in, tcp_out).await {
log::error!("Error: {}", error.display("\nCaused by: "));
}
};
tokio::select! {
_ = tcp2udp_future => {
log::trace!(
"Closing TCP->UDP end for {}->{}",
options.tcp_forward_addr,
udp_peer_addr
);
},
_ = udp2tcp_future => {
log::trace!(
"Closing UDP->TCP end for {}->{}",
udp_peer_addr,
options.tcp_forward_addr
);
}
}
log::trace!(
"Closing forwarding for {}/UDP <-> {}/TCP",
udp_peer_addr,
options.tcp_forward_addr,
);
Ok(())
}