// // OpenVPNNetworkSettingsBuilder.m // OpenVPN Adapter // // Created by Jonathan Downing on 12/10/2017. // #import "OpenVPNNetworkSettingsBuilder.h" #import @interface OpenVPNNetworkSettingsBuilder () @property (nonatomic) NSMutableArray *ipv4LocalAddresses; @property (nonatomic) NSMutableArray *ipv4SubnetMasks; @property (nonatomic) NSMutableArray *ipv4IncludedRoutes; @property (nonatomic) NSMutableArray *ipv4ExcludedRoutes; @property (nonatomic) NSMutableArray *ipv6LocalAddresses; @property (nonatomic) NSMutableArray *ipv6NetworkPrefixLengths; @property (nonatomic) NSMutableArray *ipv6IncludedRoutes; @property (nonatomic) NSMutableArray *ipv6ExcludedRoutes; @property (nonatomic) NSMutableArray *dnsServers; @property (nonatomic) NSMutableArray *searchDomains; @property (nonatomic) NSMutableArray *proxyExceptionList; @end @implementation OpenVPNNetworkSettingsBuilder #pragma mark - NEPacketTunnelNetworkSettings Generation - (NEPacketTunnelNetworkSettings *)networkSettings { if (!self.remoteAddress.length) { return nil; } NEPacketTunnelNetworkSettings *networkSettings = [[NEPacketTunnelNetworkSettings alloc] initWithTunnelRemoteAddress:self.remoteAddress]; if (self.ipv4LocalAddresses.count && (self.ipv4LocalAddresses.count == self.ipv4SubnetMasks.count)) { NEIPv4Settings *ipv4Settings = [[NEIPv4Settings alloc] initWithAddresses:self.ipv4LocalAddresses subnetMasks:self.ipv4SubnetMasks]; ipv4Settings.includedRoutes = self.ipv4IncludedRoutes; ipv4Settings.excludedRoutes = self.ipv4ExcludedRoutes; networkSettings.IPv4Settings = ipv4Settings; } if (self.ipv6LocalAddresses.count && (self.ipv6LocalAddresses.count == self.ipv6NetworkPrefixLengths.count)) { NEIPv6Settings *ipv6Settings = [[NEIPv6Settings alloc] initWithAddresses:self.ipv6LocalAddresses networkPrefixLengths:self.ipv6NetworkPrefixLengths]; ipv6Settings.includedRoutes = self.ipv6IncludedRoutes; ipv6Settings.excludedRoutes = self.ipv6ExcludedRoutes; networkSettings.IPv6Settings = ipv6Settings; } if (self.dnsServers.count) { NEDNSSettings *dnsSettings = [[NEDNSSettings alloc] initWithServers:self.dnsServers]; dnsSettings.searchDomains = self.searchDomains; networkSettings.DNSSettings = dnsSettings; } if (self.autoProxyConfigurationEnabled || self.httpProxyServerEnabled || self.httpsProxyServerEnabled) { NEProxySettings *proxySettings = [[NEProxySettings alloc] init]; proxySettings.autoProxyConfigurationEnabled = self.autoProxyConfigurationEnabled; proxySettings.proxyAutoConfigurationURL = self.proxyAutoConfigurationURL; proxySettings.exceptionList = self.proxyExceptionList; proxySettings.HTTPServer = self.httpProxyServer; proxySettings.HTTPEnabled = self.httpProxyServerEnabled; proxySettings.HTTPSServer = self.httpsProxyServer; proxySettings.HTTPSEnabled = self.httpsProxyServerEnabled; networkSettings.proxySettings = proxySettings; } networkSettings.MTU = self.mtu; return networkSettings; } #pragma mark - Lazy Initializers - (NSMutableArray *)ipv4LocalAddresses { if (!_ipv4LocalAddresses) { _ipv4LocalAddresses = [[NSMutableArray alloc] init]; } return _ipv4LocalAddresses; } - (NSMutableArray *)ipv4SubnetMasks { if (!_ipv4SubnetMasks) { _ipv4SubnetMasks = [[NSMutableArray alloc] init]; } return _ipv4SubnetMasks; } - (NSMutableArray *)ipv4IncludedRoutes { if (!_ipv4IncludedRoutes) { _ipv4IncludedRoutes = [[NSMutableArray alloc] init]; } return _ipv4IncludedRoutes; } - (NSMutableArray *)ipv4ExcludedRoutes { if (!_ipv4ExcludedRoutes) { _ipv4ExcludedRoutes = [[NSMutableArray alloc] init]; } return _ipv4ExcludedRoutes; } - (NSMutableArray *)ipv6LocalAddresses { if (!_ipv6LocalAddresses) { _ipv6LocalAddresses = [[NSMutableArray alloc] init]; } return _ipv6LocalAddresses; } - (NSMutableArray *)ipv6NetworkPrefixLengths { if (!_ipv6NetworkPrefixLengths) { _ipv6NetworkPrefixLengths = [[NSMutableArray alloc] init]; } return _ipv6NetworkPrefixLengths; } - (NSMutableArray *)ipv6IncludedRoutes { if (!_ipv6IncludedRoutes) { _ipv6IncludedRoutes = [[NSMutableArray alloc] init]; } return _ipv6IncludedRoutes; } - (NSMutableArray *)ipv6ExcludedRoutes { if (!_ipv6ExcludedRoutes) { _ipv6ExcludedRoutes = [[NSMutableArray alloc] init]; } return _ipv6ExcludedRoutes; } - (NSMutableArray *)dnsServers { if (!_dnsServers) { _dnsServers = [[NSMutableArray alloc] init]; } return _dnsServers; } - (NSMutableArray *)searchDomains { if (!_searchDomains) { _searchDomains = [[NSMutableArray alloc] init]; } return _searchDomains; } - (NSMutableArray *)proxyExceptionList { if (!_proxyExceptionList) { _proxyExceptionList = [[NSMutableArray alloc] init]; } return _proxyExceptionList; } @end