From e94c730def88a234616b1d9af82f314d8f304dd2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 Apr 2026 20:59:28 +0800 Subject: [PATCH] feat: add cross-platform support (Windows/Linux/macOS), update README --- README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++---- cmd/main.go | 4 ++- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a729e0a..899cdba 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# FTP Server for Windows +# FTP Server -基于 Go 语言开发的轻量级 FTP 服务器,带 Web 管理面板,专为 Windows 环境设计。 +基于 Go 语言开发的轻量级 FTP 服务器,带 Web 管理面板,**跨平台支持 Windows / Linux / macOS**。 ## 功能特性 @@ -25,11 +25,23 @@ 确保已安装 Go 1.21+,然后执行: ```bash -go build -o ftp-server.exe ./cmd/ +# 编译为当前平台 +go build -o ftp-server ./cmd/ + +# 交叉编译 - Windows (64位) +GOOS=windows GOARCH=amd64 go build -o ftp-server.exe ./cmd/ + +# 交叉编译 - Linux (64位) +GOOS=linux GOARCH=amd64 go build -o ftp-server ./cmd/ + +# 交叉编译 - macOS +GOOS=darwin GOARCH=amd64 go build -o ftp-server ./cmd/ ``` ### 运行 +**Windows:** + ```bash # 使用默认配置启动 .\ftp-server.exe @@ -38,6 +50,19 @@ go build -o ftp-server.exe ./cmd/ .\ftp-server.exe -config myconfig.json ``` +**Linux / macOS:** + +```bash +# 使用默认配置启动 +./ftp-server + +# 指定配置文件 +./ftp-server -config myconfig.json + +# 后台运行 +nohup ./ftp-server > ftp-server.log 2>&1 & +``` + 首次运行会自动生成 `config.json` 配置文件和 `ftp_root` 根目录。 ### 访问 @@ -56,6 +81,48 @@ go build -o ftp-server.exe ./cmd/ > 请在首次登录后立即修改默认密码。 +## Linux Systemd 服务(可选) + +在 Linux 上可以配置为 systemd 服务,实现开机自启: + +```bash +# 创建服务文件 +sudo nano /etc/systemd/system/ftp-server.service +``` + +写入以下内容(按实际路径修改): + +```ini +[Unit] +Description=FTP Server +After=network.target + +[Service] +Type=simple +User=root +WorkingDirectory=/opt/ftp-server +ExecStart=/opt/ftp-server/ftp-server -config /opt/ftp-server/config.json +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target +``` + +启动并设置开机自启: + +```bash +sudo systemctl daemon-reload +sudo systemctl start ftp-server +sudo systemctl enable ftp-server + +# 查看状态 +sudo systemctl status ftp-server + +# 查看日志 +sudo journalctl -u ftp-server -f +``` + ## 配置说明 配置文件为 `config.json`,结构如下: @@ -103,7 +170,7 @@ go build -o ftp-server.exe ./cmd/ ## FTP 客户端连接 -推荐使用 [FileZilla](https://filezilla-project.org/) 或 Windows 资源管理器连接: +推荐使用 [FileZilla](https://filezilla-project.org/) 连接: ``` 主机: localhost @@ -127,7 +194,7 @@ FTP-server/ ## 技术栈 -- **Go** — 无需运行时依赖,单文件部署 +- **Go** — 无需运行时依赖,单文件部署,跨平台支持 - **标准库 net/http** — Web 管理面板 - **标准库 net** — FTP 协议实现 - **内嵌 HTML/CSS/JS** — 无需额外前端文件 diff --git a/cmd/main.go b/cmd/main.go index 6fba2e9..175fa2d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "os/signal" + "runtime" "syscall" "ftp-server/config" @@ -24,8 +25,9 @@ func main() { } fmt.Println("======================================") - fmt.Println(" FTP Server for Windows") + fmt.Println(" FTP Server (Cross-Platform)") fmt.Println("======================================") + fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) fmt.Printf("FTP Port: %d\n", cfg.FTP.Port) fmt.Printf("Web Admin: http://127.0.0.1:%d\n", cfg.Web.Port) fmt.Printf("Admin User: %s\n", cfg.Admin.Username)