Skip to content

Commit

Permalink
Opt: Remove useless features
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Mar 9, 2024
1 parent 9885387 commit 5ab45ba
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 504 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ cache/
server/
tmp/
main

.DS_Store
72 changes: 13 additions & 59 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"os"
"runtime"
"strconv"

pty "github.com/MCSManager/pty/console"
"github.com/MCSManager/pty/utils"
Expand All @@ -16,9 +15,8 @@ import (
)

var (
dir, cmd, coder, ptySize, pid, mode string
cmds []string
colorAble, exhaustive, skipExistFile bool
dir, cmd, coder, ptySize string
cmds []string
)

type PtyInfo struct {
Expand All @@ -32,56 +30,24 @@ func init() {
flag.StringVar(&cmd, "cmd", "[\"sh\"]", "command")
}

flag.BoolVar(&colorAble, "color", true, "colorable (default true)")
flag.BoolVar(&skipExistFile, "s", false, "Skip Exist File (default false)")
flag.BoolVar(&exhaustive, "e", false, "Zip Exhaustive (default false)")
flag.StringVar(&coder, "coder", "auto", "Coder")
flag.StringVar(&pid, "pid", "0", "detect pid info")
flag.StringVar(&dir, "dir", ".", "command work path")
flag.StringVar(&ptySize, "size", "80,50", "Initialize pty size, stdin will be forwarded directly")
flag.StringVar(&mode, "m", "pty", "set mode")
}

func Main() {
flag.Parse()
args := flag.Args()
switch mode {
case "zip":
if err := utils.Zip(args[:len(args)-1], args[len(args)-1], utils.ZipCfg{Exhaustive: exhaustive}); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
case "unzip":
if err := utils.Unzip(args[0], args[1], utils.UnzipCfg{CoderTypes: utils.CoderToType(coder), SkipExistFile: skipExistFile, Exhaustive: exhaustive}); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
case "info":
runtime.GOMAXPROCS(2)
info := utils.NewInfo()
upid, err := strconv.ParseInt(pid, 10, 32)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
utils.Detect(int32(upid), info)
pinfo, err := json.Marshal(info)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(string(pinfo))
default:
runtime.GOMAXPROCS(6)
runPTY()
}
runPTY()
}

func runPTY() {
json.Unmarshal([]byte(cmd), &cmds)
if err := json.Unmarshal([]byte(cmd), &cmds); err != nil {
fmt.Println("[MCSMANAGER-PTY] Unmarshal command error: ", err)
return
}
con := pty.New(utils.CoderToType(coder))
if err := con.ResizeWithString(ptySize); err != nil {
fmt.Printf("[MCSMANAGER-PTY] PTY ReSize Error: %v\n", err)
fmt.Printf("[MCSMANAGER-PTY] PTY Resize error: %v\n", err)
return
}
err := con.Start(dir, cmds)
Expand All @@ -90,12 +56,12 @@ func runPTY() {
})
fmt.Println(string(info))
if err != nil {
fmt.Printf("[MCSMANAGER-PTY] Process Start Error: %v\n", err)
fmt.Printf("[MCSMANAGER-PTY] Process start error: %v\n", err)
return
}
defer con.Close()
handleStdIO(con)
con.Wait()
_, _ = con.Wait()
}

func handleStdIO(c pty.Console) {
Expand All @@ -107,26 +73,14 @@ func handleStdIO(c pty.Console) {
defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }()
go func() { _, _ = io.Copy(c.StdIn(), os.Stdin) }()
} else {
go io.Copy(c.StdIn(), os.Stdin)
go func() { _, _ = io.Copy(c.StdIn(), os.Stdin) }()
}
if runtime.GOOS == "windows" && c.StdErr() != nil {
var stdErr io.Reader
if colorAble {
stdErr = c.StdErr()
} else {
stdErr = colorable.NewNonColorableReader(c.StdErr())
}
go io.Copy(colorable.NewColorableStderr(), stdErr)
go func() { _, _ = io.Copy(colorable.NewColorableStderr(), c.StdErr()) }()
}
handleStdOut(c)
}

func handleStdOut(c pty.Console) {
var stdOut io.Reader
if colorAble {
stdOut = c.StdOut()
} else {
stdOut = colorable.NewNonColorableReader(c.StdOut())
}
io.Copy(colorable.NewColorableStdout(), stdOut)
_, _ = io.Copy(colorable.NewColorableStdout(), c.StdOut())
}
71 changes: 51 additions & 20 deletions console/console_windows.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package console

