|
|
|
@@ -28,12 +28,16 @@ import java.util.concurrent.Executors
|
|
|
|
|
* on the home Wi-Fi, which a phone opens by scanning a QR code on the TV. No app, no account, and the Emby sign-in never
|
|
|
|
|
* leaves the TV. The rules:
|
|
|
|
|
* - Only addresses on the home network are answered; anything else is closed unread.
|
|
|
|
|
* - A phone joins with a one-time code from the TV, good for two minutes and five tries, and the Grown-ups PIN if one is set.
|
|
|
|
|
* - A phone joins with a one-time code from the TV, good for two minutes and five tries, and the Grown-ups PIN if one is set
|
|
|
|
|
* (three tries). Each phone address also has its own limit: five wrong answers in ten minutes and it's refused for ten.
|
|
|
|
|
* - Requests must name the TV by its address (Host is an IP), so a web page elsewhere can't reach it through a borrowed name.
|
|
|
|
|
* - A phone's token works only on the TV that issued it, and the phone page carries catalogue metadata only: titles, years,
|
|
|
|
|
* reasons and channel settings. No Emby or Jellyfin credentials, server addresses or playback tokens ever go to a phone.
|
|
|
|
|
* - A joined phone holds a long random token in a cookie (HttpOnly, SameSite=Strict); the TV keeps only its hash, and
|
|
|
|
|
* Settings lists phones and removes them. Changes also need a header a cross-site page can't send.
|
|
|
|
|
* - The server runs only while Orbit is open, and only once a phone has joined or one is joining.
|
|
|
|
|
*/
|
|
|
|
|
internal class Pairing(val code: String, val secret: String, val untilMs: Long) { var tries = 0 }
|
|
|
|
|
internal class Pairing(val code: String, val secret: String, val untilMs: Long) { var tries = 0; var pinTries = 0 }
|
|
|
|
|
|
|
|
|
|
/** What the phone may ask of the screen that holds the library: Create your own's [ChannelMaker], what's on, and tuning. */
|
|
|
|
|
internal class PhoneHandler(val maker: ChannelMaker, val isOn: (Int) -> Boolean, val watch: (Int) -> Unit)
|
|
|
|
@@ -50,6 +54,8 @@ private class PrefsPhoneStore(context: Context) : PhoneStore {
|
|
|
|
|
|
|
|
|
|
internal object PhoneLink {
|
|
|
|
|
const val PORT = 8099
|
|
|
|
|
private const val STRIKE_LIMIT = 5
|
|
|
|
|
private const val STRIKE_WINDOW_MS = 10 * 60_000L
|
|
|
|
|
private const val COOKIE = "orbit_phone"
|
|
|
|
|
private val random = SecureRandom()
|
|
|
|
|
@Volatile private var server: ServerSocket? = null
|
|
|
|
@@ -71,6 +77,10 @@ internal object PhoneLink {
|
|
|
|
|
/** Where the server listens: every interface on the TV (tests keep it to this computer). */
|
|
|
|
|
internal var bindTo: InetAddress? = null
|
|
|
|
|
internal var port = PORT
|
|
|
|
|
/** Wrong answers per phone address: how many, since when, and until when it's refused. */
|
|
|
|
|
private class Strikes(var count: Int, var sinceMs: Long, var refusedUntilMs: Long = 0)
|
|
|
|
|
private val strikes = java.util.concurrent.ConcurrentHashMap<String, Strikes>()
|
|
|
|
|
internal var clock: () -> Long = System::currentTimeMillis
|
|
|
|
|
/** Changes the phone asks for run on the screen's own thread. */
|
|
|
|
|
internal var mainDispatcher: kotlinx.coroutines.CoroutineDispatcher = Dispatchers.Main
|
|
|
|
|
|
|
|
|
@@ -89,13 +99,13 @@ internal object PhoneLink {
|
|
|
|
|
|
|
|
|
|
fun init(context: Context) { appContext = context.applicationContext; store = PrefsPhoneStore(context.applicationContext); loadPhones(); if (phones.isNotEmpty()) start() }
|
|
|
|
|
/** For tests: a store of its own, no Android. */
|
|
|
|
|
internal fun initForTest(testStore: PhoneStore) { store = testStore; loadPhones() }
|
|
|
|
|
internal fun initForTest(testStore: PhoneStore) { store = testStore; strikes.clear(); loadPhones() }
|
|
|
|
|
|
|
|
|
|
/** A new one-time code, and the server running to receive it. */
|
|
|
|
|
fun beginPairing(): Pairing {
|
|
|
|
|
val code = "%04d".format(random.nextInt(10_000))
|
|
|
|
|
val secret = ByteArray(16).also(random::nextBytes).joinToString("") { "%02x".format(it) }
|
|
|
|
|
return Pairing(code, secret, System.currentTimeMillis() + 2 * 60_000L).also { pairing = it; start() }
|
|
|
|
|
return Pairing(code, secret, clock() + 2 * 60_000L).also { pairing = it; start() }
|
|
|
|
|
}
|
|
|
|
|
fun pairingUrl(p: Pairing): String? = homeAddress()?.let { "http://$it:$PORT/join?k=${p.secret}" }
|
|
|
|
|
|
|
|
|
@@ -153,14 +163,21 @@ internal object PhoneLink {
|
|
|
|
|
return prefs().get(id)?.let { id }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The Host a request names: an address, never a name a page elsewhere could point at this TV. */
|
|
|
|
|
private fun hostIsAnAddress(host: String?): Boolean {
|
|
|
|
|
val name = host?.trim()?.let { if (it.startsWith("[")) it.substringBefore(']') + "]" else it.substringBefore(':') } ?: return false
|
|
|
|
|
return Regex("^\\d{1,3}(\\.\\d{1,3}){3}$").matches(name) || Regex("^\\[[0-9a-fA-F:.]+]$").matches(name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun serve(socket: Socket) {
|
|
|
|
|
socket.soTimeout = 15_000
|
|
|
|
|
if (!allowed(socket.inetAddress)) return
|
|
|
|
|
val input = BufferedInputStream(socket.getInputStream()); val out = socket.getOutputStream()
|
|
|
|
|
val r = readRequest(input) ?: return
|
|
|
|
|
if (!hostIsAnAddress(r.headers["host"])) return respond(out, 400, "text/plain", "Use the TV's address.".toByteArray())
|
|
|
|
|
val phone = phoneOf(r)
|
|
|
|
|
when {
|
|
|
|
|
r.path == "/join" -> join(r, out)
|
|
|
|
|
r.path == "/join" -> join(r, out, socket.inetAddress.hostAddress.orEmpty())
|
|
|
|
|
phone == null -> page(out, PhonePages.join(null))
|
|
|
|
|
r.path == "/" -> page(out, PhonePages.app())
|
|
|
|
|
r.method == "POST" && r.headers["x-orbit"] != "1" -> json(out, 403, JSONObject().put("error", "Refused"))
|
|
|
|
@@ -168,22 +185,43 @@ internal object PhoneLink {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun join(r: Request, out: OutputStream) {
|
|
|
|
|
/** A wrong answer from [who]; true when that phone address has now had too many. */
|
|
|
|
|
private fun strike(who: String): Boolean {
|
|
|
|
|
val now = clock()
|
|
|
|
|
val s = strikes.compute(who) { _, old -> if (old == null || now - old.sinceMs > STRIKE_WINDOW_MS) Strikes(1, now) else old.apply { count++ } }!!
|
|
|
|
|
if (s.count >= STRIKE_LIMIT) s.refusedUntilMs = now + STRIKE_WINDOW_MS
|
|
|
|
|
return s.count >= STRIKE_LIMIT
|
|
|
|
|
}
|
|
|
|
|
private fun refused(who: String) = (strikes[who]?.refusedUntilMs ?: 0) > clock()
|
|
|
|
|
/** Joining's outcomes, counted only: never a code, PIN or address. */
|
|
|
|
|
private fun note(outcome: String) = android.util.Log.i("OrbitPhone", "join outcome=$outcome")
|
|
|
|
|
|
|
|
|
|
private fun join(r: Request, out: OutputStream, who: String) {
|
|
|
|
|
if (refused(who)) { note("refused"); return respond(out, 429, "text/html; charset=utf-8", PhonePages.join("Too many wrong tries from this phone. Wait ten minutes, then ask the TV for a new code.").toByteArray()) }
|
|
|
|
|
val p = pairing
|
|
|
|
|
// The code and PIN come in a posted form, so they never sit in an address or history.
|
|
|
|
|
val form = if (r.method == "POST") r.body.split('&').filter { '=' in it }.associate { URLDecoder.decode(it.substringBefore('='), "UTF-8") to URLDecoder.decode(it.substringAfter('='), "UTF-8") } else emptyMap()
|
|
|
|
|
val given = form["k"] ?: r.query["k"]; val code = form["code"]?.filter(Char::isDigit); val pin = form["pin"]?.filter(Char::isDigit)
|
|
|
|
|
if (p == null || System.currentTimeMillis() > p.untilMs) return page(out, PhonePages.join("That code has run out. Ask the TV for a new one."))
|
|
|
|
|
if (p == null || clock() > p.untilMs) return page(out, PhonePages.join("That code has run out. Ask the TV for a new one."))
|
|
|
|
|
if (given == null && code == null) return page(out, PhonePages.join(null))
|
|
|
|
|
if (p.tries++ >= 5) { pairing = null; return page(out, PhonePages.join("Too many tries. Ask the TV for a new code.")) }
|
|
|
|
|
if (p.tries++ >= 5) { pairing = null; note("used_up"); return page(out, PhonePages.join("Too many tries. Ask the TV for a new code.")) }
|
|
|
|
|
val ok = (given != null && MessageDigest.isEqual(given.toByteArray(), p.secret.toByteArray())) || (code != null && code == p.code)
|
|
|
|
|
if (!ok) return page(out, PhonePages.join("That code didn't match. Check the TV."))
|
|
|
|
|
if (pinNeeded() && (pin == null || !pinCheck(pin))) return page(out, PhonePages.pin(given, code, pin != null))
|
|
|
|
|
if (!ok) { note("wrong_code"); strike(who); return page(out, PhonePages.join("That code didn't match. Check the TV.")) }
|
|
|
|
|
if (pinNeeded()) {
|
|
|
|
|
// The code is right; a PIN page doesn't spend the code's tries, but wrong PINs have their own three.
|
|
|
|
|
p.tries--
|
|
|
|
|
if (pin == null) return page(out, PhonePages.pin(given, code, false))
|
|
|
|
|
if (!pinCheck(pin)) {
|
|
|
|
|
note("wrong_pin"); strike(who)
|
|
|
|
|
if (++p.pinTries >= 3) { pairing = null; return page(out, PhonePages.join("That PIN didn't match three times. Ask the TV for a new code.")) }
|
|
|
|
|
return page(out, PhonePages.pin(given, code, true))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Joined: a long random token the phone keeps; the TV keeps only its hash.
|
|
|
|
|
val token = ByteArray(32).also(random::nextBytes).joinToString("") { "%02x".format(it) }
|
|
|
|
|
val name = phoneName(r.headers["user-agent"].orEmpty())
|
|
|
|
|
prefs().put(hash(token), "$name|${System.currentTimeMillis()}")
|
|
|
|
|
pairing = null; loadPhones(); active = name
|
|
|
|
|
pairing = null; loadPhones(); active = name; strikes.remove(who); note("joined")
|
|
|
|
|
respond(out, 302, "text/plain", ByteArray(0), listOf("Location: /", "Set-Cookie: $COOKIE=$token; HttpOnly; SameSite=Strict; Path=/; Max-Age=31536000"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|