-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
ProviderConnectionProfile.swift
180 lines (147 loc) · 5.17 KB
/
ProviderConnectionProfile.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
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
//
// ProviderConnectionProfile.swift
// Passepartout
//
// Created by Davide De Rosa on 9/2/18.
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout 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.
//
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import TunnelKit
public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable {
public let name: Infrastructure.Name
public var infrastructure: Infrastructure {
return InfrastructureFactory.shared.get(name)
}
public var poolId: String {
didSet {
validateEndpoint()
}
}
public var pool: Pool? {
return infrastructure.pool(for: poolId) ?? infrastructure.pool(for: infrastructure.defaults.pool)
}
public var presetId: String {
didSet {
validateEndpoint()
}
}
public var preset: InfrastructurePreset? {
return infrastructure.preset(for: presetId)
}
public var manualAddress: String?
public var manualProtocol: EndpointProtocol?
public var usesProviderEndpoint: Bool {
return (manualAddress != nil) || (manualProtocol != nil)
}
public init(name: Infrastructure.Name) {
self.name = name
poolId = ""
presetId = ""
username = nil
poolId = infrastructure.defaults.pool
presetId = infrastructure.defaults.preset
}
public func sortedPools() -> [Pool] {
return infrastructure.pools.sorted()
}
private func validateEndpoint() {
guard let pool = pool, let preset = preset else {
manualAddress = nil
manualProtocol = nil
return
}
if let address = manualAddress, !pool.hasAddress(address) {
manualAddress = nil
}
if let proto = manualProtocol, !preset.hasProtocol(proto) {
manualProtocol = nil
}
}
// MARK: ConnectionProfile
public let context: Context = .provider
public var id: String {
return name.rawValue
}
public var username: String?
public var requiresCredentials: Bool {
return true
}
public func generate(from configuration: TunnelKitProvider.Configuration, preferences: Preferences) throws -> TunnelKitProvider.Configuration {
guard let pool = pool else {
preconditionFailure("Nil pool?")
}
guard let preset = preset else {
preconditionFailure("Nil preset?")
}
// assert(!pool.numericAddresses.isEmpty)
// XXX: copy paste, error prone
var builder = preset.configuration.builder()
builder.mtu = configuration.mtu
builder.shouldDebug = configuration.shouldDebug
builder.debugLogFormat = configuration.debugLogFormat
builder.masksPrivateData = configuration.masksPrivateData
if let address = manualAddress {
builder.prefersResolvedAddresses = true
builder.resolvedAddresses = [address]
} else {
builder.prefersResolvedAddresses = !preferences.resolvesHostname
builder.resolvedAddresses = pool.addresses()
}
if let proto = manualProtocol {
builder.sessionConfiguration.endpointProtocols = [proto]
} else {
builder.sessionConfiguration.endpointProtocols = preset.configuration.sessionConfiguration.endpointProtocols
// builder.sessionConfiguration.endpointProtocols = [
// EndpointProtocol(.udp, 8080),
// EndpointProtocol(.tcp, 443)
// ]
}
return builder.build()
}
public func with(newId: String) -> ConnectionProfile {
fatalError("Cannot rename a ProviderConnectionProfile")
}
}
public extension ProviderConnectionProfile {
static func ==(lhs: ProviderConnectionProfile, rhs: ProviderConnectionProfile) -> Bool {
return lhs.id == rhs.id
}
}
public extension ProviderConnectionProfile {
var mainAddress: String {
assert(pool != nil, "Getting provider main address but no pool set")
return pool?.hostname ?? ""
}
var addresses: [String] {
return pool?.addresses() ?? []
}
var protocols: [EndpointProtocol] {
return preset?.configuration.sessionConfiguration.endpointProtocols ?? []
}
var canCustomizeEndpoint: Bool {
return true
}
var customAddress: String? {
return manualAddress
}
var customProtocol: EndpointProtocol? {
return manualProtocol
}
}