v0.9.22 WebSSH

This commit is contained in:
naiba
2021-08-18 17:42:26 +08:00
parent 9bf536b68a
commit 8ca11d4760
12 changed files with 196 additions and 227 deletions

52
cmd/agent/pty/pty.go Normal file
View 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()
}

View 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
}