Allow self-hosted port to be selected

This commit is contained in:
Kegan Dougal 2023-05-04 16:55:34 +01:00
parent 6393b06a27
commit 328d3aaffd
4 changed files with 25 additions and 13 deletions

View file

@ -28,6 +28,8 @@ var (
flagMatrixKey string flagMatrixKey string
flagOrigin string flagOrigin string
flagPort int
) )
func init() { func init() {
@ -45,17 +47,20 @@ func init() {
flag.StringVar(&flagOrigin, "O", "", "The server name that the request should originate from. The remote server will use this to request server keys. There MUST be a TLS listener at the .well-known address for this server name, i.e it needs to be pointing to a real homeserver. If blank, furl will self-host this on a random high numbered port, but only if the target is localhost. Use $PORT in request URLs/bodies to substitute the port number in.") flag.StringVar(&flagOrigin, "O", "", "The server name that the request should originate from. The remote server will use this to request server keys. There MUST be a TLS listener at the .well-known address for this server name, i.e it needs to be pointing to a real homeserver. If blank, furl will self-host this on a random high numbered port, but only if the target is localhost. Use $PORT in request URLs/bodies to substitute the port number in.")
flag.StringVar(&flagOrigin, "origin", "", "The server name that the request should originate from. The remote server will use this to request server keys. There MUST be a TLS listener at the .well-known address for this server name, i.e it needs to be pointing to a real homeserver. If blank, furl will self-host this on a random high numbered port, but only if the target is localhost. Use $PORT in request URLs/bodies to substitute the port number in.") flag.StringVar(&flagOrigin, "origin", "", "The server name that the request should originate from. The remote server will use this to request server keys. There MUST be a TLS listener at the .well-known address for this server name, i.e it needs to be pointing to a real homeserver. If blank, furl will self-host this on a random high numbered port, but only if the target is localhost. Use $PORT in request URLs/bodies to substitute the port number in.")
flag.IntVar(&flagPort, "p", 0, "Port to self-host on. If set, always self-hosts. Required because sometimes requests need the same origin.")
} }
type args struct { type args struct {
SkipVerify bool SkipVerify bool
Method string Method string
Data []byte Data []byte
MatrixKey ed25519.PrivateKey MatrixKey ed25519.PrivateKey
MatrixKeyID gomatrixserverlib.KeyID MatrixKeyID gomatrixserverlib.KeyID
Origin spec.ServerName Origin spec.ServerName
SelfHostKey bool SelfHostKey bool
TargetURL *url.URL SelfHostPort int
TargetURL *url.URL
} }
func processArgs() (*args, error) { func processArgs() (*args, error) {
@ -91,8 +96,9 @@ func processArgs() (*args, error) {
a.SkipVerify = flagSkipVerify a.SkipVerify = flagSkipVerify
a.Method = strings.ToUpper(flagMethod) a.Method = strings.ToUpper(flagMethod)
a.Origin = spec.ServerName(flagOrigin) a.Origin = spec.ServerName(flagOrigin)
a.SelfHostPort = flagPort
a.TargetURL = targetURL a.TargetURL = targetURL
a.SelfHostKey = a.Origin == "" && a.TargetURL.Hostname() == "localhost" a.SelfHostKey = a.SelfHostPort != 0 || (a.Origin == "" && a.TargetURL.Hostname() == "localhost")
// load data // load data
isFile := strings.HasPrefix(flagData, "@") isFile := strings.HasPrefix(flagData, "@")
@ -119,7 +125,7 @@ func main() {
if a.SelfHostKey { if a.SelfHostKey {
fmt.Printf("Self-hosting key...") fmt.Printf("Self-hosting key...")
apiURL, cancel := test.ListenAndServe(tt{}, http.DefaultServeMux, true) apiURL, cancel := test.ListenAndServe(tt{}, http.DefaultServeMux, true, a.SelfHostPort)
defer cancel() defer cancel()
parsedURL, _ := url.Parse(apiURL) parsedURL, _ := url.Parse(apiURL)
a.Origin = spec.ServerName(parsedURL.Host) a.Origin = spec.ServerName(parsedURL.Host)
@ -167,6 +173,11 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if a.Data != nil {
data := string(a.Data)
data = strings.ReplaceAll(data, "$PORT", port)
a.Data = []byte(data)
}
} }
client := fclient.NewFederationClient( client := fclient.NewFederationClient(

View file

@ -314,7 +314,7 @@ func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
// TODO: This is pretty fragile, as if anything calls anything on these nils this test will break. // TODO: This is pretty fragile, as if anything calls anything on these nils this test will break.
// Unfortunately, it makes little sense to instantiate these dependencies when we just want to test routing. // Unfortunately, it makes little sense to instantiate these dependencies when we just want to test routing.
federationapi.AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, nil, keyRing, nil, &internal.FederationInternalAPI{}, caching.DisableMetrics) federationapi.AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, nil, keyRing, nil, &internal.FederationInternalAPI{}, caching.DisableMetrics)
baseURL, cancel := test.ListenAndServe(t, routers.Federation, true) baseURL, cancel := test.ListenAndServe(t, routers.Federation, true, 0)
defer cancel() defer cancel()
serverName := spec.ServerName(strings.TrimPrefix(baseURL, "https://")) serverName := spec.ServerName(strings.TrimPrefix(baseURL, "https://"))

View file

@ -45,6 +45,7 @@ func MakeJoin(
) util.JSONResponse { ) util.JSONResponse {
roomVersion, err := rsAPI.QueryRoomVersionForRoom(httpReq.Context(), roomID) roomVersion, err := rsAPI.QueryRoomVersionForRoom(httpReq.Context(), roomID)
if err != nil { if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryRoomVersionForRoom failed")
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusInternalServerError, Code: http.StatusInternalServerError,
JSON: jsonerror.InternalServerError(), JSON: jsonerror.InternalServerError(),

View file

@ -59,8 +59,8 @@ type testInterface interface {
// ListenAndServe will listen on a random high-numbered port and attach the given router. // ListenAndServe will listen on a random high-numbered port and attach the given router.
// Returns the base URL to send requests to. Call `cancel` to shutdown the server, which will block until it has closed. // Returns the base URL to send requests to. Call `cancel` to shutdown the server, which will block until it has closed.
func ListenAndServe(t testInterface, router http.Handler, withTLS bool) (apiURL string, cancel func()) { func ListenAndServe(t testInterface, router http.Handler, withTLS bool, customPort int) (apiURL string, cancel func()) {
listener, err := net.Listen("tcp", "127.0.0.1:0") listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", customPort))
if err != nil { if err != nil {
t.Fatalf("failed to listen: %s", err) t.Fatalf("failed to listen: %s", err)
} }