Skip to content

Commit

Permalink
fix: manually enable running behind proxy
Browse files Browse the repository at this point in the history
Workaround for quarkus/nginx? messing up $scheme if we run behind a proxy.
Theoretically, this should be fixed by the settings in
`resources/application.properties`, but it just isn't.

We just force `wss` unless called via local address.
  • Loading branch information
sne11ius committed May 24, 2024
1 parent f795d23 commit 1ad6b67
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion api/src/main/kotlin/pp/api/RoomsResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,23 @@ class RoomsResource(
val roomId = saoItems.random() + " " + locations.random()
Log.info("Redirecting to new room $roomId")
val redirectLocation = uri.requestUri.resolve(encode(roomId, UTF_8)).toString()
val wsRedirectLocation = redirectLocation.replace("http", "ws")
val wsRedirectLocation = redirectLocation
.replace("http", "ws")
.let { url ->
// Workaround for quarkus/nginx? messing up $scheme if we run behind a proxy.
// Theoretically, this should be fixed by the settings in `resources/application.properties`, but it
// just isn't.
// We just force `wss` unless called via local address.
if (url.startsWith("wss")) {
url
} else {
if (setOf("127.0.0.1", "localhost").any { it in url }) {
url
} else {
url.replace("ws://", "wss://")
}
}
}
val newUri = URI(wsRedirectLocation)
Log.info("Redirecting to $newUri")
return temporaryRedirect(newUri).build()
Expand Down
26 changes: 26 additions & 0 deletions api/src/test/kotlin/pp/api/RoomsResourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import io.quarkus.test.common.http.TestHTTPEndpoint
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import io.restassured.http.ContentType.JSON
import jakarta.ws.rs.core.UriInfo
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.matchesPattern
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever
import pp.api.data.Room
import pp.api.data.User
import pp.api.data.UserType.PARTICIPANT
import java.net.URI

/**
* Test the [RoomsResource]
Expand Down Expand Up @@ -79,4 +83,26 @@ class RoomsResourceTest {
)
)
}

@Test
fun testEnforceScheme() {
val resource = RoomsResource(rooms)
val uriInfo: UriInfo = mock()
val uri: URI = mock()
whenever(uriInfo.requestUri).thenReturn(uri)
val resolvedUri: URI = mock()
whenever(uri.resolve(any<String>())).thenReturn(resolvedUri)

whenever(resolvedUri.toString()).thenReturn("http://example.com")
assertEquals("wss://example.com", resource.createRandomRoom(uriInfo).getHeaderString("Location"))

whenever(resolvedUri.toString()).thenReturn("https://example.com")
assertEquals("wss://example.com", resource.createRandomRoom(uriInfo).getHeaderString("Location"))

whenever(resolvedUri.toString()).thenReturn("http://localhost")
assertEquals("ws://localhost", resource.createRandomRoom(uriInfo).getHeaderString("Location"))

whenever(resolvedUri.toString()).thenReturn("http://127.0.0.1")
assertEquals("ws://127.0.0.1", resource.createRandomRoom(uriInfo).getHeaderString("Location"))
}
}

0 comments on commit 1ad6b67

Please sign in to comment.