star/ftp/ftp.go
2023-04-05 23:33:03 +08:00

50 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ftp
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 Cmd = &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() {
Cmd.Flags().IntVarP(&ports, "port", "p", 21, "监听端口")
Cmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听地址")
Cmd.Flags().StringVarP(&username, "user", "u", "1", "用户名默认为1")
Cmd.Flags().StringVarP(&pwd, "pwd", "k", "1", "密码默认为1")
Cmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址")
}