|
|
@@ -3,6 +3,7 @@ package main
|
|
|
import (
|
|
|
"fmt"
|
|
|
"log"
|
|
|
+ "net"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
@@ -10,12 +11,70 @@ import (
|
|
|
|
|
|
const scriptsDir = "scripts"
|
|
|
|
|
|
+func getLocalIP() (string, error) {
|
|
|
+ addrs, err := net.InterfaceAddrs()
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, addr := range addrs {
|
|
|
+ var ip net.IP
|
|
|
+ switch v := addr.(type) {
|
|
|
+ case *net.IPNet:
|
|
|
+ ip = v.IP
|
|
|
+ case *net.IPAddr:
|
|
|
+ ip = v.IP
|
|
|
+ }
|
|
|
+
|
|
|
+ if ip == nil || ip.IsLoopback() {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ ip = ip.To4()
|
|
|
+ if ip == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ return ip.String(), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ return "", fmt.Errorf("no local IP address found")
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
+ // 服务端口号
|
|
|
+ port := "18123"
|
|
|
+
|
|
|
// 确保脚本目录存在
|
|
|
if _, err := os.Stat(scriptsDir); os.IsNotExist(err) {
|
|
|
os.Mkdir(scriptsDir, os.ModePerm)
|
|
|
}
|
|
|
|
|
|
+ // 遍历脚本目录并输出文件列表
|
|
|
+ files, err := os.ReadDir(scriptsDir)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("无法读取脚本目录: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(files) > 0 {
|
|
|
+ ip, err := getLocalIP()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error:", err)
|
|
|
+ } else {
|
|
|
+ fmt.Println("Local IP address:", ip)
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println("脚本目录下的文件有:")
|
|
|
+ for _, file := range files {
|
|
|
+ if !file.IsDir() {
|
|
|
+ fmt.Printf("http://%s:%s/scripts/%s\n", ip, port, file.Name())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fmt.Println("脚本目录为空。")
|
|
|
+ }
|
|
|
+ fmt.Println("")
|
|
|
+
|
|
|
// 提供脚本下载
|
|
|
http.HandleFunc("/scripts/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
if r.Method == http.MethodGet {
|
|
|
@@ -51,7 +110,6 @@ func main() {
|
|
|
})
|
|
|
|
|
|
// 启动服务器
|
|
|
- port := "18123"
|
|
|
fmt.Printf("服务器已启动,监听端口: %s\n", port)
|
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
|
}
|