federation

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: MPL-2.0 Imports: 22 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidServerName = errors.New("invalid server name")
)

Functions

func ParseServerName added in v0.20.0

func ParseServerName(serverName string) (host string, port uint16, ok bool)

ParseServerName parses the port and hostname from a Matrix server name and validates that it matches the grammar specified in https://spec.matrix.org/v1.11/appendices/#server-name

func RequestSRV added in v0.20.0

func RequestSRV(ctx context.Context, cli *net.Resolver, hostname string) ([]*net.SRV, error)

RequestSRV resolves the `_matrix-fed._tcp` SRV record for the given hostname. If the new matrix-fed record is not found, it falls back to the old `_matrix._tcp` record.

Types

type Client added in v0.20.0

type Client struct {
	HTTP       *http.Client
	ServerName string
	UserAgent  string
	Key        *SigningKey
}

func NewClient added in v0.20.0

func NewClient(serverName string, key *SigningKey) *Client

func (*Client) Backfill added in v0.20.0

func (c *Client) Backfill(ctx context.Context, req *ReqBackfill) (resp *RespBackfill, err error)

func (*Client) GetEvent added in v0.20.0

func (c *Client) GetEvent(ctx context.Context, serverName string, eventID id.EventID) (resp *RespBackfill, err error)

func (*Client) GetEventAuthChain added in v0.20.0

func (c *Client) GetEventAuthChain(ctx context.Context, serverName string, roomID id.RoomID, eventID id.EventID) (resp *RespGetEventAuthChain, err error)

func (*Client) GetMissingEvents added in v0.20.0

func (c *Client) GetMissingEvents(ctx context.Context, req *ReqGetMissingEvents) (resp *RespGetMissingEvents, err error)

func (*Client) GetOpenIDUserInfo added in v0.20.0

func (c *Client) GetOpenIDUserInfo(ctx context.Context, serverName, accessToken string) (resp *RespOpenIDUserInfo, err error)

func (*Client) GetState added in v0.20.0

func (c *Client) GetState(ctx context.Context, serverName string, roomID id.RoomID, eventID id.EventID) (resp *RespGetState, err error)

func (*Client) GetStateIDs added in v0.20.0

func (c *Client) GetStateIDs(ctx context.Context, serverName string, roomID id.RoomID, eventID id.EventID) (resp *RespGetStateIDs, err error)

func (*Client) MakeFullRequest added in v0.20.0

func (c *Client) MakeFullRequest(ctx context.Context, params RequestParams) ([]byte, *http.Response, error)

func (*Client) MakeRequest added in v0.20.0

func (c *Client) MakeRequest(ctx context.Context, serverName string, authenticate bool, method string, path mautrix.PrefixableURLPath, reqJSON, respJSON any) error

func (*Client) Query added in v0.20.0

func (c *Client) Query(ctx context.Context, serverName, queryType string, queryParams url.Values, respStruct any) (err error)

func (*Client) QueryDirectory added in v0.20.0

func (c *Client) QueryDirectory(ctx context.Context, serverName string, roomAlias id.RoomAlias) (resp *mautrix.RespAliasResolve, err error)

func (*Client) QueryKeys added in v0.20.0

func (c *Client) QueryKeys(ctx context.Context, serverName string, req *ReqQueryKeys) (resp *ServerKeyResponse, err error)

func (*Client) QueryProfile added in v0.20.0

func (c *Client) QueryProfile(ctx context.Context, serverName string, userID id.UserID) (resp *mautrix.RespUserProfile, err error)

func (*Client) SendTransaction added in v0.20.0

func (c *Client) SendTransaction(ctx context.Context, req *ReqSendTransaction) (resp *RespSendTransaction, err error)

func (*Client) ServerKeys added in v0.20.0

func (c *Client) ServerKeys(ctx context.Context, serverName string) (resp *ServerKeyResponse, err error)

