2020-10-11 21:11:30 +00:00
|
|
|
package general
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2020-10-12 14:16:28 +00:00
|
|
|
"git.nutfactory.org/hoernschen/Matrix/config"
|
|
|
|
"git.nutfactory.org/hoernschen/Matrix/entities/device"
|
|
|
|
"git.nutfactory.org/hoernschen/Matrix/utils"
|
|
|
|
"git.nutfactory.org/hoernschen/Matrix/utils/database"
|
2020-10-11 21:11:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type resolveServerNameResponse struct {
|
|
|
|
Server string `json:"m.server,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type getServerImplementationResponse struct {
|
|
|
|
Server serverImplementation `json:"server,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type serverImplementation struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:16:28 +00:00
|
|
|
type ResetBody struct {
|
2020-10-11 21:11:30 +00:00
|
|
|
Packetloss float32 `json:"packetloss,omitempty"`
|
|
|
|
UnavailableTill int `json:"unavailableTill,omitempty"`
|
|
|
|
AuthentificationCheck bool `json:"authentificationCheck,omitempty"`
|
|
|
|
Signing bool `json:"signing,omitempty"`
|
|
|
|
Encryption bool `json:"encryption,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResolveServerName(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
response := resolveServerNameResponse{Server: fmt.Sprintf("%s:%s", config.Homeserver, config.Port)}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetServerImplementation(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
response := getServerImplementationResponse{Server: serverImplementation{Name: config.ServerName, Version: config.Version}}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:16:28 +00:00
|
|
|
// TODO: Add a function to set the Config-Params
|
2020-10-11 21:11:30 +00:00
|
|
|
func Reset(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
2020-10-12 14:16:28 +00:00
|
|
|
request := ResetBody{}
|
2020-10-11 21:11:30 +00:00
|
|
|
errResponse := utils.CheckRequest(r)
|
|
|
|
if errResponse != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&request)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
if err := json.NewEncoder(w).Encode(utils.ErrorResponse{ErrorMessage: fmt.Sprintf("Could not parse JSON: %s", err)}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := device.InitServerSigningKey(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
config.VerifyKeys = make(map[string]map[string][]byte)
|
|
|
|
|
|
|
|
os.Remove("sqlite.db")
|
|
|
|
|
|
|
|
if err := database.InitDB("sqlite.db"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Packetloss = request.Packetloss
|
|
|
|
config.UnavailableTill = request.UnavailableTill
|
|
|
|
config.AuthentificationCheck = request.AuthentificationCheck
|
|
|
|
config.Signing = request.Signing
|
|
|
|
config.Encryption = request.Signing
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|