-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.go
More file actions
300 lines (261 loc) · 7.99 KB
/
ssh.go
File metadata and controls
300 lines (261 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
// SSHStats represents SSH connection statistics
type SSHStats struct {
ActiveConnections int
TotalConnections int
FailedConnections int
LastFailedAttempt time.Time
SSHDStatus string
ConnectedUsers []ConnectedUser
RecentConnections []RecentConnection
}
type ConnectedUser struct {
User string
Terminal string
Host string
Started time.Time
Idle string
}
type RecentConnection struct {
User string
Host string
Timestamp time.Time
Status string // "success", "failed", "disconnect"
Method string // "password", "publickey"
}
// Check if SSH binaries are available
func checkSSHBinaries() []string {
var missing []string
binaries := []string{"ssh", "sshd", "ssh-keygen", "systemctl"}
for _, binary := range binaries {
if _, err := exec.LookPath(binary); err != nil {
missing = append(missing, binary)
}
}
return missing
}
// Generate SSH key pair
func generateSSHKeyPair(keyType string, bits int, comment string) (privateKeyPath, publicKey string, err error) {
// Create ~/.ssh directory if it doesn't exist
homeDir, err := os.UserHomeDir()
if err != nil {
return "", "", fmt.Errorf("failed to get home directory: %v", err)
}
sshDir := filepath.Join(homeDir, ".ssh")
if err := os.MkdirAll(sshDir, 0700); err != nil {
return "", "", fmt.Errorf("failed to create .ssh directory: %v", err)
}
// Generate unique filename
timestamp := time.Now().Format("20060102_150405")
keyName := fmt.Sprintf("ssh_key_%s_%s", keyType, timestamp)
privateKeyPath = filepath.Join(sshDir, keyName)
publicKeyPath := privateKeyPath + ".pub"
// Build ssh-keygen command
var cmd *exec.Cmd
switch keyType {
case "rsa":
if bits == 0 {
bits = 4096 // Default RSA key size
}
cmd = exec.Command("ssh-keygen", "-t", keyType, "-b", strconv.Itoa(bits), "-f", privateKeyPath, "-N", "", "-C", comment)
case "ed25519":
cmd = exec.Command("ssh-keygen", "-t", keyType, "-f", privateKeyPath, "-N", "", "-C", comment)
case "ecdsa":
if bits == 0 {
bits = 256 // Default ECDSA key size
}
cmd = exec.Command("ssh-keygen", "-t", keyType, "-b", strconv.Itoa(bits), "-f", privateKeyPath, "-N", "", "-C", comment)
default:
return "", "", fmt.Errorf("unsupported key type: %s", keyType)
}
// Execute ssh-keygen
output, err := cmd.CombinedOutput()
if err != nil {
return "", "", fmt.Errorf("ssh-keygen failed: %v\nOutput: %s", err, string(output))
}
// Read the public key
publicKeyData, err := os.ReadFile(publicKeyPath)
if err != nil {
return "", "", fmt.Errorf("failed to read public key: %v", err)
}
return privateKeyPath, string(publicKeyData), nil
}
// Get SSH daemon status
func (c *AppConfig) getSSHDStatus() (string, error) {
var status strings.Builder
status.WriteString("=== SSH Daemon Status ===\n")
// Check if sshd is running
cmd := exec.Command("systemctl", "is-active", "ssh")
output, err := cmd.Output()
if err != nil {
// Try alternative service name
cmd = exec.Command("systemctl", "is-active", "sshd")
output, err = cmd.Output()
}
serviceStatus := strings.TrimSpace(string(output))
if err != nil || serviceStatus != "active" {
status.WriteString("Service Status: ❌ NOT RUNNING\n")
} else {
status.WriteString("Service Status: ✅ RUNNING\n")
}
// Check SSH configuration file
if _, err := os.Stat(c.SSHConfigPath); os.IsNotExist(err) {
status.WriteString(fmt.Sprintf("Config File: ❌ MISSING (%s)\n", c.SSHConfigPath))
// Provide diagnostics when config file is missing
diag := c.DiagnoseSSHInstallation()
status.WriteString("\n🔍 Installation Diagnostics:\n")
for _, suggestion := range diag.Suggestions {
status.WriteString(fmt.Sprintf(" • %s\n", suggestion))
}
return status.String(), nil
} else {
status.WriteString(fmt.Sprintf("Config File: ✅ EXISTS (%s)\n", c.SSHConfigPath))
}
// Try to read current SSH configuration
sshConfig, err := c.ReadSSHServerConfig()
if err != nil {
status.WriteString(fmt.Sprintf("Config Reading: ❌ ERROR (%v)\n", err))
return status.String(), nil
}
// Check SSH port
status.WriteString(fmt.Sprintf("SSH Port: %d\n", sshConfig.Port))
// Check host keys
if _, err := os.Stat(sshConfig.HostKeyPath); os.IsNotExist(err) {
status.WriteString(fmt.Sprintf("Host Key: ❌ MISSING (%s)\n", sshConfig.HostKeyPath))
} else {
status.WriteString(fmt.Sprintf("Host Key: ✅ EXISTS (%s)\n", sshConfig.HostKeyPath))
}
// Get listening ports
cmd = exec.Command("ss", "-tlnp")
output, err = cmd.Output()
if err != nil {
status.WriteString("Port Check: ❌ UNABLE TO CHECK\n")
} else {
portStr := fmt.Sprintf(":%d ", sshConfig.Port)
if strings.Contains(string(output), portStr) {
status.WriteString(fmt.Sprintf("Port %d: ✅ LISTENING\n", sshConfig.Port))
} else {
status.WriteString(fmt.Sprintf("Port %d: ❌ NOT LISTENING\n", sshConfig.Port))
}
}
return status.String(), nil
}
// Start SSH daemon
func (c *AppConfig) startSSHD() error {
// Try both possible service names
cmd := exec.Command("systemctl", "start", "ssh")
output, err := cmd.CombinedOutput()
if err != nil {
// Try alternative service name
cmd = exec.Command("systemctl", "start", "sshd")
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to start SSH daemon: %v\nOutput: %s", err, string(output))
}
}
return nil
}
// Stop SSH daemon
func (c *AppConfig) stopSSHD() error {
cmd := exec.Command("systemctl", "stop", "ssh")
output, err := cmd.CombinedOutput()
if err != nil {
// Try alternative service name
cmd = exec.Command("systemctl", "stop", "sshd")
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to stop SSH daemon: %v\nOutput: %s", err, string(output))
}
}
return nil
}
// Restart SSH daemon
func (c *AppConfig) restartSSHD() error {
// Test configuration first
if err := c.testSSHDConfig(); err != nil {
return fmt.Errorf("SSH configuration test failed: %v", err)
}
cmd := exec.Command("systemctl", "restart", "ssh")
output, err := cmd.CombinedOutput()
if err != nil {
// Try alternative service name
cmd = exec.Command("systemctl", "restart", "sshd")
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to restart SSH daemon: %v\nOutput: %s", err, string(output))
}
}
return nil
}
// Test SSH daemon configuration
func (c *AppConfig) testSSHDConfig() error {
cmd := exec.Command("sshd", "-t", "-f", c.SSHConfigPath)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("configuration test failed: %v\nOutput: %s", err, string(output))
}
return nil
}
// Get SSH statistics and connection information
func (c *AppConfig) getSSHStats() (*SSHStats, error) {
stats := &SSHStats{
ConnectedUsers: []ConnectedUser{},
RecentConnections: []RecentConnection{},
}
// Get SSH daemon status
status, err := c.getSSHDStatus()
if err != nil {
return nil, err
}
stats.SSHDStatus = status
// Get connected users using 'who' command
cmd := exec.Command("who")
output, err := cmd.Output()
if err == nil {
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
// Parse 'who' output format
fields := strings.Fields(line)
if len(fields) >= 3 {
user := ConnectedUser{
User: fields[0],
Terminal: fields[1],
Started: time.Now(), // Simplified - would need parsing
}
if len(fields) >= 4 {
user.Host = fields[len(fields)-1]
}
stats.ConnectedUsers = append(stats.ConnectedUsers, user)
}
}
}
stats.ActiveConnections = len(stats.ConnectedUsers)
// Get recent SSH connections from auth.log (simplified)
// This would typically parse /var/log/auth.log for SSH events
stats.TotalConnections = stats.ActiveConnections // Simplified
return stats, nil
}
// Utility function to copy files
func copyFile(src, dst string) error {
input, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, input, 0644)
if err != nil {
return err
}
return nil
}