ios vpn app
This commit is contained in:
1 parent
96f16399d7
commit
1b506b9ee7
48 files changed
+4934
-45
No files matched your search
@@ -0,0 +1,192 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
enum SocksAuthMode: String, CaseIterable, Identifiable {
|
||||
case auto = "AUTO"
|
||||
case manual = "MANUAL"
|
||||
var id: String { rawValue }
|
||||
var label: String { self == .auto ? NSLocalizedString("settings_auth_auto", comment: "") : NSLocalizedString("settings_auth_manual", comment: "") }
|
||||
var sub: String { self == .auto ? NSLocalizedString("proxy_auth_auto_sub", comment: "") : NSLocalizedString("proxy_auth_manual_sub", comment: "") }
|
||||
}
|
||||
|
||||
enum DnsMode: String, CaseIterable, Identifiable {
|
||||
case system = "SYSTEM"
|
||||
case custom = "CUSTOM"
|
||||
var id: String { rawValue }
|
||||
var label: String { self == .system ? NSLocalizedString("dns_mode_system", comment: "") : NSLocalizedString("dns_mode_custom", comment: "") }
|
||||
}
|
||||
|
||||
enum DKey {
|
||||
static let savedCalls = "savedCalls"
|
||||
static let activeCallId = "activeCallId"
|
||||
static let tunnelMode = "tunnelMode"
|
||||
static let autofillEnabled = "autofillEnabled"
|
||||
static let autofillName = "autofillName"
|
||||
static let themeMode = "themeMode"
|
||||
static let connectOnStart = "connectOnStart"
|
||||
static let socksPort = "socksPort"
|
||||
static let socksAuthMode = "socksAuthMode"
|
||||
static let socksUser = "socksUser"
|
||||
static let socksPass = "socksPass"
|
||||
static let vp8Fps = "vp8Fps"
|
||||
static let vp8Batch = "vp8Batch"
|
||||
static let dualTrack = "dualTrack"
|
||||
static let reliable = "reliable"
|
||||
static let debug = "debug"
|
||||
static let dnsMode = "dnsMode"
|
||||
static let dnsPrimary = "dnsPrimary"
|
||||
static let dnsSecondary = "dnsSecondary"
|
||||
}
|
||||
|
||||
enum Defaults {
|
||||
static let autofillName = "Hello"
|
||||
static let vp8Fps = 24
|
||||
static let vp8Batch = 30
|
||||
}
|
||||
|
||||
final class AppState: ObservableObject {
|
||||
@Published var savedCalls: [CallConfig] { didSet { store.set(CallConfig.encodeList(savedCalls), forKey: DKey.savedCalls) } }
|
||||
@Published var activeCallId: String? {
|
||||
didSet {
|
||||
if let activeCallId { store.set(activeCallId, forKey: DKey.activeCallId) }
|
||||
else { store.removeObject(forKey: DKey.activeCallId) }
|
||||
}
|
||||
}
|
||||
|
||||
@Published var tunnelMode: TunnelMode { didSet { store.set(tunnelMode.rawValue, forKey: DKey.tunnelMode) } }
|
||||
@Published var autofillEnabled: Bool { didSet { store.set(autofillEnabled, forKey: DKey.autofillEnabled) } }
|
||||
@Published var autofillName: String { didSet { store.set(autofillName, forKey: DKey.autofillName) } }
|
||||
@Published var themeMode: ThemeMode { didSet { store.set(themeMode.rawValue, forKey: DKey.themeMode) } }
|
||||
@Published var connectOnStart: Bool { didSet { store.set(connectOnStart, forKey: DKey.connectOnStart) } }
|
||||
@Published var socksPort: Int { didSet { store.set(socksPort, forKey: DKey.socksPort) } }
|
||||
@Published var socksAuthMode: SocksAuthMode { didSet { store.set(socksAuthMode.rawValue, forKey: DKey.socksAuthMode) } }
|
||||
@Published var manualSocksUser: String { didSet { store.set(manualSocksUser, forKey: DKey.socksUser) } }
|
||||
@Published var manualSocksPass: String { didSet { store.set(manualSocksPass, forKey: DKey.socksPass) } }
|
||||
@Published var vp8Fps: Int { didSet { store.set(vp8Fps, forKey: DKey.vp8Fps) } }
|
||||
@Published var vp8Batch: Int { didSet { store.set(vp8Batch, forKey: DKey.vp8Batch) } }
|
||||
@Published var dualTrack: Bool { didSet { store.set(dualTrack, forKey: DKey.dualTrack) } }
|
||||
@Published var reliable: Bool { didSet { store.set(reliable, forKey: DKey.reliable) } }
|
||||
@Published var debug: Bool { didSet { store.set(debug, forKey: DKey.debug) } }
|
||||
@Published var dnsMode: DnsMode { didSet { store.set(dnsMode.rawValue, forKey: DKey.dnsMode) } }
|
||||
@Published var dnsPrimary: String { didSet { store.set(dnsPrimary, forKey: DKey.dnsPrimary) } }
|
||||
@Published var dnsSecondary: String { didSet { store.set(dnsSecondary, forKey: DKey.dnsSecondary) } }
|
||||
|
||||
var activeSocksUser: String { socksAuthMode == .manual ? manualSocksUser : autoUser }
|
||||
var activeSocksPass: String { socksAuthMode == .manual ? manualSocksPass : autoPass }
|
||||
var activeCall: CallConfig? { savedCalls.first { $0.id == activeCallId } }
|
||||
|
||||
private let store = UserDefaults(suiteName: AppIdentifiers.appGroup) ?? .standard
|
||||
private let autoUser: String
|
||||
private let autoPass: String
|
||||
|
||||
init() {
|
||||
let s = UserDefaults(suiteName: AppIdentifiers.appGroup) ?? .standard
|
||||
savedCalls = CallConfig.decodeList(s.string(forKey: DKey.savedCalls) ?? "[]")
|
||||
activeCallId = s.string(forKey: DKey.activeCallId)
|
||||
tunnelMode = TunnelMode(rawValue: s.string(forKey: DKey.tunnelMode) ?? "") ?? .video
|
||||
autofillEnabled = s.object(forKey: DKey.autofillEnabled) as? Bool ?? true
|
||||
autofillName = s.string(forKey: DKey.autofillName) ?? Defaults.autofillName
|
||||
themeMode = ThemeMode(rawValue: s.string(forKey: DKey.themeMode) ?? "") ?? .system
|
||||
connectOnStart = s.object(forKey: DKey.connectOnStart) as? Bool ?? false
|
||||
socksPort = s.object(forKey: DKey.socksPort) as? Int ?? VpnConfig.defaultSocksPort
|
||||
socksAuthMode = SocksAuthMode(rawValue: s.string(forKey: DKey.socksAuthMode) ?? "") ?? .auto
|
||||
manualSocksUser = s.string(forKey: DKey.socksUser) ?? ""
|
||||
manualSocksPass = s.string(forKey: DKey.socksPass) ?? ""
|
||||
vp8Fps = s.object(forKey: DKey.vp8Fps) as? Int ?? Defaults.vp8Fps
|
||||
vp8Batch = s.object(forKey: DKey.vp8Batch) as? Int ?? Defaults.vp8Batch
|
||||
dualTrack = s.object(forKey: DKey.dualTrack) as? Bool ?? false
|
||||
reliable = s.object(forKey: DKey.reliable) as? Bool ?? false
|
||||
debug = s.object(forKey: DKey.debug) as? Bool ?? false
|
||||
dnsMode = DnsMode(rawValue: s.string(forKey: DKey.dnsMode) ?? "") ?? .system
|
||||
dnsPrimary = s.string(forKey: DKey.dnsPrimary) ?? VpnConfig.dnsPrimary
|
||||
dnsSecondary = s.string(forKey: DKey.dnsSecondary) ?? VpnConfig.dnsSecondary
|
||||
|
||||
let chars = Array("abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
autoUser = String((0..<16).map { _ in chars.randomElement()! })
|
||||
autoPass = String((0..<24).map { _ in chars.randomElement()! })
|
||||
}
|
||||
|
||||
func addCall(name: String, url: String) {
|
||||
let call = CallConfig.make(name: name, url: url)
|
||||
savedCalls.append(call)
|
||||
activeCallId = call.id
|
||||
}
|
||||
|
||||
func removeCall(id: String) {
|
||||
savedCalls.removeAll { $0.id == id }
|
||||
if activeCallId == id { activeCallId = savedCalls.first?.id }
|
||||
}
|
||||
|
||||
func renameCall(id: String, name: String) {
|
||||
guard let index = savedCalls.firstIndex(where: { $0.id == id }) else { return }
|
||||
let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty else { return }
|
||||
savedCalls[index].name = trimmed
|
||||
}
|
||||
|
||||
func updateCall(_ call: CallConfig) {
|
||||
guard let index = savedCalls.firstIndex(where: { $0.id == call.id }) else { return }
|
||||
savedCalls[index] = call
|
||||
}
|
||||
|
||||
func selectCall(id: String) { activeCallId = id }
|
||||
|
||||
func forgetAllCalls() {
|
||||
savedCalls = []
|
||||
activeCallId = nil
|
||||
}
|
||||
|
||||
func resetAllSettings() {
|
||||
tunnelMode = .video
|
||||
autofillEnabled = true
|
||||
autofillName = Defaults.autofillName
|
||||
themeMode = .system
|
||||
connectOnStart = false
|
||||
socksPort = VpnConfig.defaultSocksPort
|
||||
socksAuthMode = .auto
|
||||
manualSocksUser = ""
|
||||
manualSocksPass = ""
|
||||
vp8Fps = Defaults.vp8Fps
|
||||
vp8Batch = Defaults.vp8Batch
|
||||
dualTrack = false
|
||||
reliable = false
|
||||
debug = false
|
||||
dnsMode = .system
|
||||
dnsPrimary = VpnConfig.dnsPrimary
|
||||
dnsSecondary = VpnConfig.dnsSecondary
|
||||
}
|
||||
|
||||
func tunnelConfig() -> TunnelConfig? {
|
||||
guard let call = activeCall else { return nil }
|
||||
let platform = call.platform
|
||||
var mode = call.tunnelMode ?? tunnelMode
|
||||
if mode == .dc && (platform == .telemost || platform == .dion) {
|
||||
mode = .video
|
||||
}
|
||||
let dns = dnsMode == .custom ? [dnsPrimary, dnsSecondary].filter { !$0.isEmpty } : []
|
||||
return TunnelConfig(
|
||||
url: call.url,
|
||||
displayName: autofillEnabled ? autofillName : "",
|
||||
platform: platform,
|
||||
tunnelMode: mode,
|
||||
socksPort: socksPort,
|
||||
socksUser: activeSocksUser,
|
||||
socksPass: activeSocksPass,
|
||||
vp8Fps: call.vp8Fps ?? vp8Fps,
|
||||
vp8Batch: call.vp8Batch ?? vp8Batch,
|
||||
dualTrack: call.dualTrack ?? dualTrack,
|
||||
reliable: call.reliable ?? reliable,
|
||||
debug: debug,
|
||||
dnsServers: dns
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
enum RandomNames {
|
||||
static let pool: [String] = {
|
||||
guard let url = Bundle.main.url(forResource: "names", withExtension: "txt"),
|
||||
let text = try? String(contentsOf: url, encoding: .utf8) else { return [Defaults.autofillName] }
|
||||
let names = text.split(whereSeparator: \.isNewline).map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }
|
||||
return names.isEmpty ? [Defaults.autofillName] : names
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import Foundation
|
||||
|
||||
extension CallPlatform {
|
||||
var label: String {
|
||||
switch self {
|
||||
case .vk: return "VK"
|
||||
case .telemost: return "Telemost"
|
||||
case .wbstream: return "WB Stream"
|
||||
case .dion: return "DION"
|
||||
}
|
||||
}
|
||||
|
||||
var suggestedName: String {
|
||||
switch self {
|
||||
case .vk: return NSLocalizedString("suggest_name_vk", comment: "")
|
||||
case .telemost: return NSLocalizedString("suggest_name_telemost", comment: "")
|
||||
case .wbstream: return NSLocalizedString("suggest_name_wbstream", comment: "")
|
||||
case .dion: return NSLocalizedString("suggest_name_dion", comment: "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CallConfig: Identifiable, Codable, Equatable {
|
||||
var id: String
|
||||
var name: String
|
||||
var url: String
|
||||
var tunnelMode: TunnelMode?
|
||||
var vp8Fps: Int?
|
||||
var vp8Batch: Int?
|
||||
var dualTrack: Bool?
|
||||
var reliable: Bool?
|
||||
|
||||
var platform: CallPlatform { CallPlatform.detect(url: url) }
|
||||
var glyph: String { platform.glyph }
|
||||
var platformLabel: String { platform.label }
|
||||
|
||||
static func make(name: String, url: String) -> CallConfig {
|
||||
let trimmedUrl = url.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let trimmedName = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let finalName = trimmedName.isEmpty ? CallPlatform.detect(url: trimmedUrl).suggestedName : trimmedName
|
||||
return CallConfig(id: UUID().uuidString, name: finalName, url: trimmedUrl,
|
||||
tunnelMode: nil, vp8Fps: nil, vp8Batch: nil, dualTrack: nil, reliable: nil)
|
||||
}
|
||||
|
||||
static func decodeList(_ json: String) -> [CallConfig] {
|
||||
guard let data = json.data(using: .utf8),
|
||||
let list = try? JSONDecoder().decode([CallConfig].self, from: data) else { return [] }
|
||||
return list
|
||||
}
|
||||
|
||||
static func encodeList(_ list: [CallConfig]) -> String {
|
||||
guard let data = try? JSONEncoder().encode(list),
|
||||
let json = String(data: data, encoding: .utf8) else { return "[]" }
|
||||
return json
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
import Foundation
|
||||
import os
|
||||
|
||||
enum Log {
|
||||
static let subsystem = "whitelist.bypass.vpn"
|
||||
static let tunnel = Logger(subsystem: subsystem, category: "tunnel")
|
||||
static let relay = Logger(subsystem: subsystem, category: "relay")
|
||||
static let status = Logger(subsystem: subsystem, category: "status")
|
||||
static let app = Logger(subsystem: subsystem, category: "app")
|
||||
}
|
||||
|
||||
enum AppIdentifiers {
|
||||
static let appGroup = "group.whitelist.bypass.vpn"
|
||||
static let tunnelBundleId = "whitelist.bypass.vpn.PacketTunnel"
|
||||
static let vpnLocalizedDescription = "Whitelist Bypass"
|
||||
|
||||
static var relayLogURL: URL? {
|
||||
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?.appendingPathComponent("relay.log")
|
||||
}
|
||||
}
|
||||
|
||||
enum VpnConfig {
|
||||
static let address = "10.0.0.2"
|
||||
static let subnetMask = "255.255.255.255"
|
||||
static let address6 = "fd00::2"
|
||||
static let prefix6: NSNumber = 128
|
||||
static let mtu = 1500
|
||||
static let dnsPrimary = "8.8.8.8"
|
||||
static let dnsSecondary = "8.8.4.4"
|
||||
static let tunnelRemote = "127.0.0.1"
|
||||
static let defaultSocksPort = 1080
|
||||
}
|
||||
|
||||
enum ConfigKey {
|
||||
static let url = "url"
|
||||
static let displayName = "displayName"
|
||||
static let platform = "platform"
|
||||
static let tunnelMode = "tunnelMode"
|
||||
static let socksPort = "socksPort"
|
||||
static let socksUser = "socksUser"
|
||||
static let socksPass = "socksPass"
|
||||
static let vp8Fps = "vp8Fps"
|
||||
static let vp8Batch = "vp8Batch"
|
||||
static let dualTrack = "dualTrack"
|
||||
static let reliable = "reliable"
|
||||
static let debug = "debug"
|
||||
static let dnsPrimary = "dnsPrimary"
|
||||
static let dnsSecondary = "dnsSecondary"
|
||||
}
|
||||
|
||||
struct LogSnapshot: Codable {
|
||||
let status: String
|
||||
let revision: Int
|
||||
let lines: [String]
|
||||
}
|
||||
|
||||
enum GoStatus {
|
||||
static let idle = "IDLE"
|
||||
static let ready = "READY"
|
||||
static let connecting = "CONNECTING"
|
||||
static let reconnecting = "RECONNECTING"
|
||||
static let tunnelConnected = "TUNNEL_CONNECTED"
|
||||
static let tunnelLost = "TUNNEL_LOST"
|
||||
static let error = "ERROR"
|
||||
}
|
||||
|
||||
enum CallPlatform: String {
|
||||
case vk
|
||||
case telemost
|
||||
case wbstream
|
||||
case dion
|
||||
|
||||
static let wbstreamPrefix = "wbstream://"
|
||||
static let dionPrefix = "dion://"
|
||||
static let dionEventInfix = "dion.vc/event/"
|
||||
|
||||
static func detect(url: String) -> CallPlatform {
|
||||
if url.hasPrefix(dionPrefix) || url.contains(dionEventInfix) { return .dion }
|
||||
if url.hasPrefix(wbstreamPrefix) { return .wbstream }
|
||||
if url.contains("telemost") { return .telemost }
|
||||
return .vk
|
||||
}
|
||||
|
||||
static func extractRoomId(url: String) -> String {
|
||||
let trimmed = url.trimmingCharacters(in: .whitespaces)
|
||||
if trimmed.hasPrefix(wbstreamPrefix) {
|
||||
return String(trimmed.dropFirst(wbstreamPrefix.count)).trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
if trimmed.hasPrefix(dionPrefix) {
|
||||
return String(trimmed.dropFirst(dionPrefix.count)).trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
if let range = trimmed.range(of: dionEventInfix) {
|
||||
var slug = String(trimmed[range.upperBound...])
|
||||
if let qmark = slug.firstIndex(of: "?") { slug = String(slug[..<qmark]) }
|
||||
if let slash = slug.firstIndex(of: "/") { slug = String(slug[..<slash]) }
|
||||
return slug.trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
var glyph: String {
|
||||
switch self {
|
||||
case .vk: return "VK"
|
||||
case .telemost: return "TM"
|
||||
case .wbstream: return "WB"
|
||||
case .dion: return "DN"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum TunnelMode: String, CaseIterable, Identifiable, Codable {
|
||||
case dc
|
||||
case video
|
||||
var id: String { rawValue }
|
||||
var label: String { self == .dc ? NSLocalizedString("settings_tunnel_dc", comment: "") : NSLocalizedString("settings_tunnel_video", comment: "") }
|
||||
}
|
||||
|
||||
struct TunnelConfig {
|
||||
var url: String
|
||||
var displayName: String
|
||||
var platform: CallPlatform
|
||||
var tunnelMode: TunnelMode
|
||||
var socksPort: Int
|
||||
var socksUser: String
|
||||
var socksPass: String
|
||||
var vp8Fps: Int
|
||||
var vp8Batch: Int
|
||||
var dualTrack: Bool
|
||||
var reliable: Bool
|
||||
var debug: Bool
|
||||
var dnsServers: [String]
|
||||
|
||||
var roomId: String { CallPlatform.extractRoomId(url: url) }
|
||||
}
|
||||
|
||||
extension TunnelConfig {
|
||||
init(dictionary d: [String: Any]) {
|
||||
let primary = d[ConfigKey.dnsPrimary] as? String ?? ""
|
||||
let secondary = d[ConfigKey.dnsSecondary] as? String ?? ""
|
||||
let dns = [primary, secondary].filter { !$0.isEmpty }
|
||||
|
||||
self.init(
|
||||
url: d[ConfigKey.url] as? String ?? "",
|
||||
displayName: d[ConfigKey.displayName] as? String ?? "",
|
||||
platform: CallPlatform(rawValue: d[ConfigKey.platform] as? String ?? "") ?? .vk,
|
||||
tunnelMode: TunnelMode(rawValue: d[ConfigKey.tunnelMode] as? String ?? "") ?? .video,
|
||||
socksPort: (d[ConfigKey.socksPort] as? NSNumber)?.intValue ?? VpnConfig.defaultSocksPort,
|
||||
socksUser: d[ConfigKey.socksUser] as? String ?? "",
|
||||
socksPass: d[ConfigKey.socksPass] as? String ?? "",
|
||||
vp8Fps: (d[ConfigKey.vp8Fps] as? NSNumber)?.intValue ?? 24,
|
||||
vp8Batch: (d[ConfigKey.vp8Batch] as? NSNumber)?.intValue ?? 30,
|
||||
dualTrack: (d[ConfigKey.dualTrack] as? NSNumber)?.boolValue ?? false,
|
||||
reliable: (d[ConfigKey.reliable] as? NSNumber)?.boolValue ?? false,
|
||||
debug: (d[ConfigKey.debug] as? NSNumber)?.boolValue ?? false,
|
||||
dnsServers: dns
|
||||
)
|
||||
}
|
||||
|
||||
var dictionary: [String: Any] {
|
||||
[
|
||||
ConfigKey.url: url,
|
||||
ConfigKey.displayName: displayName,
|
||||
ConfigKey.platform: platform.rawValue,
|
||||
ConfigKey.tunnelMode: tunnelMode.rawValue,
|
||||
ConfigKey.socksPort: socksPort,
|
||||
ConfigKey.socksUser: socksUser,
|
||||
ConfigKey.socksPass: socksPass,
|
||||
ConfigKey.vp8Fps: vp8Fps,
|
||||
ConfigKey.vp8Batch: vp8Batch,
|
||||
ConfigKey.dualTrack: dualTrack,
|
||||
ConfigKey.reliable: reliable,
|
||||
ConfigKey.debug: debug,
|
||||
ConfigKey.dnsPrimary: dnsServers.first ?? "",
|
||||
ConfigKey.dnsSecondary: dnsServers.count > 1 ? dnsServers[1] : "",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
enum TokenCache {
|
||||
private static let defaults = UserDefaults(suiteName: AppIdentifiers.appGroup)
|
||||
|
||||
static func save(_ key: String, _ value: String) {
|
||||
defaults?.set(value, forKey: "cache_\(key)")
|
||||
}
|
||||
|
||||
static func load(_ key: String) -> String {
|
||||
defaults?.string(forKey: "cache_\(key)") ?? ""
|
||||
}
|
||||
|
||||
static func clear(_ key: String) {
|
||||
defaults?.removeObject(forKey: "cache_\(key)")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user