diff options
| author | Felix Hanley <felix@userspace.com.au> | 2017-11-22 04:12:39 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2017-11-22 04:17:48 +0000 |
| commit | 49235fb5ec33569ff7de00ce8198f8c18320210e (patch) | |
| tree | 51f6272c4df6e8e2370bcdfada1871073bcb5571 | |
| parent | f80bcc60d27fd5fb6a5b1c638984f84cb03f91ea (diff) | |
| download | dyndnsd-49235fb5ec33569ff7de00ce8198f8c18320210e.tar.gz dyndnsd-49235fb5ec33569ff7de00ce8198f8c18320210e.tar.bz2 | |
| -rw-r--r-- | http.go | 73 |
1 files changed, 73 insertions, 0 deletions
@@ -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, ",") + +} |
