star/ftp/ftp.go
2025-06-13 13:05:50 +08:00

110 lines
3.2 KiB
Go
Raw 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"
"net"
"os/user"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"goftp.io/server/v2"
)
var ports int
var authuser, authpwd string
var path, ip, publicIp string
var username, groupname, passiveports string
var cert, key string
var forceTls bool
type Auth struct {
}
func (a Auth) CheckPasswd(ctx *server.Context, user string, name string) (bool, error) {
if authuser == "" && authpwd == "" {
return true, nil
}
if authuser == user && authpwd == name {
return true, nil
}
return false, nil
}
// ftpCmd represents the ftp command
var Cmd = &cobra.Command{
Use: "ftp",
Short: `FTP文件服务器`,
Long: `FTP文件服务器`,
Run: func(cmd *cobra.Command, args []string) {
if username == "" || groupname == "" {
u, err := user.Current()
if err == nil {
username = u.Username
groupname = u.Username
} else {
username = "root"
groupname = "root"
}
}
if publicIp == "" {
if tmp, err := net.Dial("udp", "139.199.163.65:443"); err == nil {
publicIp = strings.Split(tmp.LocalAddr().String(), ":")[0]
tmp.Close()
}
}
path, _ = filepath.Abs(path)
driver, err := NewDriver(path)
if err != nil {
log.Fatal("Error starting server:", err)
}
opts := &server.Options{
Auth: Auth{},
Driver: driver,
Perm: server.NewSimplePerm(username, groupname),
Name: "B612 FTP Server",
Hostname: ip,
PublicIP: publicIp,
PassivePorts: passiveports,
Port: ports,
WelcomeMessage: "B612 FTP Server",
Logger: nil,
RateLimit: 0,
TLS: key != "" && cert != "",
ForceTLS: forceTls,
CertFile: cert,
KeyFile: key,
}
log.Printf("Starting ftp server on %v:%v", opts.Hostname, opts.Port)
log.Printf("AuthUser %v, Password %v", authuser, authpwd)
log.Printf("Public IP %v", publicIp)
log.Printf("Path %v", path)
log.Printf("Passive Ports %v", passiveports)
log.Printf("TLS %v", opts.TLS)
log.Printf("Username %v, Groupname %v", username, groupname)
server, err := server.NewServer(opts)
if err != nil {
log.Fatal("Error starting server:", err)
}
err = server.ListenAndServe()
if err != nil {
log.Fatal("Error starting server:", err)
}
},
}
func init() {
Cmd.Flags().StringVarP(&publicIp, "public-ip", "I", "", "公网IP")
Cmd.Flags().StringVarP(&username, "username", "U", "", "FTP用户名")
Cmd.Flags().StringVarP(&groupname, "groupname", "G", "", "FTP组名")
Cmd.Flags().StringVarP(&passiveports, "passiveports", "P", "50000-60000", "被动模式端口范围格式50000-60000")
Cmd.Flags().IntVarP(&ports, "port", "p", 21, "监听端口")
Cmd.Flags().StringVarP(&ip, "ip", "i", "0.0.0.0", "监听地址")
Cmd.Flags().StringVarP(&authuser, "auth-user", "u", "", "用户名,默认为任意")
Cmd.Flags().StringVarP(&authpwd, "auth-passwd", "k", "", "密码,默认为任意")
Cmd.Flags().StringVarP(&path, "folder", "f", "./", "本地文件地址")
Cmd.Flags().BoolVarP(&forceTls, "force-tls", "t", false, "强制使用TLS加密传输")
Cmd.Flags().StringVarP(&cert, "cert", "C", "", "TLS证书文件")
Cmd.Flags().StringVarP(&key, "key", "K", "", "TLS密钥文件")
}