Skip to content

Commit 396f526

Browse files
committed
tcp-relay
1 parent 9589dbf commit 396f526

File tree

8 files changed

+786
-0
lines changed

8 files changed

+786
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ install_manifest.txt
99
compile_commands.json
1010
CTestTestfile.cmake
1111
_deps
12+
.vscode

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "third_party/asio"]
2+
path = third_party/asio
3+
url = https://github.com/chriskohlhoff/asio.git
4+
[submodule "third_party/fmt"]
5+
path = third_party/fmt
6+
url = https://github.com/fmtlib/fmt.git

CMakeLists.txt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
project(tcp-relay)
3+
4+
option(USE_STD_FORMAT "Use std::format instead of fmt::format" OFF)
5+
6+
set(CMAKE_CXX_STANDARD 20)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
if(NOT CMAKE_BUILD_TYPE)
10+
set(CMAKE_BUILD_TYPE Release)
11+
endif()
12+
13+
add_definitions(-DASIO_STANDALONE)
14+
15+
set(ASIO_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/asio/asio/include)
16+
17+
include_directories(${ASIO_INCLUDE_DIR})
18+
19+
if (USE_STD_FORMAT)
20+
add_definitions(-DUSE_STD_FORMAT)
21+
else()
22+
add_definitions(-DFMT_HEADER_ONLY=1)
23+
set(FMT_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/fmt/include)
24+
include_directories(${FMT_INCLUDE_DIR})
25+
endif()
26+
27+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
28+
if (CMAKE_CXX_COMPILER MATCHES ".*musl\\-g\\+\\+.*")
29+
# fix openwrt build issue
30+
add_definitions(-DASIO_HAS_CO_AWAIT=1)
31+
add_definitions(-DASIO_HAS_STD_COROUTINE=1)
32+
endif()
33+
endif()
34+
35+
add_executable(tcp-relay src/tcp_relay.cpp)
36+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
37+
target_compile_options(tcp-relay PRIVATE -fcoroutines)
38+
endif()

Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM alpine:3.18 as builder
2+
3+
RUN apk update \
4+
&& apk add alpine-sdk cmake linux-headers
5+
6+
WORKDIR /ahp
7+
8+
COPY . .
9+
10+
RUN cmake -B build -DCMAKE_BUILD_TYPE=Release \
11+
&& cmake --build build
12+
13+
FROM alpine:3.18
14+
15+
RUN apk update && apk add libgcc libstdc++
16+
17+
COPY --from=builder /ahp/build/tcp-relay /usr/local/bin/tcp-relay

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# TCP-Relay
2+
[![License: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
3+
4+
A high-performance and easy-to-use TCP-Relay write in C++20 based on [AISO](https://think-async.com/Asio/) and [Coroutines](https://en.cppreference.com/w/cpp/language/coroutines). In addition to basic TCP relay, it also supports relaying through intermediate proxies (such as HTTP-Proxy).
5+
6+
# Getting Started
7+
## Build from source
8+
This project relies on some C++20 features. The following compilers and their respective versions are supported:
9+
- GCC >= 12.0
10+
- Clang >= 14.0
11+
- Microsoft Visual Studio (MSVC) >= 2019
12+
13+
``` bash
14+
# clone
15+
git clone --recursive https://github.com/lxrite/tcp-relay.git
16+
cd tcp-relay
17+
18+
# build
19+
cmake -B build -DCMAKE_BUILD_TYPE=Release
20+
cmake --build build
21+
```
22+
23+
## Usage
24+
``` bash
25+
$ ./tcp-relay --help
26+
Usage: tcp-relay [options]
27+
28+
options:
29+
-h, --help Show this help message and exit
30+
-v, --version Print the program version and exit
31+
-l, --listen_addr string Local address to listen on (default: 0.0.0.0)
32+
-p, --port number Local port to listen on (default: 8886)
33+
-t, --target string Taget address (host:port) to connect
34+
--timeout number Connection timeout (in seconds) (default: 240)
35+
--via [none | http_proxy] Transfer via other proxy (default: none)
36+
--http_proxy string HTTP-Proxy address (host:port)
37+
--log_level string [trace | debug | info | warn | error | disable] Log level (default: info)
38+
```
39+
40+
## Examples
41+
``` bash
42+
# basic
43+
./tcp-relay -t 172.16.1.1:8080
44+
45+
# IPv6
46+
./tcp-relay -t [fd12:3456:789a:bcde::1]:8080
47+
48+
# domain
49+
./tcp-relay -t example.com:8080
50+
51+
# relay through HTTP intermediate proxy
52+
./tcp-relay -t example.com:8080 --via http_proxy --http_proxy proxy.example.com:1234
53+
```
54+
55+
## Using Docker
56+
``` bash
57+
# build from local Dockerfile
58+
docker build -t lxrite/tcp-relay .
59+
# build from URL
60+
docker build -t lxrite/tcp-relay https://github.com/lxrite/tcp-relay.git
61+
62+
# run
63+
docker run -d -p 8886:8886 lxrite/tcp-relay tcp-relay -t 172.16.1.1:8080
64+
```

0 commit comments

Comments
 (0)