forked from x/icebergs
add some
This commit is contained in:
parent
aad9c09e96
commit
8dc2aaa35b
@ -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 })
|
kit.If(total == 0, func() { total = 1 })
|
||||||
count, buf := 0, make([]byte, cache)
|
count, buf := 0, make([]byte, cache)
|
||||||
for {
|
for {
|
||||||
|
@ -2,7 +2,6 @@ package nfs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
ice "shylinux.com/x/icebergs"
|
ice "shylinux.com/x/icebergs"
|
||||||
@ -15,7 +14,7 @@ func _trash_create(m *ice.Message, from string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
s, e := StatFile(m, from)
|
s, e := StatFile(m, from)
|
||||||
defer os.Remove(from)
|
defer Remove(m, from)
|
||||||
if m.Warn(e, ice.ErrNotFound, from) {
|
if m.Warn(e, ice.ErrNotFound, from) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,12 @@ func _port_right(m *ice.Message, arg ...string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
PORT_22 = "22"
|
||||||
|
PORT_80 = "80"
|
||||||
|
PORT_443 = "443"
|
||||||
|
PORT_9020 = "9020"
|
||||||
|
PORT_9022 = "9022"
|
||||||
|
|
||||||
SOCKET = "socket"
|
SOCKET = "socket"
|
||||||
BEGIN = "begin"
|
BEGIN = "begin"
|
||||||
CURRENT = "current"
|
CURRENT = "current"
|
||||||
|
@ -103,7 +103,7 @@ func PushImages(m *ice.Message, name string) {
|
|||||||
}
|
}
|
||||||
func PushNotice(m *ice.Message, arg ...ice.Any) {
|
func PushNotice(m *ice.Message, arg ...ice.Any) {
|
||||||
opts := ice.Map{ice.MSG_OPTION: []string{}, ice.MSG_OPTS: []string{}}
|
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[ice.MSG_OPTION] = kit.Simple(opts[ice.MSG_OPTION], key)
|
||||||
opts[key] = m.Option(key)
|
opts[key] = m.Option(key)
|
||||||
})
|
})
|
||||||
|
@ -41,7 +41,7 @@ func _xterm_get(m *ice.Message, h string) xterm.XTerm {
|
|||||||
for {
|
for {
|
||||||
if n, e := term.Read(buf); !m.Warn(e) && e == nil {
|
if n, e := term.Read(buf); !m.Warn(e) && e == nil {
|
||||||
if _xterm_echo(m, h, string(buf[:n])); len(text) > 0 {
|
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:]
|
text = text[1:]
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -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) {
|
func _ssh_conn(m *ice.Message, cb func(*ssh.Client), arg ...string) {
|
||||||
methods := []ssh.AuthMethod{}
|
methods := []ssh.AuthMethod{}
|
||||||
methods = append(methods, ssh.PublicKeysCallback(func() ([]ssh.Signer, error) {
|
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
|
return []ssh.Signer{key}, err
|
||||||
}))
|
}))
|
||||||
methods = append(methods, ssh.PasswordCallback(func() (string, error) { return m.Option(aaa.PASSWORD), nil }))
|
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 SSH = "ssh"
|
||||||
const (
|
const (
|
||||||
DIRECT = "direct"
|
DIRECT = "direct"
|
||||||
|
|
||||||
|
ID_RSA_KEY = ".ssh/id_rsa"
|
||||||
|
ID_RSA_PUB = ".ssh/id_rsa.pub"
|
||||||
)
|
)
|
||||||
const CONNECT = "connect"
|
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 (s session) Close() error { return s.sess.Close() }
|
||||||
|
|
||||||
func init() { xterm.AddCommand(SSH, NewSession) }
|
func init() { xterm.AddCommand(SSH, NewSession) }
|
||||||
|
|
||||||
func CombinedOutput(m *ice.Message, cmd string, cb func(string)) {
|
func CombinedOutput(m *ice.Message, cmd string, cb func(string)) {
|
||||||
_ssh_conn(m, func(c *ssh.Client) {
|
_ssh_conn(m, func(c *ssh.Client) {
|
||||||
if s, e := c.NewSession(); !m.Warn(e, ice.ErrNotValid) {
|
if s, e := c.NewSession(); !m.Warn(e, ice.ErrNotValid) {
|
||||||
|
74
misc/ssh/file.go
Normal file
74
misc/ssh/file.go
Normal file
@ -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})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
41
misc/ssh/xterm.go
Normal file
41
misc/ssh/xterm.go
Normal file
@ -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})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user