import (
"bytes"
"embed"
_ "embed"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/MCSManager/pty/console/go-winpty"
Expand All @@ -16,8 +17,8 @@ import (
mutex "github.com/juju/mutex/v2"
)

//go:embed winpty
var winpty_zip []byte
//go:embed all:winpty
var winpty_embed embed.FS

var _ iface.Console = (*console)(nil)

Expand All @@ -44,15 +45,20 @@ func (c *console) Start(dir string, command []string) error {
defer r.Release()
if dir, err = filepath.Abs(dir); err != nil {
return err
} else if err := os.Chdir(dir); err != nil {
}
if err := os.Chdir(dir); err != nil {
return err
}
dllDir, err := c.findDll()
if err != nil {
return err
}
cmd, err := c.buildCmd(command)
if err != nil {
return err
}
option := winpty.Options{
DllDir: filepath.Join(os.TempDir(), "pty_winpty"),
DllDir: dllDir,
Command: cmd,
Dir: dir,
Env: c.env,
Expand All @@ -65,12 +71,7 @@ func (c *console) Start(dir string, command []string) error {

var pty *winpty.WinPTY
if pty, err = winpty.OpenWithOptions(option); err != nil {
if option.DllDir, err = c.findDll(); err != nil {
return err
}
if pty, err = winpty.OpenWithOptions(option); err != nil {
return err
}
return err
}
c.stdIn = pty.Stdin
c.stdOut = pty.Stdout
Expand All @@ -79,16 +80,16 @@ func (c *console) Start(dir string, command []string) error {
return nil
}

// splice command
func (c *console) buildCmd(args []string) (string, error) {
if len(args) == 0 {
return "", ErrInvalidCmd
}
var cmds = fmt.Sprintf("cmd /C chcp %s > nul & ", utils.CodePage(c.coder))
for _, v := range args {
cmds += v + ` `
}
return cmds[:len(cmds)-1], nil
var cmds = fmt.Sprintf(
"cmd /C chcp %s > nul & %s",
utils.CodePage(c.coder),
strings.Join(args, " "),
)
return cmds, nil
}

type fakeClock struct {
Expand All @@ -110,9 +111,39 @@ func (c *console) findDll() (string, error) {
return "", err
}

return dllDir, utils.UnzipWithFile(bytes.NewReader(winpty_zip), dllDir, utils.UnzipCfg{
CoderTypes: utils.T_UTF8,
})
dir, err := winpty_embed.ReadDir("winpty")
if err != nil {
return "", fmt.Errorf("read embed dir error: %w", err)
}

for _, de := range dir {
info, err := de.Info()
if err != nil {
return "", err
}
var exist bool
df, err := os.Stat(filepath.Join(dllDir, de.Name()))
if err != nil {
if !os.IsNotExist(err) {
return "", err
}
} else {
if !df.ModTime().Before(info.ModTime()) {
exist = true
}
}
if !exist {
data, err := winpty_embed.ReadFile(fmt.Sprintf("winpty/%s", de.Name()))
if err != nil {
return "", fmt.Errorf("read embed file error: %w", err)
}
if err := os.WriteFile(filepath.Join(dllDir, de.Name()), data, os.ModePerm); err != nil {
return "", fmt.Errorf("write file error: %w", err)
}
}
}

return dllDir, nil
}

// set pty window size
Expand Down
Binary file removed console/winpty
Binary file not shown.
Binary file added console/winpty/winpty-agent.exe
Binary file not shown.
Binary file added console/winpty/winpty.dll
Binary file not shown.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/MCSManager/pty
go 1.18

require (
github.com/creack/pty v1.1.18
github.com/creack/pty v1.1.21
github.com/juju/mutex/v2 v2.0.0
github.com/klauspost/compress v1.16.5
github.com/mholt/archiver/v4 v4.0.0-alpha.8
github.com/shirou/gopsutil/v3 v3.23.4
github.com/zijiren233/go-colorable v0.0.0-20230522040028-05f4e204585c
golang.org/x/term v0.8.0
golang.org/x/text v0.9.0
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb
golang.org/x/term v0.18.0
golang.org/x/text v0.14.0
)

require (
Expand All @@ -27,7 +27,7 @@ require (
github.com/juju/errors v1.0.0 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
Expand All @@ -38,5 +38,5 @@ require (
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/connesc/cipherio v0.2.1 h1:FGtpTPMbKNNWByNrr9aEBtaJtXjqOzkIXNYJp6OEyc
github.com/connesc/cipherio v0.2.1/go.mod h1:ukY0MWJDFnJEbXMQtOcn2VmTpRfzcTz4OoVrWGGJZcA=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -127,6 +129,8 @@ github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPn
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mholt/archiver/v4 v4.0.0-alpha.7.0.20230223155640-de8cf229f727 h1:9ScivJvfYiMaoHng1p5wvhM4iAKzXUVVKBIo+QotEAI=
github.com/mholt/archiver/v4 v4.0.0-alpha.7.0.20230223155640-de8cf229f727/go.mod h1:5f7FUYGXdJWUjESffJaYR4R60VhnHxb2X3T1teMyv5A=
github.com/mholt/archiver/v4 v4.0.0-alpha.8 h1:tRGQuDVPh66WCOelqe6LIGh0gwmfwxUrSSDunscGsRM=
Expand Down Expand Up @@ -181,6 +185,8 @@ github.com/zijiren233/go-colorable v0.0.0-20230304035935-641eddfc7ecf h1:EwTsEmz
github.com/zijiren233/go-colorable v0.0.0-20230304035935-641eddfc7ecf/go.mod h1:TJFyVPDSW/YIGewz0BLYt4sACOV0xXJbNbfaRMJPXHc=
github.com/zijiren233/go-colorable v0.0.0-20230522040028-05f4e204585c h1:5Q2rC1jyLWlhjbGii+94yK7xefMstT2WXJnsR5G7qDA=
github.com/zijiren233/go-colorable v0.0.0-20230522040028-05f4e204585c/go.mod h1:TJFyVPDSW/YIGewz0BLYt4sACOV0xXJbNbfaRMJPXHc=
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb h1:0DyOxf/TbbGodHhOVHNoPk+7v/YBJACs22gKpKlatWw=
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb/go.mod h1:6TCzjDiQ8+5gWZiwsC3pnA5M0vUy2jV2Y7ciHJh729g=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down Expand Up @@ -275,12 +281,16 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -291,6 +301,8 @@ golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
Loading

0 comments on commit 5ab45ba

Please sign in to comment.