{"id":20279553,"url":"https://github.com/xukeawsl/socks-server","last_synced_at":"2026-03-12T11:38:03.550Z","repository":{"id":62300337,"uuid":"547267609","full_name":"xukeawsl/socks-server","owner":"xukeawsl","description":"A C++11 socks5 proxy server based on asio network library","archived":false,"fork":false,"pushed_at":"2025-01-22T12:44:39.000Z","size":10075,"stargazers_count":63,"open_issues_count":0,"forks_count":15,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T05:09:28.741Z","etag":null,"topics":["asio","cpp11","linux-server","socks5-proxy","spdlog","windows-server"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xukeawsl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-07T12:02:51.000Z","updated_at":"2025-04-07T07:04:29.000Z","dependencies_parsed_at":"2025-01-18T15:24:39.649Z","dependency_job_id":"0e65318d-0512-4cdb-9b55-ae7c2275a792","html_url":"https://github.com/xukeawsl/socks-server","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fsocks-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fsocks-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fsocks-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xukeawsl%2Fsocks-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xukeawsl","download_url":"https://codeload.github.com/xukeawsl/socks-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161273,"owners_count":21057555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asio","cpp11","linux-server","socks5-proxy","spdlog","windows-server"],"created_at":"2024-11-14T13:31:24.469Z","updated_at":"2026-03-12T11:37:58.473Z","avatar_url":"https://github.com/xukeawsl.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Socks5 代理服务器\n\n[![License](https://img.shields.io/npm/l/mithril.svg)](https://github.com/xukeawsl/socks-server/blob/master/LICENSE)\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d9a5e55fbad64d51886964bf0f9977c9)](https://app.codacy.com/gh/xukeawsl/socks-server/dashboard?utm_source=gh\u0026utm_medium=referral\u0026utm_content=\u0026utm_campaign=Badge_grade)\n[![Stars](https://img.shields.io/github/stars/xukeawsl/socks-server)](https://github.com/xukeawsl/socks-server)\n[![Release](https://img.shields.io/github/v/release/xukeawsl/socks-server?color=red)](https://github.com/xukeawsl/socks-server/releases)\n[![RepoSize](https://img.shields.io/github/repo-size/xukeawsl/socks-server?color=yellow)](https://img.shields.io/github/repo-size/xukeawsl/socks-server?color=yellow)\n[![Build status](https://ci.appveyor.com/api/projects/status/iwkperf2bb9xiw0v?svg=true)](https://ci.appveyor.com/project/xukeawsl/socks-server)\n\n## 平台\n* Linux (g++ 4.8+)\n* Windows (mingw)\n\n## 特性\n* 无认证模式\n* 用户名/密码认证模式\n* 支持 `CONNECTION` 和 `UDP ASSOCIATE` 命令\n* 支持通过 `IPV4(6)/域名` 访问远程机器\n\n## 优点\n* **高性能**, 采用多线程 + 异步 IO 模型\n* **可靠性强**, 良好的异常处理，无内存泄漏，日志详细\n* **代码结构清晰**, 可读性强\n\n## 使用\n### 1. 下载仓库并创建构建目录\n```bash\n$ git clone https://github.com/xukeawsl/socks-server.git\n$ cd socks-server\n$ mkdir build\n$ cd build\n```\n\n### 2. CMake 构建\n* Linux\n```bash\ncmake ..\ncmake --build .\n```\n\n*  Windows\n```bash\ncmake -G \"MinGW Makfiles\" ..\ncmake --build .\n```\n默认的构建类型是 `Debug`, 可以通过 `-DBUILD_TYPE=Release` 指定构建类型为 `Release`\n\n## 调整服务器日志级别\n* 通过 `cmake` 的 `LOG_LEVEL` 选项调整日志等级, 支持 `spdlog` 的日志级别\n* `Debug` 默认的日志级别是 `Debug`, 且日志同时输出到文件和控制台\n* `Release` 默认的日志级别是 `Info`, 日志只输出到文件中\n```bash\n# Trace, Debug, Info, Warn, Error, Critical, Off\ncmake -DLOG_LEVEL=Info ..\n```\n\n## 设置安装目录\n```bash\n# Linux\ncmake -DCMAKE_PREFIX=/usr/local ..\n\n# Windows\ncmake -G \"MinGW Makefiles\" -DCMAKE_PREFIX=D:/ ..\n```\n\n## 安装\n```bash\n# 使用之前设置的路径安装\ncmake --build . --target install\n# 或者\ncmake --install .\n\n# 安装时指定路径\ncmake --install . --prefix /usr/local\n```\n\n## 配置服务器参数\n* 通过修改 `config.json` 文件内容进行服务器参数配置\n```json\n{\n    \"server\" : {\n        \"host\" : \"127.0.0.1\",\n        \"port\" : 1080\n    },\n    \"log\" : {\n        \"log_file\" : \"logs/server.log\",\n        \"max_rotate_size\" : 1048576,\n        \"max_rotate_count\" : 10\n    },\n    \"auth\" : {\n        \"username\" : \"socks-user\",\n        \"password\" : \"socks-passwd\"\n    },\n    \"supported-methods\" : [0, 2],\n    \"timeout\" : 60\n}\n```\n\n1. `server` 配置服务器相关参数\n   * `host` : 监听的 ip 地址 (默认 `127.0.0.1`，ipv6 可以监听 `::`)\n   * `port` : 监听的端口号 (默认 `1080`)\n   * `thread_num` : 后台工作线程个数 (默认为 cpu 核心数)\n\n2. `log` 配置日志文件相关参数\n   * `log_file` : 日志文件的路径 (相对路径是基于构建目录的，默认为 `logs/server.log`)\n   * `max_rotate_size` : 单个滚动日志文件的最大大小 (默认为 `1` MB)\n   * `max_rotate_count` : 最大滚动日志文件个数 (默认 `10` 个)\n\n3. `auth` 配置代理服务器认证的用户名/密码\n   * `username` : 用户名(需要认证则必填)\n   * `password` : 密码(需要认证则必填)\n\n4. `supported-methods` 配置代理服务器支持的认证方法\n   * `0` : 不需要认证\n   * `2` : 需要用户名/密码认证\n5. `timeout` 配置连接的超时时间 (默认为 `10` 分钟，单位为 `s`)\n\n## docker-compose 部署\n* 在 `docker-compose.yml` 所在目录下执行如下命令即可在后台自动部署服务\n```bash\ndocker-compose up -d\n```\n\n## Valgrind 内存检测\n* 检测程序是否存在内存泄漏：`valgrind --leck-check=full ../bin/socks-server`\n```valgrind\n==38396== Memcheck, a memory error detector\n==38396== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.\n==38396== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info\n==38396== Command: ../bin/socks_server\n==38396== Parent PID: 38395\n==38396== \n==38396== \n==38396== HEAP SUMMARY:\n==38396==     in use at exit: 0 bytes in 0 blocks\n==38396==   total heap usage: 256,650 allocs, 256,650 frees, 184,504,189 bytes allocated\n==38396== \n==38396== All heap blocks were freed -- no leaks are possible\n==38396== \n==38396== For lists of detected and suppressed errors, rerun with: -s\n==38396== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)\n```\n\n## Benchmark 压力测试\n* 测试机器 : `AMD EPYC 7K62 48-Core @ 2.6 GHz`\n* 测试工具使用 https://github.com/cnlh/benchmark\n* 使用 cinatra 库在本地搭建 ping-pong 测试服务器 https://github.com/qicosmos/cinatra\n```cpp\n#include \"cinatra.hpp\"\nusing namespace cinatra;\n\nint main() {\n    http_server server(std::thread::hardware_concurrency());\n    server.listen(\"127.0.0.1\", \"80\");\n\n    server.set_http_handler\u003cGET, POST\u003e(\"/ping\", [](request\u0026 req, response\u0026 res) {\n\t\tres.set_status_and_content(status_type::ok, \"pong\");\n    });\n\n    server.run();\n    return 0;\n}\n```\n* 添加本地域名解析 `vim /etc/hosts`\n```bash\n127.0.0.1 www.test.com\n```\n\n* 测试结果 : `QPS 6w+`\n```bash\n# 为了对比,同时测试了不通过 socks5 代理的 qps\n\n# 单核 10w 次请求\n# -------------------------------\n# Requests/sec: 58744.86\n./benchmark -n 100000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 81741.00\n./benchmark -n 100000 http://www.test.com/ping\n# -------------------------------\n\n# 单核 100w 次请求\n# -------------------------------\n# Requests/sec: 64033.18\n./benchmark -n 1000000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 85083.83\n./benchmark -n 1000000 http://www.test.com/ping\n# -------------------------------\n\n# 多核 100 并发连接 10w 请求\n# -------------------------------\n# Requests/sec: 70531.02\n./benchmark -c 100 -n 100000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 127494.87\n./benchmark -c 100 -n 100000 http://www.test.com/ping\n# -------------------------------\n\n# 多核 1k 并发连接 10w 请求\n# -------------------------------\n# Requests/sec: 60594.40\n./benchmark -c 1000 -n 100000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 81868.17\n./benchmark -c 1000 -n 100000  http://www.test.com/ping\n# -------------------------------\n\n# 多核 1w 并发连接 10w 请求\n# -------------------------------\n# Requests/sec: 36597.17\n./benchmark -c 10000 -n 100000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 61744.69\n./benchmark -c 10000 -n 100000 http://www.test.com/ping\n# -------------------------------\n\n# 多核 100 并发连接 100w 次请求\n# -------------------------------\n# Requests/sec: 64534.48\n./benchmark -c 100 -n 1000000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 105114.19\n./benchmark -c 100 -n 1000000 http://www.test.com/ping\n# -------------------------------\n\n# 多核 1k 并发连接 100w 次请求\n# -------------------------------\n# Requests/sec: 64263.99\n./benchmark -c 1000 -n 1000000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 87528.75\n./benchmark -c 1000 -n 1000000 http://www.test.com/ping\n# -------------------------------\n\n# 多核 1w 并发连接 100w 次请求\n# -------------------------------\n# Requests/sec: 51305.73\n./benchmark -c 10000 -n 1000000 -proxy socks5://127.0.0.1:1080 http://www.test.com/ping\n# Requests/sec: 75776.02\n./benchmark -c 10000 -n 1000000 http://www.test.com/ping\n# -------------------------------\n```\n\n## FlameGraph 火焰图分析\n* `ps -ef | grep socks_server` 查看 socks_server 进程的 PID (假设为 `779810`)\n* 安装好 `perf` 工具并克隆 [FlameGraph](https://github.com/brendangregg/FlameGraph) 仓库到机器上\n* 进入 `FlameGraph` 目录，依次执行以下命令\n```bash\n# 以 999Hz 的频率对进程 779810 采样 60s\nperf record -F 999 -p 779810 -g -- sleep 60\nperf script \u003e out.perf\n\n# 折叠调用栈\n./stackcollapse-perf.pl out.perf \u003e out.folded\n\n# 生成火焰图\n./flamegraph.pl out.folded \u003e socks_server.svg\n```\n![socks_server.png](https://s2.loli.net/2023/05/28/yk8NHcIVhb1ur3z.png)\n\n## 水平扩展\n* 将 `nginx` 作为 tcp 负载均衡器, 可以实现水平拓展, `nginx` 对外监听 `1080` 端口, 然后根据指定的负载均衡策略转发到相应机器上, 也可以是同一台机器上的不同端口(实现多进程), 如下配置, 负载均衡策略是优先转发到连接数最少的节点, `5000` 作为备用节点, 当 `3000` 和 `4000` 都挂掉时启用\n```conf\nstream {\n    upstream socks_server {\n        least_conn;\n        server 127.0.0.1:3000;\n        server 127.0.0.1:4000;\n        server 127.0.0.1:5000 backup;\n    }\n\n    server {\n        listen 1080;\n        proxy_pass socks_server;\n    }\n}\n```\n\n\n## 参考文档\n* [RFC1928 : SOCKS Protocol Version 5](https://www.rfc-editor.org/rfc/inline-errata/rfc1928.html)\n* [RFC1929 : Username/Password Authentication for SOCKS V5](https://www.rfc-editor.org/rfc/rfc1929.html)\n\n## 其它实现\n\n基于多进程+协程模型实现的版本（限制C++20和Linux平台）：https://github.com/xukeawsl/coro_socks\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxukeawsl%2Fsocks-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxukeawsl%2Fsocks-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxukeawsl%2Fsocks-server/lists"}