-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change server domain to 0.0.0.0 #163
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,16 @@ func IsTCPAddrServing(url string, timeout time.Duration) (bool, error) { | |
return true, nil | ||
} | ||
|
||
func ServePortHTTP(server *http.ServeMux, port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int)) error { | ||
return ServePort(port, autoIncrPort, watchTimeout, watch, func(port int) error { | ||
return http.ListenAndServe(fmt.Sprintf("localhost:%d", port), server) | ||
func ServePortHTTP(server *http.ServeMux, host string, port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int)) error { | ||
return ServePort(host, port, autoIncrPort, watchTimeout, watch, func(port int) error { | ||
return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), server) | ||
}) | ||
} | ||
|
||
// suggested watch timeout: 500ms | ||
func ServePort(port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int), doWithPort func(port int) error) error { | ||
func ServePort(host string, port int, autoIncrPort bool, watchTimeout time.Duration, watch func(port int), doWithPort func(port int) error) error { | ||
for { | ||
addr := net.JoinHostPort("localhost", strconv.Itoa(port)) | ||
addr := net.JoinHostPort(host, strconv.Itoa(port)) | ||
serving, err := IsTCPAddrServing(addr, 20*time.Millisecond) | ||
if err != nil { | ||
return err | ||
|
@@ -72,3 +72,84 @@ func watchSignalWithinTimeout(timeout time.Duration, errSignal chan struct{}, ac | |
} | ||
action() | ||
} | ||
|
||
// get the local area network IP address by ipType | ||
func GetLocalHostByIPType(ipType string) []string { | ||
addresses, err := net.InterfaceAddrs() | ||
if err != nil { | ||
fmt.Printf("\nnet.InterfaceAddrs failed, err: %v\n", err.Error()) | ||
return nil | ||
} | ||
// ipv4 + ipv6 | ||
var ips []string | ||
for _, address := range addresses { | ||
if ipNet, ok := address.(*net.IPNet); ok { | ||
// IPv4 | ||
if ipNet.IP.To4() != nil && (ipType == "all" || ipType == "ipv4") { | ||
ips = append(ips, ipNet.IP.String()) | ||
} | ||
// IPv6 | ||
if ipNet.IP.To16() != nil && ipNet.IP.To4() == nil && (ipType == "all" || ipType == "ipv6") { | ||
ips = append(ips, ipNet.IP.String()) | ||
} | ||
} | ||
} | ||
if len(ips) == 0 { | ||
fmt.Println("no network interface found") | ||
return nil | ||
} | ||
return ips | ||
} | ||
|
||
// provide default values for host and port | ||
func GetHostAndIP(bindStr string, portStr string) (host string, port int) { | ||
// default host=localhost, ipv4 127.0.0.1 or ipv6 ::1 | ||
host = "localhost" | ||
port = 7070 | ||
|
||
if portStr != "" { | ||
parsePort, err := strconv.ParseInt(portStr, 10, 64) | ||
if err == nil { | ||
port = int(parsePort) | ||
} | ||
} | ||
|
||
localIPs := GetLocalHostByIPType("all") | ||
if localIPs == nil { | ||
return | ||
} | ||
// add default router | ||
localIPs = append(localIPs, []string{"0.0.0.0", "::"}...) | ||
for _, localIP := range localIPs { | ||
if localIP == bindStr { | ||
host = bindStr | ||
break | ||
} | ||
} | ||
// parse ipv6 | ||
ip := net.ParseIP(host) | ||
if ip != nil { | ||
// IP is valid, check if it is IPv4 or IPv6 | ||
if ip.To4() == nil { | ||
// ip is not a v4 addr, must be v6 | ||
host = fmt.Sprintf("[%s]", host) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// build URL based on host and port | ||
func BuildAndDisplayURL(host string, port int) string { | ||
url := fmt.Sprintf("http://%s:%d", host, port) | ||
fmt.Println("Server listen at:") | ||
fmt.Printf("- Open: %s \r\n", url) | ||
fmt.Printf("- Local: http://localhost:%d \r\n", port) | ||
// only print ipv4 | ||
if host == "0.0.0.0" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is valid hereif someone provides a bind to his public IP you won't display it. You will have to create a function to test if the bind is purely local I think and use icon other method, or copy paste the code here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will refer to the nodejs http-server to print the corresponding URL, like this: if (argv.a && host !== '0.0.0.0') { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, it only displays something if not listening on everything |
||
ipv4IPs := GetLocalHostByIPType("ipv4") | ||
for _, ipv4IP := range ipv4IPs { | ||
fmt.Printf("- Network: http://%s:%d \r\n", ipv4IP, port) | ||
} | ||
} | ||
return url | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package netutil | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestGetLocalHostByIPType(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
ipType string | ||
want bool | ||
}{ | ||
{name: "Test with all IPs", ipType: "all", want: true}, | ||
{name: "Test with IPv4", ipType: "ipv4", want: true}, | ||
{name: "Test unknow ipType", ipType: "test", want: false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ips := GetLocalHostByIPType(tt.ipType) | ||
if got := len(ips); got > 0 != tt.want { | ||
t.Errorf("GetLocalHostByIPType(%v) = %v; want %v", tt.ipType, got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetHostAndIP(t *testing.T) { | ||
ipv4IPs := GetLocalHostByIPType("ipv4") | ||
var tests = []struct { | ||
bindStr string | ||
portStr string | ||
wantHost string | ||
wantPort int | ||
}{ | ||
{"", "", "localhost", 7070}, // default host and port | ||
{ipv4IPs[0], "", ipv4IPs[0], 7070}, // provide host | ||
{"", "8080", "localhost", 8080}, // provide port | ||
{ipv4IPs[0], "8080", ipv4IPs[0], 8080}, // provide host and port | ||
} | ||
|
||
for _, tt := range tests { | ||
host, port := GetHostAndIP(tt.bindStr, tt.portStr) | ||
if host != tt.wantHost || port != tt.wantPort { | ||
t.Errorf("GetHostAndIP(%v, %v) => (%v, %v), want (%v, %v)", tt.bindStr, tt.portStr, host, port, tt.wantHost, tt.wantPort) | ||
} | ||
} | ||
} | ||
|
||
func TestBuildAndDisplayURL(t *testing.T) { | ||
ipv4IPs := GetLocalHostByIPType("ipv4") | ||
url := fmt.Sprintf("http://%s:7070", ipv4IPs[0]) | ||
var tests = []struct { | ||
host string | ||
port int | ||
wantURL string | ||
}{ | ||
{"localhost", 7070, "http://localhost:7070"}, | ||
{"127.0.0.1", 7070, "http://127.0.0.1:7070"}, | ||
{"0.0.0.0", 7070, "http://0.0.0.0:7070"}, | ||
{ipv4IPs[0], 7070, url}, | ||
{"::1", 7070, "http://::1:7070"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
gotURL := BuildAndDisplayURL(tt.host, tt.port) | ||
if gotURL != tt.wantURL { | ||
t.Errorf("BuildAndDisplayURL(%v, %v) => %v, want %v", tt.host, tt.port, gotURL, tt.wantURL) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a real issue
But, I would have written this 😅