-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (137 loc) · 3.72 KB
/
main.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
package main
import (
"bytes"
"context"
"errors"
"io"
"net"
"os"
"cloud.google.com/go/storage"
"github.com/pkg/sftp"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/ssh"
"google.golang.org/api/option"
)
var SFTP []byte = []byte("sftp")
func main() {
// Initialize SSH server. Get the ssh.Signer by
// parsing the private key from envvar.
sshConfig := getSSHConfig()
privateKeyBytes := []byte(mustGetenv("PRIVATE_KEY"))
privateKey, err := ssh.ParsePrivateKey(privateKeyBytes)
if err != nil {
log.Fatal().
Err(err).
Msg("invalid private key")
}
sshConfig.AddHostKey(privateKey)
listener, err := net.Listen("tcp", "0.0.0.0:3003")
if err != nil {
log.Fatal().
Err(err).
Msg("unable to initialize listener")
}
log.Info().Msgf("listening on %v", listener.Addr())
// Initialize Google Cloud Storage bucket. For now, use
// a service account cred file to access the bucket.
bucketName := mustGetenv("BUCKET_NAME")
ctx := context.Background()
storageCreds := mustGetenv("SERVICE_ACCOUNT_KEY")
storageClient, err := storage.NewClient(
ctx, option.WithCredentialsFile(storageCreds),
)
if err != nil {
log.Fatal().
Err(err).
Msg("unable to initialize storage client")
}
bucket := storageClient.Bucket(bucketName)
log.Info().Msgf("using bucket with acl (%v)", bucket.ACL())
handler := &Handler{bucket}
// Accept *all* incoming connections
for {
conn, err := listener.Accept()
if err != nil {
log.Err(err).Msg("failed to accept incoming connection")
}
sshcon, channels, requests, err := ssh.NewServerConn(conn, sshConfig)
if err != nil {
log.Err(err).Msg("handshake failed")
continue
}
log.Info().Str("addr", sshcon.RemoteAddr().String()).Msg("new connection")
go ssh.DiscardRequests(requests)
go handleChannels(channels, handler)
}
}
func handleChannels(chans <-chan ssh.NewChannel, h *Handler) {
for newChannel := range chans {
go handleChannel(newChannel, h)
}
}
func handleChannel(newChannel ssh.NewChannel, h *Handler) {
// Only handle channel types that are `session` and reject `x11`,
// `direct-tcpip` and `forwarded-tcpip`.
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
return
}
// Attempt to accept the channel
channel, requests, err := newChannel.Accept()
if err != nil {
log.Err(err).Msg("could not accept channel")
return
}
go func(in <-chan *ssh.Request) {
for req := range in {
// Sessions have out-of-band requests such as `shell`, `pty-req` and
// `env`. Handle only `subsystem` requests.
if req.Type == "subsystem" &&
bytes.Compare(req.Payload[4:], SFTP) == 0 {
req.Reply(true, nil)
continue
}
req.Reply(false, nil)
}
}(requests)
server := sftp.NewRequestServer(channel, sftp.Handlers{h, h, h, h})
err = server.Serve()
if err == io.EOF {
server.Close()
log.Info().Msg("session ended")
}
if err != nil {
log.Err(err).Msg("")
}
}
func getSSHConfig() *ssh.ServerConfig {
passwordBytes := []byte(mustGetenv("PASSWORD"))
user := mustGetenv("USER")
return &ssh.ServerConfig{
NoClientAuth: false,
ServerVersion: "SSH-2.0-BSFTP",
AuthLogCallback: func(conn ssh.ConnMetadata, method string, err error) {
zlog := log.Info().Str("user", conn.User()).
Str("addr", conn.RemoteAddr().String()).
Str("method", method)
if err != nil {
zlog.Msg("auth failed")
return
}
zlog.Msg("auth ok")
},
PasswordCallback: func(c ssh.ConnMetadata, p []byte) (*ssh.Permissions, error) {
if c.User() == user && bytes.Compare(p, passwordBytes) == 0 {
return nil, nil
}
return nil, errors.New("invalid credentials")
},
}
}
func mustGetenv(e string) string {
n := os.Getenv(e)
if n == "" {
log.Fatal().Msgf("missing env %s", e)
}
return n
}