This commit is contained in:
Martin Polden 2014-11-01 14:36:57 +01:00
parent e7356dba8f
commit 26f53cea7e
3 changed files with 84 additions and 84 deletions

View File

@ -6,7 +6,7 @@ clean:
rm -f -- $(TARGET)
fmt:
gofmt -tabs=false -tabwidth=4 -w=true *.go
gofmt -w=true *.go
install:
go build $(TARGET).go

View File

@ -1,97 +1,97 @@
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"regexp"
"strings"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"regexp"
"strings"
)
type Client struct {
IP net.IP
JSON string
Header http.Header
IP net.IP
JSON string
Header http.Header
}
func isCli(userAgent string) bool {
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
userAgent)
return match
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
userAgent)
return match
}
func parseRealIP(req *http.Request) net.IP {
var host string
realIP := req.Header.Get("X-Real-IP")
if realIP != "" {
host = realIP
} else {
host, _, _ = net.SplitHostPort(req.RemoteAddr)
}
return net.ParseIP(host)
var host string
realIP := req.Header.Get("X-Real-IP")
if realIP != "" {
host = realIP
} else {
host, _, _ = net.SplitHostPort(req.RemoteAddr)
}
return net.ParseIP(host)
}
func pathToKey(path string) string {
re := regexp.MustCompile("^\\/|\\.json$")
return re.ReplaceAllLiteralString(strings.ToLower(path), "")
re := regexp.MustCompile("^\\/|\\.json$")
return re.ReplaceAllLiteralString(strings.ToLower(path), "")
}
func isJson(req *http.Request) bool {
return strings.HasSuffix(req.URL.Path, ".json") ||
strings.Contains(req.Header.Get("Accept"), "application/json")
return strings.HasSuffix(req.URL.Path, ".json") ||
strings.Contains(req.Header.Get("Accept"), "application/json")
}
func handler(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "Invalid request method", 405)
return
}
if req.Method != "GET" {
http.Error(w, "Invalid request method", 405)
return
}
ip := parseRealIP(req)
header := pathToKey(req.URL.Path)
ip := parseRealIP(req)
header := pathToKey(req.URL.Path)
if isJson(req) {
if header == "all" {
b, _ := json.MarshalIndent(req.Header, "", " ")
io.WriteString(w, fmt.Sprintf("%s\n", b))
} else {
m := map[string]string{
header: req.Header.Get(header),
}
b, _ := json.MarshalIndent(m, "", " ")
io.WriteString(w, fmt.Sprintf("%s\n", b))
}
} else if isCli(req.UserAgent()) {
if header == "" || header == "ip" {
io.WriteString(w, fmt.Sprintf("%s\n", ip))
} else {
value := req.Header.Get(header)
io.WriteString(w, fmt.Sprintf("%s\n", value))
}
} else {
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
}
t, _ := template.
New("index.html").
Funcs(funcMap).
ParseFiles("index.html")
b, _ := json.MarshalIndent(req.Header, "", " ")
client := &Client{IP: ip, JSON: string(b), Header: req.Header}
t.Execute(w, client)
}
if isJson(req) {
if header == "all" {
b, _ := json.MarshalIndent(req.Header, "", " ")
io.WriteString(w, fmt.Sprintf("%s\n", b))
} else {
m := map[string]string{
header: req.Header.Get(header),
}
b, _ := json.MarshalIndent(m, "", " ")
io.WriteString(w, fmt.Sprintf("%s\n", b))
}
} else if isCli(req.UserAgent()) {
if header == "" || header == "ip" {
io.WriteString(w, fmt.Sprintf("%s\n", ip))
} else {
value := req.Header.Get(header)
io.WriteString(w, fmt.Sprintf("%s\n", value))
}
} else {
funcMap := template.FuncMap{
"ToLower": strings.ToLower,
}
t, _ := template.
New("index.html").
Funcs(funcMap).
ParseFiles("index.html")
b, _ := json.MarshalIndent(req.Header, "", " ")
client := &Client{IP: ip, JSON: string(b), Header: req.Header}
t.Execute(w, client)
}
}
func main() {
http.Handle("/assets/", http.StripPrefix("/assets/",
http.FileServer(http.Dir("assets/"))))
http.HandleFunc("/", handler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
http.Handle("/assets/", http.StripPrefix("/assets/",
http.FileServer(http.Dir("assets/"))))
http.HandleFunc("/", handler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

View File

@ -3,19 +3,19 @@ package main
import "testing"
func TestIsCLi(t *testing.T) {
userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)",
"fetch libfetch/2.0"}
userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)",
"fetch libfetch/2.0"}
for _, userAgent := range userAgents {
if !isCli(userAgent) {
t.Errorf("Expected true for %s", userAgent)
}
}
for _, userAgent := range userAgents {
if !isCli(userAgent) {
t.Errorf("Expected true for %s", userAgent)
}
}
browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
"Safari/537.36"
if isCli(browserUserAgent) {
t.Errorf("Expected false for %s", browserUserAgent)
}
browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
"Safari/537.36"
if isCli(browserUserAgent) {
t.Errorf("Expected false for %s", browserUserAgent)
}
}