Remove port

This commit is contained in:
Martin Polden 2012-11-25 21:25:32 +01:00
parent ad0992ee24
commit a03c16dfd8
2 changed files with 5 additions and 13 deletions

View File

@ -13,7 +13,6 @@ import (
type Client struct { type Client struct {
IP net.IP IP net.IP
Port string
Header http.Header Header http.Header
} }
@ -23,16 +22,15 @@ func isCli(userAgent string) bool {
return match return match
} }
func parseRealIP(req *http.Request) (net.IP, string) { func parseRealIP(req *http.Request) net.IP {
var host string var host string
var port string
realIP := req.Header.Get("X-Real-IP") realIP := req.Header.Get("X-Real-IP")
if realIP != "" { if realIP != "" {
host = realIP host = realIP
} else { } else {
host, port, _ = net.SplitHostPort(req.RemoteAddr) host, _, _ = net.SplitHostPort(req.RemoteAddr)
} }
return net.ParseIP(host), port return net.ParseIP(host)
} }
func pathToKey(path string) string { func pathToKey(path string) string {
@ -51,14 +49,12 @@ func handler(w http.ResponseWriter, req *http.Request) {
return return
} }
ip, port := parseRealIP(req) ip := parseRealIP(req)
header := pathToKey(req.URL.Path) header := pathToKey(req.URL.Path)
if isCli(req.UserAgent()) { if isCli(req.UserAgent()) {
if header == "" || header == "ip" { if header == "" || header == "ip" {
io.WriteString(w, fmt.Sprintf("%s\n", ip)) io.WriteString(w, fmt.Sprintf("%s\n", ip))
} else if header == "port" {
io.WriteString(w, fmt.Sprintf("%s\n", port))
} else { } else {
value := req.Header.Get(header) value := req.Header.Get(header)
io.WriteString(w, fmt.Sprintf("%s\n", value)) io.WriteString(w, fmt.Sprintf("%s\n", value))
@ -71,7 +67,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
New("index.html"). New("index.html").
Funcs(funcMap). Funcs(funcMap).
ParseFiles("index.html") ParseFiles("index.html")
client := &Client{IP: ip, Port: port, Header: req.Header} client := &Client{IP: ip, Header: req.Header}
t.Execute(w, client) t.Execute(w, client)
} }
} }

View File

@ -50,10 +50,6 @@
<td><code><span class="command">curl</span> ifconfig.co</code></td> <td><code><span class="command">curl</span> ifconfig.co</code></td>
<td class="response">{{ .IP }}</td> <td class="response">{{ .IP }}</td>
</tr> </tr>
<tr>
<td><code><span class="command">curl</span> ifconfig.co/port</code></td>
<td class="response">{{ .Port }}</td>
</tr>
{{ if $self := . }} {{ if $self := . }}
{{ range $key, $value := .Header }} {{ range $key, $value := .Header }}
<tr> <tr>