-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsftp.go
78 lines (69 loc) · 1.86 KB
/
sftp.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
package main
import (
"fmt"
"io"
"log"
"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
)
func sftpHandler(sess ssh.Session) {
debugStream := io.Discard
serverOptions := []sftp.ServerOption{
sftp.WithDebug(debugStream),
}
server, err := sftp.NewServer(
sess,
serverOptions...,
)
if err != nil {
log.Printf("sftp server init error: %s\n", err)
return
}
if err := server.Serve(); err == io.EOF {
server.Close()
} else if err != nil {
fmt.Println("sftp server completed with error:", err)
}
}
func runSFTPServer(addr, token string) error {
sshServer := ssh.Server{
Addr: addr,
Handler: func(s ssh.Session) {
fmt.Fprintf(s, "SCP is not supported, use SFTP instead.\n")
s.Exit(1)
},
PublicKeyHandler: func(ctx ssh.Context, key ssh.PublicKey) bool {
user := ctx.User()
if user != token {
panic(fmt.Sprintf("user:'%s', token:'%s'", user, token))
}
return user == token
},
SubsystemHandlers: map[string]ssh.SubsystemHandler{
"sftp": sftpHandler,
},
}
return sshServer.ListenAndServe()
}
func setupSFTP() (*Tunnel, error) {
token := GenerateToken()
port, err := GetFreePort()
if err != nil {
return nil, fmt.Errorf("get free port to listen: %w", err)
}
addr := fmt.Sprintf("localhost:%d", port)
go func() {
if err := runSFTPServer(addr, token); err != nil {
log.Fatalf("run SFTP server: %v", err)
}
}()
// Generate a key to enable authentication
keygenCommand := "echo y | ssh-keygen -t ed25519 -f $HOME/.ssh/mitten_key -N '' -q >/dev/null 2>&1"
// Invoke sftp
sftpCommand := fmt.Sprintf("sftp -o Port=%d -o User=%s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $HOME/.ssh/mitten_key -q localhost", port, token)
tunnel := &Tunnel{
Command: fmt.Sprintf("mittenfs(){ %s; %s; };", keygenCommand, sftpCommand),
ForwardSpec: fmt.Sprintf("-R %v:%s", port, addr),
}
return tunnel, nil
}