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.
star/ftp/ftp.go

50 lines
1.3 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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", "./", "本地文件地址")
}