Skip to content

Commit cf4fb6d

Browse files
committed
FEAT: add post frp lan tranversal
1 parent 70eccaf commit cf4fb6d

File tree

1 file changed

+200
-0
lines changed
  • content/posts/2024-09-09-frp-nat-traversal

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
title: "使用 frp 实现内网穿透"
3+
date: 2024-09-09T12:11:03+08:00
4+
# weight: 1
5+
# aliases: ["/first"]
6+
tags: ["Network", "System"]
7+
author: "Square Zhong"
8+
# author: ["Me", "You"] # multiple authors
9+
showToc: true
10+
TocOpen: false
11+
draft: false
12+
hidemeta: false
13+
comments: false
14+
description: "折腾是对的"
15+
canonicalURL: "https://canonical.url/to/page"
16+
disableHLJS: true # to disable highlightjs
17+
disableShare: false
18+
disableHLJS: false
19+
hideSummary: false
20+
searchHidden: false
21+
ShowReadingTime: true
22+
ShowBreadCrumbs: true
23+
ShowPostNavLinks: true
24+
ShowWordCount: true
25+
ShowRssButtonInSectionTermList: true
26+
UseHugoToc: true
27+
cover:
28+
image: "<image path/url>" # image path/url
29+
alt: "<alt text>" # alt text
30+
caption: "<text>" # display caption under cover
31+
relative: false # when using page bundles set this to true
32+
hidden: true # only hide on current single page
33+
editPost:
34+
URL: "https://github.com/squarezhong.github.io/content"
35+
Text: "Suggest Changes" # edit text
36+
appendFilePath: true # to append file path to Edit link
37+
---
38+
39+
## 前置准备
40+
41+
1. 需要进行穿透的机器
42+
2. 一台云服务器
43+
3. 一个域名(如果在大陆使用最好在云服务器提供商处购买,备案会比较方便)
44+
45+
## 配置 frp
46+
47+
项目地址:https://github.com/fatedier/frp
48+
49+
文档地址:https://gofrp.org/zh-cn/
50+
51+
### Server
52+
53+
1. 下载 frp
54+
55+
```bash
56+
# make sure you choose the filename corresponds to your system architecture
57+
$ wget https://github.com/fatedier/frp/releases/download/v0.60.0/frp_0.60.0_linux_amd64.tar.gz
58+
$ tar -zxf frp_0.60.0_linux_amd64.tar.gz
59+
$ mv frp_0.60.0_linux_amd64 frp
60+
```
61+
62+
frp 文件夹包含以下文件:
63+
64+
```bash
65+
LICENSE
66+
frpc
67+
frpc.toml
68+
frps
69+
frps.toml
70+
```
71+
72+
2. 编辑 frp server 配置文件
73+
74+
`$ vim ~/frp/frps.toml`
75+
76+
添加如下内容:
77+
78+
```bash
79+
bindPort = 7000 # any port you like
80+
```
81+
82+
3. 使用 `systemd` 管理 frps 服务
83+
84+
首先创建一个 frps 服务文件
85+
86+
`$ sudo vim /etc/systemd/system/frps.service`
87+
添加如下内容:
88+
89+
```bash
90+
[Unit]
91+
Description = frp server
92+
After = network.target syslog.target
93+
Wants = network.target
94+
95+
[Service]
96+
Type = simple
97+
# change the path to real absolute path
98+
ExecStart = /path/to/frps -c /path/to/frps.toml
99+
100+
[Install]
101+
WantedBy = multi-user.target
102+
```
103+
104+
开始服务并且设置为自启动
105+
106+
```bash
107+
$ sudo systemctl start frps
108+
$ sudo systemctl enable frps
109+
```
110+
111+
### Client
112+
113+
1. 下载 frp(同 server 端)
114+
115+
2. 编辑 frp client 配置文件
116+
117+
`$ vim ~/frp/frpc.toml`
118+
119+
添加如下内容:
120+
121+
```bash
122+
serverAddr = "x.x.x.x" # change it to your server's ip address
123+
serverPort = 7000
124+
125+
# reconnect if fail
126+
loginFailExit = false
127+
128+
[[proxies]]
129+
name = "web"
130+
type = "tcp"
131+
localPort = 5080 # any port you like
132+
remotePort = 6080 # any port you like
133+
```
134+
135+
3. 使用 systemd 管理 frpc 服务
136+
137+
首先创建一个 frpc 服务文件
138+
139+
`$ sudo vim /etc/systemd/system/frpc.service`
140+
141+
添加如下内容
142+
143+
```bash
144+
[Unit]
145+
Description = frp client
146+
After = network.target syslog.target
147+
Wants = network.target
148+
149+
[Service]
150+
Type = simple
151+
# change the path to real absolute path
152+
ExecStart = /path/to/frpc -c /path/to/frpc.toml
153+
Restart = always
154+
RestartSec = 5
155+
156+
[Install]
157+
WantedBy = multi-user.target
158+
```
159+
160+
开始服务并且设置为自启动
161+
162+
```bash
163+
$ sudo systemctl start frpc
164+
$ sudo systemctl enable frpc
165+
```
166+
167+
现在你可以使用 `[server ip]:port` (e.g. [ip]:6080) 来远程访问你的客户端机器服务。
168+
169+
## 配置 Nginx 反向代理
170+
171+
使用 [ip]:port 访问的方式既不优雅也不安全,推荐使用 nginx 反向代理的方式实现域名访问并且隐去端口号。
172+
173+
### 设置域名解析
174+
175+
访问你的域名提供商,它应当会提供域名解析服务。
176+
177+
将你要使用的域名解析到云服务器的 公网 ip
178+
179+
### 设置反向代理
180+
181+
个人实践过程中才用过以下三种方式
182+
183+
- **Nginx Proxy Manager** (Recommended)
184+
185+
使用 docker 部署 Nginx Proxy Manger,非常优雅。
186+
187+
在 [SSL Certificate] 中申请域名对应的 Let’s Encrypt 证书。
188+
189+
申请过程中 [DNS Challenge] 选择你的解析服务提供商,比如我的域名是在腾讯云购买的,就选择 [DNSPod], id 和 tokens 在 dnspod api 界面生成,不同服务商可能存在差异。
190+
191+
- 宝塔面板(极其不稳定,强烈不推荐使用)
192+
193+
- 使用 `certbot` 或许证书,在 Nginx 手动配置。
194+
195+
你都选择这样操作了,应该不需要看这种教程,直接看官方文档就好了。
196+
197+
198+
现在你可以直接使用域名访问你的内网服务。
199+
200+
如果你的选择的云服务器在国内,记得**备案**

0 commit comments

Comments
 (0)