-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.go
167 lines (150 loc) · 3.75 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
157
158
159
160
161
162
163
164
165
166
167
package main
// An example SCP server. This will serve files from and to ./examples/scp/testdata.
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/scp"
"github.com/pkg/sftp"
)
const (
host = "localhost"
port = 23235
)
func main() {
root, _ := filepath.Abs("./examples/scp/testdata")
handler := scp.NewFileSystemHandler(root)
s, err := wish.NewServer(
wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
wish.WithHostKeyPath(".ssh/term_info_ed25519"),
wish.WithSubsystem("sftp", sftpSubsystem(root)),
wish.WithMiddleware(
scp.Middleware(handler, handler),
),
)
if err != nil {
log.Error("could not start server", "error", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Info("Starting SSH server", "host", host, "port", port)
go func() {
if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("could not start server", "error", err)
done <- nil
}
}()
<-done
log.Info("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Error("could not stop server", "error", err)
}
}
func sftpSubsystem(root string) ssh.SubsystemHandler {
return func(s ssh.Session) {
log.Info("sftp", "root", root)
fs := &sftpHandler{root}
srv := sftp.NewRequestServer(s, sftp.Handlers{
FileList: fs,
FileGet: fs,
})
if err := srv.Serve(); err == io.EOF {
if err := srv.Close(); err != nil {
wish.Fatalln(s, "sftp:", err)
}
} else if err != nil {
wish.Fatalln(s, "sftp:", err)
}
}
}
// Example readonly handler implementation for sftp.
//
// other example implementations:
// - https://github.com/gravitational/teleport/blob/f57dc2fe2a9900ec198779aae747ac4f833b278d/tool/teleport/common/sftp.go
// - https://github.com/minio/minio/blob/c66c5828eacb4a7fa9a49b4c890c77dd8684b171/cmd/sftp-server.go
type sftpHandler struct {
root string
}
var (
_ sftp.FileLister = &sftpHandler{}
_ sftp.FileReader = &sftpHandler{}
)
type listerAt []fs.FileInfo
func (l listerAt) ListAt(ls []fs.FileInfo, offset int64) (int, error) {
if offset >= int64(len(l)) {
return 0, io.EOF
}
n := copy(ls, l[offset:])
if n < len(ls) {
return n, io.EOF
}
return n, nil
}
// Fileread implements sftp.FileReader.
func (s *sftpHandler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
var flags int
pflags := r.Pflags()
if pflags.Append {
flags |= os.O_APPEND
}
if pflags.Creat {
flags |= os.O_CREATE
}
if pflags.Excl {
flags |= os.O_EXCL
}
if pflags.Trunc {
flags |= os.O_TRUNC
}
if pflags.Read && pflags.Write {
flags |= os.O_RDWR
} else if pflags.Read {
flags |= os.O_RDONLY
} else if pflags.Write {
flags |= os.O_WRONLY
}
f, err := os.OpenFile(filepath.Join(s.root, r.Filepath), flags, 0600)
if err != nil {
return nil, err
}
return f, nil
}
// Filelist implements sftp.FileLister.
func (s *sftpHandler) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
switch r.Method {
case "List":
entries, err := os.ReadDir(filepath.Join(s.root, r.Filepath))
if err != nil {
return nil, fmt.Errorf("sftp: %w", err)
}
infos := make([]fs.FileInfo, len(entries))
for i, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, err
}
infos[i] = info
}
return listerAt(infos), nil
case "Stat":
fi, err := os.Stat(filepath.Join(s.root, r.Filepath))
if err != nil {
return nil, err
}
return listerAt{fi}, nil
default:
return nil, sftp.ErrSSHFxOpUnsupported
}
}