0. 概述
有时,你想在公网开放一个 TCP 服务但是又不能在内部做鉴权,这个场景下使用 TLS 是一个不错的选择,但是通用的 HTTP TLS 代理有很多,而 TCP 的却不常见,所以,在这篇文章中,我介绍一款可以用于 TCP 的 TLS 代理软件。
1. 安装
安装其实很简单了,可以直接在 Release 页面上下载到编译好的二进制文件:
[[email protected]]# wget https://github.com/square/ghostunnel/releases/download/v1.5.2/ghostunnel-v1.5.2-linux-amd64-with-pkcs11 -O ghostunnel
[[email protected]]# chmod +x ghostunnel
[[email protected]]# ./ghostunnel --version
rev v1.5.2 built with go1.13.4
2. 作为服务端
ghostunnel 既可以作为客户端也可以作为服务端,这里先演示一下如何作为服务端:
[[email protected]]# nc -l localhost 8080 # 开启一个服务器
[[email protected]]# ghostunnel server \
--listen localhost:8443 \
--target localhost:8080 \
--keystore test-keys/server-keystore.p12 \
--cacert test-keys/cacert.pem \
--allow-cn client
然后尝试一下:
[[email protected]]# openssl s_client \
-connect localhost:8443 \
-cert test-keys/client-combined.pem \
-key test-keys/client-combined.pem \
-CAfile test-keys/cacert.pem
3. 作为客户端
作为客户端的话,首先需要有一个 TLS 的 Server 端先:
[[email protected]]# openssl s_server \
-accept 8443 \
-cert test-keys/server-combined.pem \
-key test-keys/server-combined.pem \
-CAfile test-keys/cacert.pem
[[email protected]]# ghostunnel client \
--listen localhost:8080 \
--target localhost:8443 \
--keystore test-keys/client-combined.pem \
--cacert test-keys/cacert.pem
然后真正的客户端就可以通过 TCP 直接访问了:
[[email protected]]# nc -v localhost 8080