-
Notifications
You must be signed in to change notification settings - Fork 85
/
VPNRegion.swift
137 lines (128 loc) · 6 KB
/
VPNRegion.swift
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
//
// VPNRegion.swift
// LockdowniOS
//
// Created by Oleg Dreyman on 28.09.2020.
// Copyright © 2020 Confirmed Inc. All rights reserved.
//
import Foundation
// MARK: - VPN Region
let kSavedVPNRegionServerPrefix = "vpn_region_server_prefix"
struct VPNRegion {
var regionDisplayName: String = ""
var regionDisplayNameShort: String = ""
var regionFlagEmoji: String = ""
var serverPrefix: String = ""
}
let vpnRegions:[VPNRegion] = [
VPNRegion(regionDisplayName: NSLocalizedString("United States - West", comment: ""),
regionDisplayNameShort: NSLocalizedString("USA West", comment: ""),
regionFlagEmoji: "🇺🇸",
serverPrefix: "us-west"),
VPNRegion(regionDisplayName: NSLocalizedString("United States - East", comment: ""),
regionDisplayNameShort: NSLocalizedString("USA East", comment: ""),
regionFlagEmoji: "🇺🇸",
serverPrefix: "us-east"),
VPNRegion(regionDisplayName: NSLocalizedString("United Kingdom", comment: ""),
regionDisplayNameShort: NSLocalizedString("United Kingdom", comment: ""),
regionFlagEmoji: "🇬🇧",
serverPrefix: "eu-london"),
VPNRegion(regionDisplayName: NSLocalizedString("France", comment: ""),
regionDisplayNameShort: NSLocalizedString("France", comment: ""),
regionFlagEmoji: "🇫🇷",
serverPrefix: "eu-paris"),
VPNRegion(regionDisplayName: NSLocalizedString("Ireland", comment: ""),
regionDisplayNameShort: NSLocalizedString("Ireland", comment: ""),
regionFlagEmoji: "🇮🇪",
serverPrefix: "eu-ireland"),
VPNRegion(regionDisplayName: NSLocalizedString("Germany", comment: ""),
regionDisplayNameShort: NSLocalizedString("Germany", comment: ""),
regionFlagEmoji: "🇩🇪",
serverPrefix: "eu-frankfurt"),
VPNRegion(regionDisplayName: NSLocalizedString("Canada", comment: ""),
regionDisplayNameShort: NSLocalizedString("Canada", comment: ""),
regionFlagEmoji: "🇨🇦",
serverPrefix: "canada"),
VPNRegion(regionDisplayName: NSLocalizedString("India", comment: ""),
regionDisplayNameShort: NSLocalizedString("India", comment: ""),
regionFlagEmoji: "🇮🇳",
serverPrefix: "ap-mumbai"),
VPNRegion(regionDisplayName: NSLocalizedString("Japan", comment: ""),
regionDisplayNameShort: NSLocalizedString("Japan", comment: ""),
regionFlagEmoji: "🇯🇵",
serverPrefix: "ap-tokyo"),
VPNRegion(regionDisplayName: NSLocalizedString("Australia", comment: ""),
regionDisplayNameShort: NSLocalizedString("Australia", comment: ""),
regionFlagEmoji: "🇦🇺",
serverPrefix: "ap-sydney"),
VPNRegion(regionDisplayName: NSLocalizedString("South Korea", comment: ""),
regionDisplayNameShort: NSLocalizedString("South Korea", comment: ""),
regionFlagEmoji: "🇰🇷",
serverPrefix: "ap-seoul"),
VPNRegion(regionDisplayName: NSLocalizedString("Singapore", comment: ""),
regionDisplayNameShort: NSLocalizedString("Singapore", comment: ""),
regionFlagEmoji: "🇸🇬",
serverPrefix: "ap-singapore"),
VPNRegion(regionDisplayName: NSLocalizedString("Brazil", comment: ""),
regionDisplayNameShort: NSLocalizedString("Brazil", comment: ""),
regionFlagEmoji: "🇧🇷",
serverPrefix: "sa")
]
func getVPNRegionForServerPrefix(serverPrefix: String) -> VPNRegion {
for vpnRegion in vpnRegions {
if vpnRegion.serverPrefix == serverPrefix {
return vpnRegion
}
}
return vpnRegions[0]
}
func getSavedVPNRegion() -> VPNRegion {
if let savedVPNRegionServerPrefix = defaults.string(forKey: kSavedVPNRegionServerPrefix) {
return getVPNRegionForServerPrefix(serverPrefix: savedVPNRegionServerPrefix)
}
// get default savedRegion by locale
let locale = NSLocale.autoupdatingCurrent
if let regionCode = locale.regionCode {
switch regionCode {
case "US":
if let timezone = TimeZone.autoupdatingCurrent.abbreviation() {
if timezone == "EST" || timezone == "EDT" || timezone == "CST" {
return getVPNRegionForServerPrefix(serverPrefix: "us-east")
}
}
else {
return getVPNRegionForServerPrefix(serverPrefix: "us-west")
}
case "FR", "PT":
return getVPNRegionForServerPrefix(serverPrefix: "eu-paris")
case "GB":
return getVPNRegionForServerPrefix(serverPrefix: "eu-london")
case "IE":
return getVPNRegionForServerPrefix(serverPrefix: "eu-london")
case "CA":
return getVPNRegionForServerPrefix(serverPrefix: "canada")
case "KO":
return getVPNRegionForServerPrefix(serverPrefix: "ap-seoul")
case "ID", "SG", "MY", "PH", "TH", "TW", "VN":
return getVPNRegionForServerPrefix(serverPrefix: "ap-singapore")
case "DE", "IT", "ES", "AT", "PL", "RU", "UA", "NG", "TR", "ZA":
return getVPNRegionForServerPrefix(serverPrefix: "eu-frankfurt")
case "AU", "NZ":
return getVPNRegionForServerPrefix(serverPrefix: "ap-sydney")
case "AE", "IN", "PK", "BD", "QA", "SA":
return getVPNRegionForServerPrefix(serverPrefix: "ap-mumbai")
case "EG":
return getVPNRegionForServerPrefix(serverPrefix: "eu-frankfurt")
case "JP":
return getVPNRegionForServerPrefix(serverPrefix: "ap-tokyo")
case "BR", "CO", "VE", "AR":
return getVPNRegionForServerPrefix(serverPrefix: "sa")
default:
return vpnRegions[0]
}
}
return vpnRegions[0]
}
func setSavedVPNRegion(vpnRegion: VPNRegion) {
defaults.set(vpnRegion.serverPrefix, forKey: kSavedVPNRegionServerPrefix)
}