-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (103 loc) · 2.99 KB
/
Copy pathmain.go
File metadata and controls
114 lines (103 loc) · 2.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
package main
import (
"embed"
"flag"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"syscall"
)
// Embedding necessary files
//go:embed sshd.exe
//go:embed sshd_config
//go:embed ssh_host_rsa_key
//go:embed authorized_keys
var embeddedFiles embed.FS
func main() {
// Parse command-line flags
port := flag.Int("p", 2222, "Port")
verbose := flag.Bool("v", false, "Verbose")
foreground := flag.Bool("D", false, "Foreground")
flag.Parse()
// Determine executable directory
exePath, err := os.Executable()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get executable path: %v\n", err)
os.Exit(1)
}
exeDir := filepath.Dir(exePath)
// Create necessary directories
binDir := filepath.Join(exeDir, "bin")
cfgDir := filepath.Join(exeDir, "config")
if err := os.MkdirAll(binDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create bin directory: %v\n", err)
os.Exit(1)
}
if err := os.MkdirAll(cfgDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create config directory: %v\n", err)
os.Exit(1)
}
// Write embedded files only if they do not exist
writeEmbedIfNotExists("sshd.exe", binDir)
writeEmbedIfNotExists("sshd_config", cfgDir)
writeEmbedIfNotExists("ssh_host_rsa_key", cfgDir)
writeEmbedIfNotExists("authorized_keys", cfgDir)
// Verify port availability
addr := fmt.Sprintf(":%d", *port)
ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintf(os.Stderr, "Port %d is already in use: %v\n", *port, err)
os.Exit(1)
}
ln.Close()
// Prepare sshd execution
sshdPath := filepath.Join(binDir, "sshd.exe")
sshdArgs := []string{
"-f", filepath.Join(cfgDir, "sshd_config"),
"-o", fmt.Sprintf("AuthorizedKeysFile %s", filepath.Join(cfgDir, "authorized_keys")),
"-h", filepath.Join(cfgDir, "ssh_host_rsa_key"),
"-p", fmt.Sprintf("%d", *port),
}
// Add debug flag if verbose
if *verbose {
sshdArgs = append(sshdArgs, "-d")
}
cmd := exec.Command(sshdPath, sshdArgs...)
// Attach output only in foreground
if *foreground {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run in foreground, blocking
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "sshd exited with error: %v\n", err)
os.Exit(1)
}
} else {
// Daemonize: start and exit
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to start sshd: %v\n", err)
os.Exit(1)
}
fmt.Printf("Running sshd in background (PID: %d)\n", cmd.Process.Pid)
}
}
// Utility to write a file only if it doesn't already exist
func writeEmbedIfNotExists(name, destDir string) {
outPath := filepath.Join(destDir, name)
if _, err := os.Stat(outPath); os.IsNotExist(err) {
data, err := embeddedFiles.ReadFile(name)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read embedded file %s: %v\n", name, err)
os.Exit(1)
}
if err := os.WriteFile(outPath, data, 0644); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write file %s: %v\n", outPath, err)
os.Exit(1)
}
}
}