小言_互联网的博客

frp内网穿透https

284人阅读  评论(0)

在公网服务器搭建frps(service),在内网本地机子搭建frpc(client),流量通过访问公网ip,经过frps服务端转发到fprc客户端,fprc再转发到本地web应用。 

官方下载地址​ https://github.com/fatedier/frp/releases
官方文档地址https://gofrp.org/docs/

服务端:frp_0.44.0_linux_amd64.tar.gz
客户端:frp_0.44.0_windows_amd64.zip

一、http

服务端frps(公网服务器)

这里我用虚拟机模拟了一台公网服务器:192.168.111.201


   
  1. # 解压
  2. tar -xzvf frp_0.44.0_linux_amd64.tar.gz
  3. cd frp_0.44.0_linux_amd64
  4. # 配置服务端
  5. vi frps.ini

配置 frps.ini


   
  1. [ common]
  2. # frp服务端的端口,frp客户端需要连接这个端口握手
  3. bind_port = 7000
  4. # 公网服务端对外提供的http端口
  5. vhost_http_port= 8080

 启动服务端

./frps -c frps.ini

后台启动运行服务端

nohup ./frps -c frps.ini >/dev/null 2>&1 &

客户端frpc(内网本地)

配置 frpc.ini


   
  1. [common]
  2. # frps服务端的ip和端口
  3. server_addr = 192.168. 111.201
  4. server_port = 7000
  5. [web01]
  6. type = http
  7. # 本地web应用的ip和端口
  8. local_ip = 127.0. 0.1
  9. local_port = 8080
  10. #公网服务器的ip或域名
  11. custom_domains = 192.168. 111.201

到解压根目录启动客户端

frpc -c frpc.ini

访问公网ip已经可以反向代理到本地web服务了。 

以上就是一个简单的内网穿透 http 例子。

二、https

使用官方推荐的如下配置:

frps.ini


  
  1. [ common]
  2. bind_port = 7000
  3. vhost_https_port = 443

frpc.ini


  
  1. [common]
  2. server_addr = 192.168. 111.201
  3. server_port = 7000
  4. [plugin_https2https]
  5. type = https
  6. custom_domains = 192.168. 111.201
  7. plugin = https2https
  8. plugin_local_addr = 127.0. 0.1: 8443
  9. #plugin_crt_path = ./server.crt
  10. #plugin_key_path = ./server.key
  11. plugin_host_header_rewrite = 127.0. 0.1
  12. plugin_header_X- From- Where = frp

结果一直报如下错误,没找到解决办。 

三、tcp实现https

研究了好久,才反应过来,http协议在应用层,tcp在传输层,且http是基于tcp的。
既然https失败了,直接转发tcp就是,把ssl放在本地web应用中就可以了,frp只做tcp流量转发。

 frps.ini


  
  1. [ common]
  2. bind_port = 7000

frpc.ini 


  
  1. [common]
  2. server_addr = 192.168. 111.201
  3. server_port = 7000
  4. [tcp]
  5. type = tcp
  6. # 本地web应用的ip和端口
  7. local_ip = 127.0. 0.1
  8. local_port = 8443
  9. # 公网服务器提供给外网访问的端口
  10. remote_port = 443

 四、安全加固


上面有2个不安全的地方:
1、frpc 和 frps 之间的身份验证不安全,默认为 token,这种方式存在被中间人攻击的威胁。
2、frpc 和 frps 之间的流量未加密,可以通过 TLS 协议加密,解决被中间人攻击的威胁。

 frps.ini


  
  1. [ common]
  2. #frps服务端监听的端口,frpc客户端要来连接。
  3. bind_port = 7000
  4. #密码设置复杂点,frps服务端密码和frpc客户端密码要一致。
  5. token = mm123456789

frpc.ini 


  
  1. [common]
  2. # frps服务端的ip和端口
  3. server_addr = 192.168. 111.201
  4. server_port = 7000
  5. #密码设置复杂点,frps服务端密码和frpc客户端密码要一致。
  6. token = mm123456789
  7. [tcp]
  8. type = tcp
  9. # 本地web应用的ip和端口
  10. local_ip = 127.0. 0.1
  11. local_port = 8443
  12. # 公网服务器提供给外网访问的端口
  13. remote_port = 443

双向验证

双向验证即 frpc 和 frps 通过本地 ca 证书去验证对方的身份。理论上 frpc 和 frps 的 ca 证书可以不同,只要能验证对方身份即可。

