summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2017-11-22 04:12:39 +0000
committerFelix Hanley <felix@userspace.com.au>2017-11-22 04:17:48 +0000
commit49235fb5ec33569ff7de00ce8198f8c18320210e (patch)
tree51f6272c4df6e8e2370bcdfada1871073bcb5571
parentf80bcc60d27fd5fb6a5b1c638984f84cb03f91ea (diff)
downloaddyndnsd-49235fb5ec33569ff7de00ce8198f8c18320210e.tar.gz
dyndnsd-49235fb5ec33569ff7de00ce8198f8c18320210e.tar.bz2
Add start of HTTP request handlingHEADmaster
-rw-r--r--http.go73
1 files changed, 73 insertions, 0 deletions
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, ",")
+
+}