2016-11-11 21:48:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-11-15 04:54:29 +07:00
|
|
|
"encoding/json"
|
2016-11-11 21:48:00 +07:00
|
|
|
"fmt"
|
2024-12-15 18:00:37 +07:00
|
|
|
"io"
|
2017-11-15 04:54:29 +07:00
|
|
|
"net/http"
|
2017-01-30 17:19:22 +07:00
|
|
|
|
2017-11-15 04:54:29 +07:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2017-08-02 23:25:27 +07:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-11-11 21:48:00 +07:00
|
|
|
)
|
|
|
|
|
2017-11-15 04:54:29 +07:00
|
|
|
// RegResponse is a struct for registration response JSON
|
|
|
|
type RegResponse struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Fulldomain string `json:"fulldomain"`
|
|
|
|
Subdomain string `json:"subdomain"`
|
|
|
|
Allowfrom []string `json:"allowfrom"`
|
2016-11-13 19:50:44 +07:00
|
|
|
}
|
|
|
|
|
2017-11-15 04:54:29 +07:00
|
|
|
func webRegisterPost(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2016-11-17 00:15:36 +07:00
|
|
|
var regStatus int
|
2017-11-15 04:54:29 +07:00
|
|
|
var reg []byte
|
2019-02-22 21:53:11 +07:00
|
|
|
var err error
|
2016-12-03 15:31:15 +07:00
|
|
|
aTXT := ACMETxt{}
|
2024-12-15 18:00:37 +07:00
|
|
|
bdata, _ := io.ReadAll(r.Body)
|
2021-01-11 22:31:09 +07:00
|
|
|
if len(bdata) > 0 {
|
2019-02-22 21:53:11 +07:00
|
|
|
err = json.Unmarshal(bdata, &aTXT)
|
2017-11-15 18:52:27 +07:00
|
|
|
if err != nil {
|
|
|
|
regStatus = http.StatusBadRequest
|
|
|
|
reg = jsonError("malformed_json_payload")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(regStatus)
|
2021-01-11 22:31:09 +07:00
|
|
|
_, _ = w.Write(reg)
|
2017-11-15 18:52:27 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 21:53:11 +07:00
|
|
|
|
|
|
|
// Fail with malformed CIDR mask in allowfrom
|
|
|
|
err = aTXT.AllowFrom.isValid()
|
|
|
|
if err != nil {
|
|
|
|
regStatus = http.StatusBadRequest
|
|
|
|
reg = jsonError("invalid_allowfrom_cidr")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(regStatus)
|
2021-01-11 22:31:09 +07:00
|
|
|
_, _ = w.Write(reg)
|
2019-02-22 21:53:11 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-02 20:42:10 +07:00
|
|
|
// Create new user
|
2016-12-03 15:31:15 +07:00
|
|
|
nu, err := DB.Register(aTXT.AllowFrom)
|
2016-11-11 21:48:00 +07:00
|
|
|
if err != nil {
|
|
|
|
errstr := fmt.Sprintf("%v", err)
|
2017-11-15 04:54:29 +07:00
|
|
|
reg = jsonError(errstr)
|
|
|
|
regStatus = http.StatusInternalServerError
|
2016-11-26 20:42:35 +07:00
|
|
|
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration")
|
2016-11-11 21:48:00 +07:00
|
|
|
} else {
|
2016-11-26 20:42:35 +07:00
|
|
|
log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user")
|
2017-11-15 04:54:29 +07:00
|
|
|
regStruct := RegResponse{nu.Username.String(), nu.Password, nu.Subdomain + "." + Config.General.Domain, nu.Subdomain, nu.AllowFrom.ValidEntries()}
|
|
|
|
regStatus = http.StatusCreated
|
|
|
|
reg, err = json.Marshal(regStruct)
|
|
|
|
if err != nil {
|
|
|
|
regStatus = http.StatusInternalServerError
|
|
|
|
reg = jsonError("json_error")
|
|
|
|
log.WithFields(log.Fields{"error": "json"}).Debug("Could not marshal JSON")
|
|
|
|
}
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|
2017-11-15 04:54:29 +07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(regStatus)
|
2021-01-11 22:31:09 +07:00
|
|
|
_, _ = w.Write(reg)
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|
|
|
|
|
2017-11-15 04:54:29 +07:00
|
|
|
func webUpdatePost(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
var updStatus int
|
|
|
|
var upd []byte
|
|
|
|
// Get user
|
|
|
|
a, ok := r.Context().Value(ACMETxtKey).(ACMETxt)
|
|
|
|
if !ok {
|
|
|
|
log.WithFields(log.Fields{"error": "context"}).Error("Context error")
|
|
|
|
}
|
2018-02-28 07:20:35 +07:00
|
|
|
// NOTE: An invalid subdomain should not happen - the auth handler should
|
|
|
|
// reject POSTs with an invalid subdomain before this handler. Reject any
|
|
|
|
// invalid subdomains anyway as a matter of caution.
|
|
|
|
if !validSubdomain(a.Subdomain) {
|
|
|
|
log.WithFields(log.Fields{"error": "subdomain", "subdomain": a.Subdomain, "txt": a.Value}).Debug("Bad update data")
|
|
|
|
updStatus = http.StatusBadRequest
|
|
|
|
upd = jsonError("bad_subdomain")
|
|
|
|
} else if !validTXT(a.Value) {
|
|
|
|
log.WithFields(log.Fields{"error": "txt", "subdomain": a.Subdomain, "txt": a.Value}).Debug("Bad update data")
|
|
|
|
updStatus = http.StatusBadRequest
|
|
|
|
upd = jsonError("bad_txt")
|
|
|
|
} else if validSubdomain(a.Subdomain) && validTXT(a.Value) {
|
2019-06-12 19:41:02 +07:00
|
|
|
err := DB.Update(a.ACMETxtPost)
|
2016-11-13 19:50:44 +07:00
|
|
|
if err != nil {
|
2016-11-26 20:42:35 +07:00
|
|
|
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error while trying to update record")
|
2017-11-15 04:54:29 +07:00
|
|
|
updStatus = http.StatusInternalServerError
|
|
|
|
upd = jsonError("db_error")
|
|
|
|
} else {
|
|
|
|
log.WithFields(log.Fields{"subdomain": a.Subdomain, "txt": a.Value}).Debug("TXT updated")
|
|
|
|
updStatus = http.StatusOK
|
|
|
|
upd = []byte("{\"txt\": \"" + a.Value + "\"}")
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 04:54:29 +07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(updStatus)
|
2021-01-11 22:31:09 +07:00
|
|
|
_, _ = w.Write(upd)
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|
2019-01-26 00:22:53 +07:00
|
|
|
|
|
|
|
// Endpoint used to check the readiness and/or liveness (health) of the server.
|
|
|
|
func healthCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|