From 8dc2aaa35bd11429d3a87f43b7fde26a18ed59b9 Mon Sep 17 00:00:00 2001 From: shy Date: Tue, 5 Dec 2023 21:56:10 +0800 Subject: [PATCH] add some --- base/nfs/save.go | 2 +- base/nfs/trash.go | 3 +- base/tcp/port.go | 6 ++++ base/web/option.go | 2 +- core/code/xterm.go | 2 +- misc/ssh/connect.go | 6 +++- misc/ssh/file.go | 74 +++++++++++++++++++++++++++++++++++++++++++++ misc/ssh/xterm.go | 41 +++++++++++++++++++++++++ 8 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 misc/ssh/file.go create mode 100644 misc/ssh/xterm.go diff --git a/base/nfs/save.go b/base/nfs/save.go index df6b733b..53a252d4 100644 --- a/base/nfs/save.go +++ b/base/nfs/save.go @@ -153,7 +153,7 @@ func Copy(m *ice.Message, w io.Writer, r io.Reader, cb ice.Any) { } } } -func CopyStream(m *ice.Message, to io.WriteCloser, from io.ReadCloser, cache, total int, cb ice.Any) { +func CopyStream(m *ice.Message, to io.Writer, from io.Reader, cache, total int, cb ice.Any) { kit.If(total == 0, func() { total = 1 }) count, buf := 0, make([]byte, cache) for { diff --git a/base/nfs/trash.go b/base/nfs/trash.go index c1a9a6b7..4f8c07f5 100644 --- a/base/nfs/trash.go +++ b/base/nfs/trash.go @@ -2,7 +2,6 @@ package nfs import ( "io" - "os" "path" ice "shylinux.com/x/icebergs" @@ -15,7 +14,7 @@ func _trash_create(m *ice.Message, from string) { return } s, e := StatFile(m, from) - defer os.Remove(from) + defer Remove(m, from) if m.Warn(e, ice.ErrNotFound, from) { return } diff --git a/base/tcp/port.go b/base/tcp/port.go index 16cab4a6..254aa216 100644 --- a/base/tcp/port.go +++ b/base/tcp/port.go @@ -33,6 +33,12 @@ func _port_right(m *ice.Message, arg ...string) string { } const ( + PORT_22 = "22" + PORT_80 = "80" + PORT_443 = "443" + PORT_9020 = "9020" + PORT_9022 = "9022" + SOCKET = "socket" BEGIN = "begin" CURRENT = "current" diff --git a/base/web/option.go b/base/web/option.go index c96ada2d..b3174a9e 100644 --- a/base/web/option.go +++ b/base/web/option.go @@ -103,7 +103,7 @@ func PushImages(m *ice.Message, name string) { } func PushNotice(m *ice.Message, arg ...ice.Any) { opts := ice.Map{ice.MSG_OPTION: []string{}, ice.MSG_OPTS: []string{}} - kit.For([]string{ctx.DISPLAY, ctx.STYLE, ice.LOG_DEBUG, ice.LOG_TRACEID}, func(key string) { + kit.For([]string{ctx.DISPLAY, ctx.STYLE, "delay", ice.LOG_DEBUG, ice.LOG_TRACEID}, func(key string) { opts[ice.MSG_OPTION] = kit.Simple(opts[ice.MSG_OPTION], key) opts[key] = m.Option(key) }) diff --git a/core/code/xterm.go b/core/code/xterm.go index eaef7ae1..8a208e80 100644 --- a/core/code/xterm.go +++ b/core/code/xterm.go @@ -41,7 +41,7 @@ func _xterm_get(m *ice.Message, h string) xterm.XTerm { for { if n, e := term.Read(buf); !m.Warn(e) && e == nil { if _xterm_echo(m, h, string(buf[:n])); len(text) > 0 { - kit.If(text[0], func(cmd string) { m.Go(func() { m.Sleep30ms(); term.Write([]byte(cmd + lex.NL)) }) }) + kit.If(text[0], func(cmd string) { m.Go(func() { m.Sleep300ms(); term.Write([]byte(cmd + lex.NL)) }) }) text = text[1:] } } else { diff --git a/misc/ssh/connect.go b/misc/ssh/connect.go index 37b7bfb5..392e755f 100644 --- a/misc/ssh/connect.go +++ b/misc/ssh/connect.go @@ -94,7 +94,7 @@ func _ssh_dial(m *ice.Message, cb func(net.Conn), arg ...string) { func _ssh_conn(m *ice.Message, cb func(*ssh.Client), arg ...string) { methods := []ssh.AuthMethod{} methods = append(methods, ssh.PublicKeysCallback(func() ([]ssh.Signer, error) { - key, err := ssh.ParsePrivateKey([]byte(m.Cmdx(nfs.CAT, kit.HomePath(m.OptionDefault(PRIVATE, ".ssh/id_rsa"))))) + key, err := ssh.ParsePrivateKey([]byte(m.Cmdx(nfs.CAT, kit.HomePath(m.OptionDefault(PRIVATE, ID_RSA_KEY))))) return []ssh.Signer{key}, err })) methods = append(methods, ssh.PasswordCallback(func() (string, error) { return m.Option(aaa.PASSWORD), nil })) @@ -146,6 +146,9 @@ func _ssh_target(m *ice.Message, name string) *ssh.Client { const SSH = "ssh" const ( DIRECT = "direct" + + ID_RSA_KEY = ".ssh/id_rsa" + ID_RSA_PUB = ".ssh/id_rsa.pub" ) const CONNECT = "connect" @@ -229,6 +232,7 @@ func (s session) Read(buf []byte) (int, error) { return s.pty.Read(buf) } func (s session) Close() error { return s.sess.Close() } func init() { xterm.AddCommand(SSH, NewSession) } + func CombinedOutput(m *ice.Message, cmd string, cb func(string)) { _ssh_conn(m, func(c *ssh.Client) { if s, e := c.NewSession(); !m.Warn(e, ice.ErrNotValid) { diff --git a/misc/ssh/file.go b/misc/ssh/file.go new file mode 100644 index 00000000..9d5cce32 --- /dev/null +++ b/misc/ssh/file.go @@ -0,0 +1,74 @@ +package ssh + +import ( + "io" + "os" + + "github.com/pkg/sftp" + "golang.org/x/crypto/ssh" + ice "shylinux.com/x/icebergs" + "shylinux.com/x/toolkits/file" +) + +type FileSystem struct{ *sftp.Client } + +func (s *FileSystem) StatFile(p string) (os.FileInfo, error) { + return s.Client.Stat(p) +} +func (s *FileSystem) OpenFile(p string) (io.ReadCloser, error) { + return s.Client.Open(p) +} +func (s *FileSystem) CreateFile(p string) (io.WriteCloser, string, error) { + if f, p, e := file.CreateFiles(s, p); f != nil { + return f, p, e + } + f, e := s.Client.Create(p) + return f, p, e +} +func (s *FileSystem) AppendFile(p string) (io.ReadWriteCloser, error) { + if f, _, e := file.CreateFiles(s, p); f != nil { + return f, e + } + return s.Client.OpenFile(p, os.O_RDWR|os.O_APPEND|os.O_CREATE) +} +func (s *FileSystem) WriteFile(p string, b []byte) error { + f, p, e := s.CreateFile(p) + if e != nil { + return e + } + defer f.Close() + _, e = f.Write(b) + return e +} + +func (s *FileSystem) ReadDir(p string) ([]os.FileInfo, error) { + return s.Client.ReadDir(p) +} +func (s *FileSystem) MkdirAll(p string, m os.FileMode) error { + return s.Client.MkdirAll(p) +} +func (s *FileSystem) RemoveAll(p string) error { + return s.Client.RemoveAll(p) +} +func (s *FileSystem) Remove(p string) error { + return s.Client.Remove(p) +} +func (s *FileSystem) Rename(oldname string, newname string) error { + return s.Client.Rename(oldname, newname) +} +func (s *FileSystem) Symlink(oldname string, newname string) error { + return s.Client.Symlink(oldname, newname) +} +func (s *FileSystem) Link(oldname string, newname string) error { + return s.Client.Link(oldname, newname) +} +func (s *FileSystem) Close() error { return nil } + +func Open(m *ice.Message, cb func(*FileSystem)) { + _ssh_conn(m, func(c *ssh.Client) { + defer c.Close() + if s, e := sftp.NewClient(c); !m.Warn(e) { + cb(&FileSystem{s}) + } + }) +} diff --git a/misc/ssh/xterm.go b/misc/ssh/xterm.go new file mode 100644 index 00000000..9467b57e --- /dev/null +++ b/misc/ssh/xterm.go @@ -0,0 +1,41 @@ +package ssh + +import ( + "io" + "os" + + "golang.org/x/crypto/ssh" + "golang.org/x/crypto/ssh/terminal" + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/cli" + "shylinux.com/x/icebergs/misc/xterm" + kit "shylinux.com/x/toolkits" +) + +type XTerm struct { + io.Reader + io.Writer + *ssh.Session +} + +func (s XTerm) Setsize(rows, cols string) error { + return s.Session.WindowChange(kit.Int(rows), kit.Int(cols)) +} +func (s XTerm) Close() error { + return s.Session.Close() +} + +func Shell(m *ice.Message, cb func(xterm.XTerm)) { + _ssh_conn(m, func(c *ssh.Client) { + if s, e := c.NewSession(); !m.Warn(e, ice.ErrNotValid) { + defer s.Close() + w, _ := s.StdinPipe() + r, _ := s.StdoutPipe() + width, height, _ := terminal.GetSize(int(os.Stdin.Fd())) + s.RequestPty(kit.Env(cli.TERM), height, width, ssh.TerminalModes{ssh.ECHO: 1, ssh.TTY_OP_ISPEED: 14400, ssh.TTY_OP_OSPEED: 14400}) + defer s.Wait() + s.Shell() + cb(&XTerm{r, w, s}) + } + }) +}