func (*Client) TimestampToEvent added in v0.20.0

func (c *Client) TimestampToEvent(ctx context.Context, serverName string, roomID id.RoomID, timestamp time.Time, dir mautrix.Direction) (resp *mautrix.RespTimestampToEvent, err error)

func (*Client) Version added in v0.20.0

func (c *Client) Version(ctx context.Context, serverName string) (resp *RespServerVersion, err error)

type EDU added in v0.20.0

type EDU = json.RawMessage

type GetQueryKeysResponse

type GetQueryKeysResponse struct {
	ServerKeys []*ServerKeyResponse `json:"server_keys"`
}

GetQueryKeysResponse is the response body for the `GET /_matrix/key/v2/query/{serverName}` endpoint

type KeyServer

type KeyServer struct {
	KeyProvider     ServerKeyProvider
	Version         ServerVersion
	WellKnownTarget string
}

KeyServer implements a basic Matrix key server that can serve its own keys, plus the federation version endpoint.

It does not implement querying keys of other servers, nor any other federation endpoints.

func (*KeyServer) GetQueryKeys

func (ks *KeyServer) GetQueryKeys(w http.ResponseWriter, r *http.Request)

GetQueryKeys implements the `GET /_matrix/key/v2/query/{serverName}` endpoint

https://spec.matrix.org/v1.9/server-server-api/#get_matrixkeyv2queryservername

func (*KeyServer) GetServerKey

func (ks *KeyServer) GetServerKey(w http.ResponseWriter, r *http.Request)

GetServerKey implements the `GET /_matrix/key/v2/server` endpoint.

https://spec.matrix.org/v1.9/server-server-api/#get_matrixkeyv2server

func (*KeyServer) GetServerVersion

func (ks *KeyServer) GetServerVersion(w http.ResponseWriter, r *http.Request)

GetServerVersion implements the `GET /_matrix/federation/v1/version` endpoint

https://spec.matrix.org/v1.9/server-server-api/#get_matrixfederationv1version

func (*KeyServer) GetWellKnown

func (ks *KeyServer) GetWellKnown(w http.ResponseWriter, r *http.Request)

GetWellKnown implements the `GET /.well-known/matrix/server` endpoint

https://spec.matrix.org/v1.9/server-server-api/#get_well-knownmatrixserver

func (*KeyServer) PostQueryKeys

func (ks *KeyServer) PostQueryKeys(w http.ResponseWriter, r *http.Request)

PostQueryKeys implements the `POST /_matrix/key/v2/query` endpoint

https://spec.matrix.org/v1.9/server-server-api/#post_matrixkeyv2query

func (*KeyServer) Register

func (ks *KeyServer) Register(r *mux.Router)

Register registers the key server endpoints to the given router.

type KeyURLPath added in v0.20.0

type KeyURLPath []any

func (KeyURLPath) FullPath added in v0.20.0

func (fkup KeyURLPath) FullPath() []any

type OldVerifyKey

type OldVerifyKey struct {
	Key       id.SigningKey      `json:"key"`
	ExpiredTS jsontime.UnixMilli `json:"expired_ts"`
}

type PDU added in v0.20.0

type PDU = json.RawMessage

type PDUProcessingResult added in v0.20.0

type PDUProcessingResult struct {
	Error string `json:"error,omitempty"`
}

type PostQueryKeysResponse

type PostQueryKeysResponse struct {
	ServerKeys map[string]*ServerKeyResponse `json:"server_keys"`
}

PostQueryKeysResponse is the response body for the `POST /_matrix/key/v2/query` endpoint

type QueryKeysCriteria

type QueryKeysCriteria struct {
	MinimumValidUntilTS jsontime.UnixMilli `json:"minimum_valid_until_ts"`
}

type ReqBackfill added in v0.20.0

type ReqBackfill struct {
	ServerName   string
	RoomID       id.RoomID
	Limit        int
	BackfillFrom []id.EventID
}

