app/src/main/kotlin/com/honjimaku/subrep/Room.kt (1389 bytes)
1 package com.honjimaku.subrep 2 3 import java.security.SecureRandom 4 import java.util.Base64 5 6 /** 7 * The share room of this device on the relay, the same as the desktop app makes it. 8 * 9 * The room id is a capability: whoever has the link reads the feed. The secret lets only this 10 * device publish into the room. Both are made once and kept, so the link stays the same across 11 * restarts. The relay refuses a secret shorter than 16 characters. 12 */ 13 object Room { 14 15 const val RELAY = "wss://honjimaku.com/subrep" 16 17 private val random = SecureRandom() 18 19 /** A room id: 12 characters of the URL alphabet, as the desktop app makes them. */ 20 fun newId(): String = token(9) 21 22 /** A publisher secret: 32 characters. */ 23 fun newSecret(): String = token(24) 24 25 /** The link that the viewers open, for the relay [relay] and the room [room]. */ 26 fun watchUrl(relay: String, room: String): String { 27 val base = relay.trimEnd('/') 28 val http = when { 29 base.startsWith("wss://") -> "https://" + base.removePrefix("wss://") 30 base.startsWith("ws://") -> "http://" + base.removePrefix("ws://") 31 else -> base 32 } 33 return "$http/w/$room" 34 } 35 36 private fun token(bytes: Int): String { 37 val raw = ByteArray(bytes).also(random::nextBytes) 38 return Base64.getUrlEncoder().withoutPadding().encodeToString(raw) 39 } 40 }