1# Copyright 2021 The Matrix.org Foundation C.I.C. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import logging 15from http import HTTPStatus 16from typing import TYPE_CHECKING, Tuple 17 18from synapse.api.errors import Codes, NotFoundError, SynapseError 19from synapse.http.servlet import RestServlet, parse_integer, parse_string 20from synapse.http.site import SynapseRequest 21from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin 22from synapse.storage.databases.main.transactions import DestinationSortOrder 23from synapse.types import JsonDict 24 25if TYPE_CHECKING: 26 from synapse.server import HomeServer 27 28logger = logging.getLogger(__name__) 29 30 31class ListDestinationsRestServlet(RestServlet): 32 """Get request to list all destinations. 33 This needs user to have administrator access in Synapse. 34 35 GET /_synapse/admin/v1/federation/destinations?from=0&limit=10 36 37 returns: 38 200 OK with list of destinations if success otherwise an error. 39 40 The parameters `from` and `limit` are required only for pagination. 41 By default, a `limit` of 100 is used. 42 The parameter `destination` can be used to filter by destination. 43 The parameter `order_by` can be used to order the result. 44 """ 45 46 PATTERNS = admin_patterns("/federation/destinations$") 47 48 def __init__(self, hs: "HomeServer"): 49 self._auth = hs.get_auth() 50 self._store = hs.get_datastore() 51 52 async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]: 53 await assert_requester_is_admin(self._auth, request) 54 55 start = parse_integer(request, "from", default=0) 56 limit = parse_integer(request, "limit", default=100) 57 58 if start < 0: 59 raise SynapseError( 60 HTTPStatus.BAD_REQUEST, 61 "Query parameter from must be a string representing a positive integer.", 62 errcode=Codes.INVALID_PARAM, 63 ) 64 65 if limit < 0: 66 raise SynapseError( 67 HTTPStatus.BAD_REQUEST, 68 "Query parameter limit must be a string representing a positive integer.", 69 errcode=Codes.INVALID_PARAM, 70 ) 71 72 destination = parse_string(request, "destination") 73 74 order_by = parse_string( 75 request, 76 "order_by", 77 default=DestinationSortOrder.DESTINATION.value, 78 allowed_values=[dest.value for dest in DestinationSortOrder], 79 ) 80 81 direction = parse_string(request, "dir", default="f", allowed_values=("f", "b")) 82 83 destinations, total = await self._store.get_destinations_paginate( 84 start, limit, destination, order_by, direction 85 ) 86 response = {"destinations": destinations, "total": total} 87 if (start + limit) < total: 88 response["next_token"] = str(start + len(destinations)) 89 90 return HTTPStatus.OK, response 91 92 93class DestinationsRestServlet(RestServlet): 94 """Get details of a destination. 95 This needs user to have administrator access in Synapse. 96 97 GET /_synapse/admin/v1/federation/destinations/<destination> 98 99 returns: 100 200 OK with details of a destination if success otherwise an error. 101 """ 102 103 PATTERNS = admin_patterns("/federation/destinations/(?P<destination>[^/]*)$") 104 105 def __init__(self, hs: "HomeServer"): 106 self._auth = hs.get_auth() 107 self._store = hs.get_datastore() 108 109 async def on_GET( 110 self, request: SynapseRequest, destination: str 111 ) -> Tuple[int, JsonDict]: 112 await assert_requester_is_admin(self._auth, request) 113 114 destination_retry_timings = await self._store.get_destination_retry_timings( 115 destination 116 ) 117 118 if not destination_retry_timings: 119 raise NotFoundError("Unknown destination") 120 121 last_successful_stream_ordering = ( 122 await self._store.get_destination_last_successful_stream_ordering( 123 destination 124 ) 125 ) 126 127 response = { 128 "destination": destination, 129 "failure_ts": destination_retry_timings.failure_ts, 130 "retry_last_ts": destination_retry_timings.retry_last_ts, 131 "retry_interval": destination_retry_timings.retry_interval, 132 "last_successful_stream_ordering": last_successful_stream_ordering, 133 } 134 135 return HTTPStatus.OK, response 136