Matrix/entities/general/generalController.go
2020-10-17 12:07:39 +02:00

119 lines
3.3 KiB
Go

package general
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"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"
)
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"`
}
type SetParamBody struct {
Packetloss int `json:"packetloss,omitempty"`
UnavailableTill int64 `json:"unavailableTill,omitempty"`
Consensus bool `json:"consensus,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)
}
}
func SetParams(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
request := SetParamBody{}
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
}
config.Packetloss = request.Packetloss
config.UnavailableTill = request.UnavailableTill
config.Consensus = request.Consensus
config.AuthentificationCheck = request.AuthentificationCheck
config.Signing = request.Signing
config.Encryption = request.Signing
config.HttpString = "https"
if !config.Encryption {
config.HttpString = "http"
}
w.WriteHeader(http.StatusOK)
}
func Reset(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
errResponse := utils.CheckRequest(r)
if errResponse != nil {
w.WriteHeader(http.StatusBadRequest)
if err := json.NewEncoder(w).Encode(errResponse); err != nil {
panic(err)
}
return
}
if err := device.InitServerSigningKey(); err != nil {
log.Fatal(err)
}
config.VerifyKeys = make(map[string]map[string][]byte)
if err := os.Remove("sqlite.db"); err != nil {
log.Println(err)
}
if err := database.InitDB(fmt.Sprintf("sqlite%s.db", time.Now().Unix())); err != nil {
log.Fatal(err)
}
config.SetDefaultParams()
w.WriteHeader(http.StatusOK)
}