3
0
Fork 0

Don't reload Nebula unless config is updated (#91)

This commit is contained in:
John Maguire 2022-11-18 14:27:27 -05:00 committed by GitHub
parent e4bbd0a31c
commit 9dd5b9cad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 7 deletions

View File

@ -40,13 +40,19 @@ class DNUpdateWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, param
private fun updateSite(site: Site) { private fun updateSite(site: Site) {
try { try {
DNUpdateLock(site).use { DNUpdateLock(site).use {
if (updater.updateSite(site)) { val res = updater.updateSite(site)
// Reload Nebula if this is the currently active site
// Reload Nebula if this is the currently active site
if (res == DNSiteUpdater.Result.CONFIG_UPDATED) {
Intent().also { intent -> Intent().also { intent ->
intent.action = NebulaVpnService.ACTION_RELOAD intent.action = NebulaVpnService.ACTION_RELOAD
intent.putExtra("id", site.id) intent.putExtra("id", site.id)
context.sendBroadcast(intent) context.sendBroadcast(intent)
} }
}
// Update the UI on any change
if (res != DNSiteUpdater.Result.NOOP) {
Intent().also { intent -> Intent().also { intent ->
intent.action = MainActivity.ACTION_REFRESH_SITES intent.action = MainActivity.ACTION_REFRESH_SITES
context.sendBroadcast(intent) context.sendBroadcast(intent)
@ -77,9 +83,13 @@ class DNSiteUpdater(
private val context: Context, private val context: Context,
private val apiClient: APIClient, private val apiClient: APIClient,
) { ) {
fun updateSite(site: Site): Boolean { enum class Result {
CONFIG_UPDATED, CREDENTIALS_UPDATED, NOOP
}
fun updateSite(site: Site): Result {
if (!site.managed) { if (!site.managed) {
return false return Result.NOOP
} }
val credentials = site.getDNCredentials(context) val credentials = site.getDNCredentials(context)
@ -97,21 +107,23 @@ class DNSiteUpdater(
if (!credentials.invalid) { if (!credentials.invalid) {
site.invalidateDNCredentials(context) site.invalidateDNCredentials(context)
Log.d(TAG, "Invalidated credentials in site ${site.name}") Log.d(TAG, "Invalidated credentials in site ${site.name}")
return Result.CREDENTIALS_UPDATED
} }
return true return Result.NOOP
} }
if (newSite != null) { if (newSite != null) {
newSite.save(context) newSite.save(context)
Log.d(TAG, "Updated site ${site.id}: ${site.name}") Log.d(TAG, "Updated site ${site.id}: ${site.name}")
return true return Result.CONFIG_UPDATED
} }
if (credentials.invalid) { if (credentials.invalid) {
site.validateDNCredentials(context) site.validateDNCredentials(context)
Log.d(TAG, "Revalidated credentials in site ${site.id}: ${site.name}") Log.d(TAG, "Revalidated credentials in site ${site.id}: ${site.name}")
return Result.CREDENTIALS_UPDATED
} }
return false return Result.NOOP
} }
} }