forked from Scalingo/go-ssh-examples
/
server.go
166 lines (140 loc) · 3.46 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
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/binary"
"errors"
"io"
"io/ioutil"
"net"
"os"
"log"
"golang.org/x/crypto/ssh"
)
var (
hostPrivateKeySigner ssh.Signer
)
func init() {
keyPath := "./host_key"
if os.Getenv("HOST_KEY") != "" {
keyPath = os.Getenv("HOST_KEY")
}
hostPrivateKey, err := ioutil.ReadFile(keyPath)
if err != nil {
panic(err)
}
hostPrivateKeySigner, err = ssh.ParsePrivateKey(hostPrivateKey)
if err != nil {
panic(err)
}
}
func keyAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
log.Println(conn.RemoteAddr(), "authenticate with", key.Type())
return nil, nil
}
func main() {
config := ssh.ServerConfig{
PublicKeyCallback: keyAuth,
}
config.AddHostKey(hostPrivateKeySigner)
port := "2222"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
socket, err := net.Listen("tcp", ":"+port)
if err != nil {
panic(err)
}
for {
conn, err := socket.Accept()
if err != nil {
panic(err)
}
// From a standard TCP connection to an encrypted SSH connection
sshConn, newChans, _, err := ssh.NewServerConn(conn, &config)
if err != nil {
panic(err)
}
log.Println("Connection from", sshConn.RemoteAddr())
go func() {
for chanReq := range newChans {
go handleChanReq(chanReq)
}
log.Println("End of connection")
sshConn.Close()
}()
}
}
func handleChanReq(chanReq ssh.NewChannel) {
if chanReq.ChannelType() != "session" {
chanReq.Reject(ssh.Prohibited, "channel type is not a session")
return
}
ch, reqs, err := chanReq.Accept()
if err != nil {
log.Println("fail to accept channel request", err)
return
}
req := <-reqs
if req.Type != "exec" {
ch.Write([]byte("request type '" + req.Type + "' is not 'exec'\r\n"))
ch.Close()
return
}
handleExec(ch, req)
}
// Payload: int: command size, string: command
func handleExec(ch ssh.Channel, req *ssh.Request) {
command := string(req.Payload[4:])
client, session, err := connectUpstream()
if err != nil {
ch.Write([]byte("fail to connect upstream: " + err.Error() + "\r\n"))
ch.Close()
return
}
exitStatus, err := pipe(ch, client, session, command)
if err != nil {
ch.Write([]byte("fail to pipe command:" + err.Error()))
ch.Close()
return
}
exitStatusBuffer := make([]byte, 4)
binary.PutUvarint(exitStatusBuffer, uint64(exitStatus))
log.Println("forward exit-code", exitStatus, "to client")
_, err = ch.SendRequest("exit-status", false, exitStatusBuffer)
if err != nil {
log.Println("Failed to forward exit-status to client:", err)
}
ch.Close()
client.Close()
log.Println("End of exec")
}
func pipe(ch ssh.Channel, client *ssh.Client, session *ssh.Session, command string) (int, error) {
targetStderr, err := session.StderrPipe()
if err != nil {
return -1, errors.New("fail to pipe stderr: " + err.Error())
}
targetStdout, err := session.StdoutPipe()
if err != nil {
return -1, errors.New("fail to pipe stdout: " + err.Error())
}
targetStdin, err := session.StdinPipe()
if err != nil {
return -1, errors.New("fail to pipe stdin: " + err.Error())
}
go io.Copy(targetStdin, ch)
go io.Copy(ch.Stderr(), targetStderr)
go io.Copy(ch, targetStdout)
err = session.Start(command)
if err != nil {
ch.Write([]byte("Error when starting '" + command + "': " + err.Error()))
ch.Close()
}
err = session.Wait()
if err != nil {
if err, ok := err.(*ssh.ExitError); ok {
return err.ExitStatus(), nil
} else {
return -1, errors.New("failed to wait ssh command: " + err.Error())
}
}
return 0, nil
}