telegram-ssh-bot 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +294 -0
  2. package/package.json +63 -0
package/README.md ADDED
@@ -0,0 +1,294 @@
1
+ # Telegram SSH Bot
2
+
3
+ A secure, production-ready Telegram bot for remote SSH server management.
4
+
5
+ ## Features
6
+
7
+ - **Server Management**: Add, remove, list, and connect to SSH servers
8
+ - **Remote Command Execution**: Execute commands on connected servers via Telegram
9
+ - **Security**:
10
+ - AES-256-GCM credential encryption
11
+ - Input validation and command sanitization
12
+ - Rate limiting
13
+ - Owner-only authorization
14
+ - **Reliability**:
15
+ - Connection pooling
16
+ - Automatic reconnection
17
+ - Graceful shutdown
18
+ - Health monitoring
19
+ - **Operations**:
20
+ - Structured logging
21
+ - Automatic backups
22
+ - Admin notifications
23
+ - Systemd service support
24
+
25
+ ## Quick Start
26
+
27
+ ### Prerequisites
28
+
29
+ - Node.js 18+ (for development)
30
+ - Linux (for production deployment)
31
+
32
+ ### Installation
33
+
34
+ #### From Binary (Recommended)
35
+
36
+ ```bash
37
+ # Download and install
38
+ ./deploy/install.sh
39
+
40
+ # Configure
41
+ nano ~/.config/telegram-ssh-bot/.env
42
+
43
+ # Start
44
+ ./deploy/manage.sh start
45
+ ```
46
+
47
+ #### From Source
48
+
49
+ ```bash
50
+ # Clone and build
51
+ git clone https://github.com/user/telegram-ssh-bot.git
52
+ cd telegram-ssh-bot
53
+ npm install
54
+ npm run build:binary
55
+
56
+ # Install
57
+ ./deploy/install.sh
58
+ ```
59
+
60
+ ## Configuration
61
+
62
+ Create `~/.config/telegram-ssh-bot/.env`:
63
+
64
+ ```bash
65
+ # Required
66
+ BOT_TOKEN=your_telegram_bot_token
67
+ BOT_CHAT_ID=your_chat_id
68
+ BOT_OWNER_IDS=123456789,987654321
69
+ ENCRYPTION_KEY=64_char_hex_key
70
+
71
+ # Optional
72
+ LOG_LEVEL=info
73
+ SSH_COMMAND_TIMEOUT=30000
74
+ ```
75
+
76
+ ### Configuration Options
77
+
78
+ | Variable | Required | Description |
79
+ | --------------------- | -------- | ----------------------------------------------------------------- |
80
+ | `BOT_TOKEN` | Yes | Telegram bot token from [@BotFather](https://t.me/botfather) |
81
+ | `BOT_CHAT_ID` | Yes | Primary chat ID for notifications |
82
+ | `BOT_OWNER_IDS` | Yes | Comma-separated list of authorized user IDs |
83
+ | `ENCRYPTION_KEY` | Yes | 64-character hex key for credential encryption |
84
+ | `LOG_LEVEL` | No | Logging level: `error`, `warn`, `info`, `debug` (default: `info`) |
85
+ | `SSH_COMMAND_TIMEOUT` | No | Command timeout in ms (default: `30000`) |
86
+ | `BACKUP_INTERVAL` | No | Backup interval in ms (default: `3600000`) |
87
+ | `MAX_CONNECTIONS` | No | Max SSH connections in pool (default: `10`) |
88
+
89
+ ### Generating Encryption Key
90
+
91
+ ```bash
92
+ # Generate a secure 64-character hex key
93
+ openssl rand -hex 32
94
+ ```
95
+
96
+ ## Usage
97
+
98
+ ### Bot Commands
99
+
100
+ | Command | Description |
101
+ | ---------------- | ----------------------- |
102
+ | `/start` | Start the bot |
103
+ | `/help` | Show help message |
104
+ | `/add` | Add a new server |
105
+ | `/rm <id>` | Remove a server |
106
+ | `/list` | List all servers |
107
+ | `/ssh <id>` | Connect to server |
108
+ | `/exit` | Disconnect from server |
109
+ | `/current` | Show current connection |
110
+ | `/health` | Show system health |
111
+ | `/cmd <command>` | Execute SSH command |
112
+
113
+ ### Example Workflow
114
+
115
+ ```
116
+ User: /add
117
+ Bot: Enter server details...
118
+ User: myserver.com:22:user:password
119
+ Bot: Server added with ID 1
120
+
121
+ User: /ssh 1
122
+ Bot: Connected to myserver.com
123
+
124
+ User: ls -la
125
+ Bot: [command output]
126
+
127
+ User: /exit
128
+ Bot: Disconnected from myserver.com
129
+ ```
130
+
131
+ ### Adding a Server
132
+
133
+ Use the `/add` command with the following format:
134
+
135
+ ```
136
+ host:port:username:password
137
+ ```
138
+
139
+ Or with private key:
140
+
141
+ ```
142
+ host:port:username::/path/to/private/key:keypass
143
+ ```
144
+
145
+ ## Architecture
146
+
147
+ See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for detailed architecture documentation.
148
+
149
+ ### Project Structure
150
+
151
+ ```
152
+ src/
153
+ ├── types/ # TypeScript interfaces
154
+ ├── errors/ # Custom error classes
155
+ ├── utils/ # Utility functions
156
+ ├── config/ # Configuration loader
157
+ ├── services/ # Business services
158
+ ├── core/ # Core modules (Bot, SSH, ServerManager)
159
+ ├── handlers/ # Command handlers
160
+ └── middleware/ # Auth & rate limiting
161
+ ```
162
+
163
+ ### Key Components
164
+
165
+ - **Bot** ([`src/core/Bot.ts`](src/core/Bot.ts)): Telegram bot initialization and command routing
166
+ - **SSHClient** ([`src/core/SSHClient.ts`](src/core/SSHClient.ts)): SSH connection management
167
+ - **ServerManager** ([`src/core/ServerManager.ts`](src/core/ServerManager.ts)): Server storage and retrieval
168
+ - **ConnectionPool** ([`src/core/ConnectionPool.ts`](src/core/ConnectionPool.ts)): SSH connection pooling
169
+ - **CryptoService** ([`src/services/CryptoService.ts`](src/services/CryptoService.ts)): Credential encryption
170
+ - **HealthService** ([`src/services/HealthService.ts`](src/services/HealthService.ts)): System health monitoring
171
+
172
+ ## Development
173
+
174
+ ### Build
175
+
176
+ ```bash
177
+ npm run build # Compile TypeScript
178
+ npm run build:binary # Create binary executable
179
+ make release # Build all platforms
180
+ ```
181
+
182
+ ### Development Mode
183
+
184
+ ```bash
185
+ npm run dev # Watch mode with TypeScript
186
+ ```
187
+
188
+ ### Testing
189
+
190
+ ```bash
191
+ # Run SSH test servers
192
+ docker pull takeyamajp/ubuntu-sshd
193
+ docker run -d -p 2222:22 --name ubuntu-server02 -e ROOT_PASSWORD="my_password" takeyamajp/ubuntu-sshd
194
+ docker run -d -p 2223:22 --name ubuntu-server03 -e ROOT_PASSWORD="my_password" takeyamajp/ubuntu-sshd
195
+ ```
196
+
197
+ ## Deployment
198
+
199
+ ### Systemd Service
200
+
201
+ The bot includes a systemd user service for production deployment:
202
+
203
+ ```bash
204
+ # Install service
205
+ ./deploy/install.sh
206
+
207
+ # Manage service
208
+ ./deploy/manage.sh start # Start service
209
+ ./deploy/manage.sh stop # Stop service
210
+ ./deploy/manage.sh restart # Restart service
211
+ ./deploy/manage.sh status # Check status
212
+ ./deploy/manage.sh logs # View logs
213
+ ```
214
+
215
+ See [deploy/README.md](deploy/README.md) for detailed deployment instructions.
216
+
217
+ ## Security
218
+
219
+ ### Credential Encryption
220
+
221
+ - All server credentials are encrypted using AES-256-GCM
222
+ - Encryption key must be provided via environment variable
223
+ - Credentials are never stored in plaintext
224
+
225
+ ### Input Validation
226
+
227
+ - Command sanitization prevents injection attacks
228
+ - Path traversal protection for file operations
229
+ - Rate limiting prevents abuse
230
+
231
+ ### Authorization
232
+
233
+ - Only configured owner IDs can interact with the bot
234
+ - Each command is authenticated before execution
235
+
236
+ ### Best Practices
237
+
238
+ 1. Use a dedicated Telegram bot token
239
+ 2. Restrict owner IDs to trusted users
240
+ 3. Generate a strong encryption key
241
+ 4. Run as a non-root user
242
+ 5. Keep the `.env` file secure (readable only by owner)
243
+
244
+ ## Migration from v1.x
245
+
246
+ If upgrading from the JavaScript version:
247
+
248
+ 1. **Backup your data**: Export your servers.json file
249
+ 2. **Generate encryption key**: `openssl rand -hex 32`
250
+ 3. **Create .env file**: Use the new configuration format
251
+ 4. **Re-add servers**: Credentials must be re-encrypted
252
+
253
+ See [CHANGELOG.md](CHANGELOG.md) for a complete list of changes.
254
+
255
+ ## Troubleshooting
256
+
257
+ ### Common Issues
258
+
259
+ **Bot not responding**
260
+
261
+ - Verify `BOT_TOKEN` is correct
262
+ - Check `BOT_OWNER_IDS` includes your Telegram user ID
263
+ - Check logs: `./deploy/manage.sh logs`
264
+
265
+ **SSH connection failed**
266
+
267
+ - Verify server credentials
268
+ - Check network connectivity
269
+ - Verify SSH service is running on target server
270
+
271
+ **Permission denied**
272
+
273
+ - Check file permissions on `.env` file
274
+ - Ensure the user running the bot has access to SSH keys
275
+
276
+ ### Logs
277
+
278
+ Logs are written to:
279
+
280
+ - Console output (stdout)
281
+ - `~/.local/share/telegram-ssh-bot/logs/` (when running as service)
282
+
283
+ ## Contributing
284
+
285
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development guidelines.
286
+
287
+ ## License
288
+
289
+ MIT
290
+
291
+ ## Acknowledgments
292
+
293
+ - [node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api)
294
+ - [ssh2](https://github.com/mscdex/ssh2)
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "telegram-ssh-bot",
3
+ "version": "2.0.0",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "bin": {
7
+ "telegram-ssh-bot": "./dist/index.js"
8
+ },
9
+ "files": [
10
+ "dist/**/*",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "engines": {
15
+ "node": ">=18.0.0"
16
+ },
17
+ "keywords": [
18
+ "telegram",
19
+ "ssh",
20
+ "bot",
21
+ "remote",
22
+ "server-management"
23
+ ],
24
+ "pkg": {
25
+ "scripts": "dist/**/*.js",
26
+ "assets": [
27
+ "node_modules/ssh2/**/*",
28
+ "node_modules/node-telegram-bot-api/**/*"
29
+ ],
30
+ "targets": [
31
+ "node18-linux-x64",
32
+ "node18-macos-x64",
33
+ "node18-win-x64"
34
+ ],
35
+ "outputPath": "build"
36
+ },
37
+ "dependencies": {
38
+ "dotenv": "^16.3.1",
39
+ "joi": "^17.11.0",
40
+ "node-telegram-bot-api": "^0.61.0",
41
+ "ssh2": "^1.14.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "^20.10.0",
45
+ "@types/node-telegram-bot-api": "^0.61.0",
46
+ "@types/ssh2": "^1.15.0",
47
+ "@yao-pkg/pkg": "^5.11.0",
48
+ "nodemon": "^3.0.1",
49
+ "tsx": "^4.6.0",
50
+ "typescript": "^5.3.0"
51
+ },
52
+ "scripts": {
53
+ "build": "tsc",
54
+ "start": "node dist/index.js",
55
+ "dev": "tsc --watch",
56
+ "clean": "rm -rf dist",
57
+ "postinstall": "node -e \"console.log('Telegram SSH Bot installed. Run: telegram-ssh-bot')\"",
58
+ "build:binary": "npm run build && pkg .",
59
+ "build:binary:linux": "npm run build && pkg . --targets node18-linux-x64",
60
+ "build:binary:macos": "npm run build && pkg . --targets node18-macos-x64",
61
+ "build:binary:windows": "npm run build && pkg . --targets node18-win-x64"
62
+ }
63
+ }