Skip to content

Commit

Permalink
Add new env selector
Browse files Browse the repository at this point in the history
  • Loading branch information
rokas-ambrazevicius committed Nov 12, 2024
1 parent 56096f1 commit 2106e0d
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 152 deletions.
Empty file removed nym-vpn-apple/Envs/.gitkeep
Empty file.
24 changes: 0 additions & 24 deletions nym-vpn-apple/Envs/qa.env

This file was deleted.

6 changes: 3 additions & 3 deletions nym-vpn-apple/NymMixnetTunnel/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
override func startTunnel(options: [String: NSObject]? = nil) async throws {
logger.info("Start tunnel...")

setup()
await setup()

await tunnelActor.setTunnelProvider(self)

Expand Down Expand Up @@ -93,9 +93,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}

extension PacketTunnelProvider {
func setup() {
func setup() async {
do {
try ConfigurationManager.shared.setup()
try await ConfigurationManager.shared.setup()
} catch {
self.logger.error("Failed to set environment: \(error)")
}
Expand Down
40 changes: 0 additions & 40 deletions nym-vpn-apple/NymVPN.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
582C30882C80B8E7008CF551 /* canary.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12C2C8077B600B319B5 /* canary.env */; };
582C30892C80B8E7008CF551 /* mainnet.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12E2C807B8E00B319B5 /* mainnet.env */; };
582C308A2C80B8E7008CF551 /* sandbox.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12F2C807B8E00B319B5 /* sandbox.env */; };
582C308B2C80B8EC008CF551 /* canary.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12C2C8077B600B319B5 /* canary.env */; };
582C308C2C80B8EC008CF551 /* mainnet.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12E2C807B8E00B319B5 /* mainnet.env */; };
582C308D2C80B8EC008CF551 /* sandbox.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12F2C807B8E00B319B5 /* sandbox.env */; };
582C5F8E2C7654A800B678AE /* ConfigurationManager in Frameworks */ = {isa = PBXBuildFile; productRef = 582C5F8D2C7654A800B678AE /* ConfigurationManager */; };
5854E81E2C80BB5A005EE1F4 /* canary.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12C2C8077B600B319B5 /* canary.env */; };
5854E81F2C80BB5A005EE1F4 /* mainnet.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12E2C807B8E00B319B5 /* mainnet.env */; };
5854E8202C80BB5A005EE1F4 /* sandbox.env in Resources */ = {isa = PBXBuildFile; fileRef = D94CC12F2C807B8E00B319B5 /* sandbox.env */; };
5879C2062C80A37200BAB142 /* TunnelSettingsConversions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5879C2052C80A37200BAB142 /* TunnelSettingsConversions.swift */; };
D9144C5B2CC1057A0021A617 /* CountriesManagerTypes in Frameworks */ = {isa = PBXBuildFile; productRef = D9144C5A2CC1057A0021A617 /* CountriesManagerTypes */; };
D9144C5D2CC1057F0021A617 /* CountriesManagerTypes in Frameworks */ = {isa = PBXBuildFile; productRef = D9144C5C2CC1057F0021A617 /* CountriesManagerTypes */; };
Expand Down Expand Up @@ -61,9 +52,6 @@
D99820982BB6E8D9000C1FEE /* MixnetLibrary in Frameworks */ = {isa = PBXBuildFile; productRef = D99820972BB6E8D9000C1FEE /* MixnetLibrary */; };
D99A147A2B357E9900F2728B /* NymVPNApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D99A14792B357E9900F2728B /* NymVPNApp.swift */; };
D99A148C2B35A74200F2728B /* Theme in Frameworks */ = {isa = PBXBuildFile; productRef = D99A148B2B35A74200F2728B /* Theme */; };
D99B70D72CC7E42800BBC105 /* qa.env in Resources */ = {isa = PBXBuildFile; fileRef = D99B70D62CC7E42800BBC105 /* qa.env */; };
D99B70D82CC7E42800BBC105 /* qa.env in Resources */ = {isa = PBXBuildFile; fileRef = D99B70D62CC7E42800BBC105 /* qa.env */; };
D99B70D92CC7E42800BBC105 /* qa.env in Resources */ = {isa = PBXBuildFile; fileRef = D99B70D62CC7E42800BBC105 /* qa.env */; };
D9ABF2842BF4989B009089FA /* Shell in Frameworks */ = {isa = PBXBuildFile; productRef = D9ABF2832BF4989B009089FA /* Shell */; };
D9AC152E2BF3845E0041E334 /* net.nymtech.vpn.helper in Embed Helper Tool */ = {isa = PBXBuildFile; fileRef = D9AC152C2BF384580041E334 /* net.nymtech.vpn.helper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
D9B7BAF82C4866A0005AF2D6 /* NymLogger in Frameworks */ = {isa = PBXBuildFile; productRef = D9B7BAF72C4866A0005AF2D6 /* NymLogger */; };
Expand Down Expand Up @@ -164,9 +152,6 @@
D9264E252B3DA062004594A8 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D9281C462B8884D400834B53 /* NymVPN.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = NymVPN.entitlements; sourceTree = "<group>"; };
D94127BA2BFF2CB7009EDB7A /* Entitlements.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Entitlements.plist; sourceTree = "<group>"; };
D94CC12C2C8077B600B319B5 /* canary.env */ = {isa = PBXFileReference; lastKnownFileType = text; path = canary.env; sourceTree = "<group>"; };
D94CC12E2C807B8E00B319B5 /* mainnet.env */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = mainnet.env; sourceTree = "<group>"; };
D94CC12F2C807B8E00B319B5 /* sandbox.env */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = sandbox.env; sourceTree = "<group>"; };
D9538E2D2B7E3F28002FA8B5 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = Resources/LaunchScreen.storyboard; sourceTree = "<group>"; };
D954C0A62B51C17200C6BAAC /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = System/Library/Frameworks/NetworkExtension.framework; sourceTree = SDKROOT; };
D954C0B82B51C1CA00C6BAAC /* NymMixnetTunnel.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = NymMixnetTunnel.appex; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -180,7 +165,6 @@
D990950D2BC6B31900C98E3F /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk/System/Library/Frameworks/SystemConfiguration.framework; sourceTree = DEVELOPER_DIR; };
D99A14762B357E9900F2728B /* NymVPN.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NymVPN.app; sourceTree = BUILT_PRODUCTS_DIR; };
D99A14792B357E9900F2728B /* NymVPNApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NymVPNApp.swift; sourceTree = "<group>"; };
D99B70D62CC7E42800BBC105 /* qa.env */ = {isa = PBXFileReference; lastKnownFileType = text; path = qa.env; sourceTree = "<group>"; };
D9AC152C2BF384580041E334 /* net.nymtech.vpn.helper */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = net.nymtech.vpn.helper; sourceTree = "<group>"; };
D9B969D42CCCD0AF002EC9D1 /* PacketTunnelProvider+Messaging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PacketTunnelProvider+Messaging.swift"; sourceTree = "<group>"; };
D9BEF2D02BEB63310036C566 /* NymVPN.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NymVPN.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -304,17 +288,6 @@
path = Resources;
sourceTree = "<group>";
};
D94CC12B2C80765600B319B5 /* Envs */ = {
isa = PBXGroup;
children = (
D94CC12C2C8077B600B319B5 /* canary.env */,
D94CC12E2C807B8E00B319B5 /* mainnet.env */,
D94CC12F2C807B8E00B319B5 /* sandbox.env */,
D99B70D62CC7E42800BBC105 /* qa.env */,
);
path = Envs;
sourceTree = "<group>";
};
D954C0BA2B51C1CA00C6BAAC /* NymMixnetTunnel */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -345,7 +318,6 @@
isa = PBXGroup;
children = (
D9E9F1A12C3D51650056EB8C /* README.md */,
D94CC12B2C80765600B319B5 /* Envs */,
D9FB33F02BEE31E2009C3419 /* Daemon */,
D99A14782B357E9900F2728B /* NymVPN */,
D9BEF2D12BEB63310036C566 /* NymVPNDaemon */,
Expand Down Expand Up @@ -628,23 +600,15 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5854E81E2C80BB5A005EE1F4 /* canary.env in Resources */,
5854E81F2C80BB5A005EE1F4 /* mainnet.env in Resources */,
5854E8202C80BB5A005EE1F4 /* sandbox.env in Resources */,
D99B70D72CC7E42800BBC105 /* qa.env in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
D99A14742B357E9900F2728B /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
582C30882C80B8E7008CF551 /* canary.env in Resources */,
582C30892C80B8E7008CF551 /* mainnet.env in Resources */,
582C308A2C80B8E7008CF551 /* sandbox.env in Resources */,
D9538E2E2B7E3F28002FA8B5 /* LaunchScreen.storyboard in Resources */,
D96B94E02C2AB62D00E98B15 /* gatewaysExitCountries.json in Resources */,
D99B70D82CC7E42800BBC105 /* qa.env in Resources */,
D96B94DE2C2AB62D00E98B15 /* gatewaysEntryCountries.json in Resources */,
D9264E262B3DA062004594A8 /* Assets.xcassets in Resources */,
D9F236232C64E64E00CCC433 /* LibLicences.json in Resources */,
Expand All @@ -656,15 +620,11 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
582C308B2C80B8EC008CF551 /* canary.env in Resources */,
582C308C2C80B8EC008CF551 /* mainnet.env in Resources */,
582C308D2C80B8EC008CF551 /* sandbox.env in Resources */,
D9BEF2DA2BEB63320036C566 /* Preview Assets.xcassets in Resources */,
D98B32F22C58F682005A15A3 /* Package.resolved in Resources */,
D96B94E12C2AB62D00E98B15 /* gatewaysExitCountries.json in Resources */,
D96B94DF2C2AB62D00E98B15 /* gatewaysEntryCountries.json in Resources */,
D9BEF2D72BEB63320036C566 /* Assets.xcassets in Resources */,
D99B70D92CC7E42800BBC105 /* qa.env in Resources */,
D9F236252C650A9200CCC433 /* LibLicences.json in Resources */,
D9F75F4E2CA409320005AC82 /* vpnCountries.json in Resources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Update env vars"
scriptText = "if ! [ -f &quot;$PROJECT_DIR/Envs/canary.env&quot; ] || ! [ -f &quot;$PROJECT_DIR/Envs/sandbox.env&quot; ] || ! [ -f &quot;$PROJECT_DIR/Envs/mainnet.env&quot; ]; then&#10; sh $PROJECT_DIR/Scripts/UpdateEnvVars.sh&#10;fi&#10;">
<EnvironmentBuildable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D99A14752B357E9900F2728B"
BuildableName = "NymVPN.app"
BlueprintName = "NymVPN"
ReferencedContainer = "container:NymVPN.xcodeproj">
</BuildableReference>
</EnvironmentBuildable>
</ActionContent>
</ExecutionAction>
</PreActions>
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand Down
4 changes: 3 additions & 1 deletion nym-vpn-apple/NymVPN/NymVPNApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ private extension NymVPNApp {
LoggingSystem.bootstrap { label in
FileLogHandler(label: label, logFileManager: logFileManager)
}
try? ConfigurationManager.shared.setup()
Task {
try await ConfigurationManager.shared.setup()
}
NotificationsManager.shared.setup()
ThemeConfiguration.setup()
SentryManager.shared.setup()
Expand Down
3 changes: 2 additions & 1 deletion nym-vpn-apple/Services/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ let package = Package(
"Constants",
"Device",
.product(name: "GRPCManager", package: "ServicesMacOS", condition: .when(platforms: [.macOS])),
"NymLogger"
"NymLogger",
.product(name: "MixnetLibrary", package: "MixnetLibrary", condition: .when(platforms: [.iOS]))
],
path: "Sources/Services/ConfigurationManager"
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Foundation
import SwiftUI
import AppSettings
import Device
#if os(macOS)
#if os(iOS)
import MixnetLibrary
#elseif os(macOS)
import GRPCManager
#endif
import Constants
Expand Down Expand Up @@ -40,10 +43,6 @@ public final class ConfigurationManager {
public let isTestFlight = Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
public let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0"

public var nymVpnApiURL: URL? {
getenv("NYM_VPN_API").flatMap { URL(string: String(cString: $0)) }
}

public var environmentDidChange: (() -> Void)?

#if os(iOS)
Expand All @@ -59,72 +58,76 @@ public final class ConfigurationManager {
}
#endif

public func setup() throws {
public func setup() async throws {
let result: Bool = await Task(priority: .background) {
do {
#if os(iOS)
try setEnvVariables(for: currentEnv)
try await setEnvVariables()
return true
#elseif os(macOS)
try setDaemonEnvironmentVariables()
try setDaemonEnvironmentVariables()
return true
#endif
} catch {
return false
}
}.value
}

public func updateEnv(to env: Env) {
guard isTestFlight || Device.isMacOS,
env != currentEnv
else {
return
Task(priority: .background) {
print("change to: \(env.rawValue)")
guard isTestFlight || Device.isMacOS,
env != currentEnv
else {
return
}
Task { @MainActor in
currentEnv = env
print(currentEnv)
}

do {
try await setup()
} catch {
logger.error("Failed to set env to \(env.rawValue): \(error.localizedDescription)")
}

environmentDidChange?()
}
currentEnv = env
try? setup()
environmentDidChange?()
}
}

private extension ConfigurationManager {
func setEnvVariables(for environment: Env) throws {
do {
let envString = try contentOfEnvFile(named: environment.rawValue)
try setEnvironmentVariables(envString: envString)
logger.info("Env vars enabled for \(environment.rawValue)")
} catch {
logger.error("setEnvVariables failed: \(error.localizedDescription)")
#if os(iOS)
func setEnvVariables() async throws {
try initEnvironment(networkName: currentEnv.rawValue)
logger.info("🔥 Enabling env \(currentEnv.rawValue)")

let result = try await verifyEnvVariables()
if !result {
logger.error("Failed verifying env. Current env: \(currentEnv.rawValue)")
throw GeneralNymError.noEnv
}
}
}

private extension ConfigurationManager {
func contentOfEnvFile(named: String) throws -> String {
guard let filePath = Bundle.main.path(forResource: named, ofType: "env")
func verifyEnvVariables(retryCount: Int = 0) async throws -> Bool {
guard retryCount < 6
else {
throw GeneralNymError.noEnvFile
return false
}
return try String(contentsOfFile: filePath, encoding: .utf8)
}

func setEnvironmentVariables(envString: String) throws {
let escapeQuote = "\""
let lines = envString.split(whereSeparator: { $0.isNewline })

try lines.forEach { line in
guard !line.isEmpty else { return }

let substrings = line.split(separator: "=", maxSplits: 2)
if substrings.count == 2 {
let key = substrings[0].trimmingCharacters(in: .whitespaces)
var value = substrings[1].trimmingCharacters(in: .whitespaces)
let libEnv = try currentEnvironment()

if value.hasPrefix(escapeQuote) && value.hasSuffix(escapeQuote) {
value.removeFirst()
value.removeLast()
}

setenv(key, value, 1)
} else {
throw ParseEnvironmentFileError(kind: .invalidValue, source: String(line))
}
guard libEnv.nymNetwork.networkName != currentEnv.rawValue
else {
try await Task.sleep(for: .seconds(1))
return try await verifyEnvVariables(retryCount: retryCount + 1)
}

return true
}
#elseif os(macOS)

#if os(macOS)
func setDaemonEnvironmentVariables() throws {
try grpcManager.switchEnvironment(to: currentEnv.rawValue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public enum GeneralNymError: Error, Equatable {
case cannotParseCountries
case library(message: String)
case noMnemonicStored
case noEnvFile
case noEnv
}

extension GeneralNymError: LocalizedError {
Expand All @@ -26,8 +26,8 @@ extension GeneralNymError: LocalizedError {
return message
case .noMnemonicStored:
return "error.noMnemonicStored".localizedString
case .noEnvFile:
return "generalNymError.noEnvFile".localizedString
case .noEnv:
return "generalNymError.noEnv".localizedString
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public final class CountriesManager: ObservableObject {
}
isLoading = true

Task { [weak self] in
Task(priority: .background) { [weak self] in
self?.fetchEntryExitCountries()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private extension CredentialsManager {

private extension CredentialsManager {
func checkCredentialImport() {
Task {
Task(priority: .background) {
do {
let isImported: Bool
#if os(iOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension TunnelsManager {
let alwaysOnRule = NEOnDemandRuleConnect()
alwaysOnRule.interfaceTypeMatch = .any
tunnelProviderManager.onDemandRules = [alwaysOnRule]
tunnelProviderManager.isOnDemandEnabled = true
tunnelProviderManager.isOnDemandEnabled = false

let activeTunnel = tunnels.first { $0.status == .connected || $0.status == .connecting }

Expand Down
Loading

0 comments on commit 2106e0d

Please sign in to comment.