-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9e238a
commit d0e3e65
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...n/kotlin/com/faforever/icebreaker/service/cloudflare/ApiKeyAuthenticationRequestFilter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.faforever.icebreaker.service.cloudflare | ||
|
||
import jakarta.annotation.Priority | ||
import jakarta.ws.rs.Priorities | ||
import jakarta.ws.rs.client.ClientRequestContext | ||
import jakarta.ws.rs.client.ClientRequestFilter | ||
import jakarta.ws.rs.core.HttpHeaders | ||
|
||
@Priority(Priorities.AUTHENTICATION) | ||
class ApiKeyAuthenticationRequestFilter(turnApiKey: String) : ClientRequestFilter { | ||
override fun filter(requestContext: ClientRequestContext) { | ||
requestContext.headers.add(HttpHeaders.AUTHORIZATION, accessToken) | ||
} | ||
|
||
private val accessToken: String = "Bearer $turnApiKey" | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/faforever/icebreaker/service/cloudflare/CloudflareApiAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.faforever.icebreaker.service.cloudflare | ||
|
||
import jakarta.inject.Singleton | ||
import org.eclipse.microprofile.faulttolerance.Retry | ||
import org.eclipse.microprofile.rest.client.RestClientBuilder | ||
import java.net.URI | ||
|
||
@Singleton | ||
class CloudflareApiAdapter( | ||
private val cloudflareProperties: CloudflareProperties, | ||
) { | ||
private val cloudflareApiClient = RestClientBuilder | ||
.newBuilder() | ||
.baseUri(URI.create("https://rtc.live.cloudflare.com")) | ||
.register(ApiKeyAuthenticationRequestFilter(turnApiKey = cloudflareProperties.turnKeyApiToken())) | ||
.build(CloudflareApiClient::class.java) | ||
|
||
@Retry | ||
fun requestIceServers(credentialRequest: CloudflareApiClient.CredentialRequest) = | ||
cloudflareApiClient.requestCredentials( | ||
turnKeyId = cloudflareProperties.turnKeyId(), | ||
credentialRequest = credentialRequest, | ||
) | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/faforever/icebreaker/service/cloudflare/CloudflareApiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.faforever.icebreaker.service.cloudflare | ||
|
||
import jakarta.enterprise.context.ApplicationScoped | ||
import jakarta.ws.rs.Consumes | ||
import jakarta.ws.rs.POST | ||
import jakarta.ws.rs.Path | ||
import jakarta.ws.rs.PathParam | ||
import jakarta.ws.rs.core.MediaType | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient | ||
|
||
@ApplicationScoped | ||
@RegisterRestClient | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
interface CloudflareApiClient { | ||
|
||
data class CredentialRequest(val ttl: Long) | ||
data class CredentialResponse(val iceServers: IceServers) { | ||
data class IceServers(val urls: List<String>, val username: String, val credential: String) | ||
} | ||
|
||
@POST | ||
@Path("/v1/turn/keys/{turnKeyId}/credentials/generate") | ||
fun requestCredentials( | ||
@PathParam("turnKeyId") turnKeyId: String, | ||
credentialRequest: CredentialRequest, | ||
): CredentialResponse | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/faforever/icebreaker/service/cloudflare/CloudflareProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.faforever.icebreaker.service.cloudflare | ||
|
||
import io.smallrye.config.ConfigMapping | ||
|
||
@ConfigMapping(prefix = "cloudflare") | ||
interface CloudflareProperties { | ||
fun enabled(): Boolean | ||
|
||
fun turnEnabled(): Boolean | ||
|
||
fun turnKeyId(): String | ||
|
||
fun turnKeyApiToken(): String | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/faforever/icebreaker/service/cloudflare/CloudflareSessionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.faforever.icebreaker.service.cloudflare | ||
|
||
import com.faforever.icebreaker.config.FafProperties | ||
import com.faforever.icebreaker.service.Server | ||
import com.faforever.icebreaker.service.Session | ||
import com.faforever.icebreaker.service.SessionHandler | ||
import jakarta.inject.Singleton | ||
|
||
@Singleton | ||
class CloudflareSessionHandler( | ||
cloudflareProperties: CloudflareProperties, | ||
val fafProperties: FafProperties, | ||
private val cloudflareApiAdapter: CloudflareApiAdapter, | ||
) : SessionHandler { | ||
companion object { | ||
const val SERVER_NAME = "cloudflare.com" | ||
} | ||
|
||
override val active = cloudflareProperties.enabled() | ||
private val turnEnabled = cloudflareProperties.turnEnabled() | ||
|
||
override fun createSession(id: String) { | ||
// Cloudflare has no session handling, we use global access | ||
} | ||
|
||
override fun deleteSession(id: String) { | ||
// Cloudflare has no session handling, we use global access | ||
} | ||
|
||
override fun getIceServers() = listOf(Server(id = SERVER_NAME, region = "Global")) | ||
|
||
override fun getIceServersSession(sessionId: String): List<Session.Server> = | ||
cloudflareApiAdapter.requestIceServers( | ||
credentialRequest = CloudflareApiClient.CredentialRequest(ttl = fafProperties.tokenLifetimeSeconds()), | ||
).let { | ||
listOf( | ||
Session.Server( | ||
id = SERVER_NAME, | ||
username = it.iceServers.username, | ||
credential = it.iceServers.credential, | ||
urls = it.iceServers.urls.map { url -> | ||
// A sample response looks like "stun:fr-turn1.xirsys.com" | ||
// The java URI class fails to read host and port due to the missing // after the : | ||
// Thus we "normalize" the uri, even though it is technically valid | ||
url.replaceFirst(":", "://") | ||
}.filter { url -> turnEnabled || !url.startsWith("turn") }, | ||
), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters