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
flagOrigin string
flagPort int
)
func init() {
@ -45,6 +47,8 @@ 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, "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 {
@ -55,6 +59,7 @@ type args struct {
MatrixKeyID gomatrixserverlib.KeyID
Origin spec.ServerName
SelfHostKey bool
SelfHostPort int
TargetURL *url.URL
}
@ -91,8 +96,9 @@ func processArgs() (*args, error) {
a.SkipVerify = flagSkipVerify
a.Method = strings.ToUpper(flagMethod)
a.Origin = spec.ServerName(flagOrigin)
a.SelfHostPort = flagPort
a.TargetURL = targetURL
a.SelfHostKey = a.Origin == "" && a.TargetURL.Hostname() == "localhost"
a.SelfHostKey = a.SelfHostPort != 0 || (a.Origin == "" && a.TargetURL.Hostname() == "localhost")
// load data
isFile := strings.HasPrefix(flagData, "@")
@ -119,7 +125,7 @@ func main() {
if a.SelfHostKey {
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()
parsedURL, _ := url.Parse(apiURL)
a.Origin = spec.ServerName(parsedURL.Host)
@ -167,6 +173,11 @@ func main() {
if err != nil {
panic(err)
}
if a.Data != nil {
data := string(a.Data)
data = strings.ReplaceAll(data, "$PORT", port)
a.Data = []byte(data)
}
}
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.
// 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)
baseURL, cancel := test.ListenAndServe(t, routers.Federation, true)
baseURL, cancel := test.ListenAndServe(t, routers.Federation, true, 0)
defer cancel()
serverName := spec.ServerName(strings.TrimPrefix(baseURL, "https://"))

View file

@ -45,6 +45,7 @@ func MakeJoin(
) util.JSONResponse {
roomVersion, err := rsAPI.QueryRoomVersionForRoom(httpReq.Context(), roomID)
if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryRoomVersionForRoom failed")
return util.JSONResponse{
Code: http.StatusInternalServerError,
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.
// 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()) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
func ListenAndServe(t testInterface, router http.Handler, withTLS bool, customPort int) (apiURL string, cancel func()) {
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", customPort))
if err != nil {
t.Fatalf("failed to listen: %s", err)
}