mirror of
https://shylinux.com/x/icebergs
synced 2025-05-05 04:37:01 +08:00
opt ssh.connect.open
This commit is contained in:
parent
e5eab46faf
commit
210847ee8c
@ -43,6 +43,8 @@ func _totp_get(key string, num int, per int64) string {
|
|||||||
return kit.Format(kit.Format("%%0%dd", num), res%int64(math.Pow10(num)))
|
return kit.Format(kit.Format("%%0%dd", num), res%int64(math.Pow10(num)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TOTP_GET(key string, num int, per int64) string { return _totp_get(key, num, per) }
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SECRET = "secret"
|
SECRET = "secret"
|
||||||
NUMBER = "number"
|
NUMBER = "number"
|
||||||
|
@ -8,19 +8,41 @@ import (
|
|||||||
"github.com/shylinux/icebergs/base/tcp"
|
"github.com/shylinux/icebergs/base/tcp"
|
||||||
kit "github.com/shylinux/toolkits"
|
kit "github.com/shylinux/toolkits"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"golang.org/x/crypto/ssh"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func _ssh_conn(m *ice.Message, conn net.Conn, username, hostport string) (*ssh.Client, error) {
|
func _ssh_conn(m *ice.Message, conn net.Conn, username, hostport string) (*ssh.Client, error) {
|
||||||
key, e := ssh.ParsePrivateKey([]byte(m.Cmdx(nfs.CAT, path.Join(os.Getenv("HOME"), m.Option("private")))))
|
|
||||||
m.Assert(e)
|
|
||||||
|
|
||||||
methods := []ssh.AuthMethod{}
|
methods := []ssh.AuthMethod{}
|
||||||
methods = append(methods, ssh.PublicKeys(key))
|
methods = append(methods, ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) (res []string, err error) {
|
||||||
|
for _, k := range questions {
|
||||||
|
switch strings.TrimSpace(strings.ToLower(k)) {
|
||||||
|
case "verification code:":
|
||||||
|
res = append(res, aaa.TOTP_GET(m.Option("verify"), 6, 30))
|
||||||
|
case "password:":
|
||||||
|
res = append(res, m.Option(aaa.PASSWORD))
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.Debug("question: %v res: %d", questions, len(res))
|
||||||
|
return
|
||||||
|
}))
|
||||||
|
|
||||||
|
methods = append(methods, ssh.PublicKeysCallback(func() ([]ssh.Signer, error) {
|
||||||
|
key, err := ssh.ParsePrivateKey([]byte(m.Cmdx(nfs.CAT, path.Join(os.Getenv("HOME"), m.Option("private")))))
|
||||||
|
m.Debug("publickeys")
|
||||||
|
return []ssh.Signer{key}, err
|
||||||
|
}))
|
||||||
|
methods = append(methods, ssh.PasswordCallback(func() (string, error) {
|
||||||
|
m.Debug("password")
|
||||||
|
return m.Option(aaa.PASSWORD), nil
|
||||||
|
}))
|
||||||
|
|
||||||
c, chans, reqs, err := ssh.NewClientConn(conn, hostport, &ssh.ClientConfig{
|
c, chans, reqs, err := ssh.NewClientConn(conn, hostport, &ssh.ClientConfig{
|
||||||
User: username, Auth: methods, HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
User: username, Auth: methods, HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
||||||
@ -29,10 +51,7 @@ func _ssh_conn(m *ice.Message, conn net.Conn, username, hostport string) (*ssh.C
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
return ssh.NewClient(c, chans, reqs), err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return ssh.NewClient(c, chans, reqs), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const CONNECT = "connect"
|
const CONNECT = "connect"
|
||||||
@ -79,27 +98,52 @@ func init() {
|
|||||||
})
|
})
|
||||||
}},
|
}},
|
||||||
|
|
||||||
"open": {Name: "dial username=shy host=shylinux.com port=22 private=.ssh/id_rsa", Help: "添加", Hand: func(m *ice.Message, arg ...string) {
|
"open": {Name: "open authfile= username=shy password= verfiy= host=shylinux.com port=22 private=.ssh/id_rsa", Help: "终端", Hand: func(m *ice.Message, arg ...string) {
|
||||||
|
if f, e := os.Open(m.Option("authfile")); e == nil {
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
var data interface{}
|
||||||
|
json.NewDecoder(f).Decode(&data)
|
||||||
|
|
||||||
|
kit.Fetch(data, func(key string, value string) { m.Option(key, value) })
|
||||||
|
}
|
||||||
|
|
||||||
m.Option(tcp.DIAL_CB, func(c net.Conn) {
|
m.Option(tcp.DIAL_CB, func(c net.Conn) {
|
||||||
client, e := _ssh_conn(m, c, kit.Select("shy", m.Option(aaa.USERNAME)),
|
client, e := _ssh_conn(m, c, m.Option(aaa.USERNAME), m.Option(tcp.HOST)+":"+m.Option(tcp.PORT))
|
||||||
kit.Select("shylinux.com", m.Option(tcp.HOST))+":"+kit.Select("22", m.Option(tcp.PORT)),
|
|
||||||
)
|
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
m.Debug("what")
|
|
||||||
m.Debug("some")
|
|
||||||
session, e := client.NewSession()
|
session, e := client.NewSession()
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
|
fd := int(os.Stdin.Fd())
|
||||||
|
oldState, err := terminal.MakeRaw(fd)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer terminal.Restore(fd, oldState)
|
||||||
|
|
||||||
|
w, h, e := terminal.GetSize(fd)
|
||||||
|
m.Assert(e)
|
||||||
|
|
||||||
session.Stdin = os.Stdin
|
session.Stdin = os.Stdin
|
||||||
session.Stdout = os.Stdout
|
session.Stdout = os.Stdout
|
||||||
session.Stderr = os.Stderr
|
session.Stderr = os.Stderr
|
||||||
session.Start("/bin/bash")
|
|
||||||
m.Debug("what")
|
modes := ssh.TerminalModes{
|
||||||
m.Debug("some")
|
ssh.ECHO: 1,
|
||||||
|
ssh.TTY_OP_ISPEED: 14400,
|
||||||
|
ssh.TTY_OP_OSPEED: 14400,
|
||||||
|
}
|
||||||
|
|
||||||
|
session.RequestPty(os.Getenv("TERM"), h, w, modes)
|
||||||
|
session.Shell()
|
||||||
|
session.Wait()
|
||||||
})
|
})
|
||||||
|
|
||||||
m.Cmd(tcp.CLIENT, tcp.DIAL, tcp.PORT, m.Option(tcp.PORT), tcp.HOST, m.Option(tcp.HOST), arg)
|
m.Cmdy(tcp.CLIENT, tcp.DIAL, kit.MDB_TYPE, "ssh", kit.MDB_NAME, m.Option(tcp.HOST),
|
||||||
|
tcp.PORT, m.Option(tcp.PORT), tcp.HOST, m.Option(tcp.HOST), arg)
|
||||||
|
|
||||||
|
m.Echo("exit %s\n", m.Option(tcp.HOST))
|
||||||
}},
|
}},
|
||||||
mdb.DELETE: {Name: "delete", Help: "删除", Hand: func(m *ice.Message, arg ...string) {
|
mdb.DELETE: {Name: "delete", Help: "删除", Hand: func(m *ice.Message, arg ...string) {
|
||||||
m.Cmdy(mdb.DELETE, CONNECT, "", mdb.HASH, kit.MDB_HASH, m.Option(kit.MDB_HASH))
|
m.Cmdy(mdb.DELETE, CONNECT, "", mdb.HASH, kit.MDB_HASH, m.Option(kit.MDB_HASH))
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Render(msg *ice.Message, cmd string, args ...interface{}) {
|
func Render(msg *ice.Message, cmd string, args ...interface{}) {
|
||||||
defer func() { msg.Log_EXPORT(mdb.RENDER, cmd, kit.MDB_TEXT, args) }()
|
defer func() { msg.Log_EXPORT(mdb.RENDER, cmd, kit.Simple(args...)) }()
|
||||||
|
|
||||||
switch arg := kit.Simple(args...); cmd {
|
switch arg := kit.Simple(args...); cmd {
|
||||||
case ice.RENDER_VOID:
|
case ice.RENDER_VOID:
|
||||||
@ -191,8 +191,6 @@ func (f *Frame) parse(m *ice.Message, line string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, one := range kit.Split(line, ";", ";", ";") {
|
for _, one := range kit.Split(line, ";", ";", ";") {
|
||||||
m.Log_IMPORT("stdin", one, "length", len(one))
|
|
||||||
|
|
||||||
async, one := false, strings.TrimSpace(one)
|
async, one := false, strings.TrimSpace(one)
|
||||||
if strings.TrimSuffix(one, "&") != one {
|
if strings.TrimSuffix(one, "&") != one {
|
||||||
async, one = true, strings.TrimSuffix(one, "&")
|
async, one = true, strings.TrimSuffix(one, "&")
|
||||||
|
@ -266,7 +266,8 @@ func init() {
|
|||||||
},
|
},
|
||||||
Commands: map[string]*ice.Command{
|
Commands: map[string]*ice.Command{
|
||||||
SERVE: {Name: "serve name auto start", Help: "服务器", Action: map[string]*ice.Action{
|
SERVE: {Name: "serve name auto start", Help: "服务器", Action: map[string]*ice.Action{
|
||||||
gdb.START: {Name: "start name=self proto=http host= port=9020 dev=", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
|
gdb.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
|
||||||
|
m.Debug("what %v %v", m.Option("name"), m.Option("dev"))
|
||||||
if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" {
|
if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" {
|
||||||
m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.Right))
|
m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.Right))
|
||||||
}
|
}
|
||||||
|
11
init.go
11
init.go
@ -154,7 +154,6 @@ func Run(arg ...string) string {
|
|||||||
Pulse.root = Pulse
|
Pulse.root = Pulse
|
||||||
Pulse.Option("cache.limit", "30")
|
Pulse.Option("cache.limit", "30")
|
||||||
Pulse.Option("begin_time", Pulse.Time())
|
Pulse.Option("begin_time", Pulse.Time())
|
||||||
_log_disable = true
|
|
||||||
switch kit.Select("", arg, 0) {
|
switch kit.Select("", arg, 0) {
|
||||||
case "space", "serve":
|
case "space", "serve":
|
||||||
if _log_disable = false; frame.Begin(Pulse.Spawns(), arg...).Start(Pulse, arg...) {
|
if _log_disable = false; frame.Begin(Pulse.Spawns(), arg...).Start(Pulse, arg...) {
|
||||||
@ -163,10 +162,14 @@ func Run(arg ...string) string {
|
|||||||
|
|
||||||
<-wait
|
<-wait
|
||||||
os.Exit(frame.code)
|
os.Exit(frame.code)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if m := Pulse.Cmdy(arg); m.Result() == "" {
|
_log_disable = false
|
||||||
m.Table()
|
if Pulse.Cmdy(arg); Pulse.Result() == "" {
|
||||||
m.Sleep("100ms")
|
Pulse.Table()
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(Pulse.Result()) == "" {
|
||||||
|
Pulse.Set(MSG_RESULT).Cmdy("cli.system", arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user