/
ftp.go
90 lines (78 loc) 路 2.02 KB
/
ftp.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
package main
import (
"crypto/tls"
"github.com/aegistudio/shaft"
"github.com/aegistudio/shaft/serpent"
ftp "github.com/fclairamb/ftpserverlib"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"github.com/aegistudio/enigma"
)
var (
ftpSettings = &ftp.Settings{
ListenAddr: ":2121",
ActiveTransferPortNon20: true,
Banner: "enigma",
}
ftpReadOnly = false
)
type ftpMainDriver struct {
hfs *enigma.Fs
}
func (ftpMainDriver) GetSettings() (*ftp.Settings, error) {
return ftpSettings, nil
}
func (ftpMainDriver) ClientConnected(ftp.ClientContext) (string, error) {
return "", nil
}
func (ftpMainDriver) ClientDisconnected(_ ftp.ClientContext) {
}
func (ftpMainDriver) GetTLSConfig() (*tls.Config, error) {
return nil, nil
}
func (d ftpMainDriver) AuthUser(
cc ftp.ClientContext, user, pass string,
) (ftp.ClientDriver, error) {
return d.hfs, nil
}
var cmdFTP = &cobra.Command{
Use: "ftp",
Short: "serve enigma file system via FTP server",
PreRunE: func(cmd *cobra.Command, args []string) error {
if !ftpReadOnly {
return serpent.AddOption(cmd, withReadWriteOption)
}
return nil
},
RunE: serpent.Executor(shaft.Module(
shaft.Invoke(func(
rootCtx serpent.CommandContext, hfs *enigma.Fs,
) error {
group, ctx := errgroup.WithContext(rootCtx)
defer func() { _ = group.Wait() }()
driver := &ftpMainDriver{hfs: hfs}
server := ftp.NewFtpServer(driver)
group.Go(func() error {
return server.ListenAndServe()
})
group.Go(func() error {
<-ctx.Done()
_ = server.Stop()
return nil
})
return group.Wait()
}),
)).RunE,
}
func init() {
cmdFTP.PersistentFlags().StringVar(
&ftpSettings.ListenAddr, "addr", ftpSettings.ListenAddr,
"address for the FTP server to listen")
cmdFTP.PersistentFlags().StringVar(
&ftpSettings.Banner, "motd", ftpSettings.Banner,
"motd for the FTP server to display")
cmdFTP.PersistentFlags().BoolVar(
&ftpReadOnly, "read-only", ftpReadOnly,
"serve the FTP server in read only mode")
rootCmd.AddCommand(cmdFTP)
}