背景

最近看到一款出自niruix大神的自建WebSSH源码。试用了一下觉得非常不错,就想推荐+记录一下。因为第一次用Heroku手动部署,所以折腾了好久才可以。本文记录了如何利用免费云平台Heroku搭建sshwifty。

什么是WebSSH

使用WebSocket通过浏览器连接linux,提供灵活的接口,可直接作为一个服务连接通过cmdb获取信息登陆服务器。从而无需Xshell之类的模拟终端工具进行SSH连接,可以比较方便的再多平台使用。

什么是sshwifty

Sshwifty is a SSH and Telnet connector made for the Web. It can be deployed on your computer or server to provide SSH and Telnet access interface for any compatible (standard) web browser.

以上来自官方项目页面的介绍。

部署方式

这个项目提供了多种部署方式。

Binary

通过release下载二进制文件直接使用。

Docker Image

如果你善于使用Docker,可以利用官方提供的Docker镜像来部署。

$ docker run --detach \
  --restart=always \
  --publish 8182:8182 \
  --name sshwifty \
  niruix/sshwifty:latest

当比需要TLS并且不想设置Docker Volumes时,可以使用SSHWIFTY_DOCKER_TLSCERTSSHWIFTY_DOCKER_TLSCERTKEY环境变量将凭据文件导入容器并自动应用它们:

$ openssl req \
  -newkey rsa:4096 -nodes -keyout domain.key -x509 -days 90 -out domain.crt
$ docker run --detach \
  --restart=always \
  --publish 8182:8182 \
  --env SSHWIFTY_DOCKER_TLSCERT="$(cat domain.crt)" \
  --env SSHWIFTY_DOCKER_TLSCERTKEY="$(cat domain.key)" \
  --name sshwifty \
  niruix/sshwifty:latest

domain.crtdomain.key必须是有效的TLS证书和密钥文件,位于要执行docker run命令的同一台计算机上。

Compile from source code (Recommanded if you’re a developer)

官方推荐开发者使用通过源码编译这个方法。

你的机器需要以下环境

  • git用于拉取源代码
  • nodenpm用于构建前端应用程序
  • go用于建立后端应用程序

执行以下命令

$ git clone https://github.com/niruix/sshwifty
$ cd sshwifty
$ npm install
$ npm run build

Deploy on the cloud *

To deploy this project onto the cloud, Google App Engine or Heroku for example, you need to first download the source code, then generate it locally before deploying it.

npm run generate command will generate all static files and automatically call go generate ./... to bind those static files directly into program source code. And you need those generated source code to get the software to function.

Trying to deploy ungenerated code directly to cloud will lead to failure, as required source code is missing.

Also keep in mind, if the cloud deployment process is git based, you may have to modify .gitignore file in order to allow all required files to be uploaded.

以上来自官方介绍,但是在issues#5作者提供了一个更简单的方法,就是使用.heroku.yml来快速部署。下面就是这次部署的教程。

heroku.yml内容

build:
  config:
    SSHWIFTY_LISTENPORT: SSHWIFTY_ENV_RENAMED:PORT
  docker:
    web: Dockerfile
run:
  web: /sshwifty.sh

部署到Heroku详细教程

本文主要详细记录了如何将sshwifty部署到免费云平台Heroku。

什么是Heroku

Heroku是一个支持多种编程语言的云平台即服务。在2010年被Salesforce.com收购。Heroku作为最元祖的云平台之一,从2007年6月起开发,当时它仅支持Ruby,但后来增加了对Java、Node.js、Scala、Clojure、Python以及PHP和Perl的支持。 – 来自维基百科

主要是Heroku提供了免费的套餐,未验证信用卡用户可以免费使用550小时,验证之后长达1000小时,用一个月绰绰有余了。

验证信用卡之后有以下好处: - Use more than one dyno in the app. - Add any add-on to the app, even if the add-on is free. The only exceptions to this are the free plans for the Heroku Postgres and Heroku Connect add-ons, which can be used without verification. - Add a custom domain to the app. - Receive the transfer of an app that has paid resources. - Exceed default one-off dyno limits on the app. - Have more than 5 apps at a time. Verified accounts may have up to 100 apps.

通过Account-Billing可以查看到当前使用时长和剩余时长 没有验证信用卡的使用情况

