Matrix/entities/general/generalController.go

120 lines
3.3 KiB
Go
Raw Permalink Normal View History

2020-10-11 21:11:30 +00:00
package general
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
2020-10-17 10:07:39 +00:00
"time"
2020-10-11 21:11:30 +00:00
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-17 10:07:39 +00:00
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"`
2020-10-11 21:11:30 +00:00
}
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-17 10:07:39 +00:00
func SetParams(w http.ResponseWriter, r *http.Request) {
2020-10-11 21:11:30 +00:00
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
2020-10-17 10:07:39 +00:00
request := SetParamBody{}
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
}
2020-10-17 10:07:39 +00:00
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
}
2020-10-11 21:11:30 +00:00
if err := device.InitServerSigningKey(); err != nil {
log.Fatal(err)
}
config.VerifyKeys = make(map[string]map[string][]byte)
2020-10-17 10:07:39 +00:00
if err := os.Remove("sqlite.db"); err != nil {
log.Println(err)
}
2020-10-11 21:11:30 +00:00
2020-10-17 10:07:39 +00:00
if err := database.InitDB(fmt.Sprintf("sqlite%s.db", time.Now().Unix())); err != nil {
2020-10-11 21:11:30 +00:00
log.Fatal(err)
}
2020-10-17 10:07:39 +00:00
config.SetDefaultParams()
2020-10-11 21:11:30 +00:00
w.WriteHeader(http.StatusOK)
}