frps.ini 


  
  1. # frps.ini
  2. [common]
  3. #frps服务端监听的端口,frpc客户端要来连接。
  4. bind_port = 7000
  5. #密码设置复杂点,frps服务端密码和frpc客户端密码要一致。
  6. token = mm123456789
  7. tls_cert_file = ./server.crt
  8. tls_key_file = ./server.key
  9. tls_trusted_ca_file = ./ca.crt

frpc.ini  


  
  1. # frpc.ini
  2. [common]
  3. # frps服务端的ip和端口
  4. server_addr = 192.168.111.201
  5. server_port = 7000
  6. #密码设置复杂点,frps服务端密码和frpc客户端密码要一致。
  7. token = mm123456789
  8. # frpc开启TLS加密功能
  9. tls_enable = true
  10. tls_cert_file = ./client.crt
  11. tls_key_file = ./client.key
  12. tls_trusted_ca_file = ./ca.crt
  13. [tcp]
  14. type = tcp
  15. # 本地web应用的ip和端口
  16. local_ip = 127.0.0.1
  17. local_port = 8443
  18. # 公网服务器提供给外网访问的端口
  19. remote_port = 443


生成SAN 证书

1、准备默认 my-openssl.cnf 配置文件于当前目录


  
  1. cat > my-openssl.cnf << EOF
  2. [ ca ]
  3. default_ca = CA_default
  4. [ CA_default ]
  5. x509_extensions = usr_cert
  6. [ req ]
  7. default_bits = 2048
  8. default_md = sha256
  9. default_keyfile = privkey. pem
  10. distinguished_name = req_distinguished_name
  11. attributes = req_attributes
  12. x509_extensions = v3_ca
  13. string_mask = utf8only
  14. [ req_distinguished_name ]
  15. [ req_attributes ]
  16. [ usr_cert ]
  17. basicConstraints = CA: FALSE
  18. nsComment = "OpenSSL Generated Certificate"
  19. subjectKeyIdentifier = hash
  20. authorityKeyIdentifier = keyid,issuer
  21. [ v3_ca ]
  22. subjectKeyIdentifier = hash
  23. authorityKeyIdentifier = keyid:always, issuer
  24. basicConstraints = CA: true
  25. EOF

2、生成默认CA


  
  1. openssl genrsa -out ca. key 2048
  2. openssl req -x509 - new -nodes - key ca. key -subj "/CN=example.ca.com" -days 3650 -out ca.crt

3、生成服务端证书

将 192.168.111.201 改成自己的服务端的ip


  
  1. openssl genrsa -out server.key 2048
  2. openssl req - new -sha256 -key server.key \
  3. -subj "/C=XX/ST=DEFAULT/L=DEFAULT/O=DEFAULT/CN=192.168.111.201" \
  4. -reqexts SAN \
  5. -config <(cat my-openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:localhost,IP:127.0.0.1,DNS:server.com,IP:192.168.111.201")) \
  6. -out server.csr
  7. openssl x509 -req -days 3650 -sha256 \
  8. - in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
  9. -extfile <(printf "subjectAltName=DNS:localhost,IP:127.0.0.1,DNS:server.com,IP:192.168.111.201") \
  10. -out server.crt

4、生成客户端证书

使用 who 命令查询客户端的ip地址。我这里是192.168.111.1
将 192.168.111.1 改成自己的客户端的ip


  
  1. openssl genrsa - out client.key 2048
  2. openssl req -new -sha256 -key client.key \
  3. -subj "/C=XX/ST=DEFAULT/L=DEFAULT/O=DEFAULT/CN=192.168.111.1" \
  4. -reqexts SAN \
  5. -config <(cat my-openssl.cnf <(printf "\n[SAN]\nsubjectAltName=DNS:client.com,IP:192.168.111.1")) \
  6. - out client.csr
  7. openssl x509 -req -days 3650 -sha256 \
  8. - in client.csr -CA ca.crt - CAkey ca.key - CAcreateserial \
  9. -extfile <(printf "subjectAltName=DNS:client.com,IP:192.168.111.1") \
  10. - out client.crt

最终生成了10个文件。
server.crt、server.key、ca.crt 拷贝到 frp服务端解压目录下。
client.crt、client.key、ca.crt 拷贝到 frp客户端解压目录下。


转载:https://blog.csdn.net/u014644574/article/details/128303747
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场