-
Notifications
You must be signed in to change notification settings - Fork 0
/
easygossh.go
112 lines (99 loc) · 2.22 KB
/
easygossh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package EasyGoSSH
import (
"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
"log"
"os/exec"
"runtime"
)
func ListenSSH(addr string, username string, password string, shellPath string) error {
sshHandler := func(s ssh.Session) {
println("test2")
serverOptions := []sftp.ServerOption{
func(server *sftp.Server) error {
println("test")
return nil
},
}
sftps, _ := sftp.NewServer(s, serverOptions...)
sftps.Serve()
log.Println("new session:", s.User())
cmd := exec.Command(shellPath)
stdIn, _ := cmd.StdinPipe()
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
return
}
go func() {
var stderrInBuf = make([]byte, 1024)
for {
i, err := stderrIn.Read(stderrInBuf)
if err != nil {
log.Println("stderrInBuf:", err.Error())
break
} else {
_, err = s.Write(TranslateOutput(stderrInBuf[:i]))
if err != nil {
log.Println("stderrInBuf:", err.Error())
break
}
}
}
}()
go func() {
var stdoutInBuf = make([]byte, 1024)
for {
i, err := stdoutIn.Read(stdoutInBuf)
if err != nil {
log.Println("stdoutInBuf:", err.Error())
break
} else {
_, err = s.Write(TranslateOutput(stdoutInBuf[:i]))
if err != nil {
log.Println("stdoutInBuf:", err.Error())
break
}
}
}
}()
go func() {
var buf = make([]byte, 1024)
for {
i, err := s.Read(buf)
if err != nil {
cmd.Process.Kill()
break
}
_, err = stdIn.Write(TranslateInput(buf[:i]))
println("input:", i)
println("input:", string(buf[:i]))
if err != nil {
cmd.Process.Kill()
break
}
}
}()
println(cmd.Wait().Error())
}
passHandler := func(ctx ssh.Context, pass string) bool {
return ctx.User() == username && pass == password
}
s := &ssh.Server{
Addr: addr,
Handler: sshHandler,
PasswordHandler: passHandler,
}
err := ssh.HostKeyPEM([]byte(RsaKey))(s)
if err != nil {
return err
}
return s.ListenAndServe()
}
func StartSSH(addr string, username string, password string, shellPath string) error {
if runtime.GOOS == "windows" {
RunningOnWin()
}
return ListenSSH(addr, username, password, shellPath)
}