Compare commits
10 Commits
9d9f89881d
...
v0.1.9
Author | SHA1 | Date | |
---|---|---|---|
6300c43495 | |||
c21ff86ee4 | |||
8d6387773a | |||
95149ffd9f | |||
a140748647 | |||
ad6955a30d | |||
4592c94586 | |||
6284870059 | |||
97b4bf6bbe | |||
59c7128f93 |
@@ -1,5 +1,5 @@
|
||||
local executableName = 'l4p';
|
||||
local build_image = 'img.kie.rs/jjkiers/rust-cross:rust1.71.1-zig';
|
||||
local build_image = 'img.kie.rs/jjkiers/rust-crossbuild:rust1.79.0-zig0.11.0-zig';
|
||||
|
||||
local archs = [
|
||||
{ target: 'aarch64-unknown-linux-musl', short: 'arm64-musl' },
|
||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/.idea
|
||||
/.vscode
|
||||
/target
|
||||
config.yaml
|
||||
|
42
CHANGELOG.md
Normal file
42
CHANGELOG.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.1.9] - 2024-06-22
|
||||
|
||||
### Deprecated
|
||||
|
||||
The ability to run `l4p` without arguments is now deprecated. Please use
|
||||
`l4p serve` going forward.
|
||||
|
||||
### Added
|
||||
|
||||
* Added self update functionality. Just run `l4p update` to use it.
|
||||
* Now keeping a change log in the `CHANGELOG.md` file.
|
||||
|
||||
### Changed
|
||||
|
||||
* Updated build pipeline to generate much smaller binaries
|
||||
|
||||
-------
|
||||
|
||||
## Previous versions
|
||||
|
||||
[unreleased]: https://code.kiers.eu/jjkiers/layer4-proxy/compare/v0.1.9...HEAD
|
||||
[0.1.9]: https://code.kiers.eu/jjkiers/layer4-proxy/compare/v0.1.8...v0.1.9
|
||||
|
||||
|
||||
|
||||
Types of changes:
|
||||
|
||||
* `Added` for new features.
|
||||
* `Changed` for changes in existing functionality.
|
||||
* `Deprecated` for soon-to-be removed features.
|
||||
* `Removed` for now removed features.
|
||||
* `Fixed` for any bug fixes.
|
||||
* `Security` in case of vulnerabilities.
|
758
Cargo.lock
generated
758
Cargo.lock
generated
@@ -58,12 +58,30 @@ dependencies = [
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
||||
|
||||
[[package]]
|
||||
name = "byte_string"
|
||||
version = "1.0.0"
|
||||
@@ -88,6 +106,19 @@ version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.15.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"unicode-width",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.3.11"
|
||||
@@ -97,6 +128,12 @@ dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||
|
||||
[[package]]
|
||||
name = "enum_primitive"
|
||||
version = "0.1.1"
|
||||
@@ -125,6 +162,37 @@ version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
|
||||
dependencies = [
|
||||
"instant",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.1"
|
||||
@@ -252,12 +320,109 @@ version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd5256b483761cd23699d0da46cc6fd2ee3be420bbe6d020ae4a091e70b7e9fd"
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"fnv",
|
||||
"itoa",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-body"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"http",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-body-util"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "httparse"
|
||||
version = "1.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "1.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"httparse",
|
||||
"itoa",
|
||||
"pin-project-lite",
|
||||
"smallvec",
|
||||
"tokio",
|
||||
"want",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-rustls"
|
||||
version = "0.27.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"http",
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"rustls",
|
||||
"rustls-pki-types",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tower-service",
|
||||
"webpki-roots",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"hyper",
|
||||
"pin-project-lite",
|
||||
"socket2",
|
||||
"tokio",
|
||||
"tower",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.5.0"
|
||||
@@ -278,6 +443,34 @@ dependencies = [
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indicatif"
|
||||
version = "0.17.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "763a5a8f45087d6bcea4222e7b72c291a054edf80e4ef6efd2a4979878c7bea3"
|
||||
dependencies = [
|
||||
"console",
|
||||
"instant",
|
||||
"number_prefix",
|
||||
"portable-atomic",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ipnet"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.12"
|
||||
@@ -296,15 +489,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
|
||||
|
||||
[[package]]
|
||||
name = "layer4-proxy"
|
||||
version = "0.1.7"
|
||||
name = "js-sys"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "l4p"
|
||||
version = "0.1.9"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"byte_string",
|
||||
"bytes",
|
||||
"futures",
|
||||
"log",
|
||||
"pico-args",
|
||||
"pretty_env_logger",
|
||||
"self_update",
|
||||
"serde",
|
||||
"serde_yaml",
|
||||
"time",
|
||||
@@ -313,12 +517,24 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.153"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.11"
|
||||
@@ -341,6 +557,12 @@ version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
@@ -442,6 +664,12 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "number_prefix"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.32.2"
|
||||
@@ -451,6 +679,12 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
@@ -518,6 +752,32 @@ dependencies = [
|
||||
"siphasher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pico-args"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3"
|
||||
dependencies = [
|
||||
"pin-project-internal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-internal"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.13"
|
||||
@@ -530,6 +790,12 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0"
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
@@ -561,6 +827,62 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quick-xml"
|
||||
version = "0.23.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11bafc859c6815fbaffbbbf4229ecb767ac913fecb27f9ad4343662e9ef099ea"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quinn"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4ceeeeabace7857413798eb1ffa1e9c905a9946a57d81fb69b4b71c4d8eb3ad"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"pin-project-lite",
|
||||
"quinn-proto",
|
||||
"quinn-udp",
|
||||
"rustc-hash",
|
||||
"rustls",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quinn-proto"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddf517c03a109db8100448a4be38d498df8a210a99fe0e1b9eaf39e78c640efe"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"rand",
|
||||
"ring",
|
||||
"rustc-hash",
|
||||
"rustls",
|
||||
"slab",
|
||||
"thiserror",
|
||||
"tinyvec",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quinn-udp"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9096629c45860fc7fb143e125eb826b5e721e10be3263160c7d60ca832cf8c46"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"once_cell",
|
||||
"socket2",
|
||||
"tracing",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.35"
|
||||
@@ -606,7 +928,7 @@ version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -638,12 +960,76 @@ version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.12.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-rustls",
|
||||
"hyper-util",
|
||||
"ipnet",
|
||||
"js-sys",
|
||||
"log",
|
||||
"mime",
|
||||
"once_cell",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"quinn",
|
||||
"rustls",
|
||||
"rustls-pemfile",
|
||||
"rustls-pki-types",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"sync_wrapper",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tower-service",
|
||||
"url",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
"webpki-roots",
|
||||
"winreg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.17.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"getrandom",
|
||||
"libc",
|
||||
"spin",
|
||||
"untrusted",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "rusticata-macros"
|
||||
version = "4.1.0"
|
||||
@@ -653,6 +1039,60 @@ dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
|
||||
dependencies = [
|
||||
"bitflags 2.5.0",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.23.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"ring",
|
||||
"rustls-pki-types",
|
||||
"rustls-webpki",
|
||||
"subtle",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pemfile"
|
||||
version = "2.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustls-pki-types"
|
||||
version = "1.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d"
|
||||
|
||||
[[package]]
|
||||
name = "rustls-webpki"
|
||||
version = "0.102.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ff448f7e92e913c4b7d4c6d8e4540a1724b319b4152b8aef6d4cf8339712b33e"
|
||||
dependencies = [
|
||||
"ring",
|
||||
"rustls-pki-types",
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.14"
|
||||
@@ -671,6 +1111,42 @@ version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "self-replace"
|
||||
version = "1.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "525db198616b2bcd0f245daf7bfd8130222f7ee6af9ff9984c19a61bf1160c55"
|
||||
dependencies = [
|
||||
"fastrand 1.9.0",
|
||||
"tempfile",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "self_update"
|
||||
version = "0.40.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e4997484b55df069a4773d822715695b2cc27b23829eca2a4b41690e948bdeb"
|
||||
dependencies = [
|
||||
"hyper",
|
||||
"indicatif",
|
||||
"log",
|
||||
"quick-xml",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"self-replace",
|
||||
"semver",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"urlencoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.197"
|
||||
@@ -691,6 +1167,29 @@ dependencies = [
|
||||
"syn 2.0.50",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.9.32"
|
||||
@@ -744,6 +1243,18 @@ dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d0208408ba0c3df17ed26eb06992cb1a1268d41b2c0e12e65203fbe3972cee5"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
@@ -766,6 +1277,24 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sync_wrapper"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394"
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"fastrand 2.1.0",
|
||||
"rustix",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.4.1"
|
||||
@@ -775,6 +1304,26 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.61"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.34"
|
||||
@@ -867,6 +1416,81 @@ dependencies = [
|
||||
"syn 2.0.50",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-rustls"
|
||||
version = "0.26.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4"
|
||||
dependencies = [
|
||||
"rustls",
|
||||
"rustls-pki-types",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower"
|
||||
version = "0.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"pin-project",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower-layer"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
|
||||
|
||||
[[package]]
|
||||
name = "tower-service"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
|
||||
dependencies = [
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "try-lock"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.15"
|
||||
@@ -888,12 +1512,24 @@ dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
|
||||
|
||||
[[package]]
|
||||
name = "unsafe-libyaml"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.5.0"
|
||||
@@ -905,12 +1541,112 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "urlencoding"
|
||||
version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
|
||||
|
||||
[[package]]
|
||||
name = "want"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e"
|
||||
dependencies = [
|
||||
"try-lock",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "webpki-roots"
|
||||
version = "0.26.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd7c23921eeb1713a4e851530e9b9756e4fb0e89978582942612524cf09f01cd"
|
||||
dependencies = [
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
@@ -1073,3 +1809,19 @@ name = "windows_x86_64_msvc"
|
||||
version = "0.52.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6"
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde"
|
||||
|
10
Cargo.toml
10
Cargo.toml
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "layer4-proxy"
|
||||
version = "0.1.7"
|
||||
name = "l4p"
|
||||
version = "0.1.9"
|
||||
edition = "2021"
|
||||
authors = ["Jacob Kiers <code@kiers.eu>"]
|
||||
license = "Apache-2.0"
|
||||
@@ -25,6 +25,7 @@ byte_string = "1"
|
||||
bytes = "1.1"
|
||||
futures = "0.3"
|
||||
log = "0.4"
|
||||
pico-args = "0.5.0"
|
||||
pretty_env_logger = "0.5"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_yaml = "0.9.21"
|
||||
@@ -32,3 +33,8 @@ time = { version = "0.3.1", features = ["local-offset", "formatting"] }
|
||||
tls-parser = "0.11"
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
url = "2.2.2"
|
||||
|
||||
[dependencies.self_update]
|
||||
version = "0.40.0"
|
||||
default-features = false
|
||||
features = ["rustls"]
|
||||
|
@@ -4,7 +4,7 @@
|
||||
|
||||

|
||||
|
||||
`l4p` is a layer 4 proxy implemented by Rust to listen on specific ports and transfer TCP/KCP data to remote addresses(only TCP) according to configuration.
|
||||
`l4p` is a layer 4 proxy implemented by Rust to listen on specific ports and transfer TCP data to remote addresses (only TCP) according to the configuration.
|
||||
|
||||
## Features
|
||||
|
||||
@@ -57,8 +57,8 @@ For detailed configuration, check [this example](./config.yaml.example).
|
||||
|
||||
## Thanks
|
||||
|
||||
- [`l4p`](https://crates.io/crates/`l4p`), of which this is a heavily modified fork.
|
||||
- [`fourth`](https://crates.io/crates/fourth), of which this is a heavily modified fork.
|
||||
|
||||
## License
|
||||
|
||||
`l4p` is available under terms of Apache-2.0.
|
||||
`l4p` is available under terms of Apache-2.0.
|
||||
|
@@ -15,11 +15,6 @@ servers:
|
||||
listen:
|
||||
- "127.0.0.1:8081"
|
||||
default: remote
|
||||
kcp_server:
|
||||
protocol: kcp # default TCP
|
||||
listen:
|
||||
- "127.0.0.1:8082"
|
||||
default: echo
|
||||
|
||||
upstream:
|
||||
nginx: "tcp://127.0.0.1:8080"
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use crate::upstreams::ProxyToUpstream;
|
||||
use crate::upstreams::Upstream;
|
||||
use log::{debug, warn};
|
||||
use log::{debug, info, warn};
|
||||
use serde::Deserialize;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::File;
|
||||
@@ -119,9 +119,10 @@ fn load_config(path: &str) -> Result<ParsedConfigV1, ConfigError> {
|
||||
if !log_level.eq("disable") {
|
||||
std::env::set_var("FOURTH_LOG", log_level.clone());
|
||||
pretty_env_logger::init_custom_env("FOURTH_LOG");
|
||||
debug!("Set log level to {}", log_level);
|
||||
}
|
||||
|
||||
info!("Using config file: {}", &path);
|
||||
debug!("Set log level to {}", log_level);
|
||||
debug!("Config version {}", base.version);
|
||||
|
||||
let mut parsed_upstream: HashMap<String, Upstream> = HashMap::new();
|
||||
@@ -221,7 +222,7 @@ mod tests {
|
||||
let config = ConfigV1::new("tests/config.yaml").unwrap();
|
||||
assert_eq!(config.base.version, 1);
|
||||
assert_eq!(config.base.log.unwrap(), "disable");
|
||||
assert_eq!(config.base.servers.len(), 5);
|
||||
assert_eq!(config.base.servers.len(), 3);
|
||||
assert_eq!(config.base.upstream.len(), 3 + 2); // Add ban and echo upstreams
|
||||
}
|
||||
}
|
@@ -1,3 +1,3 @@
|
||||
mod config;
|
||||
pub(crate) use config::ConfigV1;
|
||||
pub(crate) use config::ParsedConfigV1;
|
||||
mod config_v1;
|
||||
pub(crate) use config_v1::ConfigV1;
|
||||
pub(crate) use config_v1::ParsedConfigV1;
|
||||
|
50
src/main.rs
50
src/main.rs
@@ -1,15 +1,33 @@
|
||||
mod config;
|
||||
mod plugins;
|
||||
mod servers;
|
||||
mod update;
|
||||
mod upstreams;
|
||||
|
||||
use crate::config::ConfigV1;
|
||||
use crate::servers::Server;
|
||||
|
||||
use log::{debug, error};
|
||||
use log::{debug, error, info};
|
||||
use pico_args::Arguments;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
let mut args = Arguments::from_env();
|
||||
|
||||
match args.subcommand().expect("Unexpected error").as_deref() {
|
||||
Some("serve") => serve(),
|
||||
Some("update") => update::update(),
|
||||
Some(cmd) => {
|
||||
eprintln!("Invalid command: {cmd}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
None => {
|
||||
eprintln!("Calling l4p without argument is deprecated now. Please use: l4p serve");
|
||||
serve();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn serve() {
|
||||
let config_path = match find_config() {
|
||||
Ok(p) => p,
|
||||
Err(paths) => {
|
||||
@@ -38,20 +56,28 @@ fn main() {
|
||||
}
|
||||
|
||||
fn find_config() -> Result<String, Vec<String>> {
|
||||
let possible_paths = ["/etc/l4p", ""];
|
||||
let possible_locations = ["/etc/l4p", ""];
|
||||
let possible_names = ["l4p.yaml", "config.yaml"];
|
||||
|
||||
let mut tried_paths = Vec::<String>::new();
|
||||
let mut possible_paths = Vec::<PathBuf>::new();
|
||||
|
||||
for path in possible_paths
|
||||
.iter()
|
||||
.flat_map(|&path| {
|
||||
possible_names
|
||||
.iter()
|
||||
.map(move |&file| PathBuf::new().join(path).join(file))
|
||||
})
|
||||
.collect::<Vec<PathBuf>>()
|
||||
{
|
||||
if let Ok(env_path) = std::env::var("L4P_CONFIG") {
|
||||
possible_paths.push(PathBuf::from(env_path));
|
||||
}
|
||||
|
||||
possible_paths.append(
|
||||
&mut possible_locations
|
||||
.iter()
|
||||
.flat_map(|&path| {
|
||||
possible_names
|
||||
.iter()
|
||||
.map(move |&file| PathBuf::new().join(path).join(file))
|
||||
})
|
||||
.collect::<Vec<PathBuf>>(),
|
||||
);
|
||||
|
||||
for path in possible_paths {
|
||||
let path_str = path.to_string_lossy().to_string();
|
||||
if path.exists() {
|
||||
return Ok(path_str);
|
||||
|
@@ -1,110 +0,0 @@
|
||||
use std::{io::Write, time::Duration};
|
||||
|
||||
use kcp::Kcp;
|
||||
|
||||
/// Kcp Delay Config
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct KcpNoDelayConfig {
|
||||
/// Enable nodelay
|
||||
pub nodelay: bool,
|
||||
/// Internal update interval (ms)
|
||||
pub interval: i32,
|
||||
/// ACK number to enable fast resend
|
||||
pub resend: i32,
|
||||
/// Disable congetion control
|
||||
pub nc: bool,
|
||||
}
|
||||
|
||||
impl Default for KcpNoDelayConfig {
|
||||
fn default() -> KcpNoDelayConfig {
|
||||
KcpNoDelayConfig {
|
||||
nodelay: false,
|
||||
interval: 100,
|
||||
resend: 0,
|
||||
nc: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
impl KcpNoDelayConfig {
|
||||
/// Get a fastest configuration
|
||||
///
|
||||
/// 1. Enable NoDelay
|
||||
/// 2. Set ticking interval to be 10ms
|
||||
/// 3. Set fast resend to be 2
|
||||
/// 4. Disable congestion control
|
||||
pub fn fastest() -> KcpNoDelayConfig {
|
||||
KcpNoDelayConfig {
|
||||
nodelay: true,
|
||||
interval: 10,
|
||||
resend: 2,
|
||||
nc: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a normal configuration
|
||||
///
|
||||
/// 1. Disable NoDelay
|
||||
/// 2. Set ticking interval to be 40ms
|
||||
/// 3. Disable fast resend
|
||||
/// 4. Enable congestion control
|
||||
pub fn normal() -> KcpNoDelayConfig {
|
||||
KcpNoDelayConfig {
|
||||
nodelay: false,
|
||||
interval: 40,
|
||||
resend: 0,
|
||||
nc: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Kcp Config
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct KcpConfig {
|
||||
/// Max Transmission Unit
|
||||
pub mtu: usize,
|
||||
/// nodelay
|
||||
pub nodelay: KcpNoDelayConfig,
|
||||
/// Send window size
|
||||
pub wnd_size: (u16, u16),
|
||||
/// Session expire duration, default is 90 seconds
|
||||
pub session_expire: Duration,
|
||||
/// Flush KCP state immediately after write
|
||||
pub flush_write: bool,
|
||||
/// Flush ACKs immediately after input
|
||||
pub flush_acks_input: bool,
|
||||
/// Stream mode
|
||||
pub stream: bool,
|
||||
}
|
||||
|
||||
impl Default for KcpConfig {
|
||||
fn default() -> KcpConfig {
|
||||
KcpConfig {
|
||||
mtu: 1400,
|
||||
nodelay: KcpNoDelayConfig::normal(),
|
||||
wnd_size: (256, 256),
|
||||
session_expire: Duration::from_secs(90),
|
||||
flush_write: false,
|
||||
flush_acks_input: false,
|
||||
stream: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl KcpConfig {
|
||||
/// Applies config onto `Kcp`
|
||||
#[doc(hidden)]
|
||||
pub fn apply_config<W: Write>(&self, k: &mut Kcp<W>) {
|
||||
k.set_mtu(self.mtu).expect("invalid MTU");
|
||||
|
||||
k.set_nodelay(
|
||||
self.nodelay.nodelay,
|
||||
self.nodelay.interval,
|
||||
self.nodelay.resend,
|
||||
self.nodelay.nc,
|
||||
);
|
||||
|
||||
k.set_wndsize(self.wnd_size.0, self.wnd_size.1);
|
||||
}
|
||||
}
|
@@ -1,128 +0,0 @@
|
||||
use std::{
|
||||
io::{self, ErrorKind},
|
||||
net::SocketAddr,
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use byte_string::ByteStr;
|
||||
use kcp::{Error as KcpError, KcpResult};
|
||||
use log::{debug, error, trace};
|
||||
use tokio::{
|
||||
net::{ToSocketAddrs, UdpSocket},
|
||||
sync::mpsc,
|
||||
task::JoinHandle,
|
||||
time,
|
||||
};
|
||||
|
||||
use crate::plugins::kcp::{config::KcpConfig, session::KcpSessionManager, stream::KcpStream};
|
||||
|
||||
#[allow(unused)]
|
||||
pub struct KcpListener {
|
||||
udp: Arc<UdpSocket>,
|
||||
accept_rx: mpsc::Receiver<(KcpStream, SocketAddr)>,
|
||||
task_watcher: JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl Drop for KcpListener {
|
||||
fn drop(&mut self) {
|
||||
self.task_watcher.abort();
|
||||
}
|
||||
}
|
||||
|
||||
impl KcpListener {
|
||||
pub async fn bind<A: ToSocketAddrs>(config: KcpConfig, addr: A) -> KcpResult<KcpListener> {
|
||||
let udp = UdpSocket::bind(addr).await?;
|
||||
let udp = Arc::new(udp);
|
||||
let server_udp = udp.clone();
|
||||
|
||||
let (accept_tx, accept_rx) = mpsc::channel(1024 /* backlogs */);
|
||||
let task_watcher = tokio::spawn(async move {
|
||||
let (close_tx, mut close_rx) = mpsc::channel(64);
|
||||
|
||||
let mut sessions = KcpSessionManager::new();
|
||||
let mut packet_buffer = [0u8; 65536];
|
||||
loop {
|
||||
tokio::select! {
|
||||
conv = close_rx.recv() => {
|
||||
let conv = conv.expect("close_tx closed unexpectly");
|
||||
sessions.close_conv(conv);
|
||||
trace!("session conv: {} removed", conv);
|
||||
}
|
||||
|
||||
recv_res = udp.recv_from(&mut packet_buffer) => {
|
||||
match recv_res {
|
||||
Err(err) => {
|
||||
error!("udp.recv_from failed, error: {}", err);
|
||||
time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
Ok((n, peer_addr)) => {
|
||||
let packet = &mut packet_buffer[..n];
|
||||
|
||||
log::trace!("received peer: {}, {:?}", peer_addr, ByteStr::new(packet));
|
||||
|
||||
let mut conv = kcp::get_conv(packet);
|
||||
if conv == 0 {
|
||||
// Allocate a conv for client.
|
||||
conv = sessions.alloc_conv();
|
||||
debug!("allocate {} conv for peer: {}", conv, peer_addr);
|
||||
|
||||
kcp::set_conv(packet, conv);
|
||||
}
|
||||
|
||||
let session = match sessions.get_or_create(&config, conv, &udp, peer_addr, &close_tx) {
|
||||
Ok((s, created)) => {
|
||||
if created {
|
||||
// Created a new session, constructed a new accepted client
|
||||
let stream = KcpStream::with_session(s.clone());
|
||||
if let Err(..) = accept_tx.try_send((stream, peer_addr)) {
|
||||
debug!("failed to create accepted stream due to channel failure");
|
||||
|
||||
// remove it from session
|
||||
sessions.close_conv(conv);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
s
|
||||
},
|
||||
Err(err) => {
|
||||
error!("failed to create session, error: {}, peer: {}, conv: {}", err, peer_addr, conv);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// let mut kcp = session.kcp_socket().lock().await;
|
||||
// if let Err(err) = kcp.input(packet) {
|
||||
// error!("kcp.input failed, peer: {}, conv: {}, error: {}, packet: {:?}", peer_addr, conv, err, ByteStr::new(packet));
|
||||
// }
|
||||
session.input(packet).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(KcpListener {
|
||||
udp: server_udp,
|
||||
accept_rx,
|
||||
task_watcher,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn accept(&mut self) -> KcpResult<(KcpStream, SocketAddr)> {
|
||||
match self.accept_rx.recv().await {
|
||||
Some(s) => Ok(s),
|
||||
None => Err(KcpError::IoError(io::Error::new(
|
||||
ErrorKind::Other,
|
||||
"accept channel closed unexpectly",
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||
self.udp.local_addr()
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
//! Library of KCP on Tokio
|
||||
|
||||
pub use self::{
|
||||
config::{KcpConfig, KcpNoDelayConfig},
|
||||
listener::KcpListener,
|
||||
stream::KcpStream,
|
||||
};
|
||||
|
||||
mod config;
|
||||
mod listener;
|
||||
mod session;
|
||||
mod skcp;
|
||||
mod stream;
|
||||
mod utils;
|
@@ -1,256 +0,0 @@
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
net::SocketAddr,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use byte_string::ByteStr;
|
||||
use kcp::KcpResult;
|
||||
use log::{error, trace};
|
||||
use tokio::{
|
||||
net::UdpSocket,
|
||||
sync::{mpsc, Mutex},
|
||||
time::{self, Instant},
|
||||
};
|
||||
|
||||
use crate::plugins::kcp::{skcp::KcpSocket, KcpConfig};
|
||||
|
||||
pub struct KcpSession {
|
||||
socket: Mutex<KcpSocket>,
|
||||
closed: AtomicBool,
|
||||
session_expire: Duration,
|
||||
session_close_notifier: Option<mpsc::Sender<u32>>,
|
||||
input_tx: mpsc::Sender<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl KcpSession {
|
||||
fn new(
|
||||
socket: KcpSocket,
|
||||
session_expire: Duration,
|
||||
session_close_notifier: Option<mpsc::Sender<u32>>,
|
||||
input_tx: mpsc::Sender<Vec<u8>>,
|
||||
) -> KcpSession {
|
||||
KcpSession {
|
||||
socket: Mutex::new(socket),
|
||||
closed: AtomicBool::new(false),
|
||||
session_expire,
|
||||
session_close_notifier,
|
||||
input_tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_shared(
|
||||
socket: KcpSocket,
|
||||
session_expire: Duration,
|
||||
session_close_notifier: Option<mpsc::Sender<u32>>,
|
||||
) -> Arc<KcpSession> {
|
||||
let is_client = session_close_notifier.is_none();
|
||||
|
||||
let (input_tx, mut input_rx) = mpsc::channel(64);
|
||||
|
||||
let udp_socket = socket.udp_socket().clone();
|
||||
|
||||
let session = Arc::new(KcpSession::new(
|
||||
socket,
|
||||
session_expire,
|
||||
session_close_notifier,
|
||||
input_tx,
|
||||
));
|
||||
|
||||
{
|
||||
let session = session.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut input_buffer = [0u8; 65536];
|
||||
let update_timer = time::sleep(Duration::from_millis(10));
|
||||
tokio::pin!(update_timer);
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
// recv() then input()
|
||||
// Drives the KCP machine forward
|
||||
recv_result = udp_socket.recv(&mut input_buffer), if is_client => {
|
||||
match recv_result {
|
||||
Err(err) => {
|
||||
error!("[SESSION] UDP recv failed, error: {}", err);
|
||||
}
|
||||
Ok(n) => {
|
||||
let input_buffer = &input_buffer[..n];
|
||||
trace!("[SESSION] UDP recv {} bytes, going to input {:?}", n, ByteStr::new(input_buffer));
|
||||
|
||||
let mut socket = session.socket.lock().await;
|
||||
|
||||
match socket.input(input_buffer) {
|
||||
Ok(true) => {
|
||||
trace!("[SESSION] UDP input {} bytes and waked sender/receiver", n);
|
||||
}
|
||||
Ok(false) => {}
|
||||
Err(err) => {
|
||||
error!("[SESSION] UDP input {} bytes error: {}, input buffer {:?}", n, err, ByteStr::new(input_buffer));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// bytes received from listener socket
|
||||
input_opt = input_rx.recv() => {
|
||||
if let Some(input_buffer) = input_opt {
|
||||
let mut socket = session.socket.lock().await;
|
||||
match socket.input(&input_buffer) {
|
||||
Ok(..) => {
|
||||
trace!("[SESSION] UDP input {} bytes from channel {:?}", input_buffer.len(), ByteStr::new(&input_buffer));
|
||||
}
|
||||
Err(err) => {
|
||||
error!("[SESSION] UDP input {} bytes from channel failed, error: {}, input buffer {:?}",
|
||||
input_buffer.len(), err, ByteStr::new(&input_buffer));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Call update() in period
|
||||
_ = &mut update_timer => {
|
||||
let mut socket = session.socket.lock().await;
|
||||
|
||||
let is_closed = session.closed.load(Ordering::Acquire);
|
||||
if is_closed && socket.can_close() {
|
||||
trace!("[SESSION] KCP session closed");
|
||||
break;
|
||||
}
|
||||
|
||||
// server socket expires
|
||||
if !is_client {
|
||||
// If this is a server stream, close it automatically after a period of time
|
||||
let last_update_time = socket.last_update_time();
|
||||
let elapsed = last_update_time.elapsed();
|
||||
|
||||
if elapsed > session.session_expire {
|
||||
if elapsed > session.session_expire * 2 {
|
||||
// Force close. Client may have already gone.
|
||||
trace!(
|
||||
"[SESSION] force close inactive session, conv: {}, last_update: {}s ago",
|
||||
socket.conv(),
|
||||
elapsed.as_secs()
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
if !is_closed {
|
||||
trace!(
|
||||
"[SESSION] closing inactive session, conv: {}, last_update: {}s ago",
|
||||
socket.conv(),
|
||||
elapsed.as_secs()
|
||||
);
|
||||
session.closed.store(true, Ordering::Release);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match socket.update() {
|
||||
Ok(next_next) => {
|
||||
update_timer.as_mut().reset(Instant::from_std(next_next));
|
||||
}
|
||||
Err(err) => {
|
||||
error!("[SESSION] KCP update failed, error: {}", err);
|
||||
update_timer.as_mut().reset(Instant::now() + Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Close the socket.
|
||||
// Wake all pending tasks and let all send/recv return EOF
|
||||
|
||||
let mut socket = session.socket.lock().await;
|
||||
socket.close();
|
||||
}
|
||||
|
||||
if let Some(ref notifier) = session.session_close_notifier {
|
||||
let socket = session.socket.lock().await;
|
||||
let _ = notifier.send(socket.conv()).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
session
|
||||
}
|
||||
|
||||
pub fn kcp_socket(&self) -> &Mutex<KcpSocket> {
|
||||
&self.socket
|
||||
}
|
||||
|
||||
pub fn close(&self) {
|
||||
self.closed.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
pub async fn input(&self, buf: &[u8]) {
|
||||
self.input_tx
|
||||
.send(buf.to_owned())
|
||||
.await
|
||||
.expect("input channel closed")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KcpSessionManager {
|
||||
sessions: HashMap<u32, Arc<KcpSession>>,
|
||||
next_free_conv: u32,
|
||||
}
|
||||
|
||||
impl KcpSessionManager {
|
||||
pub fn new() -> KcpSessionManager {
|
||||
KcpSessionManager {
|
||||
sessions: HashMap::new(),
|
||||
next_free_conv: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close_conv(&mut self, conv: u32) {
|
||||
self.sessions.remove(&conv);
|
||||
}
|
||||
|
||||
pub fn alloc_conv(&mut self) -> u32 {
|
||||
loop {
|
||||
let (mut c, _) = self.next_free_conv.overflowing_add(1);
|
||||
if c == 0 {
|
||||
let (nc, _) = c.overflowing_add(1);
|
||||
c = nc;
|
||||
}
|
||||
self.next_free_conv = c;
|
||||
|
||||
if self.sessions.get(&self.next_free_conv).is_none() {
|
||||
let conv = self.next_free_conv;
|
||||
return conv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_or_create(
|
||||
&mut self,
|
||||
config: &KcpConfig,
|
||||
conv: u32,
|
||||
udp: &Arc<UdpSocket>,
|
||||
peer_addr: SocketAddr,
|
||||
session_close_notifier: &mpsc::Sender<u32>,
|
||||
) -> KcpResult<(Arc<KcpSession>, bool)> {
|
||||
match self.sessions.entry(conv) {
|
||||
Entry::Occupied(occ) => Ok((occ.get().clone(), false)),
|
||||
Entry::Vacant(vac) => {
|
||||
let socket = KcpSocket::new(config, conv, udp.clone(), peer_addr, config.stream)?;
|
||||
let session = KcpSession::new_shared(
|
||||
socket,
|
||||
config.session_expire,
|
||||
Some(session_close_notifier.clone()),
|
||||
);
|
||||
trace!("created session for conv: {}, peer: {}", conv, peer_addr);
|
||||
vac.insert(session.clone());
|
||||
Ok((session, true))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,288 +0,0 @@
|
||||
use std::{
|
||||
io::{self, ErrorKind, Write},
|
||||
net::SocketAddr,
|
||||
sync::Arc,
|
||||
task::{Context, Poll, Waker},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use futures::future;
|
||||
use kcp::{Error as KcpError, Kcp, KcpResult};
|
||||
use log::{error, trace};
|
||||
use tokio::{net::UdpSocket, sync::mpsc};
|
||||
|
||||
use crate::plugins::kcp::{utils::now_millis, KcpConfig};
|
||||
|
||||
/// Writer for sending packets to the underlying UdpSocket
|
||||
struct UdpOutput {
|
||||
socket: Arc<UdpSocket>,
|
||||
target_addr: SocketAddr,
|
||||
delay_tx: mpsc::UnboundedSender<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl UdpOutput {
|
||||
/// Create a new Writer for writing packets to UdpSocket
|
||||
pub fn new(socket: Arc<UdpSocket>, target_addr: SocketAddr) -> UdpOutput {
|
||||
let (delay_tx, mut delay_rx) = mpsc::unbounded_channel::<Vec<u8>>();
|
||||
|
||||
{
|
||||
let socket = socket.clone();
|
||||
tokio::spawn(async move {
|
||||
while let Some(buf) = delay_rx.recv().await {
|
||||
if let Err(err) = socket.send_to(&buf, target_addr).await {
|
||||
error!("[SEND] UDP delayed send failed, error: {}", err);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
UdpOutput {
|
||||
socket,
|
||||
target_addr,
|
||||
delay_tx,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for UdpOutput {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match self.socket.try_send_to(buf, self.target_addr) {
|
||||
Ok(n) => Ok(n),
|
||||
Err(ref err) if err.kind() == ErrorKind::WouldBlock => {
|
||||
// send return EAGAIN
|
||||
// ignored as packet was lost in transmission
|
||||
trace!(
|
||||
"[SEND] UDP send EAGAIN, packet.size: {} bytes, delayed send",
|
||||
buf.len()
|
||||
);
|
||||
|
||||
self.delay_tx
|
||||
.send(buf.to_owned())
|
||||
.expect("channel closed unexpectly");
|
||||
|
||||
Ok(buf.len())
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KcpSocket {
|
||||
kcp: Kcp<UdpOutput>,
|
||||
last_update: Instant,
|
||||
socket: Arc<UdpSocket>,
|
||||
flush_write: bool,
|
||||
flush_ack_input: bool,
|
||||
sent_first: bool,
|
||||
pending_sender: Option<Waker>,
|
||||
pending_receiver: Option<Waker>,
|
||||
closed: bool,
|
||||
}
|
||||
|
||||
impl KcpSocket {
|
||||
pub fn new(
|
||||
c: &KcpConfig,
|
||||
conv: u32,
|
||||
socket: Arc<UdpSocket>,
|
||||
target_addr: SocketAddr,
|
||||
stream: bool,
|
||||
) -> KcpResult<KcpSocket> {
|
||||
let output = UdpOutput::new(socket.clone(), target_addr);
|
||||
let mut kcp = if stream {
|
||||
Kcp::new_stream(conv, output)
|
||||
} else {
|
||||
Kcp::new(conv, output)
|
||||
};
|
||||
c.apply_config(&mut kcp);
|
||||
|
||||
// Ask server to allocate one
|
||||
if conv == 0 {
|
||||
kcp.input_conv();
|
||||
}
|
||||
|
||||
kcp.update(now_millis())?;
|
||||
|
||||
Ok(KcpSocket {
|
||||
kcp,
|
||||
last_update: Instant::now(),
|
||||
socket,
|
||||
flush_write: c.flush_write,
|
||||
flush_ack_input: c.flush_acks_input,
|
||||
sent_first: false,
|
||||
pending_sender: None,
|
||||
pending_receiver: None,
|
||||
closed: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Call every time you got data from transmission
|
||||
pub fn input(&mut self, buf: &[u8]) -> KcpResult<bool> {
|
||||
match self.kcp.input(buf) {
|
||||
Ok(..) => {}
|
||||
Err(KcpError::ConvInconsistent(expected, actual)) => {
|
||||
trace!(
|
||||
"[INPUT] Conv expected={} actual={} ignored",
|
||||
expected,
|
||||
actual
|
||||
);
|
||||
return Ok(false);
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
self.last_update = Instant::now();
|
||||
|
||||
if self.flush_ack_input {
|
||||
self.kcp.flush_ack()?;
|
||||
}
|
||||
|
||||
Ok(self.try_wake_pending_waker())
|
||||
}
|
||||
|
||||
/// Call if you want to send some data
|
||||
pub fn poll_send(&mut self, cx: &mut Context<'_>, mut buf: &[u8]) -> Poll<KcpResult<usize>> {
|
||||
if self.closed {
|
||||
return Ok(0).into();
|
||||
}
|
||||
|
||||
// If:
|
||||
// 1. Have sent the first packet (asking for conv)
|
||||
// 2. Too many pending packets
|
||||
if self.sent_first
|
||||
&& (self.kcp.wait_snd() >= self.kcp.snd_wnd() as usize || self.kcp.waiting_conv())
|
||||
{
|
||||
trace!(
|
||||
"[SEND] waitsnd={} sndwnd={} excceeded or waiting conv={}",
|
||||
self.kcp.wait_snd(),
|
||||
self.kcp.snd_wnd(),
|
||||
self.kcp.waiting_conv()
|
||||
);
|
||||
self.pending_sender = Some(cx.waker().clone());
|
||||
return Poll::Pending;
|
||||
}
|
||||
|
||||
if !self.sent_first && self.kcp.waiting_conv() && buf.len() > self.kcp.mss() as usize {
|
||||
buf = &buf[..self.kcp.mss() as usize];
|
||||
}
|
||||
|
||||
let n = self.kcp.send(buf)?;
|
||||
self.sent_first = true;
|
||||
self.last_update = Instant::now();
|
||||
|
||||
if self.flush_write {
|
||||
self.kcp.flush()?;
|
||||
}
|
||||
|
||||
Ok(n).into()
|
||||
}
|
||||
|
||||
/// Call if you want to send some data
|
||||
#[allow(dead_code)]
|
||||
pub async fn send(&mut self, buf: &[u8]) -> KcpResult<usize> {
|
||||
future::poll_fn(|cx| self.poll_send(cx, buf)).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn try_recv(&mut self, buf: &mut [u8]) -> KcpResult<usize> {
|
||||
if self.closed {
|
||||
return Ok(0);
|
||||
}
|
||||
self.kcp.recv(buf)
|
||||
}
|
||||
|
||||
pub fn poll_recv(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<KcpResult<usize>> {
|
||||
if self.closed {
|
||||
return Ok(0).into();
|
||||
}
|
||||
|
||||
match self.kcp.recv(buf) {
|
||||
Ok(n) => Ok(n).into(),
|
||||
Err(KcpError::RecvQueueEmpty) => {
|
||||
self.pending_receiver = Some(cx.waker().clone());
|
||||
Poll::Pending
|
||||
}
|
||||
Err(err) => Err(err).into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn recv(&mut self, buf: &mut [u8]) -> KcpResult<usize> {
|
||||
future::poll_fn(|cx| self.poll_recv(cx, buf)).await
|
||||
}
|
||||
|
||||
pub fn flush(&mut self) -> KcpResult<()> {
|
||||
self.kcp.flush()?;
|
||||
self.last_update = Instant::now();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn try_wake_pending_waker(&mut self) -> bool {
|
||||
let mut waked = false;
|
||||
|
||||
if self.pending_sender.is_some()
|
||||
&& self.kcp.wait_snd() < self.kcp.snd_wnd() as usize
|
||||
&& !self.kcp.waiting_conv()
|
||||
{
|
||||
let waker = self.pending_sender.take().unwrap();
|
||||
waker.wake();
|
||||
|
||||
waked = true;
|
||||
}
|
||||
|
||||
if self.pending_receiver.is_some() {
|
||||
if let Ok(peek) = self.kcp.peeksize() {
|
||||
if peek > 0 {
|
||||
let waker = self.pending_receiver.take().unwrap();
|
||||
waker.wake();
|
||||
|
||||
waked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
waked
|
||||
}
|
||||
|
||||
pub fn update(&mut self) -> KcpResult<Instant> {
|
||||
let now = now_millis();
|
||||
self.kcp.update(now)?;
|
||||
let next = self.kcp.check(now);
|
||||
|
||||
self.try_wake_pending_waker();
|
||||
|
||||
Ok(Instant::now() + Duration::from_millis(next as u64))
|
||||
}
|
||||
|
||||
pub fn close(&mut self) {
|
||||
self.closed = true;
|
||||
if let Some(w) = self.pending_sender.take() {
|
||||
w.wake();
|
||||
}
|
||||
if let Some(w) = self.pending_receiver.take() {
|
||||
w.wake();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn udp_socket(&self) -> &Arc<UdpSocket> {
|
||||
&self.socket
|
||||
}
|
||||
|
||||
pub fn can_close(&self) -> bool {
|
||||
self.kcp.wait_snd() == 0
|
||||
}
|
||||
|
||||
pub fn conv(&self) -> u32 {
|
||||
self.kcp.conv()
|
||||
}
|
||||
|
||||
pub fn peek_size(&self) -> KcpResult<usize> {
|
||||
self.kcp.peeksize()
|
||||
}
|
||||
|
||||
pub fn last_update_time(&self) -> Instant {
|
||||
self.last_update
|
||||
}
|
||||
}
|
@@ -1,183 +0,0 @@
|
||||
use std::{
|
||||
io::{self, ErrorKind},
|
||||
net::{IpAddr, SocketAddr},
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use futures::{future, ready};
|
||||
use kcp::{Error as KcpError, KcpResult};
|
||||
use log::trace;
|
||||
use tokio::{
|
||||
io::{AsyncRead, AsyncWrite, ReadBuf},
|
||||
net::UdpSocket,
|
||||
};
|
||||
|
||||
use crate::plugins::kcp::{config::KcpConfig, session::KcpSession, skcp::KcpSocket};
|
||||
|
||||
pub struct KcpStream {
|
||||
session: Arc<KcpSession>,
|
||||
recv_buffer: Vec<u8>,
|
||||
recv_buffer_pos: usize,
|
||||
recv_buffer_cap: usize,
|
||||
}
|
||||
|
||||
impl Drop for KcpStream {
|
||||
fn drop(&mut self) {
|
||||
self.session.close();
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
impl KcpStream {
|
||||
pub async fn connect(config: &KcpConfig, addr: SocketAddr) -> KcpResult<KcpStream> {
|
||||
let udp = match addr.ip() {
|
||||
IpAddr::V4(..) => UdpSocket::bind("0.0.0.0:0").await?,
|
||||
IpAddr::V6(..) => UdpSocket::bind("[::]:0").await?,
|
||||
};
|
||||
|
||||
let udp = Arc::new(udp);
|
||||
let socket = KcpSocket::new(config, 0, udp, addr, config.stream)?;
|
||||
|
||||
let session = KcpSession::new_shared(socket, config.session_expire, None);
|
||||
|
||||
Ok(KcpStream::with_session(session))
|
||||
}
|
||||
|
||||
pub(crate) fn with_session(session: Arc<KcpSession>) -> KcpStream {
|
||||
KcpStream {
|
||||
session,
|
||||
recv_buffer: Vec::new(),
|
||||
recv_buffer_pos: 0,
|
||||
recv_buffer_cap: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_send(&mut self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<KcpResult<usize>> {
|
||||
// Mutex doesn't have poll_lock, spinning on it.
|
||||
let socket = self.session.kcp_socket();
|
||||
let mut kcp = match socket.try_lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(..) => {
|
||||
cx.waker().wake_by_ref();
|
||||
return Poll::Pending;
|
||||
}
|
||||
};
|
||||
|
||||
kcp.poll_send(cx, buf)
|
||||
}
|
||||
|
||||
pub async fn send(&mut self, buf: &[u8]) -> KcpResult<usize> {
|
||||
future::poll_fn(|cx| self.poll_send(cx, buf)).await
|
||||
}
|
||||
|
||||
pub fn poll_recv(&mut self, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<KcpResult<usize>> {
|
||||
loop {
|
||||
// Consumes all data in buffer
|
||||
if self.recv_buffer_pos < self.recv_buffer_cap {
|
||||
let remaining = self.recv_buffer_cap - self.recv_buffer_pos;
|
||||
let copy_length = remaining.min(buf.len());
|
||||
|
||||
buf.copy_from_slice(
|
||||
&self.recv_buffer[self.recv_buffer_pos..self.recv_buffer_pos + copy_length],
|
||||
);
|
||||
self.recv_buffer_pos += copy_length;
|
||||
return Ok(copy_length).into();
|
||||
}
|
||||
|
||||
// Mutex doesn't have poll_lock, spinning on it.
|
||||
let socket = self.session.kcp_socket();
|
||||
let mut kcp = match socket.try_lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(..) => {
|
||||
cx.waker().wake_by_ref();
|
||||
return Poll::Pending;
|
||||
}
|
||||
};
|
||||
|
||||
// Try to read from KCP
|
||||
// 1. Read directly with user provided `buf`
|
||||
match ready!(kcp.poll_recv(cx, buf)) {
|
||||
Ok(n) => {
|
||||
trace!("[CLIENT] recv directly {} bytes", n);
|
||||
return Ok(n).into();
|
||||
}
|
||||
Err(KcpError::UserBufTooSmall) => {}
|
||||
Err(err) => return Err(err).into(),
|
||||
}
|
||||
|
||||
// 2. User `buf` too small, read to recv_buffer
|
||||
let required_size = kcp.peek_size()?;
|
||||
if self.recv_buffer.len() < required_size {
|
||||
self.recv_buffer.resize(required_size, 0);
|
||||
}
|
||||
|
||||
match ready!(kcp.poll_recv(cx, &mut self.recv_buffer)) {
|
||||
Ok(n) => {
|
||||
trace!("[CLIENT] recv buffered {} bytes", n);
|
||||
self.recv_buffer_pos = 0;
|
||||
self.recv_buffer_cap = n;
|
||||
}
|
||||
Err(err) => return Err(err).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn recv(&mut self, buf: &mut [u8]) -> KcpResult<usize> {
|
||||
future::poll_fn(|cx| self.poll_recv(cx, buf)).await
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for KcpStream {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<io::Result<()>> {
|
||||
match ready!(self.poll_recv(cx, buf.initialize_unfilled())) {
|
||||
Ok(n) => {
|
||||
buf.advance(n);
|
||||
Ok(()).into()
|
||||
}
|
||||
Err(KcpError::IoError(err)) => Err(err).into(),
|
||||
Err(err) => Err(io::Error::new(ErrorKind::Other, err)).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for KcpStream {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
match ready!(self.poll_send(cx, buf)) {
|
||||
Ok(n) => Ok(n).into(),
|
||||
Err(KcpError::IoError(err)) => Err(err).into(),
|
||||
Err(err) => Err(io::Error::new(ErrorKind::Other, err)).into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
// Mutex doesn't have poll_lock, spinning on it.
|
||||
let socket = self.session.kcp_socket();
|
||||
let mut kcp = match socket.try_lock() {
|
||||
Ok(guard) => guard,
|
||||
Err(..) => {
|
||||
cx.waker().wake_by_ref();
|
||||
return Poll::Pending;
|
||||
}
|
||||
};
|
||||
|
||||
match kcp.flush() {
|
||||
Ok(..) => Ok(()).into(),
|
||||
Err(KcpError::IoError(err)) => Err(err).into(),
|
||||
Err(err) => Err(io::Error::new(ErrorKind::Other, err)).into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||
Ok(()).into()
|
||||
}
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[inline]
|
||||
pub fn now_millis() -> u32 {
|
||||
let start = SystemTime::now();
|
||||
let since_the_epoch = start
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("time went afterwards");
|
||||
(since_the_epoch.as_secs() * 1000 + since_the_epoch.subsec_millis() as u64 / 1_000_000) as u32
|
||||
}
|
@@ -1 +0,0 @@
|
||||
//pub mod kcp;
|
@@ -90,12 +90,6 @@ impl Server {
|
||||
error!("Failed to start {}: {}", config.name, res.err().unwrap());
|
||||
}
|
||||
}
|
||||
// "kcp" => {
|
||||
// let res = kcp::proxy(config.clone()).await;
|
||||
// if res.is_err() {
|
||||
// error!("Failed to start {}: {}", config.name, res.err().unwrap());
|
||||
// }
|
||||
// }
|
||||
_ => {
|
||||
error!("Invalid protocol: {}", config.protocol)
|
||||
}
|
||||
@@ -113,7 +107,6 @@ impl Server {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
//use crate::plugins::kcp::{KcpConfig, KcpStream};
|
||||
use std::thread::{self, sleep};
|
||||
use std::time::Duration;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
@@ -176,27 +169,5 @@ mod tests {
|
||||
assert_eq!(&buf, &[i]);
|
||||
}
|
||||
conn.shutdown().await.unwrap();
|
||||
|
||||
// test KCP echo
|
||||
// let kcp_config = KcpConfig::default();
|
||||
// let server_addr: SocketAddr = "127.0.0.1:54959".parse().unwrap();
|
||||
// let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap();
|
||||
// let mut buf = [0u8; 1];
|
||||
// for i in 0..=10u8 {
|
||||
// conn.write(&[i]).await.unwrap();
|
||||
// conn.read(&mut buf).await.unwrap();
|
||||
// assert_eq!(&buf, &[i]);
|
||||
// }
|
||||
// conn.shutdown().await.unwrap();
|
||||
//
|
||||
// // test KCP proxy and close mock server
|
||||
// let kcp_config = KcpConfig::default();
|
||||
// let server_addr: SocketAddr = "127.0.0.1:54958".parse().unwrap();
|
||||
// let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap();
|
||||
// let mut buf = [0u8; 5];
|
||||
// conn.write(b"by").await.unwrap();
|
||||
// conn.read(&mut buf).await.unwrap();
|
||||
// assert_eq!(&buf, b"hello");
|
||||
// conn.shutdown().await.unwrap();
|
||||
}
|
||||
}
|
||||
|
@@ -1,98 +0,0 @@
|
||||
use crate::config::Upstream;
|
||||
use crate::plugins::kcp::{KcpConfig, KcpListener, KcpStream};
|
||||
use crate::servers::{copy, Proxy};
|
||||
use futures::future::try_join;
|
||||
use log::{debug, error, warn};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use tokio::io;
|
||||
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
pub async fn proxy(config: Arc<Proxy>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let kcp_config = KcpConfig::default();
|
||||
let mut listener = KcpListener::bind(kcp_config, config.listen).await?;
|
||||
let config = config.clone();
|
||||
|
||||
loop {
|
||||
let thread_proxy = config.clone();
|
||||
match listener.accept().await {
|
||||
Err(err) => {
|
||||
error!("Failed to accept connection: {}", err);
|
||||
return Err(Box::new(err));
|
||||
}
|
||||
Ok((stream, peer)) => {
|
||||
tokio::spawn(async move {
|
||||
match accept(stream, peer, thread_proxy).await {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
error!("Relay thread returned an error: {}", err);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn accept(
|
||||
inbound: KcpStream,
|
||||
peer: SocketAddr,
|
||||
proxy: Arc<Proxy>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
debug!("New connection from {:?}", peer);
|
||||
|
||||
let upstream_name = proxy.default_action.clone();
|
||||
|
||||
debug!("Upstream: {}", upstream_name);
|
||||
|
||||
let upstream = match proxy.upstream.get(&upstream_name) {
|
||||
Some(upstream) => upstream,
|
||||
None => {
|
||||
warn!(
|
||||
"No upstream named {:?} on server {:?}",
|
||||
proxy.default, proxy.name
|
||||
);
|
||||
return process(inbound, proxy.upstream.get(&proxy.default_action).unwrap()).await;
|
||||
// ToDo: Remove unwrap and check default option
|
||||
}
|
||||
};
|
||||
return process(inbound, upstream).await;
|
||||
}
|
||||
|
||||
async fn process(
|
||||
mut inbound: KcpStream,
|
||||
upstream: &Upstream,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
match upstream {
|
||||
Upstream::Ban => {
|
||||
let _ = inbound.shutdown();
|
||||
}
|
||||
Upstream::Echo => {
|
||||
let (mut ri, mut wi) = io::split(inbound);
|
||||
let inbound_to_inbound = copy(&mut ri, &mut wi);
|
||||
let bytes_tx = inbound_to_inbound.await;
|
||||
debug!("Bytes read: {:?}", bytes_tx);
|
||||
}
|
||||
Upstream::Custom(custom) => match custom.protocol.as_ref() {
|
||||
"tcp" => {
|
||||
let outbound = TcpStream::connect(custom.addr.clone()).await?;
|
||||
|
||||
let (mut ri, mut wi) = io::split(inbound);
|
||||
let (mut ro, mut wo) = io::split(outbound);
|
||||
|
||||
let inbound_to_outbound = copy(&mut ri, &mut wo);
|
||||
let outbound_to_inbound = copy(&mut ro, &mut wi);
|
||||
|
||||
let (bytes_tx, bytes_rx) =
|
||||
try_join(inbound_to_outbound, outbound_to_inbound).await?;
|
||||
|
||||
debug!("Bytes read: {:?} write: {:?}", bytes_tx, bytes_rx);
|
||||
}
|
||||
_ => {
|
||||
error!("Reached unknown protocol: {:?}", custom.protocol);
|
||||
}
|
||||
},
|
||||
};
|
||||
Ok(())
|
||||
}
|
@@ -1,3 +1,2 @@
|
||||
//pub mod kcp;
|
||||
pub mod tcp;
|
||||
pub mod tls;
|
||||
|
@@ -2,14 +2,16 @@ use log::debug;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io::Result;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
use time::{Duration, Instant, OffsetDateTime};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct UpstreamAddress {
|
||||
address: String,
|
||||
resolved_addresses: Vec<SocketAddr>,
|
||||
resolved_time: Option<Instant>,
|
||||
ttl: Option<Duration>,
|
||||
resolved_addresses: Arc<RwLock<Vec<SocketAddr>>>,
|
||||
resolved_time: Arc<RwLock<Option<Instant>>>,
|
||||
ttl: Arc<RwLock<Option<Duration>>>,
|
||||
}
|
||||
|
||||
impl Display for UpstreamAddress {
|
||||
@@ -27,8 +29,10 @@ impl UpstreamAddress {
|
||||
}
|
||||
|
||||
pub fn is_valid(&self) -> bool {
|
||||
if let Some(resolved) = self.resolved_time {
|
||||
if let Some(ttl) = self.ttl {
|
||||
let r = { *self.resolved_time.read().unwrap() };
|
||||
|
||||
if let Some(resolved) = r {
|
||||
if let Some(ttl) = { *self.ttl.read().unwrap() } {
|
||||
return resolved.elapsed() < ttl;
|
||||
}
|
||||
}
|
||||
@@ -37,7 +41,7 @@ impl UpstreamAddress {
|
||||
}
|
||||
|
||||
fn is_resolved(&self) -> bool {
|
||||
!self.resolved_addresses.is_empty()
|
||||
!self.resolved_addresses.read().unwrap().is_empty()
|
||||
}
|
||||
|
||||
fn time_remaining(&self) -> Duration {
|
||||
@@ -45,17 +49,19 @@ impl UpstreamAddress {
|
||||
return Duration::seconds(0);
|
||||
}
|
||||
|
||||
self.ttl.unwrap() - self.resolved_time.unwrap().elapsed()
|
||||
let rt = { *self.resolved_time.read().unwrap() };
|
||||
let ttl = { *self.ttl.read().unwrap() };
|
||||
ttl.unwrap() - rt.unwrap().elapsed()
|
||||
}
|
||||
|
||||
pub async fn resolve(&mut self, mode: ResolutionMode) -> Result<Vec<SocketAddr>> {
|
||||
pub async fn resolve(&self, mode: ResolutionMode) -> Result<Vec<SocketAddr>> {
|
||||
if self.is_resolved() && self.is_valid() {
|
||||
debug!(
|
||||
"Already got address {:?}, still valid for {:.3}s",
|
||||
&self.resolved_addresses,
|
||||
self.time_remaining().as_seconds_f64()
|
||||
);
|
||||
return Ok(self.resolved_addresses.clone());
|
||||
return Ok(self.resolved_addresses.read().unwrap().clone());
|
||||
}
|
||||
|
||||
debug!(
|
||||
@@ -70,8 +76,8 @@ impl UpstreamAddress {
|
||||
Err(e) => {
|
||||
debug!("Failed looking up {}: {}", &self.address, &e);
|
||||
// Protect against DNS flooding. Cache the result for 1 second.
|
||||
self.resolved_time = Some(Instant::now());
|
||||
self.ttl = Some(Duration::seconds(3));
|
||||
*self.resolved_time.write().unwrap() = Some(Instant::now());
|
||||
*self.ttl.write().unwrap() = Some(Duration::seconds(3));
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
@@ -103,11 +109,11 @@ impl UpstreamAddress {
|
||||
.expect("Format")
|
||||
);
|
||||
|
||||
self.resolved_addresses = addresses;
|
||||
self.resolved_time = Some(Instant::now());
|
||||
self.ttl = Some(Duration::minutes(1));
|
||||
*self.resolved_addresses.write().unwrap() = addresses.clone();
|
||||
*self.resolved_time.write().unwrap() = Some(Instant::now());
|
||||
*self.ttl.write().unwrap() = Some(Duration::minutes(1));
|
||||
|
||||
Ok(self.resolved_addresses.clone())
|
||||
Ok(addresses)
|
||||
}
|
||||
}
|
||||
|
||||
|
22
src/update.rs
Normal file
22
src/update.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use self_update::{cargo_crate_version, version};
|
||||
|
||||
pub(crate) fn update() {
|
||||
println!("Updating to the latest version...");
|
||||
|
||||
let backend = self_update::backends::gitea::Update::configure()
|
||||
.with_host("https://code.kiers.eu")
|
||||
.repo_owner("jjkiers")
|
||||
.repo_name("layer4-proxy")
|
||||
.bin_name("l4p")
|
||||
.show_download_progress(true)
|
||||
.current_version(cargo_crate_version!())
|
||||
.build()
|
||||
.expect("Should initialize correctly.");
|
||||
|
||||
let status = backend.update_extended();
|
||||
|
||||
match status {
|
||||
Err(e) => eprintln!("Error updating: {e}"),
|
||||
Ok(_) => (),
|
||||
}
|
||||
}
|
@@ -7,36 +7,25 @@ use serde::Deserialize;
|
||||
use std::net::SocketAddr;
|
||||
use tokio::io;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Addr(Mutex<UpstreamAddress>);
|
||||
|
||||
impl Clone for Addr {
|
||||
fn clone(&self) -> Self {
|
||||
tokio::task::block_in_place(|| Self(Mutex::new(self.0.blocking_lock().clone())))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
pub struct ProxyToUpstream {
|
||||
pub addr: String,
|
||||
pub protocol: String,
|
||||
#[serde(skip_deserializing)]
|
||||
addresses: Addr,
|
||||
addresses: UpstreamAddress,
|
||||
}
|
||||
|
||||
impl ProxyToUpstream {
|
||||
pub async fn resolve_addresses(&self) -> std::io::Result<Vec<SocketAddr>> {
|
||||
let mut addr = self.addresses.0.lock().await;
|
||||
addr.resolve((*self.protocol).into()).await
|
||||
self.addresses.resolve((*self.protocol).into()).await
|
||||
}
|
||||
|
||||
pub fn new(address: String, protocol: String) -> Self {
|
||||
Self {
|
||||
addr: address.clone(),
|
||||
protocol,
|
||||
addresses: Addr(Mutex::new(UpstreamAddress::new(address))),
|
||||
addresses: UpstreamAddress::new(address),
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -19,16 +19,6 @@ servers:
|
||||
listen:
|
||||
- "0.0.0.0:54956"
|
||||
default: echo
|
||||
kcp_server:
|
||||
protocol: kcp
|
||||
listen:
|
||||
- "127.0.0.1:54958"
|
||||
default: tester
|
||||
kcp_echo_server:
|
||||
protocol: kcp
|
||||
listen:
|
||||
- "127.0.0.1:54959"
|
||||
default: echo
|
||||
|
||||
upstream:
|
||||
web: "tcp://127.0.0.1:8080"
|
||||
|
Reference in New Issue
Block a user