summaryrefslogtreecommitdiff
path: root/app/src/main/java/foundation/e/advancedprivacy/domain/usecases/IpScramblingStateUseCase.kt
blob: 79c79f7937c8fd41ac8bfe5853145c5605680d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2023 MURENA SAS
 * Copyright (C) 2021 E FOUNDATION
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.advancedprivacy.domain.usecases

import android.content.Intent
import foundation.e.advancedprivacy.common.isStandaloneBuild
import foundation.e.advancedprivacy.data.repositories.AppListsRepository
import foundation.e.advancedprivacy.data.repositories.LocalStateRepository
import foundation.e.advancedprivacy.domain.entities.ApplicationDescription
import foundation.e.advancedprivacy.domain.entities.FeatureServiceState
import foundation.e.advancedprivacy.externalinterfaces.permissions.IPermissionsPrivacyModule
import foundation.e.advancedprivacy.ipscrambler.OrbotServiceSupervisor
import foundation.e.advancedprivacy.trackers.domain.externalinterfaces.TrackersServiceSupervisor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class IpScramblingStateUseCase(
    private val orbotServiceSupervisor: OrbotServiceSupervisor,
    private val permissionsPrivacyModule: IPermissionsPrivacyModule,
    private val appDesc: ApplicationDescription,
    private val localStateRepository: LocalStateRepository,
    private val appListsRepository: AppListsRepository,
    private val trackersServiceSupervisor: TrackersServiceSupervisor,
    private val coroutineScope: CoroutineScope
) {
    val internetPrivacyMode: StateFlow<FeatureServiceState> = orbotServiceSupervisor.state

    init {
        orbotServiceSupervisor.requestStatus()

        coroutineScope.launch(Dispatchers.Default) {
            localStateRepository.ipScramblingSetting.collect {
                applySettings(it)
            }
        }

        orbotServiceSupervisor.state.map {
            localStateRepository.internetPrivacyMode.value = it
        }.launchIn(coroutineScope)
    }

    fun toggle(hideIp: Boolean) {
        localStateRepository.setIpScramblingSetting(enabled = hideIp)
    }

    private fun getHiddenPackageNames(): List<String> {
        return appListsRepository.getMainProfileHiddenSystemApps().map { it.packageName }
    }

    val bypassTorApps: Set<String> get() {
        var whitelist = orbotServiceSupervisor.appList
        if (getHiddenPackageNames().any { it in whitelist }) {
            val mutable = whitelist.toMutableSet()
            mutable.removeAll(getHiddenPackageNames())
            mutable.add(appListsRepository.dummySystemApp.packageName)
            whitelist = mutable
        }
        if (AppListsRepository.compatibiltyPNames.any { it in whitelist }) {
            val mutable = whitelist.toMutableSet()
            mutable.removeAll(AppListsRepository.compatibiltyPNames)
            mutable.add(appListsRepository.dummyCompatibilityApp.packageName)
            whitelist = mutable
        }
        return whitelist
    }

    fun toggleBypassTor(packageName: String) {
        val visibleList = bypassTorApps.toMutableSet()
        val rawList = orbotServiceSupervisor.appList.toMutableSet()

        if (visibleList.contains(packageName)) {
            if (packageName == appListsRepository.dummySystemApp.packageName) {
                rawList.removeAll(getHiddenPackageNames())
            } else if (packageName == appListsRepository.dummyCompatibilityApp.packageName) {
                rawList.removeAll(AppListsRepository.compatibiltyPNames)
            } else {
                rawList.remove(packageName)
            }
        } else {
            if (packageName == appListsRepository.dummySystemApp.packageName) {
                rawList.addAll(getHiddenPackageNames())
            } else if (packageName == appListsRepository.dummyCompatibilityApp.packageName) {
                rawList.addAll(AppListsRepository.compatibiltyPNames)
            } else {
                rawList.add(packageName)
            }
        }
        orbotServiceSupervisor.appList = rawList
    }

    val availablesLocations: List<String> = orbotServiceSupervisor.getAvailablesLocations().sorted()

    val exitCountry: String get() = orbotServiceSupervisor.getExitCountryCode()

    suspend fun setExitCountry(locationId: String) {
        if (locationId != exitCountry) {
            orbotServiceSupervisor.setExitCountryCode(locationId)
        }
    }

    private suspend fun applySettings(isIpScramblingEnabled: Boolean) {
        val currentMode = localStateRepository.internetPrivacyMode.value
        when {
            isIpScramblingEnabled && currentMode in setOf(FeatureServiceState.OFF, FeatureServiceState.STOPPING) ->
                applyStartIpScrambling()

            !isIpScramblingEnabled && currentMode in setOf(FeatureServiceState.ON, FeatureServiceState.STARTING) ->
                orbotServiceSupervisor.stop()

            else -> {}
        }
    }

    private suspend fun applyStartIpScrambling() {
        val authorizeVpnIntent = orbotServiceSupervisor.prepareAndroidVpn()
        if (authorizeVpnIntent == null) {
            localStateRepository.emitStartVpnDisclaimer(null)

            startIpScrambling()
            return
        }

        acquireVpnAuthorization(authorizeVpnIntent)
    }

    private suspend fun acquireVpnAuthorization(authorizeVpnIntent: Intent) {
        val authorized = permissionsPrivacyModule.setVpnPackageAuthorization(appDesc.packageName)
        val alwaysOnVpnPackage = permissionsPrivacyModule.getAlwaysOnVpnPackage()

        when {
            authorized && alwaysOnVpnPackage == null -> {
                localStateRepository.emitStartVpnDisclaimer(null)
                startIpScrambling()
            }
            authorized && alwaysOnVpnPackage != null -> {
                localStateRepository.emitOtherVpnRunning(
                    permissionsPrivacyModule.getApplicationDescription(
                        packageName = alwaysOnVpnPackage,
                        withIcon = false
                    )
                )
                localStateRepository.setIpScramblingSetting(enabled = false)
            }
            else -> localStateRepository.emitStartVpnDisclaimer(authorizeVpnIntent)
        }
    }

    fun startIpScrambling() {
        localStateRepository.internetPrivacyMode.value = FeatureServiceState.STARTING
        orbotServiceSupervisor.setDNSFilter((trackersServiceSupervisor.dnsFilterForIpScrambling))
        orbotServiceSupervisor.start(enableNotification = isStandaloneBuild)
    }
}