package main import ( "fmt" "net/http" "os" "path/filepath" "github.com/chyok/st/config" "github.com/chyok/st/internal/discovery" "github.com/chyok/st/internal/handler" "github.com/chyok/st/web" "github.com/urfave/cli/v2" ) var ( port string ) // 初始化配置 func initConfig(c *cli.Context) error { return config.G.SetConf(port) } // 接收端 管理上传 func receiveClient() error { //接收者角色 go discovery.Send(discovery.Receiver) //udp 模式监听 go discovery.Listen(discovery.Sender, "") address := fmt.Sprintf("http://%s:%s", config.G.LocalIP, config.G.Port) fmt.Printf("Server address: %s\n", address) http.HandleFunc("/", handler.ReceiveHandler) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(web.StaticFs)))) fmt.Println("Waiting for transfer...") return http.ListenAndServe(config.G.WildcardAddress, nil) } // 发送端 func sendClient() error { // 发送者角色 go discovery.Send(discovery.Sender) //udp 监听指定路径下的文件 go discovery.Listen(discovery.Receiver, config.G.FilePath) url := fmt.Sprintf("http://%s:%s", config.G.LocalIP, config.G.Port) fmt.Printf("Server address: %s\n", url) http.HandleFunc("/", handler.SendHandler) // 加载静态资源 http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(web.StaticFs)))) http.HandleFunc("/download/", handler.FileServerHandler) // 下载压缩包 http.HandleFunc("/dlzip", handler.Downzip) // udp 传送文件 http.HandleFunc("/sendZip", handler.SendZip) // 已经打包的zip存放位置 http.HandleFunc("/files", handler.Dfiles) fmt.Println("send file to server...") return http.ListenAndServe(config.G.WildcardAddress, nil) } // 入口函数 func main() { app := &cli.App{ Name: "xtfs", Usage: "simple file transfer tool", UsageText: "xtfs [global options] [filename|foldername]", Description: "xtfs is a simple command-line tool for fast local file/folder sharing", Flags: []cli.Flag{ &cli.StringFlag{ Name: "port", Value: "9099", Usage: "server port", Aliases: []string{"p"}, Destination: &port, }, &cli.BoolFlag{ Name: "version", Aliases: []string{"v"}, Usage: "print the version", }, }, Action: func(c *cli.Context) error { if c.Bool("version") { fmt.Printf("xtfs version %s\n", config.G.Version) return nil } if c.NArg() > 0 { currentPath := filepath.ToSlash(c.Args().Get(0)) config.G.FilePath = currentPath return sendClient() } return receiveClient() }, Before: initConfig, } err := app.Run(os.Args) if err != nil { fmt.Println(err.Error()) } }