没有验证信用卡的使用情况
没有验证信用卡的使用情况

验证信用卡后的使用情况

验证信用卡后的使用情况
验证信用卡后的使用情况

Heroku的使用教程,会写一个的。这里先略过。

部署sshwifty到Heroku

1.首先确保本地安装了git,node,npm,heroku CLI

$ git version
git version 1.8.3.1
$ node -v
v10.19.0
$ npm -v
6.13.4
$ heroku -v
heroku/7.38.2 linux-x64 node-v12.13.0

如果版本结果,说明已经安装了。

如果没有安装,一下几个教程可做参考: - Git安装教程 - 安装Node.js和npm - The Heroku CLI

2.接着拉取作者源码到本地

$ git clone https://github.com/niruix/sshwifty
$ cd sshwifty

2.查看.heroku.yml是否存在

$ ls
application      go.mod      package.json       sshwifty.conf.example.json
babel.config.js  go.sum      package-lock.json  sshwifty.go
DEPENDENCIES.md  heroku.yml  README.md          ui
Dockerfile       LICENSE.md  Screenshot.png     webpack.config.js

3.登录Heroku

$ heroku login
heroku: Press any key to open up the browser to login or q to exit
 ›   Warning: If browser does not open, visit
 ›   https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com

我这边浏览器没弹出登录窗口,需要自己将终端显示的URL复制到浏览器打开点击登录后,终端就会自动识别登录状态。

4.添加Heroku远程库 yourapp为你在Heroku创建的应用名字(App name)

$ heroku git:remote -a yourapp

5.查看是否成功添加远程库

$ git remote -v
heroku  https://git.heroku.com/yourapp.git (fetch)
heroku  https://git.heroku.com/yourapp.git (push)
origin  https://github.com/niruix/sshwifty (fetch)
origin  https://github.com/niruix/sshwifty (push)

出现以上的话,说明添加成功。

6.将应用添加为容器 这一步是作者在issues#5回复的官方教程的”Get started” step 3。

$ heroku stack:set container

7.推送应用到Heroku远程库并部署

$ git push heroku master

最后应该出现类似以下结果

remote: Removing intermediate container 157d1927853a
remote:  ---> 461f43c3b47f
remote: Successfully built 461f43c3b47f
remote: Successfully tagged 85bc4064899667389e87d36d0d60e0356cf6e759:latest
remote:
remote: === Pushing web (Dockerfile)
remote: Tagged image "85bc4064899667389e87d36d0d60e0356cf6e759" as "registry.heroku.com/wssh/web"
remote: The push refers to repository [registry.heroku.com/wssh/web]
remote: b06b44c43368: Preparing
remote: 58ad0d2fd01d: Preparing
remote: 8781a95c3755: Preparing
remote: 5216338b40a7: Preparing
remote: b06b44c43368: Pushed
remote: 8781a95c3755: Pushed
remote: 5216338b40a7: Pushed
remote: 58ad0d2fd01d: Pushed
remote: latest: digest: sha256:274d6bc3c207bd51629313a4a5b4535f22d35652f344e583064e4f860d980ef2 size: 1157
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/yourapp.git
 * [new branch]      master -> master

这时Heroku的后台也会显示Deployed b2210ce2状态 但是这样还是无法访问。还需要执行下面一步。

8.Heroku后台添加一条配置信息 这是为了告诉Sshwifty从Heroku运行时的环境变量PORT中读取端口号。 在 Heroku 你的应用的后台

https://dashboard.heroku.com/apps/yourapp/settings 

yourapp为你设置的应用名

在页面中找到Config Vars项目,点击Reveal Config Vars显示配置。

显示config前
显示config前

KEY填入SSHWIFTY_LISTENPORTVALUE 填入 SSHWIFTY_ENV_RENAMED:PORT 后点击 Add

填入相关值
填入相关值

添加后如下所示

填好的状态
填好的状态

这时候你再打开你的app页面就可以访问啦~

官方演示
官方演示

我这里搭建的地址是下面这个,如果有需要可以使用。

https://wssh.herokuapp.com/

注意点

最后一步的添加端口号设置很重要,不然无法访问。

Heroku的免费套餐如果应用在30分钟内没有访问,应用将会进入休眠状态,再次访问的时候会花点时间激活,但是也不会等太久。

参考资料