You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Victorique/vtqe/ftp.go

51 lines
1.3 KiB
Go

4 years ago
package main
import (
"log"
"path/filepath"
filedriver "github.com/goftp/file-driver"
"github.com/goftp/server"
"github.com/spf13/cobra"
)
var ports int
var username, pwd string
var path, ip string
// ftpCmd represents the ftp command
var ftpcmd = &cobra.Command{
Use: "ftp",
Short: `FTP文件服务器`,
Long: `FTP文件服务器`,
Run: func(cmd *cobra.Command, args []string) {
path, _ = filepath.Abs(path)
factory := &filedriver.FileDriverFactory{
RootPath: path,
Perm: server.NewSimplePerm("user", "group"),
}
opts := &server.ServerOpts{
Factory: factory,
Port: ports,
Hostname: ip,
Auth: &server.SimpleAuth{Name: username, Password: pwd},
}
log.Printf("Starting ftp server on %v:%v", opts.Hostname, opts.Port)
log.Printf("Username %v, Password %v", username, pwd)
server := server.NewServer(opts)
err := server.ListenAndServe()
if err != nil {
log.Fatal("Error starting server:", err)
}
},
}
func init() {
ftpcmd.Flags().IntVarP(&ports, "port", "p", 21, "监听端口")
ftpcmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听地址")
ftpcmd.Flags().StringVarP(&username, "user", "u", "1", "用户名默认为1")
ftpcmd.Flags().StringVarP(&pwd, "pwd", "k", "1", "密码默认为1")
ftpcmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址")
}