This commit is contained in:
Martin Polden 2014-12-22 00:19:29 +01:00
parent 959c0a8360
commit e16e9a7aa9
6 changed files with 20 additions and 26 deletions

8
.gitignore vendored
View File

@ -1,5 +1,3 @@
ifconfig
*.lock
*.pid
*.log
cmd.js
bin/
pkg/
src/

View File

@ -1,15 +1,13 @@
TARGET = ifconfig
NAME = ifconfig
all: install
clean:
rm -f -- $(TARGET)
all: test build
fmt:
gofmt -w=true *.go
install:
go build $(TARGET).go
build:
@mkdir bin
go build -o bin/$(NAME)
test:
go test

View File

@ -29,13 +29,10 @@ Features
* Open source
* Fast
* Supports typical CLI tools (curl, wget and fetch)
* JSON output (optional)
Why?
====
* To scratch an itch
* An excuse to use Go for something
* Faster than ifconfig.me and has IPv6 support
Code style
==========
$ gofmt -tabs=false -tabwidth=4

1
Vagrantfile vendored
View File

@ -3,6 +3,7 @@
Vagrant.configure("2") do |config|
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 "salt/roots/", "/srv/salt/"
config.vm.provider :virtualbox do |vb|

View File

@ -12,16 +12,16 @@ import (
"strings"
)
var agentExp = regexp.MustCompile("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$")
type Client struct {
IP net.IP
JSON string
Header http.Header
}
func isCli(userAgent string) bool {
match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$",
userAgent)
return match
func isCLI(userAgent string) bool {
return agentExp.MatchString(userAgent)
}
func parseRealIP(req *http.Request) net.IP {
@ -40,7 +40,7 @@ func pathToKey(path string) string {
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") ||
strings.Contains(req.Header.Get("Accept"), "application/json")
}
@ -54,7 +54,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
ip := parseRealIP(req)
header := pathToKey(req.URL.Path)
if isJson(req) {
if isJSON(req) {
if header == "all" {
b, _ := json.MarshalIndent(req.Header, "", " ")
io.WriteString(w, fmt.Sprintf("%s\n", b))
@ -65,7 +65,7 @@ func handler(w http.ResponseWriter, req *http.Request) {
b, _ := json.MarshalIndent(m, "", " ")
io.WriteString(w, fmt.Sprintf("%s\n", b))
}
} else if isCli(req.UserAgent()) {
} else if isCLI(req.UserAgent()) {
if header == "" || header == "ip" {
io.WriteString(w, fmt.Sprintf("%s\n", ip))
} else {

View File

@ -2,12 +2,12 @@ package main
import "testing"
func TestIsCLi(t *testing.T) {
func TestIsCLI(t *testing.T) {
userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)",
"fetch libfetch/2.0"}
for _, userAgent := range userAgents {
if !isCli(userAgent) {
if !isCLI(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) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
"Safari/537.36"
if isCli(browserUserAgent) {
if isCLI(browserUserAgent) {
t.Errorf("Expected false for %s", browserUserAgent)
}
}