-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
380 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,15 @@ | ||
NAME = ifconfig | ||
NAME=ifconfigd | ||
|
||
all: deps test build | ||
|
||
fmt: | ||
gofmt -w=true *.go | ||
all: deps test install | ||
|
||
deps: | ||
go get -d -v | ||
|
||
build: | ||
@mkdir -p bin | ||
go build -o bin/$(NAME) | ||
fmt: | ||
go fmt ./... | ||
|
||
test: | ||
go test | ||
go test ./... | ||
|
||
docker-image: | ||
docker build -t martinp/ifconfig . | ||
install: | ||
go install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"html/template" | ||
|
||
"github.com/gorilla/mux" | ||
geoip2 "github.com/oschwald/geoip2-golang" | ||
) | ||
|
||
const ( | ||
IP_HEADER = "x-ifconfig-ip" | ||
COUNTRY_HEADER = "x-ifconfig-country" | ||
) | ||
|
||
var cliUserAgentExp = regexp.MustCompile("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$") | ||
|
||
type API struct { | ||
db *geoip2.Reader | ||
CORS bool | ||
Template string | ||
} | ||
|
||
func New() *API { return &API{} } | ||
|
||
func NewWithGeoIP(filepath string) (*API, error) { | ||
db, err := geoip2.Open(filepath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &API{db: db}, nil | ||
} | ||
|
||
type Cmd struct { | ||
Name string | ||
Args string | ||
} | ||
|
||
func (c *Cmd) String() string { | ||
return c.Name + " " + c.Args | ||
} | ||
|
||
func cmdFromQueryParams(values url.Values) Cmd { | ||
cmd, exists := values["cmd"] | ||
if !exists || len(cmd) == 0 { | ||
return Cmd{Name: "curl"} | ||
} | ||
switch cmd[0] { | ||
case "fetch": | ||
return Cmd{Name: "fetch", Args: "-qo -"} | ||
case "wget": | ||
return Cmd{Name: "wget", Args: "-qO -"} | ||
} | ||
return Cmd{Name: "curl"} | ||
} | ||
|
||
func ipFromRequest(r *http.Request) (net.IP, error) { | ||
var host string | ||
realIP := r.Header.Get("X-Real-IP") | ||
var err error | ||
if realIP != "" { | ||
host = realIP | ||
} else { | ||
host, _, err = net.SplitHostPort(r.RemoteAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
ip := net.ParseIP(host) | ||
if ip == nil { | ||
return nil, fmt.Errorf("could not parse IP: %s", host) | ||
} | ||
return ip, nil | ||
} | ||
|
||
func headerKeyFromRequest(r *http.Request) string { | ||
vars := mux.Vars(r) | ||
key, ok := vars["key"] | ||
if !ok { | ||
return "" | ||
} | ||
return key | ||
} | ||
|
||
func (a *API) LookupCountry(ip net.IP) (string, error) { | ||
if a.db == nil { | ||
return "", nil | ||
} | ||
record, err := a.db.Country(ip) | ||
if err != nil { | ||
return "", err | ||
} | ||
if country, exists := record.Country.Names["en"]; exists { | ||
return country, nil | ||
} | ||
if country, exists := record.RegisteredCountry.Names["en"]; exists { | ||
return country, nil | ||
} | ||
return "", fmt.Errorf("could not determine country for IP: %s", ip) | ||
} | ||
|
||
func (a *API) defaultHandler(w http.ResponseWriter, r *http.Request) { | ||
cmd := cmdFromQueryParams(r.URL.Query()) | ||
funcMap := template.FuncMap{"ToLower": strings.ToLower} | ||
t, err := template.New(filepath.Base(a.Template)).Funcs(funcMap).ParseFiles(a.Template) | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
b, err := json.MarshalIndent(r.Header, "", " ") | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
|
||
var data = struct { | ||
IP string | ||
JSON string | ||
Header http.Header | ||
Cmd | ||
}{r.Header.Get(IP_HEADER), string(b), r.Header, cmd} | ||
|
||
if err := t.Execute(w, &data); err != nil { | ||
log.Print(err) | ||
} | ||
} | ||
|
||
func (a *API) jsonHandler(w http.ResponseWriter, r *http.Request) { | ||
key := headerKeyFromRequest(r) | ||
if key == "" { | ||
key = IP_HEADER | ||
} | ||
value := map[string]string{key: r.Header.Get(key)} | ||
b, err := json.MarshalIndent(value, "", " ") | ||
if err != nil { | ||
log.Print(err) | ||
return | ||
} | ||
w.Write(b) | ||
} | ||
|
||
func (a *API) cliHandler(w http.ResponseWriter, r *http.Request) { | ||
key := headerKeyFromRequest(r) | ||
if key == "" { | ||
key = IP_HEADER | ||
} | ||
value := r.Header.Get(key) | ||
if !strings.HasSuffix(value, "\n") { | ||
value += "\n" | ||
} | ||
io.WriteString(w, value) | ||
} | ||
|
||
func cliMatcher(r *http.Request, rm *mux.RouteMatch) bool { | ||
return cliUserAgentExp.MatchString(r.UserAgent()) | ||
} | ||
|
||
func (a *API) requestFilter(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ip, err := ipFromRequest(r) | ||
if err != nil { | ||
r.Header.Set(IP_HEADER, err.Error()) | ||
} else { | ||
r.Header.Set(IP_HEADER, ip.String()) | ||
country, err := a.LookupCountry(ip) | ||
if err != nil { | ||
r.Header.Set(COUNTRY_HEADER, err.Error()) | ||
} else { | ||
r.Header.Set(COUNTRY_HEADER, country) | ||
} | ||
} | ||
if a.CORS { | ||
w.Header().Set("Access-Control-Allow-Methods", "GET") | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (a *API) Handlers() http.Handler { | ||
r := mux.NewRouter() | ||
|
||
// JSON | ||
r.HandleFunc("/", a.jsonHandler).Methods("GET").Headers("Accept", "application/json") | ||
r.HandleFunc("/{key}", a.jsonHandler).Methods("GET").Headers("Accept", "application/json") | ||
r.HandleFunc("/{key}.json", a.jsonHandler).Methods("GET") | ||
|
||
// CLI | ||
r.HandleFunc("/", a.cliHandler).Methods("GET").MatcherFunc(cliMatcher) | ||
r.HandleFunc("/{key}", a.cliHandler).Methods("GET").MatcherFunc(cliMatcher) | ||
|
||
// Default | ||
r.HandleFunc("/", a.defaultHandler).Methods("GET") | ||
|
||
// Pass all requests through the request filter | ||
return a.requestFilter(r) | ||
} | ||
|
||
func (a *API) ListenAndServe(addr string) error { | ||
http.Handle("/", a.Handlers()) | ||
return http.ListenAndServe(addr, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func httpGet(url string, json bool, userAgent string) (string, error) { | ||
r, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
if json { | ||
r.Header.Set("Accept", "application/json") | ||
} | ||
r.Header.Set("User-Agent", userAgent) | ||
res, err := http.DefaultClient.Do(r) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer res.Body.Close() | ||
data, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} | ||
|
||
func TestGetIP(t *testing.T) { | ||
toJSON := func(k string, v string) string { | ||
return fmt.Sprintf("{\n \"%s\": \"%s\"\n}", k, v) | ||
} | ||
s := httptest.NewServer(New().Handlers()) | ||
var tests = []struct { | ||
url string | ||
json bool | ||
out string | ||
userAgent string | ||
}{ | ||
{s.URL, false, "127.0.0.1\n", "curl/7.26.0"}, | ||
{s.URL, false, "127.0.0.1\n", "Wget/1.13.4 (linux-gnu)"}, | ||
{s.URL, false, "127.0.0.1\n", "fetch libfetch/2.0"}, | ||
{s.URL + "/x-ifconfig-ip.json", false, toJSON("x-ifconfig-ip", "127.0.0.1"), ""}, | ||
{s.URL, true, toJSON("x-ifconfig-ip", "127.0.0.1"), ""}, | ||
} | ||
for _, tt := range tests { | ||
out, err := httpGet(tt.url, tt.json, tt.userAgent) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if out != tt.out { | ||
t.Errorf("Expected %q, got %q", tt.out, out) | ||
} | ||
} | ||
} | ||
|
||
func TestIPFromRequest(t *testing.T) { | ||
var tests = []struct { | ||
in *http.Request | ||
out net.IP | ||
}{ | ||
{&http.Request{RemoteAddr: "1.3.3.7:9999"}, net.ParseIP("1.3.3.7")}, | ||
{&http.Request{Header: http.Header{"X-Real-Ip": []string{"1.3.3.7"}}}, net.ParseIP("1.3.3.7")}, | ||
} | ||
for _, tt := range tests { | ||
ip, err := ipFromRequest(tt.in) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !ip.Equal(tt.out) { | ||
t.Errorf("Expected %s, got %s", tt.out, ip) | ||
} | ||
} | ||
} | ||
|
||
func TestCmdFromParameters(t *testing.T) { | ||
var tests = []struct { | ||
in url.Values | ||
out Cmd | ||
}{ | ||
{url.Values{}, Cmd{Name: "curl"}}, | ||
{url.Values{"cmd": []string{"foo"}}, Cmd{Name: "curl"}}, | ||
{url.Values{"cmd": []string{"curl"}}, Cmd{Name: "curl"}}, | ||
{url.Values{"cmd": []string{"fetch"}}, Cmd{Name: "fetch", Args: "-qo -"}}, | ||
{url.Values{"cmd": []string{"wget"}}, Cmd{Name: "wget", Args: "-qO -"}}, | ||
} | ||
for _, tt := range tests { | ||
cmd := cmdFromQueryParams(tt.in) | ||
if !reflect.DeepEqual(cmd, tt.out) { | ||
t.Errorf("Expected %+v, got %+v", tt.out, cmd) | ||
} | ||
} | ||
} | ||
|
||
func TestCLIMatcher(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" | ||
var tests = []struct { | ||
in string | ||
out bool | ||
}{ | ||
{"curl/7.26.0", true}, | ||
{"Wget/1.13.4 (linux-gnu)", true}, | ||
{"fetch libfetch/2.0", true}, | ||
{browserUserAgent, false}, | ||
} | ||
for _, tt := range tests { | ||
r := &http.Request{Header: http.Header{"User-Agent": []string{tt.in}}} | ||
if got := cliMatcher(r, nil); got != tt.out { | ||
t.Errorf("Expected %t, got %t for %q", tt.out, got, tt.in) | ||
} | ||
} | ||
} |
Oops, something went wrong.