From f4c676ccddcd661b7167e8f249f71635a0b03491 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Thu, 4 Jun 2020 14:27:10 +0100 Subject: [PATCH] Refactor how federationsender gets created (#1095) * Refactor how federationsender gets created * s/httpint/inthttp/ for alphabetical niceness with internal package --- cmd/dendrite-client-api-server/main.go | 2 +- cmd/dendrite-demo-libp2p/main.go | 2 +- cmd/dendrite-federation-api-server/main.go | 2 +- cmd/dendrite-monolith-server/main.go | 14 +-- cmd/dendrite-public-rooms-api-server/main.go | 2 +- cmd/dendrite-room-server/main.go | 2 +- cmd/dendritejs/main.go | 2 +- federationsender/api/api.go | 68 +++++++++-- federationsender/api/perform.go | 112 ------------------ federationsender/api/query.go | 63 ----------- federationsender/federationsender.go | 9 +- federationsender/internal/api.go | 90 --------------- federationsender/inthttp/client.go | 113 +++++++++++++++++++ federationsender/inthttp/server.go | 95 ++++++++++++++++ internal/basecomponent/base.go | 15 +-- 15 files changed, 288 insertions(+), 303 deletions(-) delete mode 100644 federationsender/api/perform.go delete mode 100644 federationsender/api/query.go create mode 100644 federationsender/inthttp/client.go create mode 100644 federationsender/inthttp/server.go diff --git a/cmd/dendrite-client-api-server/main.go b/cmd/dendrite-client-api-server/main.go index f919243de..280e9cdef 100644 --- a/cmd/dendrite-client-api-server/main.go +++ b/cmd/dendrite-client-api-server/main.go @@ -37,7 +37,7 @@ func main() { asQuery := base.CreateHTTPAppServiceAPIs() rsAPI := base.CreateHTTPRoomserverAPIs() - fsAPI := base.CreateHTTPFederationSenderAPIs() + fsAPI := base.FederationSenderHTTPClient() rsAPI.SetFederationSenderAPI(fsAPI) eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) diff --git a/cmd/dendrite-demo-libp2p/main.go b/cmd/dendrite-demo-libp2p/main.go index c00316ec7..86909685a 100644 --- a/cmd/dendrite-demo-libp2p/main.go +++ b/cmd/dendrite-demo-libp2p/main.go @@ -178,7 +178,7 @@ func main() { base.Base.PublicAPIMux, base.Base.InternalAPIMux, &cfg, - base.Base.EnableHTTPAPIs, + base.Base.UseHTTPAPIs, ) // Expose the matrix APIs directly rather than putting them under a /api path. diff --git a/cmd/dendrite-federation-api-server/main.go b/cmd/dendrite-federation-api-server/main.go index af63b549a..3c42532c0 100644 --- a/cmd/dendrite-federation-api-server/main.go +++ b/cmd/dendrite-federation-api-server/main.go @@ -34,7 +34,7 @@ func main() { serverKeyAPI := base.CreateHTTPServerKeyAPIs() keyRing := serverKeyAPI.KeyRing() - fsAPI := base.CreateHTTPFederationSenderAPIs() + fsAPI := base.FederationSenderHTTPClient() rsAPI := base.CreateHTTPRoomserverAPIs() asAPI := base.CreateHTTPAppServiceAPIs() diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 41907cc0c..eedd4412e 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -73,7 +73,7 @@ func main() { serverKeyAPI := serverkeyapi.SetupServerKeyAPIComponent( base, federation, ) - if base.EnableHTTPAPIs { + if base.UseHTTPAPIs { serverKeyAPI = base.CreateHTTPServerKeyAPIs() } keyRing := serverKeyAPI.KeyRing() @@ -82,29 +82,29 @@ func main() { base, keyRing, federation, ) rsAPI := rsComponent - if base.EnableHTTPAPIs { + if base.UseHTTPAPIs { rsAPI = base.CreateHTTPRoomserverAPIs() } eduInputAPI := eduserver.SetupEDUServerComponent( base, cache.New(), deviceDB, ) - if base.EnableHTTPAPIs { + if base.UseHTTPAPIs { eduInputAPI = base.CreateHTTPEDUServerAPIs() } asAPI := appservice.SetupAppServiceAPIComponent( base, accountDB, deviceDB, federation, rsAPI, transactions.New(), ) - if base.EnableHTTPAPIs { + if base.UseHTTPAPIs { asAPI = base.CreateHTTPAppServiceAPIs() } fsAPI := federationsender.SetupFederationSenderComponent( base, federation, rsAPI, keyRing, ) - if base.EnableHTTPAPIs { - fsAPI = base.CreateHTTPFederationSenderAPIs() + if base.UseHTTPAPIs { + fsAPI = base.FederationSenderHTTPClient() } rsComponent.SetFederationSenderAPI(fsAPI) @@ -132,7 +132,7 @@ func main() { base.PublicAPIMux, base.InternalAPIMux, cfg, - base.EnableHTTPAPIs, + base.UseHTTPAPIs, ) // Expose the matrix APIs directly rather than putting them under a /api path. diff --git a/cmd/dendrite-public-rooms-api-server/main.go b/cmd/dendrite-public-rooms-api-server/main.go index d506e32d1..c2df1b3f8 100644 --- a/cmd/dendrite-public-rooms-api-server/main.go +++ b/cmd/dendrite-public-rooms-api-server/main.go @@ -28,7 +28,7 @@ func main() { deviceDB := base.CreateDeviceDB() - fsAPI := base.CreateHTTPFederationSenderAPIs() + fsAPI := base.FederationSenderHTTPClient() rsAPI := base.CreateHTTPRoomserverAPIs() rsAPI.SetFederationSenderAPI(fsAPI) diff --git a/cmd/dendrite-room-server/main.go b/cmd/dendrite-room-server/main.go index b86b74972..f4da560a6 100644 --- a/cmd/dendrite-room-server/main.go +++ b/cmd/dendrite-room-server/main.go @@ -28,7 +28,7 @@ func main() { serverKeyAPI := base.CreateHTTPServerKeyAPIs() keyRing := serverKeyAPI.KeyRing() - fsAPI := base.CreateHTTPFederationSenderAPIs() + fsAPI := base.FederationSenderHTTPClient() rsAPI := roomserver.SetupRoomServerComponent(base, keyRing, federation) rsAPI.SetFederationSenderAPI(fsAPI) diff --git a/cmd/dendritejs/main.go b/cmd/dendritejs/main.go index c1aef44d9..4c1b81979 100644 --- a/cmd/dendritejs/main.go +++ b/cmd/dendritejs/main.go @@ -235,7 +235,7 @@ func main() { base.PublicAPIMux, base.InternalAPIMux, cfg, - base.EnableHTTPAPIs, + base.UseHTTPAPIs, ) // Expose the matrix APIs via libp2p-js - for federation traffic diff --git a/federationsender/api/api.go b/federationsender/api/api.go index 4eb20cb67..d8251b54d 100644 --- a/federationsender/api/api.go +++ b/federationsender/api/api.go @@ -2,8 +2,9 @@ package api import ( "context" - "errors" - "net/http" + + "github.com/matrix-org/dendrite/federationsender/types" + "github.com/matrix-org/gomatrixserverlib" ) // FederationSenderInternalAPI is used to query information from the federation sender. @@ -50,16 +51,59 @@ type FederationSenderInternalAPI interface { ) error } -// NewFederationSenderInternalAPIHTTP creates a FederationSenderInternalAPI implemented by talking to a HTTP POST API. -// If httpClient is nil an error is returned -func NewFederationSenderInternalAPIHTTP(federationSenderURL string, httpClient *http.Client) (FederationSenderInternalAPI, error) { - if httpClient == nil { - return nil, errors.New("NewFederationSenderInternalAPIHTTP: httpClient is ") - } - return &httpFederationSenderInternalAPI{federationSenderURL, httpClient}, nil +type PerformDirectoryLookupRequest struct { + RoomAlias string `json:"room_alias"` + ServerName gomatrixserverlib.ServerName `json:"server_name"` } -type httpFederationSenderInternalAPI struct { - federationSenderURL string - httpClient *http.Client +type PerformDirectoryLookupResponse struct { + RoomID string `json:"room_id"` + ServerNames []gomatrixserverlib.ServerName `json:"server_names"` +} + +type PerformJoinRequest struct { + RoomID string `json:"room_id"` + UserID string `json:"user_id"` + // The sorted list of servers to try. Servers will be tried sequentially, after de-duplication. + ServerNames types.ServerNames `json:"server_names"` + Content map[string]interface{} `json:"content"` +} + +type PerformJoinResponse struct { +} + +type PerformLeaveRequest struct { + RoomID string `json:"room_id"` + UserID string `json:"user_id"` + ServerNames types.ServerNames `json:"server_names"` +} + +type PerformLeaveResponse struct { +} + +type PerformServersAliveRequest struct { + Servers []gomatrixserverlib.ServerName +} + +type PerformServersAliveResponse struct { +} + +// QueryJoinedHostsInRoomRequest is a request to QueryJoinedHostsInRoom +type QueryJoinedHostsInRoomRequest struct { + RoomID string `json:"room_id"` +} + +// QueryJoinedHostsInRoomResponse is a response to QueryJoinedHostsInRoom +type QueryJoinedHostsInRoomResponse struct { + JoinedHosts []types.JoinedHost `json:"joined_hosts"` +} + +// QueryJoinedHostServerNamesRequest is a request to QueryJoinedHostServerNames +type QueryJoinedHostServerNamesInRoomRequest struct { + RoomID string `json:"room_id"` +} + +// QueryJoinedHostServerNamesResponse is a response to QueryJoinedHostServerNames +type QueryJoinedHostServerNamesInRoomResponse struct { + ServerNames []gomatrixserverlib.ServerName `json:"server_names"` } diff --git a/federationsender/api/perform.go b/federationsender/api/perform.go deleted file mode 100644 index 5e4d7fe97..000000000 --- a/federationsender/api/perform.go +++ /dev/null @@ -1,112 +0,0 @@ -package api - -import ( - "context" - - "github.com/matrix-org/dendrite/federationsender/types" - internalHTTP "github.com/matrix-org/dendrite/internal/http" - "github.com/matrix-org/gomatrixserverlib" - "github.com/opentracing/opentracing-go" -) - -const ( - // FederationSenderPerformJoinRequestPath is the HTTP path for the PerformJoinRequest API. - FederationSenderPerformDirectoryLookupRequestPath = "/federationsender/performDirectoryLookup" - - // FederationSenderPerformJoinRequestPath is the HTTP path for the PerformJoinRequest API. - FederationSenderPerformJoinRequestPath = "/federationsender/performJoinRequest" - - // FederationSenderPerformLeaveRequestPath is the HTTP path for the PerformLeaveRequest API. - FederationSenderPerformLeaveRequestPath = "/federationsender/performLeaveRequest" - - // FederationSenderPerformServersAlivePath is the HTTP path for the PerformServersAlive API. - FederationSenderPerformServersAlivePath = "/federationsender/performServersAlive" -) - -type PerformDirectoryLookupRequest struct { - RoomAlias string `json:"room_alias"` - ServerName gomatrixserverlib.ServerName `json:"server_name"` -} - -type PerformDirectoryLookupResponse struct { - RoomID string `json:"room_id"` - ServerNames []gomatrixserverlib.ServerName `json:"server_names"` -} - -// Handle an instruction to make_join & send_join with a remote server. -func (h *httpFederationSenderInternalAPI) PerformDirectoryLookup( - ctx context.Context, - request *PerformDirectoryLookupRequest, - response *PerformDirectoryLookupResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "PerformDirectoryLookup") - defer span.Finish() - - apiURL := h.federationSenderURL + FederationSenderPerformDirectoryLookupRequestPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -type PerformJoinRequest struct { - RoomID string `json:"room_id"` - UserID string `json:"user_id"` - // The sorted list of servers to try. Servers will be tried sequentially, after de-duplication. - ServerNames types.ServerNames `json:"server_names"` - Content map[string]interface{} `json:"content"` -} - -type PerformJoinResponse struct { -} - -// Handle an instruction to make_join & send_join with a remote server. -func (h *httpFederationSenderInternalAPI) PerformJoin( - ctx context.Context, - request *PerformJoinRequest, - response *PerformJoinResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "PerformJoinRequest") - defer span.Finish() - - apiURL := h.federationSenderURL + FederationSenderPerformJoinRequestPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -type PerformLeaveRequest struct { - RoomID string `json:"room_id"` - UserID string `json:"user_id"` - ServerNames types.ServerNames `json:"server_names"` -} - -type PerformLeaveResponse struct { -} - -// Handle an instruction to make_leave & send_leave with a remote server. -func (h *httpFederationSenderInternalAPI) PerformLeave( - ctx context.Context, - request *PerformLeaveRequest, - response *PerformLeaveResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "PerformLeaveRequest") - defer span.Finish() - - apiURL := h.federationSenderURL + FederationSenderPerformLeaveRequestPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -type PerformServersAliveRequest struct { - Servers []gomatrixserverlib.ServerName -} - -type PerformServersAliveResponse struct { -} - -func (h *httpFederationSenderInternalAPI) PerformServersAlive( - ctx context.Context, - request *PerformServersAliveRequest, - response *PerformServersAliveResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "PerformServersAlive") - defer span.Finish() - - apiURL := h.federationSenderURL + FederationSenderPerformServersAlivePath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/federationsender/api/query.go b/federationsender/api/query.go deleted file mode 100644 index 4c0f757b2..000000000 --- a/federationsender/api/query.go +++ /dev/null @@ -1,63 +0,0 @@ -package api - -import ( - "context" - - "github.com/matrix-org/dendrite/federationsender/types" - internalHTTP "github.com/matrix-org/dendrite/internal/http" - "github.com/matrix-org/gomatrixserverlib" - - "github.com/opentracing/opentracing-go" -) - -// FederationSenderQueryJoinedHostsInRoomPath is the HTTP path for the QueryJoinedHostsInRoom API. -const FederationSenderQueryJoinedHostsInRoomPath = "/federationsender/queryJoinedHostsInRoom" - -// FederationSenderQueryJoinedHostServerNamesInRoomPath is the HTTP path for the QueryJoinedHostServerNamesInRoom API. -const FederationSenderQueryJoinedHostServerNamesInRoomPath = "/federationsender/queryJoinedHostServerNamesInRoom" - -// QueryJoinedHostsInRoomRequest is a request to QueryJoinedHostsInRoom -type QueryJoinedHostsInRoomRequest struct { - RoomID string `json:"room_id"` -} - -// QueryJoinedHostsInRoomResponse is a response to QueryJoinedHostsInRoom -type QueryJoinedHostsInRoomResponse struct { - JoinedHosts []types.JoinedHost `json:"joined_hosts"` -} - -// QueryJoinedHostsInRoom implements FederationSenderInternalAPI -func (h *httpFederationSenderInternalAPI) QueryJoinedHostsInRoom( - ctx context.Context, - request *QueryJoinedHostsInRoomRequest, - response *QueryJoinedHostsInRoomResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryJoinedHostsInRoom") - defer span.Finish() - - apiURL := h.federationSenderURL + FederationSenderQueryJoinedHostsInRoomPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} - -// QueryJoinedHostServerNamesRequest is a request to QueryJoinedHostServerNames -type QueryJoinedHostServerNamesInRoomRequest struct { - RoomID string `json:"room_id"` -} - -// QueryJoinedHostServerNamesResponse is a response to QueryJoinedHostServerNames -type QueryJoinedHostServerNamesInRoomResponse struct { - ServerNames []gomatrixserverlib.ServerName `json:"server_names"` -} - -// QueryJoinedHostServerNamesInRoom implements FederationSenderInternalAPI -func (h *httpFederationSenderInternalAPI) QueryJoinedHostServerNamesInRoom( - ctx context.Context, - request *QueryJoinedHostServerNamesInRoomRequest, - response *QueryJoinedHostServerNamesInRoomResponse, -) error { - span, ctx := opentracing.StartSpanFromContext(ctx, "QueryJoinedHostServerNamesInRoom") - defer span.Finish() - - apiURL := h.federationSenderURL + FederationSenderQueryJoinedHostServerNamesInRoomPath - return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) -} diff --git a/federationsender/federationsender.go b/federationsender/federationsender.go index 9e5cc8dd1..e25c27237 100644 --- a/federationsender/federationsender.go +++ b/federationsender/federationsender.go @@ -18,6 +18,7 @@ import ( "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/federationsender/consumers" "github.com/matrix-org/dendrite/federationsender/internal" + "github.com/matrix-org/dendrite/federationsender/inthttp" "github.com/matrix-org/dendrite/federationsender/producers" "github.com/matrix-org/dendrite/federationsender/queue" "github.com/matrix-org/dendrite/federationsender/storage" @@ -65,12 +66,8 @@ func SetupFederationSenderComponent( logrus.WithError(err).Panic("failed to start typing server consumer") } - queryAPI := internal.NewFederationSenderInternalAPI( - federationSenderDB, base.Cfg, roomserverProducer, federation, keyRing, - statistics, queues, - ) - - queryAPI.SetupHTTP(base.InternalAPIMux) + queryAPI := internal.NewFederationSenderInternalAPI(federationSenderDB, base.Cfg, roomserverProducer, federation, keyRing, statistics, queues) + inthttp.AddRoutes(queryAPI, base.InternalAPIMux) return queryAPI } diff --git a/federationsender/internal/api.go b/federationsender/internal/api.go index edf8fb4e9..c2ea0d411 100644 --- a/federationsender/internal/api.go +++ b/federationsender/internal/api.go @@ -1,19 +1,13 @@ package internal import ( - "encoding/json" - "net/http" - - "github.com/gorilla/mux" "github.com/matrix-org/dendrite/federationsender/api" "github.com/matrix-org/dendrite/federationsender/producers" "github.com/matrix-org/dendrite/federationsender/queue" "github.com/matrix-org/dendrite/federationsender/storage" "github.com/matrix-org/dendrite/federationsender/types" - "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/config" "github.com/matrix-org/gomatrixserverlib" - "github.com/matrix-org/util" ) // FederationSenderInternalAPI is an implementation of api.FederationSenderInternalAPI @@ -46,87 +40,3 @@ func NewFederationSenderInternalAPI( queues: queues, } } - -// SetupHTTP adds the FederationSenderInternalAPI handlers to the http.ServeMux. -func (f *FederationSenderInternalAPI) SetupHTTP(internalAPIMux *mux.Router) { - internalAPIMux.Handle( - api.FederationSenderQueryJoinedHostsInRoomPath, - internal.MakeInternalAPI("QueryJoinedHostsInRoom", func(req *http.Request) util.JSONResponse { - var request api.QueryJoinedHostsInRoomRequest - var response api.QueryJoinedHostsInRoomResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := f.QueryJoinedHostsInRoom(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle( - api.FederationSenderQueryJoinedHostServerNamesInRoomPath, - internal.MakeInternalAPI("QueryJoinedHostServerNamesInRoom", func(req *http.Request) util.JSONResponse { - var request api.QueryJoinedHostServerNamesInRoomRequest - var response api.QueryJoinedHostServerNamesInRoomResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.ErrorResponse(err) - } - if err := f.QueryJoinedHostServerNamesInRoom(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.FederationSenderPerformJoinRequestPath, - internal.MakeInternalAPI("PerformJoinRequest", func(req *http.Request) util.JSONResponse { - var request api.PerformJoinRequest - var response api.PerformJoinResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := f.PerformJoin(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.FederationSenderPerformLeaveRequestPath, - internal.MakeInternalAPI("PerformLeaveRequest", func(req *http.Request) util.JSONResponse { - var request api.PerformLeaveRequest - var response api.PerformLeaveResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := f.PerformLeave(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.FederationSenderPerformDirectoryLookupRequestPath, - internal.MakeInternalAPI("PerformDirectoryLookupRequest", func(req *http.Request) util.JSONResponse { - var request api.PerformDirectoryLookupRequest - var response api.PerformDirectoryLookupResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := f.PerformDirectoryLookup(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) - internalAPIMux.Handle(api.FederationSenderPerformServersAlivePath, - internal.MakeInternalAPI("PerformServersAliveRequest", func(req *http.Request) util.JSONResponse { - var request api.PerformServersAliveRequest - var response api.PerformServersAliveResponse - if err := json.NewDecoder(req.Body).Decode(&request); err != nil { - return util.MessageResponse(http.StatusBadRequest, err.Error()) - } - if err := f.PerformServersAlive(req.Context(), &request, &response); err != nil { - return util.ErrorResponse(err) - } - return util.JSONResponse{Code: http.StatusOK, JSON: &response} - }), - ) -} diff --git a/federationsender/inthttp/client.go b/federationsender/inthttp/client.go new file mode 100644 index 000000000..8b3a106d8 --- /dev/null +++ b/federationsender/inthttp/client.go @@ -0,0 +1,113 @@ +package inthttp + +import ( + "context" + "errors" + "net/http" + + "github.com/matrix-org/dendrite/federationsender/api" + internalHTTP "github.com/matrix-org/dendrite/internal/http" + "github.com/opentracing/opentracing-go" +) + +// HTTP paths for the internal HTTP API +const ( + FederationSenderQueryJoinedHostsInRoomPath = "/federationsender/queryJoinedHostsInRoom" + FederationSenderQueryJoinedHostServerNamesInRoomPath = "/federationsender/queryJoinedHostServerNamesInRoom" + + FederationSenderPerformDirectoryLookupRequestPath = "/federationsender/performDirectoryLookup" + FederationSenderPerformJoinRequestPath = "/federationsender/performJoinRequest" + FederationSenderPerformLeaveRequestPath = "/federationsender/performLeaveRequest" + FederationSenderPerformServersAlivePath = "/federationsender/performServersAlive" +) + +// NewFederationSenderClient creates a FederationSenderInternalAPI implemented by talking to a HTTP POST API. +// If httpClient is nil an error is returned +func NewFederationSenderClient(federationSenderURL string, httpClient *http.Client) (api.FederationSenderInternalAPI, error) { + if httpClient == nil { + return nil, errors.New("NewFederationSenderInternalAPIHTTP: httpClient is ") + } + return &httpFederationSenderInternalAPI{federationSenderURL, httpClient}, nil +} + +type httpFederationSenderInternalAPI struct { + federationSenderURL string + httpClient *http.Client +} + +// Handle an instruction to make_leave & send_leave with a remote server. +func (h *httpFederationSenderInternalAPI) PerformLeave( + ctx context.Context, + request *api.PerformLeaveRequest, + response *api.PerformLeaveResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "PerformLeaveRequest") + defer span.Finish() + + apiURL := h.federationSenderURL + FederationSenderPerformLeaveRequestPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +func (h *httpFederationSenderInternalAPI) PerformServersAlive( + ctx context.Context, + request *api.PerformServersAliveRequest, + response *api.PerformServersAliveResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "PerformServersAlive") + defer span.Finish() + + apiURL := h.federationSenderURL + FederationSenderPerformServersAlivePath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryJoinedHostServerNamesInRoom implements FederationSenderInternalAPI +func (h *httpFederationSenderInternalAPI) QueryJoinedHostServerNamesInRoom( + ctx context.Context, + request *api.QueryJoinedHostServerNamesInRoomRequest, + response *api.QueryJoinedHostServerNamesInRoomResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryJoinedHostServerNamesInRoom") + defer span.Finish() + + apiURL := h.federationSenderURL + FederationSenderQueryJoinedHostServerNamesInRoomPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// QueryJoinedHostsInRoom implements FederationSenderInternalAPI +func (h *httpFederationSenderInternalAPI) QueryJoinedHostsInRoom( + ctx context.Context, + request *api.QueryJoinedHostsInRoomRequest, + response *api.QueryJoinedHostsInRoomResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "QueryJoinedHostsInRoom") + defer span.Finish() + + apiURL := h.federationSenderURL + FederationSenderQueryJoinedHostsInRoomPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// Handle an instruction to make_join & send_join with a remote server. +func (h *httpFederationSenderInternalAPI) PerformJoin( + ctx context.Context, + request *api.PerformJoinRequest, + response *api.PerformJoinResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "PerformJoinRequest") + defer span.Finish() + + apiURL := h.federationSenderURL + FederationSenderPerformJoinRequestPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} + +// Handle an instruction to make_join & send_join with a remote server. +func (h *httpFederationSenderInternalAPI) PerformDirectoryLookup( + ctx context.Context, + request *api.PerformDirectoryLookupRequest, + response *api.PerformDirectoryLookupResponse, +) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "PerformDirectoryLookup") + defer span.Finish() + + apiURL := h.federationSenderURL + FederationSenderPerformDirectoryLookupRequestPath + return internalHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response) +} diff --git a/federationsender/inthttp/server.go b/federationsender/inthttp/server.go new file mode 100644 index 000000000..c94a14295 --- /dev/null +++ b/federationsender/inthttp/server.go @@ -0,0 +1,95 @@ +package inthttp + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" + "github.com/matrix-org/dendrite/federationsender/api" + "github.com/matrix-org/dendrite/internal" + "github.com/matrix-org/util" +) + +// AddRoutes adds the FederationSenderInternalAPI handlers to the http.ServeMux. +func AddRoutes(intAPI api.FederationSenderInternalAPI, internalAPIMux *mux.Router) { + internalAPIMux.Handle( + FederationSenderQueryJoinedHostsInRoomPath, + internal.MakeInternalAPI("QueryJoinedHostsInRoom", func(req *http.Request) util.JSONResponse { + var request api.QueryJoinedHostsInRoomRequest + var response api.QueryJoinedHostsInRoomResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := intAPI.QueryJoinedHostsInRoom(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle( + FederationSenderQueryJoinedHostServerNamesInRoomPath, + internal.MakeInternalAPI("QueryJoinedHostServerNamesInRoom", func(req *http.Request) util.JSONResponse { + var request api.QueryJoinedHostServerNamesInRoomRequest + var response api.QueryJoinedHostServerNamesInRoomResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := intAPI.QueryJoinedHostServerNamesInRoom(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(FederationSenderPerformJoinRequestPath, + internal.MakeInternalAPI("PerformJoinRequest", func(req *http.Request) util.JSONResponse { + var request api.PerformJoinRequest + var response api.PerformJoinResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := intAPI.PerformJoin(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(FederationSenderPerformLeaveRequestPath, + internal.MakeInternalAPI("PerformLeaveRequest", func(req *http.Request) util.JSONResponse { + var request api.PerformLeaveRequest + var response api.PerformLeaveResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := intAPI.PerformLeave(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(FederationSenderPerformDirectoryLookupRequestPath, + internal.MakeInternalAPI("PerformDirectoryLookupRequest", func(req *http.Request) util.JSONResponse { + var request api.PerformDirectoryLookupRequest + var response api.PerformDirectoryLookupResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := intAPI.PerformDirectoryLookup(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) + internalAPIMux.Handle(FederationSenderPerformServersAlivePath, + internal.MakeInternalAPI("PerformServersAliveRequest", func(req *http.Request) util.JSONResponse { + var request api.PerformServersAliveRequest + var response api.PerformServersAliveResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.MessageResponse(http.StatusBadRequest, err.Error()) + } + if err := intAPI.PerformServersAlive(req.Context(), &request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: http.StatusOK, JSON: &response} + }), + ) +} diff --git a/internal/basecomponent/base.go b/internal/basecomponent/base.go index 42d236f85..a1a908760 100644 --- a/internal/basecomponent/base.go +++ b/internal/basecomponent/base.go @@ -38,6 +38,7 @@ import ( appserviceAPI "github.com/matrix-org/dendrite/appservice/api" eduServerAPI "github.com/matrix-org/dendrite/eduserver/api" federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" + "github.com/matrix-org/dendrite/federationsender/inthttp" "github.com/matrix-org/dendrite/internal/config" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" serverKeyAPI "github.com/matrix-org/dendrite/serverkeyapi/api" @@ -58,7 +59,7 @@ type BaseDendrite struct { // PublicAPIMux should be used to register new public matrix api endpoints PublicAPIMux *mux.Router InternalAPIMux *mux.Router - EnableHTTPAPIs bool + UseHTTPAPIs bool httpClient *http.Client Cfg *config.Dendrite ImmutableCache caching.ImmutableCache @@ -72,7 +73,7 @@ const HTTPClientTimeout = time.Second * 30 // NewBaseDendrite creates a new instance to be used by a component. // The componentName is used for logging purposes, and should be a friendly name // of the compontent running, e.g. "SyncAPI" -func NewBaseDendrite(cfg *config.Dendrite, componentName string, enableHTTPAPIs bool) *BaseDendrite { +func NewBaseDendrite(cfg *config.Dendrite, componentName string, useHTTPAPIs bool) *BaseDendrite { internal.SetupStdLogging() internal.SetupHookLogging(cfg.Logging, componentName) internal.SetupPprof() @@ -118,7 +119,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, enableHTTPAPIs return &BaseDendrite{ componentName: componentName, - EnableHTTPAPIs: enableHTTPAPIs, + UseHTTPAPIs: useHTTPAPIs, tracerCloser: closer, Cfg: cfg, ImmutableCache: cache, @@ -165,10 +166,10 @@ func (b *BaseDendrite) CreateHTTPEDUServerAPIs() eduServerAPI.EDUServerInputAPI return e } -// CreateHTTPFederationSenderAPIs returns FederationSenderInternalAPI for hitting +// FederationSenderHTTPClient returns FederationSenderInternalAPI for hitting // the federation sender over HTTP -func (b *BaseDendrite) CreateHTTPFederationSenderAPIs() federationSenderAPI.FederationSenderInternalAPI { - f, err := federationSenderAPI.NewFederationSenderInternalAPIHTTP(b.Cfg.FederationSenderURL(), b.httpClient) +func (b *BaseDendrite) FederationSenderHTTPClient() federationSenderAPI.FederationSenderInternalAPI { + f, err := inthttp.NewFederationSenderClient(b.Cfg.FederationSenderURL(), b.httpClient) if err != nil { logrus.WithError(err).Panic("NewFederationSenderInternalAPIHTTP failed", b.httpClient) } @@ -241,7 +242,7 @@ func (b *BaseDendrite) SetupAndServeHTTP(bindaddr string, listenaddr string) { b.PublicAPIMux, b.InternalAPIMux, b.Cfg, - b.EnableHTTPAPIs, + b.UseHTTPAPIs, ) logrus.Infof("Starting %s server on %s", b.componentName, serv.Addr)