-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
BraveVPNService.kt
1266 lines (1065 loc) · 50.4 KB
/
BraveVPNService.kt
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 Jigsaw Operations LLC
* Copyright 2020 RethinkDNS and its authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.celzero.bravedns.service
import android.app.*
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.pm.PackageManager
import android.net.Network
import android.net.ProxyInfo
import android.net.VpnService
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.ParcelFileDescriptor
import android.os.SystemClock.elapsedRealtime
import android.service.quicksettings.TileService.requestListeningState
import android.util.Log
import android.view.accessibility.AccessibilityManager
import android.widget.Toast
import androidx.annotation.GuardedBy
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.celzero.bravedns.R
import com.celzero.bravedns.automaton.FirewallManager
import com.celzero.bravedns.automaton.FirewallManager.NOTIF_CHANNEL_ID_FIREWALL_ALERTS
import com.celzero.bravedns.automaton.FirewallRules
import com.celzero.bravedns.automaton.FirewallRules.UID_EVERYBODY
import com.celzero.bravedns.data.AppConfig
import com.celzero.bravedns.data.ConnectionRules
import com.celzero.bravedns.data.IPDetails
import com.celzero.bravedns.database.AppInfo
import com.celzero.bravedns.database.RefreshDatabase
import com.celzero.bravedns.net.go.GoIntraListener
import com.celzero.bravedns.net.go.GoVpnAdapter
import com.celzero.bravedns.net.go.GoVpnAdapter.Companion.establish
import com.celzero.bravedns.net.manager.ConnectionTracer
import com.celzero.bravedns.receiver.NotificationActionReceiver
import com.celzero.bravedns.ui.HomeScreenActivity
import com.celzero.bravedns.ui.HomeScreenActivity.GlobalVariable.DEBUG
import com.celzero.bravedns.ui.NotificationHandlerDialog
import com.celzero.bravedns.util.*
import com.celzero.bravedns.util.Constants.Companion.INIT_TIME_MS
import com.celzero.bravedns.util.Constants.Companion.INVALID_PORT
import com.celzero.bravedns.util.Constants.Companion.NOTIF_INTENT_EXTRA_ACCESSIBILITY_NAME
import com.celzero.bravedns.util.Constants.Companion.NOTIF_INTENT_EXTRA_ACCESSIBILITY_VALUE
import com.celzero.bravedns.util.LoggerConstants.Companion.LOG_TAG_VPN
import com.celzero.bravedns.util.Utilities.Companion.getThemeAccent
import com.celzero.bravedns.util.Utilities.Companion.isAtleastO
import com.celzero.bravedns.util.Utilities.Companion.isAtleastQ
import com.celzero.bravedns.util.Utilities.Companion.isMissingOrInvalidUid
import com.celzero.bravedns.util.Utilities.Companion.showToastUiCentered
import com.google.common.collect.Sets
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.withLock
import org.koin.android.ext.android.inject
import protect.Blocker
import protect.Protector
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.math.min
import kotlin.random.Random
class BraveVPNService : VpnService(), ConnectionMonitor.NetworkListener, Protector, Blocker,
OnSharedPreferenceChangeListener {
@GuardedBy("vpnController") private var connectionMonitor: ConnectionMonitor? = null
@GuardedBy("vpnController") private var vpnAdapter: GoVpnAdapter? = null
companion object {
const val SERVICE_ID = 1 // Only has to be unique within this app.
private const val MAIN_CHANNEL_ID = "vpn"
private const val WARNING_CHANNEL_ID = "warning"
// maximum time delay before sending block connection response
val DELAY_FIREWALL_RESPONSE_MS: Long = TimeUnit.SECONDS.toMillis(30)
// notification request codes
private const val NOTIF_ACTION_MODE_RESUME = 98
private const val NOTIF_ACTION_MODE_PAUSE = 99
private const val NOTIF_ACTION_MODE_STOP = 100
private const val NOTIF_ACTION_MODE_DNS_ONLY = 101
private const val NOTIF_ACTION_MODE_DNS_FIREWALL = 102
private const val NOTIF_ID_LOAD_RULES_FAIL = 103
private const val NOTIF_ID_ACCESSIBILITY_FAILURE = 104
// IPv4 VPN constants
private const val IPV4_TEMPLATE: String = "10.111.222.%d"
private const val IPV4_PREFIX_LENGTH: Int = 24
// This value must match the hardcoded MTU in outline-go-tun2socks.
// TODO: Make outline-go-tun2socks's MTU configurable.
private const val VPN_INTERFACE_MTU: Int = 1500
private val fakeDnsIp: String = LanIp.DNS.make(IPV4_TEMPLATE)
private val fakeDns = "$fakeDnsIp:${KnownPorts.DNS_PORT}"
}
private var isLockDownPrevious: Boolean = false
private val vpnScope = MainScope()
private lateinit var connTracer: ConnectionTracer
private val rand: Random = Random
private val appConfig by inject<AppConfig>()
private val orbotHelper by inject<OrbotHelper>()
private val ipTracker by inject<IPTracker>()
private val dnsLogTracker by inject<DNSLogTracker>()
private val persistentState by inject<PersistentState>()
private val refreshDatabase by inject<RefreshDatabase>()
@Volatile private var isAccessibilityServiceFunctional: Boolean = false
@Volatile var accessibilityHearbeatTimestamp: Long = INIT_TIME_MS
var settingUpOrbot: AtomicBoolean = AtomicBoolean(false)
private lateinit var notificationManager: NotificationManager
private lateinit var activityManager: ActivityManager
private lateinit var accessibilityManager: AccessibilityManager
private var keyguardManager: KeyguardManager? = null
private lateinit var appInfoObserver: Observer<Collection<AppInfo>>
private lateinit var orbotStartStatusObserver: Observer<Boolean>
private var excludedApps: MutableSet<String> = mutableSetOf()
private var userInitiatedStop: Boolean = false
private var accessibilityListener: AccessibilityManager.AccessibilityStateChangeListener? = null
enum class State {
NEW, WORKING, FAILING, PAUSED
}
override fun block(protocol: Int, _uid: Long, sourceAddress: String,
destAddress: String): Boolean {
handleVpnLockdownStateAsync()
val first = sourceAddress.split(":")
val second = destAddress.split(":")
val srcIp = first[0]
val srcPort = first[first.count() - 1].toInt()
val dstIp = second[0]
val dstPort = second[second.count() - 1].toInt()
val uid = if (VERSION.SDK_INT >= VERSION_CODES.Q) {
connTracer.getUidQ(protocol, srcIp, srcPort, dstIp, dstPort)
} else {
_uid.toInt() // uid must have been retrieved from procfs by the caller
}
// skip the block-ceremony for dns conns
if (isDns(dstPort) && isVpnDns(dstIp)) return false
// FIXME: replace currentTimeMillis with elapsed-time
val connInfo = IPDetails(uid, srcIp, srcPort, dstIp, dstPort,
System.currentTimeMillis(), /*blocked?*/ false, /*rule*/ "",
protocol)
val rule = firewall(connInfo)
val blocked = FirewallRuleset.ground(rule)
connInfo.blockedByRule = rule.id
connInfo.isBlocked = blocked
if (DEBUG) Log.i(LOG_TAG_VPN, "firewall-rule $rule on conn $connInfo")
connTrack(connInfo)
if (FirewallRuleset.stall(rule)) {
stallResponse()
}
return blocked
}
/**
* Checks if incoming connection is blocked by any user-set firewall rule
*/
private fun firewall(connInfo: IPDetails): FirewallRuleset {
try {
val uid = connInfo.uid
val appStatus = FirewallManager.appStatus(uid)
if (allowOrbot(uid)) {
return FirewallRuleset.RULE9B
}
if (unknownAppBlocked(uid)) {
return FirewallRuleset.RULE5
}
// if the app is new (ie unknown), refresh the db
if (FirewallManager.AppStatus.UNTRACKED == appStatus) {
io("dbRefresh") {
refreshDatabase.handleNewlyConnectedApp(uid)
}
if (newAppBlocked(uid)) {
return FirewallRuleset.RULE1B
}
}
if (FirewallManager.AppStatus.WHITELISTED == appStatus) {
return if (dnsProxied(connInfo.destPort)) {
FirewallRuleset.RULE9
} else {
FirewallRuleset.RULE8
}
}
if (ipBlocked(connInfo.destIP, connInfo.destPort, connInfo.protocol)) {
return FirewallRuleset.RULE2
}
if (FirewallManager.AppStatus.BLOCKED == appStatus) {
if (persistentState.killAppOnFirewall) {
killFirewalledApplication(uid)
}
return FirewallRuleset.RULE1
}
if (deviceLocked()) {
return FirewallRuleset.RULE3
}
if (udpBlocked(uid, connInfo.protocol, connInfo.destPort)) {
return FirewallRuleset.RULE6
}
if (blockBackgroundData(uid)) {
return FirewallRuleset.RULE4
}
// if all packets on port 53 needs to be trapped
if (dnsProxied(connInfo.destPort)) {
return FirewallRuleset.RULE9
}
// whether the destination ip was resolved by the dns set by the user
if (dnsBypassed(connInfo.destIP)) {
return FirewallRuleset.RULE7
}
} catch (iex: Exception) {
// TODO: show alerts to user on such exceptions, in a separate ui?
Log.e(LOG_TAG_VPN, "err blocking conn, block anyway", iex)
return FirewallRuleset.RULE1C
}
return FirewallRuleset.RULE0
}
private fun allowOrbot(uid: Int): Boolean {
return settingUpOrbot.get() && OrbotHelper.ORBOT_PACKAGE_NAME == FirewallManager.getPackageNameByUid(
uid)
}
private fun dnsProxied(port: Int): Boolean {
return (appConfig.getBraveMode().isDnsFirewallMode() && persistentState.preventDnsLeaks && isDns(
port))
}
private fun dnsBypassed(destIp: String): Boolean {
return if (!persistentState.disallowDnsBypass || !appConfig.canEnableDnsBypassFirewallSetting()) {
false
} else {
unresolvedIp(destIp)
}
}
private fun waitAndCheckIfUidBlocked(uid: Int): Boolean {
val allowed = testWithBackoff {
FirewallManager.hasUid(uid) && !FirewallManager.isUidFirewalled(uid)
}
return !allowed
}
private fun newAppBlocked(uid: Int): Boolean {
return if (!persistentState.blockNewlyInstalledApp || isMissingOrInvalidUid(uid)) {
false
} else {
waitAndCheckIfUidBlocked(uid)
}
}
private fun ipBlocked(destIp: String, destPort: Int, protocol: Int): Boolean {
val connectionRules = ConnectionRules(destIp, destPort,
Protocol.getProtocolName(protocol).name)
return FirewallRules.hasRule(UID_EVERYBODY, connectionRules)
}
private fun unknownAppBlocked(uid: Int): Boolean {
return if (!persistentState.blockUnknownConnections) {
false
} else {
isMissingOrInvalidUid(uid)
}
}
private fun testWithBackoff(stallSec: Long = 20, durationSec: Long = 10,
test: () -> Boolean): Boolean {
val minWaitMs = TimeUnit.SECONDS.toMillis(stallSec)
var remainingWaitMs = TimeUnit.SECONDS.toMillis(durationSec)
var attempt = 0
while (remainingWaitMs > 0) {
if (test()) return true
remainingWaitMs = exponentialBackoff(remainingWaitMs, attempt)
attempt += 1
}
Thread.sleep(minWaitMs + remainingWaitMs)
return false
}
private fun unresolvedIp(ip: String): Boolean {
val resolvedIp = testWithBackoff {
val now = System.currentTimeMillis()
val ttl = dnsLogTracker.ipDomainLookup.getIfPresent(ip)?.ttl ?: Long.MIN_VALUE
ttl >= now
}
return !resolvedIp
}
private fun udpBlocked(uid: Int, protocol: Int, port: Int): Boolean {
val hasUserBlockedUdp = persistentState.udpBlockedSettings
if (!hasUserBlockedUdp) return false
val isUdp = protocol == Protocol.UDP.protocolType
if (!isUdp) return false
val isNtpFromSystemApp = KnownPorts.isNtp(port) && FirewallManager.isUidSystemApp(uid)
if (isNtpFromSystemApp) return false
return true
}
private fun isVpnDns(ip: String): Boolean {
return ip == fakeDnsIp
}
private fun isDns(port: Int): Boolean {
return KnownPorts.isDns(port)
}
// Modified the logic of "Block connections when screen is locked".
// Earlier the screen lock is detected with receiver received for Action user_present/screen_off.
// Now the code only checks whether the KeyguardManager#isKeyguardLocked() is true/false.
// if isKeyguardLocked() is true, the connections will be blocked.
private fun deviceLocked(): Boolean {
if (!persistentState.blockWhenDeviceLocked) return false
if (keyguardManager == null) {
keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
}
return (keyguardManager?.isKeyguardLocked == true)
}
private fun blockBackgroundData(uid: Int): Boolean {
if (!persistentState.blockAppWhenBackground) return false
if (!accessibilityServiceFunctional()) {
Log.w(LOG_TAG_VPN, "accessibility service not functional, disable bg-block")
handleAccessibilityFailure()
return false
}
val allowed = testWithBackoff {
FirewallManager.isAppForeground(uid, keyguardManager)
}
return !allowed
}
private fun handleAccessibilityFailure() {
// Disable app not in use behaviour when the accessibility failure is detected.
persistentState.blockAppWhenBackground = false
showAccessibilityStoppedNotification()
}
private fun showAccessibilityStoppedNotification() {
Log.i(LOG_TAG_VPN, "app not in use failure, show notification")
val intent = Intent(this, NotificationHandlerDialog::class.java)
intent.putExtra(NOTIF_INTENT_EXTRA_ACCESSIBILITY_NAME,
NOTIF_INTENT_EXTRA_ACCESSIBILITY_VALUE)
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
var builder: NotificationCompat.Builder
if (isAtleastO()) {
val name: CharSequence = getString(R.string.notif_channel_firewall_alerts)
val description = this.resources.getString(R.string.notif_channel_desc_firewall_alerts)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(NOTIF_CHANNEL_ID_FIREWALL_ALERTS, name, importance)
channel.description = description
notificationManager.createNotificationChannel(channel)
builder = NotificationCompat.Builder(this, NOTIF_CHANNEL_ID_FIREWALL_ALERTS)
} else {
builder = NotificationCompat.Builder(this, NOTIF_CHANNEL_ID_FIREWALL_ALERTS)
}
val contentTitle: String = this.resources.getString(
R.string.accessibility_notification_title)
val contentText: String = this.resources.getString(
R.string.accessibility_notification_content)
builder.setSmallIcon(R.drawable.dns_icon).setContentTitle(contentTitle).setContentIntent(
pendingIntent).setContentText(contentText)
builder.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
builder.color = ContextCompat.getColor(this, getThemeAccent(this))
// Secret notifications are not shown on the lock screen. No need for this app to show there.
// Only available in API >= 21
builder = builder.setVisibility(NotificationCompat.VISIBILITY_SECRET)
// Cancel the notification after clicking.
builder.setAutoCancel(true)
notificationManager.notify(NOTIF_CHANNEL_ID_FIREWALL_ALERTS, NOTIF_ID_ACCESSIBILITY_FAILURE,
builder.build())
}
private fun accessibilityServiceFunctional(): Boolean {
val now = elapsedRealtime()
// Added the INIT_TIME_MS check, encountered a bug during phone restart
// isAccessibilityServiceRunning default value(false) is passed instead of
// checking it from accessibility service for the first time.
if (accessibilityHearbeatTimestamp == INIT_TIME_MS || Math.abs(
now - accessibilityHearbeatTimestamp) > Constants.ACCESSIBILITY_SERVICE_HEARTBEAT_THRESHOLD_MS) {
accessibilityHearbeatTimestamp = now
isAccessibilityServiceFunctional = Utilities.isAccessibilityServiceEnabled(this,
BackgroundAccessibilityService::class.java) && Utilities.isAccessibilityServiceEnabledViaSettingsSecure(
this, BackgroundAccessibilityService::class.java)
}
return isAccessibilityServiceFunctional
}
private fun notifyEmptyFirewallRules() {
val intent = Intent(this, HomeScreenActivity::class.java)
val mainActivityIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
if (isAtleastO()) {
val name: CharSequence = getString(R.string.notif_channel_firewall_alerts)
val description = resources.getString(R.string.notif_channel_desc_firewall_alerts)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(FirewallManager.NOTIF_CHANNEL_ID_FIREWALL_ALERTS,
name, importance)
channel.description = description
notificationManager.createNotificationChannel(channel)
}
var builder: NotificationCompat.Builder = NotificationCompat.Builder(this,
FirewallManager.NOTIF_CHANNEL_ID_FIREWALL_ALERTS)
val contentTitle = resources.getString(R.string.rules_load_failure_heading)
val contentText = resources.getString(R.string.rules_load_failure_desc)
builder.setSmallIcon(R.drawable.dns_icon).setContentTitle(contentTitle).setContentIntent(
mainActivityIntent).setContentText(contentText)
builder.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
builder.color = ContextCompat.getColor(this, getThemeAccent(this))
val openIntent = makeVpnIntent(NOTIF_ID_LOAD_RULES_FAIL,
Constants.NOTIF_ACTION_RULES_FAILURE)
val notificationAction: NotificationCompat.Action = NotificationCompat.Action(0,
resources.getString(
R.string.rules_load_failure_reload),
openIntent)
builder.addAction(notificationAction)
// Secret notifications are not shown on the lock screen. No need for this app to show there.
// Only available in API >= 21
builder = builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
builder.build()
notificationManager.notify(NOTIF_CHANNEL_ID_FIREWALL_ALERTS, NOTIF_ID_LOAD_RULES_FAIL,
builder.build())
}
private fun stallResponse() {
Thread.sleep(DELAY_FIREWALL_RESPONSE_MS)
}
// ref: https://stackoverflow.com/a/363692
private val baseWaitMs = TimeUnit.MILLISECONDS.toMillis(50)
private fun exponentialBackoff(remainingWaitMs: Long, attempt: Int): Long {
var tempRemainingWaitMs = remainingWaitMs
val exponent = exp(attempt)
val randomValue = rand.nextLong(exponent - baseWaitMs + 1) + baseWaitMs
val waitTimeMs = min(randomValue, remainingWaitMs)
tempRemainingWaitMs -= waitTimeMs
Thread.sleep(waitTimeMs)
return tempRemainingWaitMs
}
private fun exp(pow: Int): Long {
return if (pow == 0) {
baseWaitMs
} else {
(1 shl pow) * baseWaitMs
}
}
private fun killFirewalledApplication(uid: Int) {
if (DEBUG) Log.i(LOG_TAG_VPN,
"Firewalled application trying to connect - Kill app is enabled - uid - $uid")
io("killBgApps") {
val appUIDList = FirewallManager.getNonSystemAppsPackageNameByUid(uid)
appUIDList.forEach {
if (DEBUG) Log.d(LOG_TAG_VPN, "app $uid / $it killed")
Utilities.killBg(activityManager, it)
}
}
}
/**
* Records the network transaction in local database
* The logs will be shown in network monitor screen
*/
private fun connTrack(info: IPDetails?) {
ipTracker.recordTransaction(info)
}
override fun getResolvers(): String {
// TODO: remove stub, unused
throw UnsupportedOperationException("stub method")
}
private suspend fun newBuilder(): Builder {
var builder = Builder()
if (!VpnController.isVpnLockdown() && persistentState.allowBypass) {
Log.i(LOG_TAG_VPN, "allow apps to bypass vpn on-demand")
builder = builder.allowBypass()
}
// underlying networks is set to null, which prompts Android to set it to whatever is the
// current active network. Later, ConnectionMonitor#onVpnStarted, depending on user
// chosen preferences, sets appropriate underlying network/s.
builder.setUnderlyingNetworks(null)
// Fix - Cloud Backups were failing thinking that the VPN connection is metered.
// The below code will fix that.
if (isAtleastQ()) {
builder.setMetered(false)
}
// re-hydrate exclude-apps incase it has changed in the interim
excludedApps = FirewallManager.getExcludedApps()
if (isAtleastQ()) {
if (appConfig.canEnableProxy() && appConfig.isCustomHttpProxyEnabled()) {
getHttpProxyInfo()?.let { builder.setHttpProxy(it) }
}
}
try {
if (!VpnController.isVpnLockdown() && isAppPaused()) {
val nonFirewalledApps = FirewallManager.getNonFirewalledAppsPackageNames()
Log.i(LOG_TAG_VPN,
"app is in pause state, exclude all the non firewalled apps, size: ${nonFirewalledApps.count()}")
nonFirewalledApps.forEach {
builder.addDisallowedApplication(it.packageInfo)
}
builder = builder.addDisallowedApplication(this.packageName)
return builder
}
if (appConfig.getFirewallMode().isFirewallSinkMode()) {
for (packageName in excludedApps) {
builder = builder.addAllowedApplication(packageName)
}
} else {
// ignore excluded-apps settings when vpn is lockdown because
// those apps would lose all internet connectivity, otherwise
if (!VpnController.isVpnLockdown()) {
excludedApps.forEach {
builder = builder.addDisallowedApplication(it)
Log.i(LOG_TAG_VPN, "Excluded package - $it")
}
} else {
Log.w(LOG_TAG_VPN, "vpn is lockdown, ignoring exclude-apps list")
}
builder = builder.addDisallowedApplication(this.packageName)
}
if (appConfig.isCustomSocks5Enabled()) {
// For Socks5 if there is a app selected, add that app in excluded list
val socks5ProxyEndpoint = appConfig.getConnectedSocks5Proxy()
val appName = socks5ProxyEndpoint?.proxyAppName
Log.i(LOG_TAG_VPN,
"Proxy mode - Socks5 is selected - $socks5ProxyEndpoint, with app name - $appName")
if (appName?.equals(getString(
R.string.settings_app_list_default_app)) == false && isExcludePossible(
appName, getString(R.string.socks5_proxy_toast_parameter))) {
builder = builder.addDisallowedApplication(appName)
}
}
if (appConfig.isOrbotProxyEnabled() && isExcludePossible(getString(R.string.orbot),
getString(
R.string.orbot_toast_parameter))) {
builder = builder.addDisallowedApplication(OrbotHelper.ORBOT_PACKAGE_NAME)
}
if (appConfig.isDnsProxyActive()) {
// For DNS proxy mode, if any app is set then exclude the application from the list
val dnsProxyEndpoint = appConfig.getConnectedProxyDetails()
val appName = dnsProxyEndpoint.proxyAppName
Log.i(LOG_TAG_VPN, "DNS Proxy mode is set with the app name as $appName")
if (appName?.equals(getString(
R.string.settings_app_list_default_app)) == false && isExcludePossible(
appName, getString(R.string.dns_proxy_toast_parameter))) {
builder = builder.addDisallowedApplication(appName)
}
}
} catch (e: PackageManager.NameNotFoundException) {
Log.w(LOG_TAG_VPN, "cannot exclude the dns proxy app", e)
}
return builder
}
private fun isExcludePossible(appName: String?, message: String): Boolean {
if (!VpnController.isVpnLockdown()) return (appName?.equals(
getString(R.string.settings_app_list_default_app)) == false)
ui {
showToastUiCentered(this,
getString(R.string.dns_proxy_connection_failure_lockdown, appName,
message), Toast.LENGTH_SHORT)
}
return false
}
override fun onCreate() {
connTracer = ConnectionTracer(this)
VpnController.onVpnCreated(this)
notificationManager = this.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
activityManager = this.getSystemService(ACTIVITY_SERVICE) as ActivityManager
accessibilityManager = this.getSystemService(ACCESSIBILITY_SERVICE) as AccessibilityManager
keyguardManager = this.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (persistentState.blockAppWhenBackground) {
registerAccessibilityServiceState()
}
observeChanges()
}
private fun observeChanges() {
appInfoObserver = makeAppInfoObserver()
FirewallManager.getApplistObserver().observeForever(appInfoObserver)
persistentState.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
orbotStartStatusObserver = makeOrbotStartStatusObserver()
persistentState.orbotConnectionStatus.observeForever(orbotStartStatusObserver)
Log.i(LOG_TAG_VPN, "observe pref and app list changes")
}
private fun makeAppInfoObserver(): Observer<Collection<AppInfo>> {
return Observer<Collection<AppInfo>> { t ->
val latestExcludedApps = t.filter(AppInfo::isExcluded).map(AppInfo::packageInfo).toSet()
if (Sets.symmetricDifference(excludedApps,
latestExcludedApps).count() == 0) return@Observer
Log.i(LOG_TAG_VPN, "excluded-apps list changed, restart vpn")
io("excludeApps") {
restartVpn(appConfig.newTunnelOptions(this, GoIntraListener, fakeDns))
}
}
}
private fun makeOrbotStartStatusObserver(): Observer<Boolean> {
return Observer<Boolean> {
settingUpOrbot.set(it)
}
}
private fun getHttpProxyInfo(): ProxyInfo? {
val proxyInfo: ProxyInfo?
val host = persistentState.httpProxyHostAddress
val port = persistentState.httpProxyPort
Log.i(LOG_TAG_VPN, "Http proxy enabled - builder updated with $host, $port")
if (host.isNotEmpty() && port != INVALID_PORT) {
proxyInfo = ProxyInfo.buildDirectProxy(host, port)
return proxyInfo
}
return null
}
private fun updateNotificationBuilder(): NotificationCompat.Builder {
val mainActivityIntent = PendingIntent.getActivity(this, 0, Intent(this,
HomeScreenActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT)
var builder: NotificationCompat.Builder
if (isAtleastO()) {
val name: CharSequence = resources.getString(R.string.notif_channel_vpn_notification)
// LOW is the lowest importance that is allowed with startForeground in Android O
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(MAIN_CHANNEL_ID, name, importance)
channel.description = resources.getString(R.string.notif_channel_desc_vpn_notification)
notificationManager.createNotificationChannel(channel)
builder = NotificationCompat.Builder(this, MAIN_CHANNEL_ID)
} else {
builder = NotificationCompat.Builder(this, MAIN_CHANNEL_ID)
}
var contentTitle: String = when (appConfig.getBraveMode()) {
AppConfig.BraveMode.DNS -> resources.getString(R.string.dns_mode_notification_title)
AppConfig.BraveMode.FIREWALL -> resources.getString(
R.string.firewall_mode_notification_title)
AppConfig.BraveMode.DNS_FIREWALL -> resources.getString(
R.string.hybrid_mode_notification_title)
}
if (isAppPaused()) {
contentTitle = resources.getString(R.string.pause_mode_notification_title)
}
builder.setSmallIcon(R.drawable.dns_icon).setContentTitle(contentTitle).setContentIntent(
mainActivityIntent)
// New action button options in the notification
// 1. Pause / Resume, Stop action button.
// 2. RethinkDNS modes (dns & dns+firewall mode)
// 3. No action button.
if (DEBUG) Log.d(LOG_TAG_VPN,
"notification action type: ${persistentState.notificationActionType}")
builder.color = ContextCompat.getColor(this, getThemeAccent(this))
when (NotificationActionType.getNotificationActionType(
persistentState.notificationActionType)) {
NotificationActionType.PAUSE_STOP -> {
// Add the action based on AppState (PAUSE/ACTIVE)
val openIntent1 = makeVpnIntent(NOTIF_ACTION_MODE_STOP,
Constants.NOTIF_ACTION_STOP_VPN)
val notificationAction1 = NotificationCompat.Action(0, resources.getString(
R.string.notification_action_stop_vpn), openIntent1)
builder.addAction(notificationAction1)
if (isAppPaused()) {
val openIntent2 = makeVpnIntent(NOTIF_ACTION_MODE_RESUME,
Constants.NOTIF_ACTION_RESUME_VPN)
val notificationAction2 = NotificationCompat.Action(0, resources.getString(
R.string.notification_action_resume_vpn), openIntent2)
builder.addAction(notificationAction2)
} else {
val openIntent2 = makeVpnIntent(NOTIF_ACTION_MODE_PAUSE,
Constants.NOTIF_ACTION_PAUSE_VPN)
val notificationAction2 = NotificationCompat.Action(0, resources.getString(
R.string.notification_action_pause_vpn), openIntent2)
builder.addAction(notificationAction2)
}
}
NotificationActionType.DNS_FIREWALL -> {
val openIntent1 = makeVpnIntent(NOTIF_ACTION_MODE_DNS_ONLY,
Constants.NOTIF_ACTION_DNS_VPN)
val openIntent2 = makeVpnIntent(NOTIF_ACTION_MODE_DNS_FIREWALL,
Constants.NOTIF_ACTION_DNS_FIREWALL_VPN)
val notificationAction: NotificationCompat.Action = NotificationCompat.Action(0,
resources.getString(
R.string.notification_action_dns_mode),
openIntent1)
val notificationAction2: NotificationCompat.Action = NotificationCompat.Action(0,
resources.getString(
R.string.notification_action_dns_firewall_mode),
openIntent2)
builder.addAction(notificationAction)
builder.addAction(notificationAction2)
}
NotificationActionType.NONE -> {
Log.i(LOG_TAG_VPN, "No notification action")
}
}
// Secret notifications are not shown on the lock screen. No need for this app to show there.
// Only available in API >= 21
builder = builder.setVisibility(NotificationCompat.VISIBILITY_SECRET)
builder.build()
return builder
}
private fun makeVpnIntent(notificationID: Int, intentExtra: String): PendingIntent? {
val intentAction = Intent(this, NotificationActionReceiver::class.java)
intentAction.putExtra(Constants.NOTIFICATION_ACTION, intentExtra)
return PendingIntent.getBroadcast(this, notificationID, intentAction,
PendingIntent.FLAG_UPDATE_CURRENT)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
VpnController.onConnectionStateChanged(State.NEW)
ui {
var isNewVpn = true
// Initialize the value whenever the vpn is started.
accessibilityHearbeatTimestamp = INIT_TIME_MS
startOrbotAsyncIfNeeded()
VpnController.mutex.withLock {
// if service is up (aka connectionMonitor not null)
// then simply update the existing tunnel
if (connectionMonitor == null) {
connectionMonitor = ConnectionMonitor(this, this)
} else {
isNewVpn = false
}
}
val opts = appConfig.newTunnelOptions(this, GoIntraListener, fakeDns)
Log.i(LOG_TAG_VPN, "start-foreground with opts $opts (for new-vpn? $isNewVpn)")
if (!isNewVpn) {
io("tunUpdate") {
updateTun(opts)
}
} else {
io("startVpn") {
FirewallManager.loadAppFirewallRules()
if (FirewallManager.getTotalApps() <= 0) {
notifyEmptyFirewallRules()
}
restartVpn(opts)
}
startForeground(SERVICE_ID, updateNotificationBuilder().build())
persistentState.setVpnEnabled(true)
updateQuickSettingsTile()
connectionMonitor?.onVpnStart()
}
}
return Service.START_REDELIVER_INTENT
}
private fun startOrbotAsyncIfNeeded() {
if (!appConfig.isOrbotProxyEnabled()) return
io("startOrbot") {
orbotHelper.startOrbot(appConfig.getProxyType())
}
}
private fun unobserveAppInfos() {
FirewallManager.getApplistObserver().removeObserver(appInfoObserver)
}
private fun unobserveOrbotStartStatus() {
persistentState.orbotConnectionStatus.removeObserver(orbotStartStatusObserver)
}
private fun registerAccessibilityServiceState() {
accessibilityListener = AccessibilityManager.AccessibilityStateChangeListener { b ->
if (!b) {
handleAccessibilityFailure()
}
}
}
private fun unregisterAccessibilityServiceState() {
accessibilityListener?.let {
accessibilityManager.removeAccessibilityStateChangeListener(it)
}
}
private suspend fun updateTun(tunnelOptions: AppConfig.TunnelOptions) {
VpnController.mutex.withLock {
// Connection monitor can be null if onDestroy() of service
// is called, in that case no need to call updateServerConnection()
if (connectionMonitor == null) {
Log.w(LOG_TAG_VPN, "skip update-tun, connection-monitor missing")
return@withLock
}
vpnAdapter?.updateTun(tunnelOptions)
handleVpnAdapterChange()
}
}
private suspend fun updateDnscrypt(tunnelOptions: AppConfig.TunnelOptions) {
VpnController.mutex.withLock {
if (connectionMonitor == null) return@withLock
vpnAdapter?.setDnscryptMode(tunnelOptions)
}
}
override fun onSharedPreferenceChanged(preferences: SharedPreferences?, key: String?) {
/* TODO Check on the Persistent State variable
Check on updating the values for Package change and for mode change.
As of now handled manually */
val opts = appConfig.newTunnelOptions(this, GoIntraListener, fakeDns)
Log.i(LOG_TAG_VPN, "onPrefChange key: $key, opts: $opts")
when (key) {
PersistentState.BRAVE_MODE -> {
io("braveModeChange") {
restartVpn(opts)
}
notificationManager.notify(SERVICE_ID, updateNotificationBuilder().build())
}
PersistentState.LOCAL_BLOCK_LIST -> {
io("localBlocklist") {
updateTun(opts)
}
}
PersistentState.BACKGROUND_MODE -> {
if (persistentState.blockAppWhenBackground) {
registerAccessibilityServiceState()
} else {
unregisterAccessibilityServiceState()
}
}
PersistentState.LOCAL_BLOCK_LIST_STAMP -> { // update tunnel on local blocklist stamp change
spawnLocalBlocklistStampUpdate()
}
PersistentState.REMOTE_BLOCK_LIST_STAMP -> { // update tunnel on remote blocklist stamp change.
io("remoteBlocklist") {
updateTun(opts)
}
}
PersistentState.DNS_CHANGE -> {
/*
* Handles the DNS type changes.
* DNS Proxy - Requires restart of the VPN.
* DNSCrypt - Set the tunnel with DNSCrypt mode once the live servers size is not 0.
* DOH - Overwrites the tunnel values with new values.
*/
io("dnsChange") {
when (appConfig.getDnsType()) {
AppConfig.DnsType.DOH -> {
updateTun(opts)
}
AppConfig.DnsType.DNSCRYPT -> {
updateDnscrypt(opts)
}
AppConfig.DnsType.DNS_PROXY -> {
updateDnsProxy(opts)
}
}
}
}
PersistentState.DNS_RELAYS -> {
io("updateDnscrypt") {
updateDnscrypt(opts)
}
}
PersistentState.ALLOW_BYPASS -> {
io("allowBypass") {
restartVpn(opts)
}
}
PersistentState.PROXY_TYPE -> {
io("proxy") {
restartVpn(opts)
}
}
PersistentState.NETWORK -> {
connectionMonitor?.onUserPreferenceChanged()
}
PersistentState.NOTIFICATION_ACTION -> {
notificationManager.notify(SERVICE_ID, updateNotificationBuilder().build())