From 59c5a6c4950d340d248431b84452d86329024d76 Mon Sep 17 00:00:00 2001 From: runoneall Date: Thu, 11 Sep 2025 17:19:12 +0800 Subject: add ssh server --- shell.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 shell.go (limited to 'shell.go') diff --git a/shell.go b/shell.go new file mode 100644 index 0000000..3492b33 --- /dev/null +++ b/shell.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "net" + "os" + "os/exec" + + "golang.org/x/crypto/ssh" +) + +func shell(conn net.Conn, config *ssh.ServerConfig) { + sshConn, chans, reqs, err := ssh.NewServerConn(conn, config) + if err != nil { + fmt.Println("不能创建连接:", err) + return + } + defer sshConn.Close() + + fmt.Println("New connection from", sshConn.RemoteAddr(), "with client version", sshConn.ClientVersion()) + + go ssh.DiscardRequests(reqs) + + for newChannel := range chans { + if newChannel.ChannelType() != "session" { + newChannel.Reject(ssh.UnknownChannelType, "unknown channel type") + continue + } + + channel, requests, err := newChannel.Accept() + if err != nil { + fmt.Println("Can not accept channel:", err) + continue + } + defer channel.Close() + + shell := os.Getenv("SHELL") + if shell == "" { + shell = "cmd.exe" + } + + command := exec.Command(shell) + command.Stdin = channel + command.Stdout = channel + command.Stderr = channel + + if err := command.Start(); err != nil { + fmt.Println("Failed to start shell:", err) + return + } + + go func() { + if err := command.Wait(); err != nil { + fmt.Println("Run shell failed:", err) + } + channel.Close() + }() + + go func() { + for req := range requests { + switch req.Type { + case "shell": + req.Reply(true, nil) + default: + req.Reply(false, nil) + } + } + }() + } +} -- cgit v1.2.3