{"id":19751522,"url":"https://github.com/zanjie1999/tcp-over-websocket","last_synced_at":"2025-04-07T11:08:45.712Z","repository":{"id":46261123,"uuid":"362903244","full_name":"zanjie1999/tcp-over-websocket","owner":"zanjie1999","description":"将TCP/UDP放到WebSocket中传输（就能走CDN）基于ws的内网穿透 隧道代理 tcp2ws  全平台0依赖","archived":false,"fork":false,"pushed_at":"2023-10-12T08:21:38.000Z","size":1396,"stargazers_count":302,"open_issues_count":1,"forks_count":55,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-31T10:04:16.666Z","etag":null,"topics":["go","proxy","proxy-client","proxy-server","tcp","tcp2ws","websocket"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zanjie1999.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-04-29T17:57:07.000Z","updated_at":"2025-03-29T07:23:31.000Z","dependencies_parsed_at":"2023-10-12T16:09:33.109Z","dependency_job_id":null,"html_url":"https://github.com/zanjie1999/tcp-over-websocket","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zanjie1999%2Ftcp-over-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zanjie1999%2Ftcp-over-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zanjie1999%2Ftcp-over-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zanjie1999%2Ftcp-over-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zanjie1999","download_url":"https://codeload.github.com/zanjie1999/tcp-over-websocket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640465,"owners_count":20971557,"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":["go","proxy","proxy-client","proxy-server","tcp","tcp2ws","websocket"],"created_at":"2024-11-12T02:44:26.667Z","updated_at":"2025-04-07T11:08:45.705Z","avatar_url":"https://github.com/zanjie1999.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tcp over WebSocket (TCP to WebSocket, tcp2ws)\n本工具能 使用WebSocket创建隧道，实现TCP端口转发  \n在v6.0及以后的版本支持了wss，也就是说现在可以实现https ssl进行更安全的传输\n在v8.3及以后的版本支持了ip优选，会自动选择域名解析中最优的cdn ip进行连接  \n在v9.0及以后的版本支持了UDP，也就是说现在可以实现UDP端口转发了  \n也就是UDP over WebSocket (UDP to WebSocket, udp2ws) 并没有独立成新程序，写在一起了  \n启动时会同时转发指定的端口的TCP和UDP流量  \n\n## 因为经常修改优化，所以请Star，不要Fork  \n### 至于这样脱裤子放屁的操作有什么用？  \n举个例子，一个服务器只能通过cdn的http转发（或者https），它也不能联网，这时你就可以利用此工具将需要转发的端口（比如22）转换成ws协议（http）来传输，再去Nginx里面配一个反向代理，那么当本客户端访问Nginx提供的服务的特定路径时将反代到本服务端，实现内网穿透进行端口转发  \n当然Nginx不是必须的，直接把监听端口开放到公网上也行\n这时防火墙仅仅发现你连接了一个WebSocket而已\n并且在网络不稳定时，断开的ws会自动重连，保持着转发的tcp连接（断开ws超过2分钟tcp连接将被断开）  \n\n### 似乎有别的工具也能实类似的功能，写它干嘛？\n最初为了实现将ssh用Nginx转发，写的时候还没找到这种工具，所以先实现了tcp放到ws中传输，但像Nginx就算是ws长链接，根据[官方文档](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout)描述，连接超时时间无论配置多大，通常都不能连接超过75秒，时间一到ssh就会断，于是实现了重连和重发，这是别的工具没有的  \n这个工具也不会有太多花里胡哨的功能，干好它的本职工作\n\n### 那为什么要支持UDP呢？\nUDP是不可靠的，所以在传输过程中可能会丢包，但是这个工具会自动重传丢失的数据包  \n例如用做Dns转发，WebSocket会在客户端发送UDP数据包时才创建连接，并且在2分钟超时后自动断开，既保证了连接稳定性，又不会一直挂着连接占用资源\n\n## 如何使用\n在右边Releases中选择你使用的平台的程序来运行  \n服务端：  \n`tcp2ws 要代理的ip:端口 本ws服务的监听端口`   \n`tcp2ws 本地端口 本ws服务的监听端口`  \n`tcp2ws 本地端口 监听ip:本ws服务的监听端口`   \n客户端：  \n`tcp2ws ws://链接 本地监听端口`  \n`tcp2ws http://链接 本地监听端口`  \n\n另外也可以使用wss(https ssl)协议，ssl更为安全，但需要消耗更多流量，需要指定证书路径，另外顺带提一下nginx可以把wss(https)转发到ws(http)  \n服务端：  \n`tcp2ws 要代理的ip:端口 本ws服务的监听端口 证书.crt 证书.key`\n`tcp2ws 本地端口 本ws服务的监听端口 证书.crt 证书.key`   \n`tcp2ws 本地端口 监听ip:本ws服务的监听端口 证书.crt 证书.key`   \n使用默认的文件名 server.crt server.key（这里的`wss`也可以是`https`或`ssl`）   \n`tcp2ws 要代理的ip:端口 本ws服务的监听端口 wss`  \n客户端：  \n`tcp2ws wss://链接 本地监听端口`  \n`tcp2ws https://链接 本地监听端口`  \n\n生成自签证书的方法（一路回车即可）：  \n```\nopenssl genrsa -out server.key 2048\nopenssl ecparam -genkey -name secp384r1 -out server.key\nopenssl req -new -x509 -sha256 -key server.key -out server.crt -days 36500\n```\n\n举个🌰：  \n在服务器运行`tcp2ws 127.0.0.1:22 127.0.0.1:22222`  \n然后在nginx中反代了一下  \n在客户端运行`tcp2ws ws://yourdomain.com/ssh/ 222`  \n那么就可以通过客户端的222来访问服务器的ssh啦  \n是不是特别棒呢  \n\n还可以写一个死循环来守护运行 \n`while true;do;tcp2ws 127.0.0.1:22 127.0.0.1:22222;done`  \n或者用screen来后台运行  \n`screen -dmS tcp2ws bash -c \"tcp2ws 127.0.0.1:22 127.0.0.1:22222\"`\n\n## 速度\n在乞丐版M1 Pro的macOS下使用本工具来回转换iperf3端口测试得到的数据\n```\n[ ID] Interval           Transfer     Bitrate\n[  5]   0.00-1.00   sec   988 MBytes  8.28 Gbits/sec\n[  5]   1.00-2.00   sec   977 MBytes  8.19 Gbits/sec\n[  5]   2.00-3.00   sec   982 MBytes  8.23 Gbits/sec\n[  5]   3.00-4.00   sec   994 MBytes  8.34 Gbits/sec\n[  5]   4.00-5.00   sec   966 MBytes  8.10 Gbits/sec\n[  5]   5.00-6.00   sec   982 MBytes  8.24 Gbits/sec\n[  5]   6.00-7.00   sec   989 MBytes  8.30 Gbits/sec\n[  5]   7.00-8.00   sec   935 MBytes  7.84 Gbits/sec\n[  5]   8.00-9.00   sec  1004 MBytes  8.42 Gbits/sec\n[  5]   9.00-10.00  sec   984 MBytes  8.26 Gbits/sec\n- - - - - - - - - - - - - - - - - - - - - - - - -\n[ ID] Interval           Transfer     Bitrate\n[  5]   0.00-10.00  sec  9.57 GBytes  8.22 Gbits/sec                  sender\n[  5]   0.00-10.00  sec  9.56 GBytes  8.21 Gbits/sec                  receiver\n```\n走wss，因为ssl，速度肉眼可见下降:\n```\n[ ID] Interval           Transfer     Bitrate\n[  5]   0.00-10.00  sec  7.38 GBytes  6.34 Gbits/sec                  sender\n[  5]   0.00-10.00  sec  7.38 GBytes  6.33 Gbits/sec                  receiver\n```\n直连\n```\n[ ID] Interval           Transfer     Bitrate\n[  5]   0.00-10.00  sec   119 GBytes   102 Gbits/sec                  sender\n[  5]   0.00-10.00  sec   119 GBytes   102 Gbits/sec                  receiver\n```\n在i7-8550u的Windows下使用本工具来回转换iperf3端口测试得到的数据\n```\n[ ID] Interval           Transfer     Bandwidth\n[  4]   0.00-1.00   sec   300 MBytes  2.52 Gbits/sec\n[  4]   1.00-2.00   sec   336 MBytes  2.81 Gbits/sec\n[  4]   2.00-3.00   sec   320 MBytes  2.68 Gbits/sec\n[  4]   3.00-4.00   sec   317 MBytes  2.66 Gbits/sec\n[  4]   4.00-5.00   sec   302 MBytes  2.53 Gbits/sec\n[  4]   5.00-6.00   sec   328 MBytes  2.75 Gbits/sec\n[  4]   6.00-7.00   sec   312 MBytes  2.61 Gbits/sec\n[  4]   7.00-8.00   sec   319 MBytes  2.67 Gbits/sec\n[  4]   8.00-9.00   sec   322 MBytes  2.70 Gbits/sec\n[  4]   9.00-10.00  sec   348 MBytes  2.92 Gbits/sec\n- - - - - - - - - - - - - - - - - - - - - - - - -\n[ ID] Interval           Transfer     Bandwidth\n[  4]   0.00-10.00  sec  3.14 GBytes  2.70 Gbits/sec                  sender\n[  4]   0.00-10.00  sec  3.13 GBytes  2.69 Gbits/sec                  receiver\n```\n两个iperf3直连\n```\n[ ID] Interval           Transfer     Bandwidth\n[  4]   0.00-10.00  sec  9.53 GBytes  8.19 Gbits/sec                  sender\n[  4]   0.00-10.00  sec  9.53 GBytes  8.19 Gbits/sec                  receiver\n```\n\n## 伪装\n在直接访问监听端口的任意路径，默认会返回一个空白页面  \n可以写一个`index.html`放到运行目录下来代替这个空白页面\n直接访问时就会显示这个文件的内容，伪装成一个非常普通的Web服务  \n推荐用一个叫SingleFile的插件可以把页面直接存成一个文件\n\n\n### 协议 咩License\n使用此项目视为您已阅读并同意遵守 [此LICENSE](https://github.com/zanjie1999/LICENSE)   \nUsing this project is deemed to indicate that you have read and agreed to abide by [this LICENSE](https://github.com/zanjie1999/LICENSE)   \n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzanjie1999%2Ftcp-over-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzanjie1999%2Ftcp-over-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzanjie1999%2Ftcp-over-websocket/lists"}