工具推荐2023 年 10 月 17 日

命令行工具 - bore

开发阶段例如微信公众号,小程序,可以将本地服务公开,方便开发;提供一种途径迅速分享本地成果
bore生效后本地dev公开至远程服务器域名example.henryhe.cn访问
bore生效后本地dev公开至远程服务器域名example.henryhe.cn访问

1. bore简介

A modern, simple TCP tunnel in Rust that exposes local ports to a remote server, bypassing standard NAT connection firewalls. That’s all it does: no more, and no less.

Rust语言编写的命令行工具,只办一件事:建立TCP通道,暴露本地端口至远程服务器

2. 什么样的场景需要bore

  • 微信公众号、小程序开发,仅支持域名进行接口访问,在开发阶段可以快速将本地的接口通过域名形式暴露出去供调试即常说的“内网穿透”,提升开发效率。

  • 将自己的demo可以快速分享出去供大家访问,收取建议。

3. 使用方法

我使用cargo安装,大家按照自己适合的方式安装即可。

3.1 直接将本地端口暴露至作者提供的server

执行如下命令即可,bore.pub会随机分配给你一个端口,只需要访问对应端口即可,例如http://bore.pub:13622就可以访问到本地3000端口
~/De/W/F/astro-air-blog main ⇡1 !9 ?7 ❯ bore local 3000 --to bore.pub                     ✘ INT Node 16.16.0 19:29:44
2023-10-17T11:29:50.770665Z  INFO bore_cli::client: connected to server remote_port=13622
2023-10-17T11:29:50.770726Z  INFO bore_cli::client: listening at bore.pub:13622
其他参数大家可以按照文档来使用。
Starts a local proxy to the remote server

Usage: bore local [OPTIONS] --to <TO> <LOCAL_PORT>

Arguments:
  <LOCAL_PORT>  The local port to expose

Options:
  -l, --local-host <HOST>  The local host to expose [default: localhost]
  -t, --to <TO>            Address of the remote server to expose local ports to [env: BORE_SERVER=]
  -p, --port <PORT>        Optional port on the remote server to select [default: 0]
  -s, --secret <SECRET>    Optional secret for authentication [env: BORE_SECRET]
  -h, --help               Print help information

3.2 self hosting - 使用自己的server

Github的Release页面下载自己对应的包,我这里使用Linux: bore-v0.5.0-x86_64-unknown-linux-musl.tar.gz 下载后,上传至自己的服务器。
我这里使用pm2启动了bore server:
[root@xxx ~]# ll
总用量 8896
-rwxr-xr-x 1 1001 docker 7045304 428 10:06 bore
-rwxrwxrwx 1 root root        73 726 12:31 bore.sh
-rw-r--r-- 1 root root   2055641 725 10:07 bore-v0.5.0-x86_64-unknown-linux-musl.tar.gz
[root@xxx boreserver]# cat bore.sh
#bin/bash

# your_min_port ~ your_max_port控制端口开放范围
# your_min_port指定最小端口
# your_max_port指定最大端口
# 指定的端口必须在该范围内
./bore server --min-port your_min_port --max-port your_max_port

[root@xxx boreserver]# pm2 start bore.sh
[root@xxx ~]# pm2 status

>>>> In-memory PM2 is out-of-date, do:
>>>> $ pm2 update
In memory PM2 version: 5.3.0
Local PM2 version: 4.5.0

┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 3  │ app                │ fork     │ 0    │ online    │ 0%       │ 46.3mb   │
│ 1  │ bore               │ fork     │ 0    │ online    │ 0%       │ 572.0kb  │
│ 2  │ jupyter            │ fork     │ 0    │ online    │ 0%       │ 576.0kb  │
│ 0  │ main               │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
server部署启动后,本地则可以向server映射如下:
bore local 3000 --to henryhe.cn --port your_port

❗️❗️❗️bore server 需要开放7835端口,否则无法建立连接。

3.3 自己部署的server如何只能让自己使用

使用secret参数设定认证参数,这样在本地穿透至server时可以通过secret来识别,从而开启映射:
server:
./bore server --min-port your_min_port --max-port your_max_port --secret your_secret

client:
bore local 3000 --to henryhe.cn --port your_port --secret your_secret
携带secret进行连接
携带secret进行连接