mirror of
https://github.com/mpolden/echoip.git
synced 2025-01-30 03:45:02 +01:00
Fmt
This commit is contained in:
parent
e7356dba8f
commit
26f53cea7e
2
Makefile
2
Makefile
@ -6,7 +6,7 @@ clean:
|
|||||||
rm -f -- $(TARGET)
|
rm -f -- $(TARGET)
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
gofmt -tabs=false -tabwidth=4 -w=true *.go
|
gofmt -w=true *.go
|
||||||
|
|
||||||
install:
|
install:
|
||||||
go build $(TARGET).go
|
go build $(TARGET).go
|
||||||
|
140
ifconfig.go
140
ifconfig.go
@ -1,97 +1,97 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
IP net.IP
|
IP net.IP
|
||||||
JSON string
|
JSON string
|
||||||
Header http.Header
|
Header http.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
func isCli(userAgent string) bool {
|
func isCli(userAgent string) bool {
|
||||||
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
|
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
|
||||||
userAgent)
|
userAgent)
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRealIP(req *http.Request) net.IP {
|
func parseRealIP(req *http.Request) net.IP {
|
||||||
var host string
|
var host 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, _, _ = net.SplitHostPort(req.RemoteAddr)
|
host, _, _ = net.SplitHostPort(req.RemoteAddr)
|
||||||
}
|
}
|
||||||
return net.ParseIP(host)
|
return net.ParseIP(host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pathToKey(path string) string {
|
func pathToKey(path string) string {
|
||||||
re := regexp.MustCompile("^\\/|\\.json$")
|
re := regexp.MustCompile("^\\/|\\.json$")
|
||||||
return re.ReplaceAllLiteralString(strings.ToLower(path), "")
|
return re.ReplaceAllLiteralString(strings.ToLower(path), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func isJson(req *http.Request) bool {
|
func isJson(req *http.Request) bool {
|
||||||
return strings.HasSuffix(req.URL.Path, ".json") ||
|
return strings.HasSuffix(req.URL.Path, ".json") ||
|
||||||
strings.Contains(req.Header.Get("Accept"), "application/json")
|
strings.Contains(req.Header.Get("Accept"), "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, req *http.Request) {
|
func handler(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method != "GET" {
|
if req.Method != "GET" {
|
||||||
http.Error(w, "Invalid request method", 405)
|
http.Error(w, "Invalid request method", 405)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ip := parseRealIP(req)
|
ip := parseRealIP(req)
|
||||||
header := pathToKey(req.URL.Path)
|
header := pathToKey(req.URL.Path)
|
||||||
|
|
||||||
if isJson(req) {
|
if isJson(req) {
|
||||||
if header == "all" {
|
if header == "all" {
|
||||||
b, _ := json.MarshalIndent(req.Header, "", " ")
|
b, _ := json.MarshalIndent(req.Header, "", " ")
|
||||||
io.WriteString(w, fmt.Sprintf("%s\n", b))
|
io.WriteString(w, fmt.Sprintf("%s\n", b))
|
||||||
} else {
|
} else {
|
||||||
m := map[string]string{
|
m := map[string]string{
|
||||||
header: req.Header.Get(header),
|
header: req.Header.Get(header),
|
||||||
}
|
}
|
||||||
b, _ := json.MarshalIndent(m, "", " ")
|
b, _ := json.MarshalIndent(m, "", " ")
|
||||||
io.WriteString(w, fmt.Sprintf("%s\n", b))
|
io.WriteString(w, fmt.Sprintf("%s\n", b))
|
||||||
}
|
}
|
||||||
} else if isCli(req.UserAgent()) {
|
} else 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 {
|
} 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))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
funcMap := template.FuncMap{
|
funcMap := template.FuncMap{
|
||||||
"ToLower": strings.ToLower,
|
"ToLower": strings.ToLower,
|
||||||
}
|
}
|
||||||
t, _ := template.
|
t, _ := template.
|
||||||
New("index.html").
|
New("index.html").
|
||||||
Funcs(funcMap).
|
Funcs(funcMap).
|
||||||
ParseFiles("index.html")
|
ParseFiles("index.html")
|
||||||
b, _ := json.MarshalIndent(req.Header, "", " ")
|
b, _ := json.MarshalIndent(req.Header, "", " ")
|
||||||
client := &Client{IP: ip, JSON: string(b), Header: req.Header}
|
client := &Client{IP: ip, JSON: string(b), Header: req.Header}
|
||||||
t.Execute(w, client)
|
t.Execute(w, client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.Handle("/assets/", http.StripPrefix("/assets/",
|
http.Handle("/assets/", http.StripPrefix("/assets/",
|
||||||
http.FileServer(http.Dir("assets/"))))
|
http.FileServer(http.Dir("assets/"))))
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
err := http.ListenAndServe(":8080", nil)
|
err := http.ListenAndServe(":8080", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("ListenAndServe: ", err)
|
log.Fatal("ListenAndServe: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,19 +3,19 @@ package main
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestIsCLi(t *testing.T) {
|
func TestIsCLi(t *testing.T) {
|
||||||
userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)",
|
userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)",
|
||||||
"fetch libfetch/2.0"}
|
"fetch libfetch/2.0"}
|
||||||
|
|
||||||
for _, userAgent := range userAgents {
|
for _, userAgent := range userAgents {
|
||||||
if !isCli(userAgent) {
|
if !isCli(userAgent) {
|
||||||
t.Errorf("Expected true for %s", userAgent)
|
t.Errorf("Expected true for %s", userAgent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
|
browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
|
||||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
|
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
|
||||||
"Safari/537.36"
|
"Safari/537.36"
|
||||||
if isCli(browserUserAgent) {
|
if isCli(browserUserAgent) {
|
||||||
t.Errorf("Expected false for %s", browserUserAgent)
|
t.Errorf("Expected false for %s", browserUserAgent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user