-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (90 loc) · 2.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
)
var dir string
var port int
var build string
var version string
func main() {
for _, p := range os.Args {
switch p {
case "-h":
fmt.Println("Usage of SimpleFileServer [-v] [-h] [--port=8000] [--dir=.]\n -v\n version number\n --port int\n listen http port (default 8000)\n --dir string\n listen file path (default .)")
return
case "-v":
fmt.Printf("SimpleFileServer for Linux/Mac/BSD, Version %s, Build %s.\n", version, build)
return
}
if strings.Contains(p, "--port=") {
args := strings.Split(p, "=")
if len(args) == 2 {
i, _ := strconv.Atoi(args[1])
port = i
}
} else {
port = 8000
}
if strings.Contains(p, "--dir=") {
args := strings.Split(p, "=")
if len(args) == 2 {
stat, err := os.Stat(args[1]) //os.Stat获取文件信息
if err == nil && stat.IsDir() {
dir = args[1]
continue
}
fmt.Println("dir does not exist")
return
}
} else {
dir = "."
}
}
var wanIPURL = "https://myip.ipip.net/s"
wanIP := httpGet(wanIPURL)
IntranetIP := getIntranetIP()
fmt.Printf("listen port is %d\n", port)
fmt.Println("----------------------------------------------")
fmt.Printf("| loc address : http://127.0.0.1:%d\n", port)
fmt.Printf("| lan address : http://%s:%d\n", IntranetIP, port)
fmt.Printf("| wan address : http://%s:%d\n", strings.TrimSpace(wanIP), port)
fmt.Println("----------------------------------------------")
err := http.ListenAndServe(fmt.Sprintf(":%d", port), http.FileServer(http.Dir(dir)))
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func getIntranetIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func httpGet(url string) string {
resp, err := http.Get(url)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
return string(body)
}