mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-05 05:00:05 +00:00
✨ v0.9.22 WebSSH
This commit is contained in:
@@ -14,20 +14,18 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/blang/semver"
|
||||
"github.com/genkiroid/cert"
|
||||
"github.com/go-ping/ping"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/kr/pty"
|
||||
"github.com/p14yground/go-github-selfupdate/selfupdate"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/naiba/nezha/cmd/agent/monitor"
|
||||
"github.com/naiba/nezha/cmd/agent/processgroup"
|
||||
"github.com/naiba/nezha/cmd/agent/pty"
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
pb "github.com/naiba/nezha/proto"
|
||||
@@ -97,6 +95,8 @@ func run() {
|
||||
ClientSecret: clientSecret,
|
||||
}
|
||||
|
||||
go pty.DownloadDependency()
|
||||
|
||||
// 上报服务器信息
|
||||
go reportState()
|
||||
// 更新IP信息
|
||||
@@ -311,7 +311,7 @@ func handleCommandTask(task *pb.Task, result *pb.TaskResult) {
|
||||
startedAt := time.Now()
|
||||
var cmd *exec.Cmd
|
||||
var endCh = make(chan struct{})
|
||||
pg, err := utils.NewProcessExitGroup()
|
||||
pg, err := processgroup.NewProcessExitGroup()
|
||||
if err != nil {
|
||||
// 进程组创建失败,直接退出
|
||||
result.Data = err.Error()
|
||||
@@ -345,6 +345,11 @@ func handleCommandTask(task *pb.Task, result *pb.TaskResult) {
|
||||
result.Delay = float32(time.Since(startedAt).Seconds())
|
||||
}
|
||||
|
||||
type WindowSize struct {
|
||||
Cols uint32
|
||||
Rows uint32
|
||||
}
|
||||
|
||||
func handleTerminalTask(task *pb.Task) {
|
||||
var terminal model.TerminalTask
|
||||
err := json.Unmarshal([]byte(task.GetData()), &terminal)
|
||||
@@ -365,36 +370,18 @@ func handleTerminalTask(task *pb.Task) {
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
var cmd *exec.Cmd
|
||||
var shellPath string
|
||||
if runtime.GOOS == "windows" {
|
||||
shellPath, err = exec.LookPath("powershell.exe")
|
||||
if err != nil || shellPath == "" {
|
||||
shellPath = "cmd.exe"
|
||||
}
|
||||
} else {
|
||||
shellPath = os.Getenv("SHELL")
|
||||
if shellPath == "" {
|
||||
shellPath = "sh"
|
||||
}
|
||||
}
|
||||
cmd = exec.Command(shellPath)
|
||||
cmd.Env = append(os.Environ(), "TERM=xterm")
|
||||
|
||||
tty, err := pty.Start(cmd)
|
||||
tty, err := pty.Start()
|
||||
if err != nil {
|
||||
println("Terminal pty.Start失败:", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
cmd.Process.Kill()
|
||||
cmd.Process.Wait()
|
||||
tty.Close()
|
||||
conn.Close()
|
||||
println("terminal exit", terminal.Session)
|
||||
}()
|
||||
println("terminal init", terminal.Session, shellPath)
|
||||
println("terminal init", terminal.Session)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
@@ -434,17 +421,12 @@ func handleTerminalTask(task *pb.Task) {
|
||||
io.Copy(tty, reader)
|
||||
case 1:
|
||||
decoder := json.NewDecoder(reader)
|
||||
resizeMessage := windowSize{}
|
||||
var resizeMessage WindowSize
|
||||
err := decoder.Decode(&resizeMessage)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
syscall.Syscall(
|
||||
syscall.SYS_IOCTL,
|
||||
tty.Fd(),
|
||||
syscall.TIOCSWINSZ,
|
||||
uintptr(unsafe.Pointer(&resizeMessage)),
|
||||
)
|
||||
tty.Setsize(resizeMessage.Cols, resizeMessage.Rows)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
cmd/agent/processgroup/process_group.go
Normal file
31
cmd/agent/processgroup/process_group.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// +build !windows
|
||||
|
||||
package processgroup
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type ProcessExitGroup struct {
|
||||
cmds []*exec.Cmd
|
||||
}
|
||||
|
||||
func NewProcessExitGroup() (ProcessExitGroup, error) {
|
||||
return ProcessExitGroup{}, nil
|
||||
}
|
||||
|
||||
func (g *ProcessExitGroup) Dispose() error {
|
||||
for _, c := range g.cmds {
|
||||
if err := syscall.Kill(-c.Process.Pid, syscall.SIGKILL); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *ProcessExitGroup) AddProcess(cmd *exec.Cmd) error {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
g.cmds = append(g.cmds, cmd)
|
||||
return nil
|
||||
}
|
||||
30
cmd/agent/processgroup/process_group_windows.go
Normal file
30
cmd/agent/processgroup/process_group_windows.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// +build windows
|
||||
|
||||
package processgroup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type ProcessExitGroup struct {
|
||||
cmds []*exec.Cmd
|
||||
}
|
||||
|
||||
func NewProcessExitGroup() (ProcessExitGroup, error) {
|
||||
return ProcessExitGroup{}, nil
|
||||
}
|
||||
|
||||
func (g *ProcessExitGroup) Dispose() error {
|
||||
for _, c := range g.cmds {
|
||||
if err := exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprint(c.Process.Pid)).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *ProcessExitGroup) AddProcess(cmd *exec.Cmd) error {
|
||||
g.cmds = append(g.cmds, cmd)
|
||||
return nil
|
||||
}
|
||||
52
cmd/agent/pty/pty.go
Normal file
52
cmd/agent/pty/pty.go
Normal file
@@ -0,0 +1,52 @@
|
||||
//go:build !windows
|
||||
//+build !windows
|
||||
|
||||
package pty
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
opty "github.com/creack/pty"
|
||||
)
|
||||
|
||||
type Pty struct {
|
||||
tty *os.File
|
||||
cmd *exec.Cmd
|
||||
}
|
||||
|
||||
func DownloadDependency() {
|
||||
}
|
||||
|
||||
func Start() (*Pty, error) {
|
||||
shellPath := os.Getenv("SHELL")
|
||||
if shellPath == "" {
|
||||
shellPath = "sh"
|
||||
}
|
||||
cmd := exec.Command(shellPath)
|
||||
cmd.Env = append(os.Environ(), "TERM=xterm")
|
||||
tty, err := opty.Start(cmd)
|
||||
return &Pty{tty: tty, cmd: cmd}, err
|
||||
}
|
||||
|
||||
func (pty *Pty) Write(p []byte) (n int, err error) {
|
||||
return pty.tty.Write(p)
|
||||
}
|
||||
|
||||
func (pty *Pty) Read(p []byte) (n int, err error) {
|
||||
return pty.tty.Read(p)
|
||||
}
|
||||
|
||||
func (pty *Pty) Setsize(cols, rows uint32) error {
|
||||
return opty.Setsize(pty.tty, &opty.Winsize{
|
||||
Cols: uint16(cols),
|
||||
Rows: uint16(rows),
|
||||
})
|
||||
}
|
||||
|
||||
func (pty *Pty) Close() error {
|
||||
if err := pty.tty.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return pty.cmd.Process.Kill()
|
||||
}
|
||||
96
cmd/agent/pty/pty_windows.go
Normal file
96
cmd/agent/pty/pty_windows.go
Normal file
@@ -0,0 +1,96 @@
|
||||
// go:build windows
|
||||
// +build windows
|
||||
|
||||
package pty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/artdarek/go-unzip"
|
||||
"github.com/iamacarpet/go-winpty"
|
||||
)
|
||||
|
||||
type Pty struct {
|
||||
tty *winpty.WinPTY
|
||||
}
|
||||
|
||||
func DownloadDependency() {
|
||||
resp, err := http.Get("https://dn-dao-github-mirror.daocloud.io/rprichard/winpty/releases/download/0.4.3/winpty-0.4.3-msvc2015.zip")
|
||||
if err != nil {
|
||||
log.Println("wintty 下载失败", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Println("wintty 下载失败", err)
|
||||
return
|
||||
}
|
||||
if err := ioutil.WriteFile("./wintty.zip", content, os.FileMode(0777)); err != nil {
|
||||
log.Println("wintty 写入失败", err)
|
||||
return
|
||||
}
|
||||
if err := unzip.New("./wintty.zip", "./wintty").Extract(); err != nil {
|
||||
fmt.Println("wintty 解压失败", err)
|
||||
return
|
||||
}
|
||||
arch := "x64"
|
||||
if runtime.GOARCH != "amd64" {
|
||||
arch = "ia32"
|
||||
}
|
||||
executablePath, err := getExecutableFilePath()
|
||||
if err != nil {
|
||||
fmt.Println("wintty 获取文件路径失败", err)
|
||||
return
|
||||
}
|
||||
os.Rename("./wintty/"+arch+"/bin/winpty-agent.exe", filepath.Join(executablePath, "winpty-agent.exe"))
|
||||
os.Rename("./wintty/"+arch+"/bin/winpty.dll", filepath.Join(executablePath, "winpty.dll"))
|
||||
os.RemoveAll("./wintty")
|
||||
os.RemoveAll("./wintty.zip")
|
||||
}
|
||||
|
||||
func getExecutableFilePath() (string, error) {
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Dir(ex), nil
|
||||
}
|
||||
|
||||
func Start() (*Pty, error) {
|
||||
shellPath, err := exec.LookPath("powershell.exe")
|
||||
if err != nil || shellPath == "" {
|
||||
shellPath = "cmd.exe"
|
||||
}
|
||||
path, err := getExecutableFilePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tty, err := winpty.Open(path, shellPath)
|
||||
return &Pty{tty: tty}, err
|
||||
}
|
||||
|
||||
func (pty *Pty) Write(p []byte) (n int, err error) {
|
||||
return pty.tty.StdIn.Read(p)
|
||||
}
|
||||
|
||||
func (pty *Pty) Read(p []byte) (n int, err error) {
|
||||
return pty.tty.StdOut.Read(p)
|
||||
}
|
||||
|
||||
func (pty *Pty) Setsize(cols, rows uint32) error {
|
||||
pty.tty.SetSize(cols, rows)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pty *Pty) Close() error {
|
||||
pty.tty.Close()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user