Matrix/main.go

90 lines
3.6 KiB
Go
Raw Normal View History

2020-09-28 20:37:02 +00:00
package main
import (
2020-10-11 21:11:30 +00:00
"crypto/tls"
2020-09-28 20:37:02 +00:00
"log"
"net/http"
2020-09-29 07:44:35 +00:00
"os"
2020-09-28 20:37:02 +00:00
2020-10-11 21:11:30 +00:00
"nutfactory.org/Matrix/config"
"nutfactory.org/Matrix/entities/device"
"nutfactory.org/Matrix/entities/event"
"nutfactory.org/Matrix/entities/general"
"nutfactory.org/Matrix/entities/room"
"nutfactory.org/Matrix/entities/user"
2020-10-01 15:45:57 +00:00
"nutfactory.org/Matrix/utils/database"
2020-09-28 20:37:02 +00:00
"nutfactory.org/Matrix/utils/router"
)
var keyPath = "./ssl.key"
var certPath = "./ssl.crt"
var routes = router.Routes{
2020-10-11 21:11:30 +00:00
2020-09-28 20:37:02 +00:00
// General
2020-10-11 21:11:30 +00:00
router.Route{"ResolveServerName", "GET", "/.well-known/matrix/server", general.ResolveServerName},
router.Route{"GetServerImplementation", "GET", "/_matrix/federation/v1/version", general.GetServerImplementation},
router.Route{"Reset", "GET", "/reset", general.Reset},
2020-09-28 20:37:02 +00:00
// Keys
2020-10-11 21:11:30 +00:00
router.Route{"GetSigningKey", "GET", "/_matrix/key/v2/server/{keyId}", device.GetServerSigningKeyHandler},
router.Route{"GetSigningKey", "GET", "/_matrix/key/v2/server", device.GetServerSigningKeyHandler},
2020-09-28 20:37:02 +00:00
// Users
2020-10-11 21:11:30 +00:00
router.Route{"CheckUsernameAvailability", "GET", "/_matrix/client/r0/register/available", user.CheckUsernameAvailabilityHandler},
router.Route{"Register", "POST", "/_matrix/client/r0/register", user.RegisterHandler},
router.Route{"Login", "POST", "/_matrix/client/r0/login", user.LoginHandler},
router.Route{"Logout", "POST", "/_matrix/client/r0/logout", user.LogoutHandler},
router.Route{"Sync", "GET", "/_matrix/client/r0/sync", user.SyncHandler},
2020-09-28 20:37:02 +00:00
// Rooms
2020-10-11 21:11:30 +00:00
router.Route{"CreateRoom", "POST", "/_matrix/client/r0/createRoom", room.CreateRoomHandler},
router.Route{"GetRoomMembers", "GET", "/_matrix/client/r0/rooms/{roomId}/members", room.GetRoomMemberHandler},
router.Route{"JoinRoomUser", "POST", "/_matrix/client/r0/rooms/{roomId}/join", room.JoinRoomUserHandler},
2020-09-28 20:37:02 +00:00
2020-10-11 21:11:30 +00:00
router.Route{"GetPrepInfoToJoin", "GET", "/_matrix/federation/v1/make_join/{roomId}/{userId}", room.GetPrepInfoToJoinHandler},
router.Route{"JoinRoomServer", "PUT", "/_matrix/federation/v2/send_join/{roomId}/{eventId}", room.JoinRoomServerHandler},
2020-09-28 20:37:02 +00:00
// Events
2020-10-11 21:11:30 +00:00
router.Route{"CreateEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}", event.SendMessageHandler},
router.Route{"CreateStateEvent", "PUT", "/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}", event.CreateStateEventHandler},
router.Route{"GetEventUser", "GET", "/_matrix/client/r0/rooms/{roomId}/event/{eventId}", event.GetEventUserHandler},
router.Route{"GetStateEvent", "GET", "/_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey}", event.GetStateEventHandler},
2020-09-28 20:37:02 +00:00
2020-10-11 21:11:30 +00:00
router.Route{"SyncEventsServer", "PUT", "/_matrix/federation/v1/send/{txnId}", event.SyncEventsServerHandler},
router.Route{"Backfill", "GET", "/_matrix/federation/v1/backfill/{roomId}", event.BackfillHandler},
2020-09-28 20:37:02 +00:00
}
func main() {
2020-10-11 21:11:30 +00:00
// TODO: Change to something variable --> cli
// TODO: Implement Message Counter
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
config.Homeserver = "localhost"
config.Port = "80"
if err := device.InitServerSigningKey(); err != nil {
log.Fatal(err)
}
config.VerifyKeys = make(map[string]map[string][]byte)
2020-09-29 07:44:35 +00:00
os.Remove("sqlite.db")
2020-10-11 21:11:30 +00:00
if err := database.InitDB("sqlite.db"); err != nil {
log.Fatal(err)
}
defer database.DB.Close()
2020-09-28 20:37:02 +00:00
2020-10-11 21:11:30 +00:00
// TODO: Set Default Config Params here
config.HttpString = "https"
2020-09-28 20:37:02 +00:00
2020-10-11 21:11:30 +00:00
router := router.NewRouter(routes)
2020-09-28 20:37:02 +00:00
2020-10-11 21:11:30 +00:00
// TODO: Serve on Port 443 and 80 without Redirect
2020-09-28 20:37:02 +00:00
httpErr := http.ListenAndServeTLS(":443", certPath, keyPath, router)
if httpErr != nil {
log.Fatal(httpErr)
}
go http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.URL.String(), http.StatusMovedPermanently)
}))
}