File tree Expand file tree Collapse file tree 3 files changed +24
-9
lines changed Expand file tree Collapse file tree 3 files changed +24
-9
lines changed Original file line number Diff line number Diff line change @@ -55,7 +55,7 @@ const (
55
55
56
56
var (
57
57
Port = flag .Int ("port" , 3000 , "Specify the server listening port." )
58
- Host = flag .String ("host" , "localhost " , "The server's IP address or domain." )
58
+ Host = flag .String ("host" , "" , "The server's IP address or domain." )
59
59
Path = flag .String ("path" , "" , "Specify a local path to public." )
60
60
VideoPath = flag .String ("video" , "" , "Specify a folder containing videos to be made public." )
61
61
NoBrowser = flag .Bool ("no-browser" , false , "Do not open browser automatically." )
Original file line number Diff line number Diff line change @@ -23,30 +23,43 @@ func OpenBrowser(url string) {
23
23
}
24
24
25
25
func GetIp () (ip string ) {
26
+ backupIp := "127.0.0.1"
26
27
ips , err := net .InterfaceAddrs ()
27
28
if err != nil {
28
29
SysError ("failed to get IP address: " + err .Error ())
29
- return ip
30
+ return backupIp
30
31
}
31
-
32
32
for _ , a := range ips {
33
33
if ipNet , ok := a .(* net.IPNet ); ok && ! ipNet .IP .IsLoopback () {
34
34
if ipNet .IP .To4 () != nil {
35
35
ip = ipNet .IP .String ()
36
36
if strings .HasPrefix (ip , "10" ) {
37
- return
37
+ // Class A: 10.0.0.0 to 10.255.255.255
38
+ backupIp = ip
39
+ continue
38
40
}
39
41
if strings .HasPrefix (ip , "172" ) {
40
- return
42
+ // Class B: 172.16.0.0 to 172.31.255.255
43
+ parts := strings .Split (ip , "." )
44
+ secondPart , err := strconv .Atoi (parts [1 ])
45
+ if err != nil {
46
+ continue
47
+ }
48
+ if secondPart >= 16 && secondPart <= 31 {
49
+ backupIp = ip
50
+ continue
51
+ }
41
52
}
42
53
if strings .HasPrefix (ip , "192.168" ) {
43
- return
54
+ // Class C: 192.168.0.0 to 192.168.255.255
55
+ backupIp = ip
56
+ continue
44
57
}
45
- ip = ""
58
+ return
46
59
}
47
60
}
48
61
}
49
- return
62
+ return backupIp
50
63
}
51
64
52
65
var sizeKB = 1024
Original file line number Diff line number Diff line change @@ -72,10 +72,12 @@ func main() {
72
72
if realPort == "" {
73
73
realPort = strconv .Itoa (* common .Port )
74
74
}
75
- if * common .Host == "localhost " {
75
+ if * common .Host == "" {
76
76
ip := common .GetIp ()
77
77
if ip != "" {
78
78
* common .Host = ip
79
+ } else {
80
+ * common .Host = "localhost"
79
81
}
80
82
}
81
83
serverUrl := "http://" + * common .Host + ":" + realPort + "/"
You can’t perform that action at this time.
0 commit comments