网页版terminal,真香!


写在前面

在做项目过程中,由于甲方特殊网络需求,只能内网访问,并且只批准了团队内少数人通过vpn访问,其他人都无法访问。这个时候,如何能访问甲方内网的人能在本地开启一个网页版terminal,团队其他人通过网页版terminal访问甲方内容,是不是很香?

通过在网上查找资料,还真被我找到了一个。

ttyd

参考文件1中提到了5个网页版工具,本文介绍其中一个ttyd,非常简单好用。

运行方式1:本机运行

本机安装:

brew install ttyd

安装完成后输入ttyd

 #ttyd
ttyd is a tool for sharing terminal over the web

USAGE:
    ttyd [options] <command> [<arguments...>]

VERSION:
    1.7.3

OPTIONS:
    -p, --port              Port to listen (default: 7681, use `0` for random port)
    -i, --interface         Network interface to bind (eg: eth0), or UNIX domain socket path (eg: /var/run/ttyd.sock)
    -U, --socket-owner      User owner of the UNIX domain socket file, when enabled (eg: user:group)
    -c, --credential        Credential for basic authentication (format: username:password)
    -H, --auth-header       HTTP Header name for auth proxy, this will configure ttyd to let a HTTP reverse proxy handle authentication
    -u, --uid               User id to run with
    -g, --gid               Group id to run with
    -s, --signal            Signal to send to the command when exit it (default: 1, SIGHUP)
    -w, --cwd               Working directory to be set for the child program
    -a, --url-arg           Allow client to send command line arguments in URL (eg: http://localhost:7681?arg=foo&arg=bar)
    -R, --readonly          Do not allow clients to write to the TTY
    -t, --client-option     Send option to client (format: key=value), repeat to add more options
    -T, --terminal-type     Terminal type to report, default: xterm-256color
    -O, --check-origin      Do not allow websocket connection from different origin
    -m, --max-clients       Maximum clients to support (default: 0, no limit)
    -o, --once              Accept only one client and exit on disconnection
    -B, --browser           Open terminal with the default system browser
    -I, --index             Custom index.html path
    -b, --base-path         Expected base path for requests coming from a reverse proxy (eg: /mounted/here, max length: 128)
    -P, --ping-interval     Websocket ping interval(sec) (default: 5)
    -6, --ipv6              Enable IPv6 support
    -S, --ssl               Enable SSL
    -C, --ssl-cert          SSL certificate file path
    -K, --ssl-key           SSL key file path
    -A, --ssl-ca            SSL CA file path for client certificate verification
    -d, --debug             Set log level (default: 7)
    -v, --version           Print the version and exit
    -h, --help              Print this text and exit

Visit https://github.com/tsl0922/ttyd to get more information and report bugs.

启动:ttyd zsh

 Documents# ttyd zsh
[2023/06/06 23:10:34:6630] N: ttyd 1.7.3 (libwebsockets 4.3.2-unknown)
[2023/06/06 23:10:34:6641] N: tty configuration:
[2023/06/06 23:10:34:6641] N:   start command: zsh
[2023/06/06 23:10:34:6641] N:   close signal: SIGHUP (1)
[2023/06/06 23:10:34:6641] N:   terminal type: xterm-256color
[2023/06/06 23:10:34:6653] N:    /usr/local/Cellar/libwebsockets/4.3.2/lib/libwebsockets-evlib_uv.dylib
[2023/06/06 23:10:34:6653] N: lws_create_context: LWS: 4.3.2-unknown, NET CLI SRV H1 H2 WS ConMon IPV6-off
[2023/06/06 23:10:34:6657] N: elops_init_pt_uv:  Using foreign event loop...
[2023/06/06 23:10:34:6657] N: __lws_lc_tag:  ++ [wsi|0|pipe] (1)
[2023/06/06 23:10:34:6658] N: __lws_lc_tag:  ++ [vh|0|default||7681] (1)
[2023/06/06 23:10:34:6690] N: [vh|0|default||7681]: lws_socket_bind: source ads 0.0.0.0
[2023/06/06 23:10:34:6691] N: __lws_lc_tag:  ++ [wsi|1|listen|default||7681] (2)
[2023/06/06 23:10:34:6691] N:  Listening on port: 7681

访问:http://localhost:7681,即可进入web版terminal,并且可以执行所有能在terminal执行的命令,如同在本地运行terminal一样,非常方便。

它还支持更改端口、用户认证、ssh终端等。

运行方式2:docker运行

docker run -it --rm -p 7681:7681 tsl0922/ttyd

docker运行更方便了,启动之后直接访问http://localhost:7681就可以了。

公网暴露

还发现一个好玩的工具,如果想在公网上暴露自己本地的某个服务,比如上面说的web terminal该如何做呢?上面这种web terminal只能在本地或者局域网内访问。

如果团队某个成员在远方或者其他人想看看你本地开发的程序,这个时候你的程序只是在本地运行的,该如何分享给别人呢?

这里介绍一个工具ngrok,安装非常容易,官网有介绍。需要先注册一下

执行:ngrok http 7681

然后公网访问https://57b6-101-224-89-167.ngrok-free.app,就像访问http://localhost:7681一样啦,是不是很方便。

如果被强了无法访问ngrok官网,也可以参照参考文件3,自己搭建一个ngrok,它是开源的。

参考

[1] 在浏览器里面运行命令行,真香!

[2]https://dashboard.ngrok.com

[3]搭建 ngrok 服务实现内网穿透


文章作者: Alex
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex !
  目录