type ReqGetMissingEvents added in v0.20.0

type ReqGetMissingEvents struct {
	ServerName     string       `json:"-"`
	RoomID         id.RoomID    `json:"-"`
	EarliestEvents []id.EventID `json:"earliest_events"`
	LatestEvents   []id.EventID `json:"latest_events"`
	Limit          int          `json:"limit,omitempty"`
	MinDepth       int          `json:"min_depth,omitempty"`
}

type ReqQueryKeys

type ReqQueryKeys struct {
	ServerKeys map[string]map[id.KeyID]QueryKeysCriteria `json:"server_keys"`
}

ReqQueryKeys is the request body for the `POST /_matrix/key/v2/query` endpoint

type ReqSendTransaction added in v0.20.0

type ReqSendTransaction struct {
	Destination string `json:"destination"`
	TxnID       string `json:"-"`

	Origin         string             `json:"origin"`
	OriginServerTS jsontime.UnixMilli `json:"origin_server_ts"`
	PDUs           []PDU              `json:"pdus"`
	EDUs           []EDU              `json:"edus,omitempty"`
}

type RequestParams added in v0.20.0

type RequestParams struct {
	ServerName   string
	Method       string
	Path         mautrix.PrefixableURLPath
	Query        url.Values
	Authenticate bool
	RequestJSON  any

	ResponseJSON any
	DontReadBody bool
}

type ResolveServerNameOpts added in v0.20.0

type ResolveServerNameOpts struct {
	HTTPClient *http.Client
	DNSClient  *net.Resolver
}

type ResolvedServerName added in v0.20.0

type ResolvedServerName struct {
	ServerName string    `json:"server_name"`
	HostHeader string    `json:"host_header"`
	IPPort     []string  `json:"ip_port"`
	Expires    time.Time `json:"expires"`
}

func ResolveServerName added in v0.20.0

func ResolveServerName(ctx context.Context, serverName string, opts ...*ResolveServerNameOpts) (*ResolvedServerName, error)

ResolveServerName implements the full server discovery algorithm as specified in https://spec.matrix.org/v1.11/server-server-api/#resolving-server-names

type RespBackfill added in v0.20.0

type RespBackfill struct {
	Origin         string             `json:"origin"`
	OriginServerTS jsontime.UnixMilli `json:"origin_server_ts"`
	PDUs           []PDU              `json:"pdus"`
}

type RespGetEventAuthChain added in v0.20.0

type RespGetEventAuthChain struct {
	AuthChain []PDU `json:"auth_chain"`
}

type RespGetMissingEvents added in v0.20.0

type RespGetMissingEvents struct {
	Events []PDU `json:"events"`
}

type RespGetState added in v0.20.0

type RespGetState struct {
	AuthChain []PDU `json:"auth_chain"`
	PDUs      []PDU `json:"pdus"`
}

type RespGetStateIDs added in v0.20.0

type RespGetStateIDs struct {
	AuthChain []id.EventID `json:"auth_chain_ids"`
	PDUs      []id.EventID `json:"pdu_ids"`
}

type RespOpenIDUserInfo added in v0.20.0

type RespOpenIDUserInfo struct {
	Sub id.UserID `json:"sub"`
}

type RespSendTransaction added in v0.20.0

type RespSendTransaction struct {
	PDUs map[id.EventID]PDUProcessingResult `json:"pdus"`
}

type RespServerVersion

type RespServerVersion struct {
	Server ServerVersion `json:"server"`
}

RespServerVersion is the response body for the `GET /_matrix/federation/v1/version` endpoint

type RespWellKnown

type RespWellKnown struct {
	Server string `json:"m.server"`
}

RespWellKnown is the response body for the `GET /.well-known/matrix/server` endpoint.

func RequestWellKnown added in v0.20.0

func RequestWellKnown(ctx context.Context, cli *http.Client, hostname string) (*RespWellKnown, time.Time, error)

