npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

libastion

v0.0.2

Published

A lightweight bastion host built with Node.js

Readme

轻量级堡垒机 - 基于 Node.js

一个基于 Node.js 实现的原生 SSH 堡垒机,支持命令审计、SFTP 文件传输审计、端口转发审计和命令黑白名单过滤。

功能特性

  • SSH 原生接入:用户通过标准 SSH 客户端连接堡垒机
  • 本地认证:用户名/密码认证,支持失败锁定机制
  • 角色权限:admin / auditor / user 三级角色
  • 目标主机托管:加密存储目标主机凭据
  • 用户授权:细粒度用户-主机授权管理
  • 命令审计:记录所有执行的命令
  • SFTP 审计:记录文件上传/下载操作
  • 端口转发审计:记录所有 SSH 端口转发请求
  • 命令黑白名单:可配置的命令过滤规则
  • 会话管理:并发上限、空闲超时、最大会话时长

快速开始

1. 安装依赖

npm install

2. 配置环境

cp .env.example .env

编辑 .env 文件:

# 生成主密钥
# BASTION_MASTER_KEY=$(node -e "console.log(require('crypto').randomBytes(32).toString('hex'))")
BASTION_MASTER_KEY=your-32-byte-hex-key-here

# 监听端口(默认 2222)
BASTION_PORT=2222

# 日志级别
BASTION_LOG_LEVEL=info

# 首次启动创建默认管理员时使用的密码(默认 admin123,生产环境务必设置)
# BASTION_ADMIN_PASSWORD=change-me-now

4. 生成 SSH 主机密钥

ssh-keygen -t ed25519 -f host.key -N ""

5. 启动服务

npm start
# 或
node bin/server.js

6. 使用 CLI 管理

# 添加目标主机
node bin/cli.js add-host "生产服务器" 192.168.1.100 22 deploy your_password

# 添加用户
node bin/cli.js add-user alice your_password

# 授权用户访问主机
node bin/cli.js grant alice "生产服务器"

# 列出目标主机
node bin/cli.js list-hosts

# 查看审计会话
node bin/cli.js list-sessions

# 查看会话命令日志
node bin/cli.js show-commands 1

7. 连接堡垒机

ssh admin@localhost -p 2222
# 默认管理员账号:admin / admin123(首次启动自动创建)

项目结构

bastion/
├── bin/
│   ├── server.js          # SSH 服务器入口
│   └── cli.js             # CLI 管理工具
├── src/
│   ├── config/            # 配置加载
│   ├── db/                # 数据库 (SQLite)
│   ├── auth/              # 认证与授权
│   ├── hosts/             # 目标主机管理
│   ├── session/           # SSH 会话代理
│   ├── policy/            # 命令过滤
│   ├── audit/             # 审计日志
│   └── cli/               # CLI 命令实现
├── data/                  # 数据库文件
├── logs/                  # 日志文件
├── config.json            # 配置文件
├── .env.example           # 环境变量模板
├── package.json
└── README.md

配置说明

config.json

{
  "server": {
    "port": 2222,
    "maxSessions": 50,
    "sessionTimeoutSec": 1800,
    "idleTimeoutSec": 900
  },
  "auth": {
    "maxFailedAttempts": 5,
    "lockDurationSec": 900
  },
  "policy": {
    "blockedCommands": [
      "^rm\\s+(-[a-z]*\\s+)*-[rfR].*",
      "^shutdown\\b",
      "^reboot\\b",
      "^mkfs\\b"
    ]
  }
}

部署

使用 pm2

npm install -g pm2
pm2 start bin/server.js --name bastion
pm2 save
pm2 startup

使用 systemd

创建 /etc/systemd/system/bastion.service

[Unit]
Description=Bastion Host Service
After=network.target

[Service]
Type=simple
User=bastion
WorkingDirectory=/opt/bastion
ExecStart=/usr/bin/node bin/server.js
Restart=on-failure
RestartSec=5
EnvironmentFile=/opt/bastion/.env

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable bastion
sudo systemctl start bastion

默认账号

  • 用户名:admin
  • 密码:admin123

⚠️ 首次启动后请立即修改默认密码!

node bin/cli.js update-password admin new_password

技术栈

  • Node.js >= 22.5
  • ssh2 - SSH Server + Client
  • node:sqlite - 数据库
  • bcryptjs - 密码哈希
  • winston - 日志
  • commander - CLI 工具
  • dotenv - 环境变量

安全建议

  1. 主密钥管理:通过环境变量传入,不要写入代码
  2. 定期轮换:定期更换 BASTION_MASTER_KEY
  3. 日志轮转:配置日志文件轮转策略
  4. 审计完整性:后续可扩展为哈希链防篡改
  5. 网络隔离:堡垒机应部署在受信任的网络中

后续扩展

  • [ ] 对接 LDAP / AD / SSO
  • [ ] TOTP 双因素认证
  • [ ] 完整会话逐字符录制与回放
  • [ ] Web 管理面板
  • [ ] 集群化与高可用
  • [ ] API 接口(REST/GraphQL)

License

MIT