mirror of
https://github.com/mpolden/echoip.git
synced 2025-06-25 12:17:52 +02:00
13
api/api.go
13
api/api.go
@ -6,13 +6,13 @@ import (
|
||||
"html/template"
|
||||
"io"
|
||||
|
||||
"github.com/mpolden/ipd/useragent"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"math/big"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@ -24,10 +24,6 @@ const (
|
||||
textMediaType = "text/plain"
|
||||
)
|
||||
|
||||
var userAgentPattern = regexp.MustCompile(
|
||||
`^(?:curl|Wget|fetch\slibfetch|ddclient|Go-http-client|HTTPie)\/.*|Go\s1\.1\spackage\shttp$`,
|
||||
)
|
||||
|
||||
type API struct {
|
||||
Template string
|
||||
IPHeader string
|
||||
@ -210,7 +206,12 @@ func (a *API) NotFoundHandler(w http.ResponseWriter, r *http.Request) *appError
|
||||
}
|
||||
|
||||
func cliMatcher(r *http.Request, rm *mux.RouteMatch) bool {
|
||||
return userAgentPattern.MatchString(r.UserAgent())
|
||||
ua := useragent.Parse(r.UserAgent())
|
||||
switch ua.Product {
|
||||
case "curl", "HTTPie", "Wget", "fetch libfetch", "Go", "Go-http-client", "ddclient":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type appHandler func(http.ResponseWriter, *http.Request) *appError
|
||||
|
@ -150,6 +150,7 @@ func TestCLIMatcher(t *testing.T) {
|
||||
}{
|
||||
{"curl/7.26.0", true},
|
||||
{"Wget/1.13.4 (linux-gnu)", true},
|
||||
{"Wget", true},
|
||||
{"fetch libfetch/2.0", true},
|
||||
{"HTTPie/0.9.3", true},
|
||||
{"Go 1.1 package http", true},
|
||||
|
Reference in New Issue
Block a user