/
server.go
154 lines (129 loc) 路 3.37 KB
/
server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//+build !windows
package ssh
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"syscall"
"time"
"unsafe"
"github.com/creack/pty"
"github.com/gliderlabs/ssh"
"github.com/miguelmota/cointop/cointop/common/pathutil"
gossh "golang.org/x/crypto/ssh"
)
// DefaultHostKeyFile ...
var DefaultHostKeyFile = "~/.ssh/id_rsa"
// Config ...
type Config struct {
Port uint
Address string
IdleTimeout time.Duration
ExecutableBinary string
HostKeyFile string
}
// Server ...
type Server struct {
port uint
address string
idleTimeout time.Duration
executableBinary string
sshServer *ssh.Server
hostKeyFile string
}
// NewServer ...
func NewServer(config *Config) *Server {
hostKeyFile := DefaultHostKeyFile
if config.HostKeyFile != "" {
hostKeyFile = config.HostKeyFile
}
hostKeyFile = pathutil.NormalizePath(hostKeyFile)
return &Server{
port: config.Port,
address: config.Address,
idleTimeout: config.IdleTimeout,
executableBinary: config.ExecutableBinary,
hostKeyFile: hostKeyFile,
}
}
// ListenAndServe ...
func (s *Server) ListenAndServe() error {
s.sshServer = &ssh.Server{
Addr: fmt.Sprintf("%s:%v", s.address, s.port),
IdleTimeout: s.idleTimeout,
Handler: func(sshSession ssh.Session) {
ptyReq, winCh, isPty := sshSession.Pty()
if !isPty {
io.WriteString(sshSession, "Error: Non-interactive terminals are not supported")
sshSession.Exit(1)
return
}
tempDir, err := createTempDir()
if err != nil {
fmt.Println(err)
return
}
configPath := fmt.Sprintf("%s/config", tempDir)
cmdCtx, cancelCmd := context.WithCancel(sshSession.Context())
defer cancelCmd()
cmd := exec.CommandContext(cmdCtx, s.executableBinary, "--reset", "--silent", "--cache-dir", tempDir, "--config", configPath)
cmd.Env = append(sshSession.Environ(), fmt.Sprintf("TERM=%s", ptyReq.Term))
f, err := pty.Start(cmd)
if err != nil {
io.WriteString(sshSession, err.Error())
}
defer f.Close()
go func() {
for win := range winCh {
setWinsize(f, win.Width, win.Height)
}
}()
go func() {
io.Copy(f, sshSession)
}()
io.Copy(sshSession, f)
f.Close()
cmd.Wait()
os.Remove(configPath)
},
PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
// TODO: check public key hash
return true
},
PublicKeyHandler: func(ctx ssh.Context, key ssh.PublicKey) bool {
return true
},
PasswordHandler: func(ctx ssh.Context, password string) bool {
return true
},
KeyboardInteractiveHandler: func(ctx ssh.Context, challenger gossh.KeyboardInteractiveChallenge) bool {
return true
},
}
if _, err := os.Stat(s.hostKeyFile); os.IsNotExist(err) {
return errors.New("SSH key is required to start server")
}
err := s.sshServer.SetOption(ssh.HostKeyFile(s.hostKeyFile))
if err != nil {
return err
}
return s.sshServer.ListenAndServe()
}
// Shutdown ...
func (s *Server) Shutdown() {
s.sshServer.Close()
}
// setWinsize ...
func setWinsize(f *os.File, w, h int) {
syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
}
// createTempDir ...
// TODO: load saved configuration based on ssh public key hash
func createTempDir() (string, error) {
return ioutil.TempDir("", "")
}