summaryrefslogtreecommitdiff
path: root/trackersservicestandalone/src/main/java/foundation/e/advancedprivacy/trackers/service/usecases/VpnSupervisorUseCaseStandalone.kt
blob: 683f886d08e6e11335836ce82d78093f4c52cc66 (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
/*
 * Copyright (C) 2023 MURENA SAS
 *
 * 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.trackers.service.usecases

import foundation.e.advancedprivacy.domain.entities.FeatureState
import foundation.e.advancedprivacy.domain.entities.MainFeatures
import foundation.e.advancedprivacy.domain.entities.MainFeatures.IpScrambling
import foundation.e.advancedprivacy.domain.entities.MainFeatures.TrackersControl
import foundation.e.advancedprivacy.domain.repositories.LocalStateRepository
import foundation.e.advancedprivacy.domain.usecases.VpnSupervisorUseCase
import foundation.e.advancedprivacy.externalinterfaces.servicesupervisors.FeatureSupervisor
import foundation.e.advancedprivacy.ipscrambler.OrbotSupervisor
import foundation.e.advancedprivacy.trackers.domain.externalinterfaces.TrackersSupervisor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.launch

class VpnSupervisorUseCaseStandalone(
    private val localStateRepository: LocalStateRepository,
    private val trackersSupervisor: TrackersSupervisor,
    private val orbotSupervisor: OrbotSupervisor,
    private val scope: CoroutineScope,
) : VpnSupervisorUseCase {
    private var applySettingJob: Job? = null

    init {
        listenSettings()
    }

    override fun listenSettings() {
        var previousBlockTrackers: Boolean? = null

        localStateRepository.blockTrackers.combine(
            localStateRepository.ipScramblingSetting
        ) { blockTrackers, hideIp ->
            applySettingJob?.cancel()
            applySettingJob = scope.launch {
                when {
                    blockTrackers && !hideIp ->
                        launchVpnService(trackersSupervisor)

                    !blockTrackers && !hideIp ->
                        stopAllServices()

                    else -> {
                        if (blockTrackers && previousBlockTrackers != true) {
                            localStateRepository.emitStartVpnDisclaimer(IpScrambling())
                        }

                        launchVpnService(orbotSupervisor)
                    }
                }

                previousBlockTrackers = blockTrackers
            }
        }.launchIn(scope)
    }

    private fun stopAllServices() {
        listOf(orbotSupervisor, trackersSupervisor).map { stopVpnService(it) }
    }

    private fun stopVpnService(supervisor: FeatureSupervisor): FeatureSupervisor {
        when (supervisor.state.value) {
            FeatureState.ON,
            FeatureState.STARTING ->
                supervisor.stop()

            else -> {}
        }
        return supervisor
    }

    private suspend fun launchVpnService(supervisor: FeatureSupervisor) {
        stopVpnService(otherSupervisor(supervisor)).let { otherSupervisor ->
            otherSupervisor.state.first { it == FeatureState.OFF }
        }

        when (supervisor.state.value) {
            FeatureState.STOPPING -> {
                supervisor.state.first { it == FeatureState.OFF }
                initiateStartVpnService(supervisor)
            }

            FeatureState.OFF -> initiateStartVpnService(supervisor)
            else -> {}
        }
    }

    private fun otherSupervisor(supervisor: FeatureSupervisor): FeatureSupervisor {
        return when (supervisor) {
            trackersSupervisor -> orbotSupervisor
            else -> trackersSupervisor
        }
    }

    private fun getSupervisor(feature: MainFeatures): FeatureSupervisor {
        return when (feature) {
            is TrackersControl -> trackersSupervisor
            else -> orbotSupervisor
        }
    }

    private suspend fun initiateStartVpnService(supervisor: FeatureSupervisor) {
        val authorizeVpnIntent = orbotSupervisor.prepareAndroidVpn()
        val feature = when (supervisor) {
            trackersSupervisor -> TrackersControl(authorizeVpnIntent)
            else -> IpScrambling(authorizeVpnIntent)
        }

        if (authorizeVpnIntent == null) {
            localStateRepository.emitStartVpnDisclaimer(feature)
            startVpnService(feature)
        } else {
            localStateRepository.emitStartVpnDisclaimer(feature)
        }
    }

    override fun startVpnService(feature: MainFeatures) {
        if (feature is IpScrambling) {
            localStateRepository.internetPrivacyMode.value = FeatureState.STARTING
            orbotSupervisor.setDNSFilter(trackersSupervisor.dnsFilterForIpScrambling)
        }

        getSupervisor(feature).start()
    }

    override fun cancelStartVpnService(feature: MainFeatures) {
        when (feature) {
            is IpScrambling ->
                localStateRepository.setIpScramblingSetting(enabled = false)
            is TrackersControl ->
                trackersSupervisor.stop()
            else -> {}
        }
    }
}