From 49235fb5ec33569ff7de00ce8198f8c18320210e Mon Sep 17 00:00:00 2001 From: Felix Hanley Date: Wed, 22 Nov 2017 15:12:39 +1100 Subject: Add start of HTTP request handling --- http.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 http.go diff --git a/http.go b/http.go new file mode 100644 index 0000000..e6939f9 --- /dev/null +++ b/http.go @@ -0,0 +1,73 @@ +package dyndns + +import ( + "errors" + "fmt" + "net/http" + //"strings" +) + +var ( + ErrBadAuth = errors.New("badauth") + ErrNotFQDN = errors.New("notfqdn") + ErrNoHost = errors.New("nohost") + ErrNumHost = errors.New("numhost") + ErrAbuse = errors.New("abuse") + ErrBadAgent = errors.New("badagent") + ErrDNSErr = errors.New("dnserr") + Err911 = errors.New("911") +) + +var errorCode = map[error]int{ + ErrBadAuth: 401, + ErrNotFQDN: 406, + ErrNoHost: 406, + ErrNumHost: 406, + ErrAbuse: 406, + ErrBadAgent: 406, + ErrDNSErr: 500, + Err911: 500, +} + +// https://{user}:{key}@example.com/v3/update?hostname={hostname} +// https://{user}:{key}@example.com/v3/update?hostname={hostname}&myip={IP address} + +func respond(w http.ResponseWriter, e error) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.WriteHeader(errorCode[e]) + fmt.Fprintln(w, e) +} + +func basicAuth(next http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + username, password, _ := r.BasicAuth() + + if !validateAuth(username, password) { + respond(w, ErrBadAuth) + return + } + next(w, r) + }) +} + +func validateAuth(username, password string) bool { + // TODO lookup + if username == "username" && password == "password" { + return true + } + return false +} + +func handleRequest(w http.ResponseWriter, r *http.Request) { + qs := r.URL.Query() + + hnStr := qs.Get("hostname") + if hnStr == "" { + respond(w, ErrNoHost) + return + } + + //hostnames := strings.Split(hnStr, ",") + +} -- cgit v1.2.3