diff --git a/federation/handle.go b/federation/handle.go index eaaba9b7..a91c8cd0 100644 --- a/federation/handle.go +++ b/federation/handle.go @@ -15,6 +15,26 @@ import ( "github.com/matrix-org/util" ) +// HandleVersionRequests is an option which makes the server process `/_matrix/federation/v1/version` requests. +func HandleVersionRequests() func(*Server) { + return func(srv *Server) { + mux := srv.mux.PathPrefix("/_matrix/federation/v1").Subrouter() + + handlerFn := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + w.WriteHeader(200) + b, _ := json.Marshal(map[string]interface{}{ + "server": map[string]interface{}{ + "name": "complement-federation-test-server", + "version": "0.1.0", + }, + }) + w.Write(b) + }) + + mux.Handle("/version", handlerFn).Methods("GET") + } +} + // EXPERIMENTAL // MakeJoinRequestsHandler is the http.Handler implementation for the make_join part of // HandleMakeSendJoinRequests. diff --git a/tests/federation_room_join_test.go b/tests/federation_room_join_test.go index 03b12ee0..34735679 100644 --- a/tests/federation_room_join_test.go +++ b/tests/federation_room_join_test.go @@ -2,8 +2,10 @@ package tests import ( "context" + "crypto/tls" "encoding/json" "fmt" + "io" "net/http" "net/url" "strings" @@ -24,6 +26,7 @@ import ( "github.com/matrix-org/complement/client" "github.com/matrix-org/complement/federation" "github.com/matrix-org/complement/helpers" + "github.com/matrix-org/complement/internal" "github.com/matrix-org/complement/match" "github.com/matrix-org/complement/must" "github.com/matrix-org/complement/runtime" @@ -296,12 +299,17 @@ func TestJoinFederatedRoomWithUnverifiableEvents(t *testing.T) { }) } +func VersionRequestHandler(s *federation.Server, w http.ResponseWriter, req *http.Request) { + // Send it +} + // This test checks that users cannot circumvent the auth checks via send_join. func TestBannedUserCannotSendJoin(t *testing.T) { deployment := complement.Deploy(t, 1) defer deployment.Destroy(t) srv := federation.NewServer(t, deployment, + federation.HandleVersionRequests(), federation.HandleKeyRequests(), federation.HandleTransactionRequests(nil, nil), ) @@ -309,6 +317,23 @@ func TestBannedUserCannotSendJoin(t *testing.T) { origin := srv.ServerName() defer cancel() + // XXX: Sanity check that the server is listening and responding (at-least from the host perspective) + splitPieces := strings.SplitN(string(srv.ServerName()), ":", 2) + port := splitPieces[1] + // Ignore HTTPS/TLS verification for localhost + httpTransport := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + httpClient := &http.Client{Transport: httpTransport} + // Make the request + res, err := httpClient.Get("https://localhost:" + port + "/_matrix/federation/v1/version") + if err != nil { + t.Fatalf("Failed to GET: %s", err) + } + defer internal.CloseIO(res.Body, "server response body") + resBody, err := io.ReadAll(res.Body) + t.Logf("asdf request %d %s", res.StatusCode, resBody) + fedClient := srv.FederationClient(deployment) charlie := srv.UserID("charlie")