mirror of
https://github.com/mpolden/echoip.git
synced 2025-02-15 11:38:59 +01:00
Parse IP address to net.IP
This commit is contained in:
parent
f1a8819fa7
commit
459032d089
@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Host string
|
IP net.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
func isCli(userAgent string) bool {
|
func isCli(userAgent string) bool {
|
||||||
@ -34,6 +34,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
host, _, err = net.SplitHostPort(req.RemoteAddr)
|
host, _, err = net.SplitHostPort(req.RemoteAddr)
|
||||||
}
|
}
|
||||||
|
ip := net.ParseIP(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to parse remote address: %s\n", req.RemoteAddr)
|
log.Printf("Failed to parse remote address: %s\n", req.RemoteAddr)
|
||||||
http.Error(w, "Failed to parse remote address", 500)
|
http.Error(w, "Failed to parse remote address", 500)
|
||||||
@ -41,10 +42,10 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if isCli(req.UserAgent()) {
|
if isCli(req.UserAgent()) {
|
||||||
io.WriteString(w, fmt.Sprintf("%s\n", host))
|
io.WriteString(w, fmt.Sprintf("%s\n", ip))
|
||||||
} else {
|
} else {
|
||||||
t, _ := template.ParseFiles("index.html")
|
t, _ := template.ParseFiles("index.html")
|
||||||
client := &Client{Host: host}
|
client := &Client{IP: ip}
|
||||||
t.Execute(w, client)
|
t.Execute(w, client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,6 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>What is my IP address?</h1>
|
<h1>What is my IP address?</h1>
|
||||||
<p>Your IP address is <strong>{{ .Host }}</strong></p>
|
<p>Your IP address is <strong>{{ .IP }}</strong></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user