-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
349 lines (304 loc) · 78.8 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>使用1Panel阻止SSH爆破</title>
<url>/2024/12/09/1PanelFail2ban/</url>
<content><![CDATA[<h1 id="使用1Panel阻止SSH爆破"><a href="#使用1Panel阻止SSH爆破" class="headerlink" title="使用1Panel阻止SSH爆破"></a>使用1Panel阻止SSH爆破</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>今日闲来无事,顺手看了一下1Panle的SSH登录日志,不看不知道,一看…….</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241209093010476.png" alt="image-20241209093010476"></p>
<blockquote>
<p>1Panel的登录日志在 主机>SSH管理>登录日志</p>
</blockquote>
<p>不过查了一下IP发现这还是华为云自己的IP?</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241209093316837.png" alt="image-20241209093316628"></p>
<h2 id="安装Fail2ban"><a href="#安装Fail2ban" class="headerlink" title="安装Fail2ban"></a>安装Fail2ban</h2><p>不管怎么说还是加个防止SSH爆破的<strong>Fail2ban</strong>比较好</p>
<p>在1Panel面板 <strong>工具箱>Fail2ban</strong> 可以看见<strong>Fail2ban</strong>的运行状态,一般是未安装的,接下来就安装一下</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241209093730659.png" alt="image-20241209093730416"></p>
<div class="tabs" id="test"><ul class="nav-tabs"><button type="button" class="tab active" data-href="test-1">RedHat / CentOS</button><button type="button" class="tab " data-href="test-2">Ubuntu / Debian</button></ul><div class="tab-contents"><div class="tab-item-content active" id="test-1"><p><strong>1、安装 epel 源</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">yum install -y epel-release</span><br></pre></td></tr></table></figure>
<p><strong>2、安装 Fail2ban</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">yum install -y fail2ban</span><br></pre></td></tr></table></figure>
<p><strong>3、启动 Fail2ban 服务</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">systemctl start fail2ban</span><br></pre></td></tr></table></figure>
<p><strong>4、开机自启动</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">systemctl enable fail2ban</span><br></pre></td></tr></table></figure>
<p><strong>5、查看 Fail2ban 服务状态。</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">systemctl status fail2ban</span><br></pre></td></tr></table></figure></div><div class="tab-item-content" id="test-2"><p><strong>1、安装 Fail2ban</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sudo apt-get install fail2ban</span><br></pre></td></tr></table></figure>
<p><strong>2、Debian 12 及以上的版本需要手动安装 rsyslog</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sudo apt-get install rsyslog</span><br></pre></td></tr></table></figure>
<p><strong>3、启动 Fail2ban 服务</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sudo systemctl start fail2ban</span><br></pre></td></tr></table></figure>
<p><strong>4、开机自启动</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sudo systemctl enable fail2ban</span><br></pre></td></tr></table></figure>
<p><strong>5、查看 Fail2ban 服务状态。</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sudo systemctl status fail2ban</span><br></pre></td></tr></table></figure></div></div><div class="tab-to-top"><button type="button" aria-label="scroll to top"><i class="fas fa-arrow-up"></i></button></div></div>
<h3 id="由于我是Ubuntu-我就用Ubuntu进行演示了"><a href="#由于我是Ubuntu-我就用Ubuntu进行演示了" class="headerlink" title="由于我是Ubuntu,我就用Ubuntu进行演示了"></a>由于我是Ubuntu,我就用Ubuntu进行演示了</h3><h4 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h4><p><img src="https://image.admilk.us.kg/image/imgs/20241209094239674.png" alt="image-20241209094239335"></p>
<h4 id="启动"><a href="#启动" class="headerlink" title="启动"></a>启动</h4><p><img src="https://image.admilk.us.kg/image/imgs/20241209094331213.png"></p>
<h4 id="开机自启动"><a href="#开机自启动" class="headerlink" title="开机自启动"></a>开机自启动</h4><p><img src="https://image.admilk.us.kg/image/imgs/20241209094633659.png" alt="image-20241209094633456"></p>
<h4 id="查看运行状态"><a href="#查看运行状态" class="headerlink" title="查看运行状态"></a>查看运行状态</h4><p><img src="https://image.admilk.us.kg/image/imgs/20241209094706699.png" alt="image-20241209094706494"></p>
<p>运行正常即可</p>
<h2 id="配置Fail2ban"><a href="#配置Fail2ban" class="headerlink" title="配置Fail2ban"></a>配置Fail2ban</h2><p>接下来返回1Panel的 <strong>工具箱>Fail2ban</strong></p>
<p>看到以下界面即可,你可以按需更改相应的配置,基本上保持默认即可,这样就大功告成啦</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241209094847144.png" alt="image-20241209094846912"></p>
]]></content>
<tags>
<tag>1panel</tag>
</tags>
</entry>
<entry>
<title>CloudFlare搭建无限邮箱</title>
<url>/2024/12/14/CloudFlareMail/</url>
<content><![CDATA[<h2 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h2><ul>
<li>一个<strong>CloudFlare</strong>账号,如果没有就去<a href="https://dash.cloudflare.com/sign-up">CloudFlare</a>注册 <del>这不废话</del></li>
<li>一个可以添加到CloudFlare的域名</li>
</ul>
<h2 id="收邮件"><a href="#收邮件" class="headerlink" title="收邮件"></a>收邮件</h2><p>进入<strong>CloudFlare</strong>首页,点击你的域名进入详情页,点击左侧<code>电子邮件</code></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214190715185.png" alt="image-20241214190714728"></p>
<p>点击<strong>开始使用</strong></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214190755498.png" alt="image-20241214190755304"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214190902593.png" alt="image-20241214190902341"></p>
<p>这里需要注意一下</p>
<ul>
<li><p>自定义地址就是<code>@域名</code>前面的部分,比如我填<code>admin</code>,邮箱地址就是<em><a href="mailto:admin@MapleLeaf1129.us.kg">admin@MapleLeaf1129.us.kg</a></em></p>
</li>
<li><p>目标位置就是向上方的邮箱地址发送邮件后,用于接受邮件的地址,也就是发送给上方的邮件会转发到这个地址</p>
</li>
</ul>
<p>点击<strong>创建并验证</strong>,这里状态变成<code>待验证</code></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214191157391.png" alt="image-20241214191157174"></p>
<p>去刚刚你填的<code>目标位置</code>的邮箱找到<strong>CloudFlare</strong>的验证邮件,点击<code>Verify email address</code>验证</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214193527705.png" alt="image-20241214193527431"></p>
<p>出现以下界面就是验证成功了</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214193702624.png" alt="image-20241214193702394"></p>
<p>返回<strong>CloudFlare</strong>,可以看见状态变为<strong>已验证</strong>,点击<code>继续</code></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214193726585.png" alt="image-20241214193726381"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214193826175.png" alt="image-20241214193825924"></p>
<p>可以看见<strong>DNS记录</strong>目前都是<strong>缺失</strong>状态,直接点击<code>添加记录并启用</code>,<strong>CloudFlare</strong>会自动补全<strong>DNS解析</strong></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214194011081.png" alt="image-20241214194010771"> 这样,使用<strong>CloudFlare</strong>收邮件就完成了,可以发送邮件测试一下</p>
<p>另外,点击<strong>路由规则</strong>,编辑下方的<code>Catch-all 地址</code></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214194319552.png" alt="image-20241214194319275"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214194415575.png" alt="image-20241214194415366"></p>
<p>将操作改为<strong>发送到电子邮件</strong>,将目标位置改为你之前填写的邮件地址,点击保存,将状态改为<strong>活动</strong></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214194618741.png" alt="image-20241214194618532"></p>
<p>这样就可以让发送给任意<code>xxx@你的域名</code>的邮件发送给你,相当于直接拥有了无限多的邮箱</p>
<p>收邮件就完成了,有发送邮件需求的可以继续往下配置发邮件</p>
<h2 id="发邮件"><a href="#发邮件" class="headerlink" title="发邮件"></a>发邮件</h2><p>关于发邮件,这里只写<strong>outlook</strong>方式</p>
<h3 id="OutLook"><a href="#OutLook" class="headerlink" title="OutLook"></a>OutLook</h3><p>打开<a href="https://outlook.live.com/">outlook</a>, 登录账号, 点击右上角设置 > 查看全部outlook设置 > 邮件 > 同步电子邮件 > 管理或选择主别名</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214195250002.png" alt="image-20241214195249740"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214195522158.png" alt="image-20241214195521956"></p>
<p>点击<code>添加电子邮件</code>,选择<code>将现有电子邮件地址添加为 Microsoft 帐户别名</code>,填入你的邮箱地址,点击<code>添加别名</code>,<code>验证</code>,<code>发送电子邮件</code></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214195632194.png" alt="image-20241214195632018"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214195810839.png" alt="image-20241214195810333"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241214195835183.png" alt="image-20241214195834980"></p>
<p>在你的邮箱内找到验证邮件, 点击确定即可</p>
]]></content>
<tags>
<tag>邮箱</tag>
<tag>CloudFlare</tag>
</tags>
</entry>
<entry>
<title>CloudFlare免费内网穿透</title>
<url>/2024/12/08/CloudFlareIntranetPenetration/</url>
<content><![CDATA[<h1 id="CloudFlare免费内网穿透"><a href="#CloudFlare免费内网穿透" class="headerlink" title="CloudFlare免费内网穿透"></a>CloudFlare免费内网穿透</h1><h2 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h2><ul>
<li>一个<strong>CloudFlare</strong>账号,如果没有就去<a href="https://dash.cloudflare.com/sign-up">CloudFlare</a>注册 <del>这不废话</del></li>
<li>一个可以添加到<strong>CloudFlare</strong>的域名</li>
</ul>
<h2 id="开通ZeroTrust服务"><a href="#开通ZeroTrust服务" class="headerlink" title="开通ZeroTrust服务"></a>开通ZeroTrust服务</h2><p>登录你的<strong>CloudFlare</strong>账号,在主页左侧点击<strong>Zero Trust</strong></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213140216809.png" alt="image-20241213140216397"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213140506220.png" alt="image-20241213140505952"></p>
<p>这里随便取个名字,选择免费套餐即可</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213140607046.png" alt="image-20241213140606803"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213140654060.png" alt="image-20241213140653815"></p>
<p>然后你会看见一个让你添加付款方式的界面</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213140807803.png" alt="image-20241213140807571"></p>
<p>这里不需要添加付款方式,直接回到CloudFlare主页,<a href="https://dash.cloudflare.com/">https://dash.cloudflare.com</a></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213140920713.png" alt="image-20241213140920479"></p>
<p>再次点击<strong>ZeroTrust</strong>,这样就没有添加付款方式直接结进来了</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141035796.png" alt="image-20241213141035460"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141105267.png" alt="image-20241213141104936"></p>
<h2 id="新建隧道"><a href="#新建隧道" class="headerlink" title="新建隧道"></a>新建隧道</h2><p>在左侧<strong>Networks</strong>的下拉菜单里进入<strong>Tunnels</strong></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141201362.png" alt="image-20241213141201205"></p>
<p>点击<code>Add a tunnel</code>新建一条隧道,选择<strong>CloudFlare</strong>,随便取个名字</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141312280.png" alt="image-20241213141311917"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141434785.png" alt="image-20241213141434557"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141532999.png" alt="image-20241213141532744"></p>
<p>点击<code>Save tunnel</code>保存隧道,根据你的系统选择下载的应用程序</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213141735963.png" alt="image-20241213141735618"></p>
<h2 id="安装并运行连接器"><a href="#安装并运行连接器" class="headerlink" title="安装并运行连接器"></a>安装并运行连接器</h2><p>我这里以<strong>Windows64位</strong>操作系统为例,下载完成直接运行</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213142213480.png" alt="image-20241213142212614"></p>
<p>安装完成不会出现任何提示,回到<strong>CloudFlare</strong>,复制下方的一键安装命令</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213142452811.png" alt="image-20241213142452585"></p>
<p>在搜索栏搜索<code>CMD</code>,右键<code>命令提示符</code>,选择<code>以管理员身份运行</code></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213142554108.png" alt="image-20241213142553673"></p>
<p>粘贴从<strong>CloudFlare</strong>复制的代码,运行</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213142634555.png" alt="image-20241213142634368"></p>
<p>这样就成功安装完成了</p>
<h2 id="配置域名"><a href="#配置域名" class="headerlink" title="配置域名"></a>配置域名</h2><p>接下来配置域名</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213142835734.png" alt="image-20241213142835532"></p>
<ul>
<li><p>**子域名(Subdomain)**填入任意的子域名,</p>
</li>
<li><p><strong>域名(Domain)<strong>就选择托管在</strong>CloudFlare</strong>的域名</p>
</li>
<li><p>**路径(Path)**一般不需要管</p>
</li>
<li><p>**类型(Type)**按需选择,一些特殊协议需要额外配置,详情请访问CloudFlare具体文档</p>
</li>
<li><p><strong>URL</strong>填入需要穿透的地址,本机就是<code>127.0.0.1:</code>后面加端口号</p>
</li>
</ul>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213143349623.png" alt="image-20241213143349402"></p>
<p>因为我需要内网穿透的是一个开在<code>3000</code>端口的<code>HTTP</code>网站,所以<strong>Type</strong>选择的HTTP,<strong>URL</strong>填的<code>127.0.0.1:3000</code></p>
<p>最后点击<strong>Save hostname</strong>保存完成,这里的状态变为<strong>HEALTHY</strong>就是正常运行了</p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213143646251.png" alt="image-20241213143646020"></p>
<p><img src="https://image.admilk.us.kg/image/imgs/20241213143941514.png" alt="image-20241213143941340"></p>
<p>访问一下对应的网址,也是正常访问了,这样,<strong>CloudFlare</strong>搭建的内网穿透服务就成功了</p>
]]></content>
<tags>
<tag>CloudFlare</tag>
<tag>内网穿透</tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<url>/2024/11/29/hello-world/</url>
<content><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>
]]></content>
</entry>
<entry>
<title>使用CloudFlare搭建反向代理</title>
<url>/2024/11/29/CloudFlareReverseProxy/</url>
<content><![CDATA[<h1 id="使用CloudFlare搭建反向代理"><a href="#使用CloudFlare搭建反向代理" class="headerlink" title="使用CloudFlare搭建反向代理"></a>使用CloudFlare搭建反向代理</h1><div class="note warning simple"><p>[2024年12月03日] CloudFlare更新 <a href="https://www.cloudflare.com/zh-cn/terms/">服务条款 2.2.1</a><br><strong>禁止包括但不限于导致(直接或间接)您的 Cloudflare 代理域的流量发送到非 Cloudflare 为该域分配的 IP 地址</strong><br><strong>禁止使用服务提供虚拟专用网络或其他类似的代理服务</strong></p>
<p>若任使用请知晓可能出现的风险, 包括但不限于: <strong>暂停或终止您对cloudflare服务的使用或访问</strong> 等等。</p>
</div>
<div class="note info simple"><p>FREE套餐CloudFlare每日提供 <strong>10w</strong> 请求的免费额度,个人用完全足够,但并不能保证很多人同时使用,强烈建议仅自用</p>
</div>
<h2 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h2><ul>
<li>一个<strong>CloudFlare</strong>账号,如果没有就去<a href="https://dash.cloudflare.com/sign-up">CloudFlare</a>注册 <del>这不废话</del></li>
<li>一个可以添加到CloudFlare的域名(可选),主要是CloudFlare的一些域名被墙,导致无法直接访问</li>
</ul>
<h2 id="创建并配置Workers"><a href="#创建并配置Workers" class="headerlink" title="创建并配置Workers"></a>创建并配置Workers</h2><h3 id="创建-Workers"><a href="#创建-Workers" class="headerlink" title="创建 Workers"></a>创建 Workers</h3><p>在 Cloudflare 的面板左侧点击 <strong>Workers 和 Pages</strong> 进入后点击<strong>创建</strong>按钮</p>
<p><img src="https://image.admilk.us.kg/image/imgs202411291237524.png"></p>
<p><img src="https://image.admilk.us.kg/image/imgs202411291240960.png"></p>
<p>点击<strong>创建Workers</strong>按钮</p>
<p><img src="https://image.admilk.us.kg/image/imgs202411291242134.png"></p>
<p>名字随便填,我这里就直接填proxy了,然后点击<strong>部署</strong><br><img src="https://image.admilk.us.kg/image/imgs202411291243794.png"></p>
<p>点击<strong>编辑代码</strong>进入代码编辑页面<br><img src="https://image.admilk.us.kg/image/imgs202411291245172.png"></p>
<p>在这里你有两个选择</p>
<ul>
<li>通用反代(优点:可直接反代任意网站,缺点:容易寄,如果泄露被滥用的几率大)</li>
<li>指定反代(优点:稳定,不容易寄,缺点:需要一条条的配置需要反代的地址)</li>
</ul>
<p>个人推荐使用<strong>指定反代</strong>,按需选择</p>
<h3 id="指定反代"><a href="#指定反代" class="headerlink" title="指定反代"></a>指定反代</h3><p>将以下代码覆盖掉左侧代码框的内容</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title function_">addEventListener</span>(<span class="string">'fetch'</span>, <span class="function"><span class="params">event</span> =></span> {</span><br><span class="line"> event.<span class="title function_">respondWith</span>(<span class="title function_">handleRequest</span>(event.<span class="property">request</span>));</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="comment">// 域名映射表</span></span><br><span class="line"><span class="keyword">const</span> domainMappings = {</span><br><span class="line"> <span class="string">"/steam-store"</span>: <span class="string">"https://store.steampowered.com"</span>,</span><br><span class="line"> <span class="string">"/steam-api"</span>: <span class="string">"https://api.steampowered.com"</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> specialCases = {</span><br><span class="line"> <span class="string">"*"</span>: {</span><br><span class="line"> <span class="string">"Origin"</span>: <span class="string">"DELETE"</span>,</span><br><span class="line"> <span class="string">"Referer"</span>: <span class="string">"DELETE"</span></span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">handleSpecialCases</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> rules = specialCases[<span class="string">"*"</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">const</span> [key, value] <span class="keyword">of</span> <span class="title class_">Object</span>.<span class="title function_">entries</span>(rules)) {</span><br><span class="line"> <span class="keyword">switch</span> (value) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"KEEP"</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"DELETE"</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">delete</span>(key);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="attr">default</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">set</span>(key, value);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">handleRequest</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> url = <span class="keyword">new</span> <span class="title function_">URL</span>(request.<span class="property">url</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果是访问根目录就返回一个HTML</span></span><br><span class="line"><span class="keyword">if</span> (url.<span class="property">pathname</span> === <span class="string">"/"</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">`</span></span><br><span class="line"><span class="string"> <html></span></span><br><span class="line"><span class="string"> <head></span></span><br><span class="line"><span class="string"> <style></span></span><br><span class="line"><span class="string"> body {</span></span><br><span class="line"><span class="string"> display: flex;</span></span><br><span class="line"><span class="string"> justify-content: center;</span></span><br><span class="line"><span class="string"> align-items: center;</span></span><br><span class="line"><span class="string"> height: 100vh;</span></span><br><span class="line"><span class="string"> margin: 0;</span></span><br><span class="line"><span class="string"> background-color: #f9f9f9;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> h1 {</span></span><br><span class="line"><span class="string"> font-family: Arial, sans-serif;</span></span><br><span class="line"><span class="string"> color: #333;</span></span><br><span class="line"><span class="string"> text-align: center;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> </style></span></span><br><span class="line"><span class="string"> </head></span></span><br><span class="line"><span class="string"> <body></span></span><br><span class="line"><span class="string"> <h1>Why don't you play Genshin Impact?</h1></span></span><br><span class="line"><span class="string"> </body></span></span><br><span class="line"><span class="string"> </html></span></span><br><span class="line"><span class="string"> `</span>, {</span><br><span class="line"> <span class="attr">headers</span>: { <span class="string">"Content-Type"</span>: <span class="string">"text/html"</span> }</span><br><span class="line"> });</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> basePath = <span class="title class_">Object</span>.<span class="title function_">keys</span>(domainMappings).<span class="title function_">find</span>(<span class="function"><span class="params">path</span> =></span> url.<span class="property">pathname</span>.<span class="title function_">startsWith</span>(path));</span><br><span class="line"> <span class="keyword">if</span> (!basePath) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">"Path not found in domain mappings"</span>, { <span class="attr">status</span>: <span class="number">404</span> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> targetBase = domainMappings[basePath];</span><br><span class="line"> <span class="keyword">const</span> targetPath = url.<span class="property">pathname</span>.<span class="title function_">slice</span>(basePath.<span class="property">length</span>) + url.<span class="property">search</span> + url.<span class="property">hash</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> targetUrl = <span class="keyword">new</span> <span class="title function_">URL</span>(targetPath, targetBase);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> modifiedRequest = <span class="keyword">new</span> <span class="title class_">Request</span>(targetUrl, {</span><br><span class="line"> <span class="attr">headers</span>: request.<span class="property">headers</span>,</span><br><span class="line"> <span class="attr">method</span>: request.<span class="property">method</span>,</span><br><span class="line"> <span class="attr">body</span>: request.<span class="property">body</span>,</span><br><span class="line"> <span class="attr">redirect</span>: <span class="string">'follow'</span></span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> <span class="title function_">handleSpecialCases</span>(modifiedRequest);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> response = <span class="keyword">await</span> <span class="title function_">fetch</span>(modifiedRequest);</span><br><span class="line"> <span class="keyword">const</span> modifiedResponse = <span class="keyword">new</span> <span class="title class_">Response</span>(response.<span class="property">body</span>, response);</span><br><span class="line"> modifiedResponse.<span class="property">headers</span>.<span class="title function_">set</span>(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modifiedResponse;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<div class="note info modern"><p>可修改 <em>域名映射表(domainMappings)</em> 中的内容为自己想要的网站</p>
</div>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 域名映射表</span></span><br><span class="line"><span class="keyword">const</span> domainMappings = {</span><br><span class="line"> <span class="string">"/steam-store"</span>: <span class="string">"https://store.steampowered.com"</span>,</span><br><span class="line"> <span class="string">"/steam-api"</span>: <span class="string">"https://api.steampowered.com"</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>其中,前面的部分是路由,就是 https://你的域名/路由 也就如果</p>
<p>访问 <em>https://你的域名/steam-store</em> 反代的就是 <em><a href="https://store.steampowered.com/">https://store.steampowered.com</a></em><br>访问 <em>https://你的域名/steam-api</em> 反代的就是 <em><a href="https://api.steampowered.com/">https://api.steampowered.com</a></em> </p>
<p>其他同理,你可以添加也可以减少</p>
<h3 id="通用反代"><a href="#通用反代" class="headerlink" title="通用反代"></a>通用反代</h3><p>将以下代码覆盖掉左侧代码框的内容</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title function_">addEventListener</span>(<span class="string">'fetch'</span>, <span class="function"><span class="params">event</span> =></span> {</span><br><span class="line"> event.<span class="title function_">respondWith</span>(<span class="title function_">handleRequest</span>(event.<span class="property">request</span>));</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> specialCases = {</span><br><span class="line"> <span class="string">"*"</span>: {</span><br><span class="line"> <span class="string">"Origin"</span>: <span class="string">"DELETE"</span>,</span><br><span class="line"> <span class="string">"Referer"</span>: <span class="string">"DELETE"</span></span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">handleSpecialCases</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> rules = specialCases[<span class="string">"*"</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">const</span> [key, value] <span class="keyword">of</span> <span class="title class_">Object</span>.<span class="title function_">entries</span>(rules)) {</span><br><span class="line"> <span class="keyword">switch</span> (value) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"KEEP"</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"DELETE"</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">delete</span>(key);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="attr">default</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">set</span>(key, value);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">handleRequest</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> url = <span class="keyword">new</span> <span class="title function_">URL</span>(request.<span class="property">url</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 如果是访问根目录就返回一个HTML</span></span><br><span class="line"> <span class="keyword">if</span> (url.<span class="property">pathname</span> === <span class="string">"/"</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">`</span></span><br><span class="line"><span class="string"> <html></span></span><br><span class="line"><span class="string"> <head></span></span><br><span class="line"><span class="string"> <style></span></span><br><span class="line"><span class="string"> body {</span></span><br><span class="line"><span class="string"> display: flex;</span></span><br><span class="line"><span class="string"> justify-content: center;</span></span><br><span class="line"><span class="string"> align-items: center;</span></span><br><span class="line"><span class="string"> height: 100vh;</span></span><br><span class="line"><span class="string"> margin: 0;</span></span><br><span class="line"><span class="string"> background-color: #f9f9f9;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> h1 {</span></span><br><span class="line"><span class="string"> font-family: Arial, sans-serif;</span></span><br><span class="line"><span class="string"> color: #333;</span></span><br><span class="line"><span class="string"> text-align: center;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> </style></span></span><br><span class="line"><span class="string"> </head></span></span><br><span class="line"><span class="string"> <body></span></span><br><span class="line"><span class="string"> <h1>Why don't you play Genshin Impact?</h1></span></span><br><span class="line"><span class="string"> </body></span></span><br><span class="line"><span class="string"> </html></span></span><br><span class="line"><span class="string"> `</span>, {</span><br><span class="line"> <span class="attr">headers</span>: { <span class="string">"Content-Type"</span>: <span class="string">"text/html"</span> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (url.<span class="property">pathname</span>.<span class="title function_">startsWith</span>(<span class="string">"/"</span>)) {</span><br><span class="line"> <span class="keyword">let</span> targetUrl = url.<span class="property">pathname</span>.<span class="title function_">slice</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 自动补全协议(默认HTTPS)</span></span><br><span class="line"> <span class="keyword">if</span> (!<span class="regexp">/^https?:\/\//</span>.<span class="title function_">test</span>(targetUrl)) {</span><br><span class="line"> targetUrl = <span class="string">"https://"</span> + targetUrl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 动态生成URL</span></span><br><span class="line"> <span class="keyword">const</span> modifiedRequest = <span class="keyword">new</span> <span class="title class_">Request</span>(targetUrl, {</span><br><span class="line"> <span class="attr">headers</span>: request.<span class="property">headers</span>,</span><br><span class="line"> <span class="attr">method</span>: request.<span class="property">method</span>,</span><br><span class="line"> <span class="attr">body</span>: request.<span class="property">body</span>,</span><br><span class="line"> <span class="attr">redirect</span>: <span class="string">'follow'</span></span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> <span class="title function_">handleSpecialCases</span>(modifiedRequest);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 发送请求</span></span><br><span class="line"> <span class="keyword">const</span> response = <span class="keyword">await</span> <span class="title function_">fetch</span>(modifiedRequest);</span><br><span class="line"> <span class="keyword">const</span> modifiedResponse = <span class="keyword">new</span> <span class="title class_">Response</span>(response.<span class="property">body</span>, response);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 允许跨域</span></span><br><span class="line"> modifiedResponse.<span class="property">headers</span>.<span class="title function_">set</span>(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modifiedResponse;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">"Path not found"</span>, { <span class="attr">status</span>: <span class="number">404</span> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>直接在 <em>https://域名</em> 后面加反代的地址就行,也就是 <em>https://域名/需要反代的网站</em> 比如</p>
<p>访问 <em>https://你的域名/<a href="https://store.steampowered.com/">https://store.steampowered.com</a></em> 反代的就是 <em><a href="https://store.steampowered.com/">https://store.steampowered.com</a></em><br>访问 <em>https://你的域名/<a href="https://api.steampowered.com/">https://api.steampowered.com</a></em> 反代的就是 <em><a href="https://api.steampowered.com/">https://api.steampowered.com</a></em> </p>
<h3 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h3><p>点击<strong>部署</strong><br><img src="https://image.admilk.us.kg/image/imgs202411291255126.png"></p>
<p>在下方出现以下提示就可以啦<br><img src="https://image.admilk.us.kg/image/imgs202411291256535.png"></p>
<p>访问 https://你的域名 出现下面界面就成功啦<br><img src="https://image.admilk.us.kg/image/imgs202411291306877.png"></p>
<p>当然,有概率你是会出现<strong>网页无法访问</strong>,这就是CloudFlare的部分域名被墙了……</p>
<p>这就是需要下一步的原因</p>
<h2 id="绑定域名"><a href="#绑定域名" class="headerlink" title="绑定域名"></a>绑定域名</h2><p>注册域名过程略</p>
<p>我们首先要将自己的域名 DNS 托管给 Cloudflare,返回在控制台选择“网站”选项卡,输入你注册的域名,点击继续</p>
<p><img src="https://image.admilk.us.kg/image/imgs202411291311417.png"></p>
<p>个人用选择FREE套餐即可,点击继续</p>
<p><img src="https://image.admilk.us.kg/image/imgs202411291314319.png"></p>
<p>点击<strong>继续前往激活</strong><br><img src="https://image.admilk.us.kg/image/imgs202411291315703.png"></p>
<p>点击<strong>确认</strong><br><img src="https://image.admilk.us.kg/image/imgs202411291316747.png"></p>
<p>将Cloudflare 给的名称服务器填在你注册域名的地方,这里不细说<br><img src="https://image.admilk.us.kg/image/imgs202411291317834.png"></p>
<p>填完后点击<strong>继续</strong>等待名称服务器更新(等待时间在几分钟到几个小时之间,不要急,成功后Cloudflare会发送邮件到你的邮箱)<br><img src="https://image.admilk.us.kg/image/imgs202411291318257.png"></p>
<p>刷新后域名的状态变为<strong>活动</strong>就可以了<br><img src="https://image.admilk.us.kg/image/imgs202411291323629.png"></p>
<h2 id="使用域名"><a href="#使用域名" class="headerlink" title="使用域名"></a>使用域名</h2><p>继续前往 <strong>Workers 和 Pages</strong> ,点击进入刚才创建的 <strong>Workers</strong> 服务<br><img src="https://image.admilk.us.kg/image/imgs202411291325385.png"></p>
<p>进入<strong>设置</strong>,在 <strong>域和路由</strong> 点击 <strong>添加</strong><br><img src="https://image.admilk.us.kg/image/imgs202411291327489.png"></p>
<p>点击<strong>自定义域</strong><br><img src="https://image.admilk.us.kg/image/imgs202411291328315.png"><br>输入任意的域名前缀+你的域名.例如<em>proxy.example.com</em> , 将<em>example.com</em>替换为你的域名<br><img src="https://image.admilk.us.kg/image/imgs202411291328844.png"><br>点击<strong>添加域</strong>即可<br><img src="https://image.admilk.us.kg/image/imgs202411291330420.png"></p>
<p>然后将 Cloudflare 的域名替换为自己的即可</p>
<h2 id="拓展"><a href="#拓展" class="headerlink" title="拓展"></a>拓展</h2><p>即使指定反代依旧有泄露被滥用的风险,那就浅浅加个用户白名单吧(doge)</p>
<p>加了白名单的话通用反代方便一点</p>
<h3 id="IP白名单"><a href="#IP白名单" class="headerlink" title="IP白名单"></a>IP白名单</h3><p>将下面的<strong>IP白名单</strong>换成你自己的服务器IP即可,极大的保证了不被滥用以及稳定不被墙</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//IP白名单</span></span><br><span class="line"><span class="keyword">const</span> allowedIPs = [</span><br><span class="line"> <span class="comment">// IPv4 示例</span></span><br><span class="line"> <span class="string">'123.123.123.123'</span>,</span><br><span class="line"> <span class="comment">// IPv6 示例</span></span><br><span class="line"> <span class="string">'2001:0db8:85a3:0000:0000:8a2e:0370:7334'</span></span><br><span class="line">];</span><br></pre></td></tr></table></figure>
<h4 id="通用反代-1"><a href="#通用反代-1" class="headerlink" title="通用反代"></a>通用反代</h4><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title function_">addEventListener</span>(<span class="string">'fetch'</span>, <span class="function"><span class="params">event</span> =></span> {</span><br><span class="line"> event.<span class="title function_">respondWith</span>(<span class="title function_">handleRequest</span>(event.<span class="property">request</span>));</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> specialCases = {</span><br><span class="line"> <span class="string">"*"</span>: {</span><br><span class="line"> <span class="string">"Origin"</span>: <span class="string">"DELETE"</span>,</span><br><span class="line"> <span class="string">"Referer"</span>: <span class="string">"DELETE"</span></span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="comment">//IP白名单</span></span><br><span class="line"><span class="keyword">const</span> allowedIPs = [</span><br><span class="line"> <span class="comment">// IPv4 示例</span></span><br><span class="line"> <span class="string">'123.123.123.123'</span>,</span><br><span class="line"> <span class="comment">// IPv6 示例</span></span><br><span class="line"> <span class="string">'2001:0db8:85a3:0000:0000:8a2e:0370:7334'</span></span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">handleSpecialCases</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> rules = specialCases[<span class="string">"*"</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">const</span> [key, value] <span class="keyword">of</span> <span class="title class_">Object</span>.<span class="title function_">entries</span>(rules)) {</span><br><span class="line"> <span class="keyword">switch</span> (value) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"KEEP"</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"DELETE"</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">delete</span>(key);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="attr">default</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">set</span>(key, value);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">handleRequest</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> url = <span class="keyword">new</span> <span class="title function_">URL</span>(request.<span class="property">url</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 获取用户 IP 地址(支持 IPv4 和 IPv6)</span></span><br><span class="line"> <span class="keyword">const</span> userIP = request.<span class="property">headers</span>.<span class="title function_">get</span>(<span class="string">'CF-Connecting-IP'</span>) || request.<span class="property">headers</span>.<span class="title function_">get</span>(<span class="string">'X-Forwarded-For'</span>) || request.<span class="property">connection</span>.<span class="property">remoteAddress</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 检查 IP 是否在白名单内</span></span><br><span class="line"> <span class="keyword">if</span> (!allowedIPs.<span class="title function_">includes</span>(userIP)) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">`</span></span><br><span class="line"><span class="string"> <html></span></span><br><span class="line"><span class="string"> <head></span></span><br><span class="line"><span class="string"> <style></span></span><br><span class="line"><span class="string"> body {</span></span><br><span class="line"><span class="string"> display: flex;</span></span><br><span class="line"><span class="string"> justify-content: center;</span></span><br><span class="line"><span class="string"> align-items: center;</span></span><br><span class="line"><span class="string"> height: 100vh;</span></span><br><span class="line"><span class="string"> margin: 0;</span></span><br><span class="line"><span class="string"> background-color: #f9f9f9;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> h1 {</span></span><br><span class="line"><span class="string"> font-family: Arial, sans-serif;</span></span><br><span class="line"><span class="string"> color: #333;</span></span><br><span class="line"><span class="string"> text-align: center;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> </style></span></span><br><span class="line"><span class="string"> </head></span></span><br><span class="line"><span class="string"> <body></span></span><br><span class="line"><span class="string"> <h1>Caused by playing Genshin Impact</h1></span></span><br><span class="line"><span class="string"> </body></span></span><br><span class="line"><span class="string"> </html></span></span><br><span class="line"><span class="string"> `</span>, {</span><br><span class="line"> <span class="attr">headers</span>: { <span class="string">"Content-Type"</span>: <span class="string">"text/html"</span> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 如果是访问根目录就返回一个HTML</span></span><br><span class="line"> <span class="keyword">if</span> (url.<span class="property">pathname</span> === <span class="string">"/"</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">`</span></span><br><span class="line"><span class="string"> <html></span></span><br><span class="line"><span class="string"> <head></span></span><br><span class="line"><span class="string"> <style></span></span><br><span class="line"><span class="string"> body {</span></span><br><span class="line"><span class="string"> display: flex;</span></span><br><span class="line"><span class="string"> justify-content: center;</span></span><br><span class="line"><span class="string"> align-items: center;</span></span><br><span class="line"><span class="string"> height: 100vh;</span></span><br><span class="line"><span class="string"> margin: 0;</span></span><br><span class="line"><span class="string"> background-color: #f9f9f9;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> h1 {</span></span><br><span class="line"><span class="string"> font-family: Arial, sans-serif;</span></span><br><span class="line"><span class="string"> color: #333;</span></span><br><span class="line"><span class="string"> text-align: center;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> </style></span></span><br><span class="line"><span class="string"> </head></span></span><br><span class="line"><span class="string"> <body></span></span><br><span class="line"><span class="string"> <h1>Why don't you play Genshin Impact?</h1></span></span><br><span class="line"><span class="string"> </body></span></span><br><span class="line"><span class="string"> </html></span></span><br><span class="line"><span class="string"> `</span>, {</span><br><span class="line"> <span class="attr">headers</span>: { <span class="string">"Content-Type"</span>: <span class="string">"text/html"</span> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (url.<span class="property">pathname</span>.<span class="title function_">startsWith</span>(<span class="string">"/"</span>)) {</span><br><span class="line"> <span class="keyword">let</span> targetUrl = url.<span class="property">pathname</span>.<span class="title function_">slice</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 自动补全协议</span></span><br><span class="line"> <span class="keyword">if</span> (!<span class="regexp">/^https?:\/\//</span>.<span class="title function_">test</span>(targetUrl)) {</span><br><span class="line"> targetUrl = <span class="string">"https://"</span> + targetUrl;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 动态生成URL</span></span><br><span class="line"> <span class="keyword">const</span> modifiedRequest = <span class="keyword">new</span> <span class="title class_">Request</span>(targetUrl, {</span><br><span class="line"> <span class="attr">headers</span>: request.<span class="property">headers</span>,</span><br><span class="line"> <span class="attr">method</span>: request.<span class="property">method</span>,</span><br><span class="line"> <span class="attr">body</span>: request.<span class="property">body</span>,</span><br><span class="line"> <span class="attr">redirect</span>: <span class="string">'follow'</span></span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> <span class="title function_">handleSpecialCases</span>(modifiedRequest);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 发送请求</span></span><br><span class="line"> <span class="keyword">const</span> response = <span class="keyword">await</span> <span class="title function_">fetch</span>(modifiedRequest);</span><br><span class="line"> <span class="keyword">const</span> modifiedResponse = <span class="keyword">new</span> <span class="title class_">Response</span>(response.<span class="property">body</span>, response);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 允许跨域</span></span><br><span class="line"> modifiedResponse.<span class="property">headers</span>.<span class="title function_">set</span>(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modifiedResponse;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">"Path not found"</span>, { <span class="attr">status</span>: <span class="number">404</span> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="指定反代-1"><a href="#指定反代-1" class="headerlink" title="指定反代"></a>指定反代</h4><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="title function_">addEventListener</span>(<span class="string">'fetch'</span>, <span class="function"><span class="params">event</span> =></span> {</span><br><span class="line"> event.<span class="title function_">respondWith</span>(<span class="title function_">handleRequest</span>(event.<span class="property">request</span>));</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="comment">// 域名映射表</span></span><br><span class="line"><span class="keyword">const</span> domainMappings = {</span><br><span class="line"> <span class="string">"/steam-store"</span>: <span class="string">"https://store.steampowered.com"</span>,</span><br><span class="line"> <span class="string">"/steam-api"</span>: <span class="string">"https://api.steampowered.com"</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> specialCases = {</span><br><span class="line"> <span class="string">"*"</span>: {</span><br><span class="line"> <span class="string">"Origin"</span>: <span class="string">"DELETE"</span>,</span><br><span class="line"> <span class="string">"Referer"</span>: <span class="string">"DELETE"</span></span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="comment">//IP白名单</span></span><br><span class="line"><span class="keyword">const</span> allowedIPs = [</span><br><span class="line"> <span class="comment">// IPv4 示例</span></span><br><span class="line"> <span class="string">'123.123.123.123'</span>,</span><br><span class="line"> <span class="comment">// IPv6 示例</span></span><br><span class="line"> <span class="string">'2001:0db8:85a3:0000:0000:8a2e:0370:7334'</span></span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">handleSpecialCases</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> rules = specialCases[<span class="string">"*"</span>];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">const</span> [key, value] <span class="keyword">of</span> <span class="title class_">Object</span>.<span class="title function_">entries</span>(rules)) {</span><br><span class="line"> <span class="keyword">switch</span> (value) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"KEEP"</span>:</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">"DELETE"</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">delete</span>(key);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> <span class="attr">default</span>:</span><br><span class="line"> request.<span class="property">headers</span>.<span class="title function_">set</span>(key, value);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取用户 IP 地址</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">getUserIP</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">return</span> request.<span class="property">headers</span>.<span class="title function_">get</span>(<span class="string">'CF-Connecting-IP'</span>) || request.<span class="property">headers</span>.<span class="title function_">get</span>(<span class="string">'X-Forwarded-For'</span>) || request.<span class="property">connection</span>.<span class="property">remoteAddress</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">handleRequest</span>(<span class="params">request</span>) {</span><br><span class="line"> <span class="keyword">const</span> url = <span class="keyword">new</span> <span class="title function_">URL</span>(request.<span class="property">url</span>);</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">const</span> userIP = <span class="title function_">getUserIP</span>(request);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 检查 IP 是否在白名单内</span></span><br><span class="line"> <span class="keyword">if</span> (!allowedIPs.<span class="title function_">includes</span>(userIP)) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">`</span></span><br><span class="line"><span class="string"> <html></span></span><br><span class="line"><span class="string"> <head></span></span><br><span class="line"><span class="string"> <style></span></span><br><span class="line"><span class="string"> body {</span></span><br><span class="line"><span class="string"> display: flex;</span></span><br><span class="line"><span class="string"> justify-content: center;</span></span><br><span class="line"><span class="string"> align-items: center;</span></span><br><span class="line"><span class="string"> height: 100vh;</span></span><br><span class="line"><span class="string"> margin: 0;</span></span><br><span class="line"><span class="string"> background-color: #f9f9f9;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> h1 {</span></span><br><span class="line"><span class="string"> font-family: Arial, sans-serif;</span></span><br><span class="line"><span class="string"> color: #333;</span></span><br><span class="line"><span class="string"> text-align: center;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> </style></span></span><br><span class="line"><span class="string"> </head></span></span><br><span class="line"><span class="string"> <body></span></span><br><span class="line"><span class="string"> <h1>Caused by playing Genshin Impact</h1></span></span><br><span class="line"><span class="string"> </body></span></span><br><span class="line"><span class="string"> </html></span></span><br><span class="line"><span class="string"> `</span>, {</span><br><span class="line"> <span class="attr">headers</span>: { <span class="string">"Content-Type"</span>: <span class="string">"text/html"</span> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 如果是访问根目录就返回一个HTML</span></span><br><span class="line"> <span class="keyword">if</span> (url.<span class="property">pathname</span> === <span class="string">"/"</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">`</span></span><br><span class="line"><span class="string"> <html></span></span><br><span class="line"><span class="string"> <head></span></span><br><span class="line"><span class="string"> <style></span></span><br><span class="line"><span class="string"> body {</span></span><br><span class="line"><span class="string"> display: flex;</span></span><br><span class="line"><span class="string"> justify-content: center;</span></span><br><span class="line"><span class="string"> align-items: center;</span></span><br><span class="line"><span class="string"> height: 100vh;</span></span><br><span class="line"><span class="string"> margin: 0;</span></span><br><span class="line"><span class="string"> background-color: #f9f9f9;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> h1 {</span></span><br><span class="line"><span class="string"> font-family: Arial, sans-serif;</span></span><br><span class="line"><span class="string"> color: #333;</span></span><br><span class="line"><span class="string"> text-align: center;</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> </style></span></span><br><span class="line"><span class="string"> </head></span></span><br><span class="line"><span class="string"> <body></span></span><br><span class="line"><span class="string"> <h1>Why don't you play Genshin Impact?</h1></span></span><br><span class="line"><span class="string"> </body></span></span><br><span class="line"><span class="string"> </html></span></span><br><span class="line"><span class="string"> `</span>, {</span><br><span class="line"> <span class="attr">headers</span>: { <span class="string">"Content-Type"</span>: <span class="string">"text/html"</span> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> basePath = <span class="title class_">Object</span>.<span class="title function_">keys</span>(domainMappings).<span class="title function_">find</span>(<span class="function"><span class="params">path</span> =></span> url.<span class="property">pathname</span>.<span class="title function_">startsWith</span>(path));</span><br><span class="line"> <span class="keyword">if</span> (!basePath) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Response</span>(<span class="string">"Path not found in domain mappings"</span>, { <span class="attr">status</span>: <span class="number">404</span> });</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> targetBase = domainMappings[basePath];</span><br><span class="line"> <span class="keyword">const</span> targetPath = url.<span class="property">pathname</span>.<span class="title function_">slice</span>(basePath.<span class="property">length</span>) + url.<span class="property">search</span> + url.<span class="property">hash</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> targetUrl = <span class="keyword">new</span> <span class="title function_">URL</span>(targetPath, targetBase);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">const</span> modifiedRequest = <span class="keyword">new</span> <span class="title class_">Request</span>(targetUrl, {</span><br><span class="line"> <span class="attr">headers</span>: request.<span class="property">headers</span>,</span><br><span class="line"> <span class="attr">method</span>: request.<span class="property">method</span>,</span><br><span class="line"> <span class="attr">body</span>: request.<span class="property">body</span>,</span><br><span class="line"> <span class="attr">redirect</span>: <span class="string">'follow'</span></span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> <span class="title function_">handleSpecialCases</span>(modifiedRequest);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 发送请求</span></span><br><span class="line"> <span class="keyword">const</span> response = <span class="keyword">await</span> <span class="title function_">fetch</span>(modifiedRequest);</span><br><span class="line"> <span class="keyword">const</span> modifiedResponse = <span class="keyword">new</span> <span class="title class_">Response</span>(response.<span class="property">body</span>, response);</span><br><span class="line"></span><br><span class="line"> modifiedResponse.<span class="property">headers</span>.<span class="title function_">set</span>(<span class="string">'Access-Control-Allow-Origin'</span>, <span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modifiedResponse;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id=""><a href="#" class="headerlink" title=""></a></h4>]]></content>
<tags>
<tag>CloudFlare</tag>
<tag>代理</tag>
</tags>
</entry>
<entry>
<title>npm,pnpm,yarn换源</title>
<url>/2024/12/02/npm-pnpm-yarn%E6%8D%A2%E6%BA%90/</url>
<content><![CDATA[<h1 id="npm-pnpm-yarn换源以及一些问题"><a href="#npm-pnpm-yarn换源以及一些问题" class="headerlink" title="npm,pnpm,yarn换源以及一些问题"></a>npm,pnpm,yarn换源以及一些问题</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>使用<em>npm,pnpm,yarn</em>时经常出现网络问题,选择并配置国内镜像源可以显著提高包的下载速度和稳定性。</p>
<h2 id="npm"><a href="#npm" class="headerlink" title="npm"></a>npm</h2><p><strong>查询当前源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm get registry</span><br></pre></td></tr></table></figure>
<p><strong>设置淘宝镜像源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm config <span class="built_in">set</span> registry https://registry.npmmirror.com</span><br></pre></td></tr></table></figure>
<p>如果出现一些问题可以换回<strong>官方源</strong>重试</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm config <span class="built_in">set</span> registry https://registry.npmjs.org/</span><br></pre></td></tr></table></figure>
<p>npm首次运行时会出现</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm : 无法加载文件 C:\Users\用户名\software\nodejs\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execu</span><br><span class="line">tion_Policies。</span><br><span class="line">所在位置 行:1 字符: 1</span><br><span class="line">+ npm run start</span><br><span class="line">+ ~~~</span><br><span class="line"> + CategoryInfo : SecurityError: (:) [],PSSecurityException</span><br></pre></td></tr></table></figure>
<p>解决办法: 使用<strong>管理员权限</strong>打开<em>CMD</em>或者<em>PowerShell</em>执行下面的命令将计算机上的执行策略更改为 <em>RemoteSigned</em></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">set-ExecutionPolicy RemoteSigned</span><br></pre></td></tr></table></figure>
<h2 id="pnpm"><a href="#pnpm" class="headerlink" title="pnpm"></a>pnpm</h2><p><strong>安装</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install pnpm -g</span><br></pre></td></tr></table></figure>
<p><strong>查询当前源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">pnpm get registry</span><br></pre></td></tr></table></figure>
<p><strong>设置淘宝镜像源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">pnpm config <span class="built_in">set</span> registry https://registry.npmmirror.com</span><br></pre></td></tr></table></figure>
<p><strong>换回官方源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">pnpm config <span class="built_in">set</span> registry https://registry.npmjs.org</span><br></pre></td></tr></table></figure>
<h2 id="yarn"><a href="#yarn" class="headerlink" title="yarn"></a>yarn</h2><p><strong>安装</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install yarn -g</span><br></pre></td></tr></table></figure>
<p><strong>查询当前源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">yarn config get registry</span><br></pre></td></tr></table></figure>
<p><strong>设置淘宝镜像源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">yarn config <span class="built_in">set</span> registry https://registry.npmmirror.com</span><br></pre></td></tr></table></figure>
<p><strong>换回官方源</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">yarn config <span class="built_in">set</span> registry https://registry.yarnpkg.com</span><br></pre></td></tr></table></figure>
<h2 id="拓展-使用nrm切换镜像源"><a href="#拓展-使用nrm切换镜像源" class="headerlink" title="拓展:使用nrm切换镜像源"></a>拓展:使用<em>nrm</em>切换镜像源</h2><h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3><p>nrm(npm registry manager)是一个<strong>npm</strong>源管理工具,它可以帮助用户快速地在不同的<strong>npm</strong>源之间切换,以提高包的下载速度和稳定性</p>
<h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install nrm -g</span><br></pre></td></tr></table></figure>
<h3 id="查看可选择的源"><a href="#查看可选择的源" class="headerlink" title="查看可选择的源"></a>查看可选择的源</h3><p>安装完成后,你可以使用以下命令查看所有可用的 npm 源:</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm <span class="built_in">ls</span></span><br></pre></td></tr></table></figure>
<p>这将列出所有已知的<strong>npm</strong>源,包括官方源和你可能添加的自定义源。</p>
<h3 id="切换镜像源"><a href="#切换镜像源" class="headerlink" title="切换镜像源"></a>切换镜像源</h3><p>要切换到特定的源,执行以下命令</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm use <镜像源名称></span><br></pre></td></tr></table></figure>
<p>例如,如果你想切换到淘宝的<strong>npm</strong>镜像源,你可以执行</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm use taobao</span><br></pre></td></tr></table></figure>
<p>其他同理</p>
<h3 id="增加镜像源"><a href="#增加镜像源" class="headerlink" title="增加镜像源"></a>增加镜像源</h3><p>如果给出的镜像源没有你想用的,你可以增加定制的源</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm add <镜像源名称> <镜像源地址></span><br></pre></td></tr></table></figure>
<h3 id="删除镜像源"><a href="#删除镜像源" class="headerlink" title="删除镜像源"></a>删除镜像源</h3><p>如果你想要删除一个已添加的源,可以使用以下命令</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm del <镜像源名称></span><br></pre></td></tr></table></figure>
<p>例如,删除 <code>淘宝</code> 的镜像源</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm del taobao</span><br></pre></td></tr></table></figure>
<h3 id="测试镜像源的速度"><a href="#测试镜像源的速度" class="headerlink" title="测试镜像源的速度"></a>测试镜像源的速度</h3><p><code>nrm</code> 支持通过 <code>nrm test</code> 命令测试相应源的响应时间,以选择适合的镜像源</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm <span class="built_in">test</span> <registry></span><br></pre></td></tr></table></figure>
<p>例如,测试 <code>npm</code> 源的响应时间</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm <span class="built_in">test</span> npm</span><br></pre></td></tr></table></figure>
<p>你也可以直接测试所有源的响应时间</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">nrm <span class="built_in">test</span></span><br></pre></td></tr></table></figure>
<div class="note warning simple"><p><strong>注意版本</strong>:从 nrm v1.4.0 开始,最低支持的 Node 版本是 18</p>
</div>
]]></content>
<tags>
<tag>npm</tag>
</tags>
</entry>
</search>