RequestWellKnown sends a request to the well-known endpoint of a server and returns the response, plus the time when the cache should expire.

type ServerKeyProvider

type ServerKeyProvider interface {
	Get(r *http.Request) (serverName string, key *SigningKey)
}

ServerKeyProvider is an interface that returns private server keys for server key requests.

type ServerKeyResponse

type ServerKeyResponse struct {
	ServerName    string                         `json:"server_name"`
	VerifyKeys    map[id.KeyID]ServerVerifyKey   `json:"verify_keys"`
	OldVerifyKeys map[id.KeyID]OldVerifyKey      `json:"old_verify_keys,omitempty"`
	Signatures    map[string]map[id.KeyID]string `json:"signatures,omitempty"`
	ValidUntilTS  jsontime.UnixMilli             `json:"valid_until_ts"`
}

ServerKeyResponse is the response body for the `GET /_matrix/key/v2/server` endpoint. It's also used inside the query endpoint response structs.

type ServerResolvingTransport added in v0.20.0

type ServerResolvingTransport struct {
	ResolveOpts *ResolveServerNameOpts
	Transport   *http.Transport
	Dialer      *net.Dialer
	// contains filtered or unexported fields
}

ServerResolvingTransport is an http.RoundTripper that resolves Matrix server names before sending requests. It only allows requests using the "matrix-federation" scheme.

func NewServerResolvingTransport added in v0.20.0

func NewServerResolvingTransport() *ServerResolvingTransport

func (*ServerResolvingTransport) DialContext added in v0.20.0

func (srt *ServerResolvingTransport) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

func (*ServerResolvingTransport) RoundTrip added in v0.20.0

func (srt *ServerResolvingTransport) RoundTrip(request *http.Request) (*http.Response, error)

type ServerVerifyKey

type ServerVerifyKey struct {
	Key id.SigningKey `json:"key"`
}

func (*ServerVerifyKey) Decode added in v0.20.0

func (svk *ServerVerifyKey) Decode() (ed25519.PublicKey, error)

type ServerVersion

type ServerVersion struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type SigningKey

type SigningKey struct {
	ID   id.KeyID
	Pub  id.SigningKey
	Priv ed25519.PrivateKey
}

SigningKey is a Matrix federation signing key pair.

func GenerateSigningKey

func GenerateSigningKey() *SigningKey

GenerateSigningKey generates a new random signing key.

func ParseSynapseKey

func ParseSynapseKey(key string) (*SigningKey, error)

ParseSynapseKey parses a Synapse-compatible private key string into a SigningKey.

func (*SigningKey) GenerateKeyResponse

func (sk *SigningKey) GenerateKeyResponse(serverName string, oldVerifyKeys map[id.KeyID]OldVerifyKey) *ServerKeyResponse

GenerateKeyResponse generates a key response signed by this key with the given server name and optionally some old verify keys.

func (*SigningKey) SignJSON

func (sk *SigningKey) SignJSON(data any) ([]byte, error)

func (*SigningKey) SignRawJSON

func (sk *SigningKey) SignRawJSON(data json.RawMessage) []byte

func (*SigningKey) SynapseString

func (sk *SigningKey) SynapseString() string

SynapseString returns a string representation of the private key compatible with Synapse's .signing.key file format.

The output of this function can be parsed back into a SigningKey using the ParseSynapseKey function.

type StaticServerKey

type StaticServerKey struct {
	ServerName string
	Key        *SigningKey
}

StaticServerKey is an implementation of ServerKeyProvider that always returns the same server name and key.

func (*StaticServerKey) Get

func (ssk *StaticServerKey) Get(r *http.Request) (serverName string, key *SigningKey)

type URLPath added in v0.20.0

type URLPath []any

func (URLPath) FullPath added in v0.20.0

func (fup URLPath) FullPath() []any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL