95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
|
package general
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
|
||
|
"nutfactory.org/Matrix/config"
|
||
|
"nutfactory.org/Matrix/entities/device"
|
||
|
"nutfactory.org/Matrix/utils"
|
||
|
"nutfactory.org/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 resetBody struct {
|
||
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Reset(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||
|
request := resetBody{}
|
||
|
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)
|
||
|
}
|