mirror of https://github.com/mpolden/echoip
Cleanup
This commit is contained in:
parent
959c0a8360
commit
e16e9a7aa9
|
@ -1,5 +1,3 @@
|
||||||
ifconfig
|
bin/
|
||||||
*.lock
|
pkg/
|
||||||
*.pid
|
src/
|
||||||
*.log
|
|
||||||
cmd.js
|
|
12
Makefile
12
Makefile
|
@ -1,15 +1,13 @@
|
||||||
TARGET = ifconfig
|
NAME = ifconfig
|
||||||
|
|
||||||
all: install
|
all: test build
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -f -- $(TARGET)
|
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
gofmt -w=true *.go
|
gofmt -w=true *.go
|
||||||
|
|
||||||
install:
|
build:
|
||||||
go build $(TARGET).go
|
@mkdir bin
|
||||||
|
go build -o bin/$(NAME)
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test
|
go test
|
||||||
|
|
|
@ -29,13 +29,10 @@ Features
|
||||||
* Open source
|
* Open source
|
||||||
* Fast
|
* Fast
|
||||||
* Supports typical CLI tools (curl, wget and fetch)
|
* Supports typical CLI tools (curl, wget and fetch)
|
||||||
|
* JSON output (optional)
|
||||||
|
|
||||||
Why?
|
Why?
|
||||||
====
|
====
|
||||||
* To scratch an itch
|
* To scratch an itch
|
||||||
* An excuse to use Go for something
|
* An excuse to use Go for something
|
||||||
* Faster than ifconfig.me and has IPv6 support
|
* Faster than ifconfig.me and has IPv6 support
|
||||||
|
|
||||||
Code style
|
|
||||||
==========
|
|
||||||
$ gofmt -tabs=false -tabwidth=4
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
Vagrant.configure("2") do |config|
|
Vagrant.configure("2") do |config|
|
||||||
config.vm.box = "chef/ubuntu-14.04"
|
config.vm.box = "chef/ubuntu-14.04"
|
||||||
|
config.vm.network :forwarded_port, guest: 8080, host: 8080
|
||||||
config.vm.synced_folder ".", "/vagrant"
|
config.vm.synced_folder ".", "/vagrant"
|
||||||
config.vm.synced_folder "salt/roots/", "/srv/salt/"
|
config.vm.synced_folder "salt/roots/", "/srv/salt/"
|
||||||
config.vm.provider :virtualbox do |vb|
|
config.vm.provider :virtualbox do |vb|
|
||||||
|
|
14
ifconfig.go
14
ifconfig.go
|
@ -12,16 +12,16 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var agentExp = regexp.MustCompile("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$")
|
||||||
|
|
||||||
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)\\/.*$",
|
return agentExp.MatchString(userAgent)
|
||||||
userAgent)
|
|
||||||
return match
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRealIP(req *http.Request) net.IP {
|
func parseRealIP(req *http.Request) net.IP {
|
||||||
|
@ -40,7 +40,7 @@ func pathToKey(path string) string {
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
||||||
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))
|
||||||
|
@ -65,7 +65,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
|
||||||
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 {
|
||||||
|
|
|
@ -2,12 +2,12 @@ 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ func TestIsCLi(t *testing.T) {
|
||||||
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…
Reference in New Issue