|
| 1 | +package com.csguard |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.SharedPreferences |
| 5 | +import android.util.Log |
| 6 | +import org.json.JSONArray |
| 7 | +import org.json.JSONObject |
| 8 | + |
| 9 | +object AllowlistStore { |
| 10 | + |
| 11 | + private const val TAG = "CSGuard" |
| 12 | + private const val PREFS_NAME = "csguard_allowlist" |
| 13 | + private const val KEY_ALWAYS = "always_allow_hosts" |
| 14 | + private const val KEY_BLOCKED_PROVIDERS = "blocked_providers" |
| 15 | + private const val KEY_BLOCKED = "blocked_attempts_log" |
| 16 | + private const val MAX_BLOCKED_LOG = 200 |
| 17 | + |
| 18 | + @Volatile private var prefs: SharedPreferences? = null |
| 19 | + |
| 20 | + private val sessionAllowOnce = java.util.Collections.synchronizedSet(HashSet<String>()) |
| 21 | + |
| 22 | + fun init(context: Context) { |
| 23 | + if (prefs != null) return |
| 24 | + prefs = context.applicationContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 25 | + Log.i(TAG, "AllowlistStore initialized — alwaysAllow=${alwaysAllow().size} hosts") |
| 26 | + } |
| 27 | + |
| 28 | +fun alwaysAllow(): Set<String> { |
| 29 | + val raw = prefs?.getString(KEY_ALWAYS, "[]") ?: "[]" |
| 30 | + return try { |
| 31 | + val arr = JSONArray(raw) |
| 32 | + (0 until arr.length()).map { arr.getString(it).lowercase().trim() }.toSet() |
| 33 | + } catch (_: Throwable) { emptySet() } |
| 34 | + } |
| 35 | + |
| 36 | + fun addAlwaysAllow(host: String): Boolean { |
| 37 | + val normalized = host.lowercase().trim() |
| 38 | + if (normalized.isEmpty()) return false |
| 39 | + val current = alwaysAllow().toMutableSet() |
| 40 | + if (!current.add(normalized)) return false |
| 41 | + prefs?.edit()?.putString(KEY_ALWAYS, JSONArray(current.toList()).toString())?.apply() |
| 42 | + Log.i(TAG, "AllowlistStore: added always-allow → $normalized") |
| 43 | + return true |
| 44 | + } |
| 45 | + |
| 46 | + fun removeAlwaysAllow(host: String): Boolean { |
| 47 | + val normalized = host.lowercase().trim() |
| 48 | + val current = alwaysAllow().toMutableSet() |
| 49 | + if (!current.remove(normalized)) return false |
| 50 | + prefs?.edit()?.putString(KEY_ALWAYS, JSONArray(current.toList()).toString())?.apply() |
| 51 | + Log.i(TAG, "AllowlistStore: removed always-allow → $normalized") |
| 52 | + return true |
| 53 | + } |
| 54 | + |
| 55 | +fun blockedProviders(): Set<String> { |
| 56 | + val raw = prefs?.getString(KEY_BLOCKED_PROVIDERS, "[]") ?: "[]" |
| 57 | + return try { |
| 58 | + val arr = JSONArray(raw) |
| 59 | + (0 until arr.length()).map { arr.getString(it).trim() }.toSet() |
| 60 | + } catch (_: Throwable) { emptySet() } |
| 61 | + } |
| 62 | + |
| 63 | + fun addBlockedProvider(providerName: String): Boolean { |
| 64 | + val normalized = providerName.trim() |
| 65 | + if (normalized.isEmpty()) return false |
| 66 | + val current = blockedProviders().toMutableSet() |
| 67 | + if (!current.add(normalized)) return false |
| 68 | + prefs?.edit()?.putString(KEY_BLOCKED_PROVIDERS, JSONArray(current.toList()).toString())?.apply() |
| 69 | + Log.i(TAG, "AllowlistStore: added blocked provider → $normalized") |
| 70 | + return true |
| 71 | + } |
| 72 | + |
| 73 | + fun removeBlockedProvider(providerName: String): Boolean { |
| 74 | + val normalized = providerName.trim() |
| 75 | + val current = blockedProviders().toMutableSet() |
| 76 | + if (!current.remove(normalized)) return false |
| 77 | + prefs?.edit()?.putString(KEY_BLOCKED_PROVIDERS, JSONArray(current.toList()).toString())?.apply() |
| 78 | + Log.i(TAG, "AllowlistStore: removed blocked provider → $normalized") |
| 79 | + return true |
| 80 | + } |
| 81 | + |
| 82 | +fun allowOnce(host: String) { |
| 83 | + sessionAllowOnce.add(host.lowercase().trim()) |
| 84 | + Log.i(TAG, "AllowlistStore: allow-once (session) → $host") |
| 85 | + } |
| 86 | + |
| 87 | + fun clearSessionAllowOnce() { |
| 88 | + sessionAllowOnce.clear() |
| 89 | + } |
| 90 | + |
| 91 | +fun isAllowed(host: String?): Boolean { |
| 92 | + if (host.isNullOrBlank()) return false |
| 93 | + val h = host.lowercase().trim() |
| 94 | + if (h in sessionAllowOnce) return true |
| 95 | + val always = alwaysAllow() |
| 96 | + if (h in always) return true |
| 97 | + |
| 98 | + for (allowed in always) { |
| 99 | + if (h.endsWith(".$allowed")) return true |
| 100 | + } |
| 101 | + return false |
| 102 | + } |
| 103 | + |
| 104 | +data class BlockedEntry( |
| 105 | + val url: String, |
| 106 | + val caller: String, |
| 107 | + val timestamp: Long |
| 108 | + ) |
| 109 | + |
| 110 | + fun blockedLog(): List<BlockedEntry> { |
| 111 | + val raw = prefs?.getString(KEY_BLOCKED, "[]") ?: "[]" |
| 112 | + return try { |
| 113 | + val arr = JSONArray(raw) |
| 114 | + (0 until arr.length()).map { idx -> |
| 115 | + val obj = arr.getJSONObject(idx) |
| 116 | + BlockedEntry( |
| 117 | + url = obj.optString("url"), |
| 118 | + caller = obj.optString("caller"), |
| 119 | + timestamp = obj.optLong("ts") |
| 120 | + ) |
| 121 | + } |
| 122 | + } catch (_: Throwable) { emptyList() } |
| 123 | + } |
| 124 | + |
| 125 | + fun recordBlocked(url: String, caller: String) { |
| 126 | + val current = blockedLog().toMutableList() |
| 127 | + current.add(0, BlockedEntry(url, caller, System.currentTimeMillis())) |
| 128 | + |
| 129 | + val trimmed = current.take(MAX_BLOCKED_LOG) |
| 130 | + val arr = JSONArray() |
| 131 | + for (entry in trimmed) { |
| 132 | + val obj = JSONObject() |
| 133 | + obj.put("url", entry.url) |
| 134 | + obj.put("caller", entry.caller) |
| 135 | + obj.put("ts", entry.timestamp) |
| 136 | + arr.put(obj) |
| 137 | + } |
| 138 | + prefs?.edit()?.putString(KEY_BLOCKED, arr.toString())?.apply() |
| 139 | + } |
| 140 | + |
| 141 | + fun clearBlockedLog() { |
| 142 | + prefs?.edit()?.remove(KEY_BLOCKED)?.apply() |
| 143 | + } |
| 144 | +} |
0 commit comments