Skip to content

Commit

Permalink
feat: refactor V2RayPorts.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Sep 18, 2023
1 parent 8bb182f commit f142cf0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 43 deletions.
8 changes: 4 additions & 4 deletions IVPNClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
82351FCC241FBC8E00E6E0FD /* VPNStatusViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82351FCB241FBC8E00E6E0FD /* VPNStatusViewModelTests.swift */; };
82351FCE2420CE6800E6E0FD /* MapMarkerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82351FCD2420CE6800E6E0FD /* MapMarkerView.swift */; };
82351FD224222F7700E6E0FD /* ConnectionInfoPopupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82351FD124222F7700E6E0FD /* ConnectionInfoPopupView.swift */; };
82365E7F2AB86020006434C3 /* V2RayPorts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82365E7E2AB86020006434C3 /* V2RayPorts.swift */; };
82365E7F2AB86020006434C3 /* V2RaySettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82365E7E2AB86020006434C3 /* V2RaySettings.swift */; };
823ACC292626E69F006F69AB /* GeoLookupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 823ACC282626E69F006F69AB /* GeoLookupTests.swift */; };
823ACC312626FF3E006F69AB /* IpProtocolView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 823ACC302626FF3E006F69AB /* IpProtocolView.swift */; };
823ACC3926270330006F69AB /* ConnectionInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 823ACC3826270330006F69AB /* ConnectionInfoView.swift */; };
Expand Down Expand Up @@ -509,7 +509,7 @@
82351FCB241FBC8E00E6E0FD /* VPNStatusViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VPNStatusViewModelTests.swift; sourceTree = "<group>"; };
82351FCD2420CE6800E6E0FD /* MapMarkerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapMarkerView.swift; sourceTree = "<group>"; };
82351FD124222F7700E6E0FD /* ConnectionInfoPopupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionInfoPopupView.swift; sourceTree = "<group>"; };
82365E7E2AB86020006434C3 /* V2RayPorts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = V2RayPorts.swift; sourceTree = "<group>"; };
82365E7E2AB86020006434C3 /* V2RaySettings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = V2RaySettings.swift; sourceTree = "<group>"; };
823ACC282626E69F006F69AB /* GeoLookupTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeoLookupTests.swift; sourceTree = "<group>"; };
823ACC302626FF3E006F69AB /* IpProtocolView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IpProtocolView.swift; sourceTree = "<group>"; };
823ACC3826270330006F69AB /* ConnectionInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionInfoView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -931,7 +931,7 @@
isa = PBXGroup;
children = (
8247C05F2A7CF54300A7C02F /* V2RayConfig.swift */,
82365E7E2AB86020006434C3 /* V2RayPorts.swift */,
82365E7E2AB86020006434C3 /* V2RaySettings.swift */,
821E35572A95DCD200AEE5C7 /* V2RayCore.swift */,
821E35592A95F77700AEE5C7 /* config.json */,
);
Expand Down Expand Up @@ -2287,7 +2287,7 @@
822563922431E03A00AE7F8D /* AccountView.swift in Sources */,
9CBFF02C21021D2100FE1757 /* ConnectionManager.swift in Sources */,
82D598C821A6ADF1000FABDE /* NetworkType.swift in Sources */,
82365E7F2AB86020006434C3 /* V2RayPorts.swift in Sources */,
82365E7F2AB86020006434C3 /* V2RaySettings.swift in Sources */,
9CC29DF81E1D59E90080F799 /* Alerts+Ext.swift in Sources */,
9CB2CE1F1DAA5258007A4D2D /* Authentication.swift in Sources */,
82DAB37B2457013900302F4C /* Service.swift in Sources */,
Expand Down
20 changes: 9 additions & 11 deletions IVPNClient/Models/V2Ray/V2RayCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,18 @@ class V2RayCore {
}

func makeConfig() -> V2RayConfig? {
guard let v2rayPorts = V2RayPorts.load() else {
guard let settings = V2RaySettings.load() else {
return nil
}

let host = v2rayPorts.host
let outboundIp = host.v2ray
let outboundPort = v2rayPorts.port
let inboundIp = host.host
let inboundPort = v2rayPorts.wireguard.first?.port ?? 0
let outboundUserId = v2rayPorts.id
let tlsSrvName = host.dnsName.replacingOccurrences(of: "ivpn.net", with: "inet-telecom.com")
let config = V2RayConfig.createQuick(outboundIp: outboundIp, outboundPort: outboundPort, inboundIp: inboundIp, inboundPort: inboundPort, outboundUserId: outboundUserId, tlsSrvName: tlsSrvName)

return config
return V2RayConfig.createQuick(
outboundIp: settings.outboundIp,
outboundPort: settings.outboundPort,
inboundIp: settings.inboundIp,
inboundPort: settings.inboundPort,
outboundUserId: settings.id,
tlsSrvName: settings.tlsSrvName()
)
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// V2RayPorts.swift
// V2RaySettings.swift
// IVPN iOS app
// https://github.com/ivpn/ios-app
//
Expand All @@ -23,22 +23,39 @@

import Foundation

struct V2RayPorts: Codable {
struct V2RaySettings: Codable {

let id: String
var port: Int
var host: V2RayHost
let wireguard: [V2RayPort]
var id: String
var outboundIp: String
var outboundPort: Int
var inboundIp: String
var inboundPort: Int
var dnsName: String
var wireguard: [V2RayPort]

init(id: String = "", outboundIp: String = "", outboundPort: Int = 0, inboundIp: String = "", inboundPort: Int = 0, dnsName: String = "", wireguard: [V2RayPort] = []) {
self.id = id
self.outboundIp = outboundIp
self.outboundPort = outboundPort
self.inboundIp = inboundIp
self.inboundPort = inboundPort
self.dnsName = dnsName
self.wireguard = wireguard
}

func save() {
if let encoded = try? JSONEncoder().encode(self) {
UserDefaults.shared.set(encoded, forKey: UserDefaults.Key.v2RayPorts)
UserDefaults.shared.set(encoded, forKey: UserDefaults.Key.v2raySettings)
}
}

static func load() -> V2RayPorts? {
if let saved = UserDefaults.shared.object(forKey: UserDefaults.Key.v2RayPorts) as? Data {
if let loaded = try? JSONDecoder().decode(V2RayPorts.self, from: saved) {
func tlsSrvName() -> String {
return dnsName.replacingOccurrences(of: "ivpn.net", with: "inet-telecom.com")
}

static func load() -> V2RaySettings? {
if let saved = UserDefaults.shared.object(forKey: UserDefaults.Key.v2raySettings) as? Data {
if let loaded = try? JSONDecoder().decode(V2RaySettings.self, from: saved) {
return loaded
}
}
Expand All @@ -52,9 +69,3 @@ struct V2RayPort: Codable {
let type: String
let port: Int
}

struct V2RayHost: Codable {
let host: String
let dnsName: String
let v2ray: String
}
7 changes: 4 additions & 3 deletions IVPNClient/Models/VPNServerList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@ class VPNServerList {
if let v2ray = portsObj["v2ray"] as? [String: Any] {
if let wireguard = v2ray["wireguard"] as? [[String: Any]] {
var ports = [V2RayPort]()
var inboundPort = 0
for port in wireguard {
let type = port["type"] as? String ?? ""
let port = port["port"] as? Int ?? 0
ports.append(V2RayPort(type: type, port: port))
inboundPort = port
}
let id = v2ray["id"] as? String ?? ""
let v2rayHost = V2RayHost(host: "", dnsName: "", v2ray: "")
let v2rayPorts = V2RayPorts(id: id, port: 0, host: v2rayHost, wireguard: ports)
v2rayPorts.save()
let v2raySettings = V2RaySettings(id: id, inboundPort: inboundPort, wireguard: ports)
v2raySettings.save()
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions IVPNClient/Utilities/Extensions/NETunnelProviderProtocol+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ extension NETunnelProviderProtocol {
var publicKey = host.publicKey
let port = getPort(settings: settings)
var endpoint = Peer.endpoint(host: host.host, port: port)
var v2rayHost = host
var v2rayPort = port
var v2rayInboundIp = host.host
var v2rayOutboundIp = host.v2ray
var v2rayOutboundPort = port
var v2rayDnsName = host.dnsName

if UserDefaults.shared.isMultiHop, Application.shared.serviceStatus.isEnabled(capability: .multihop), let exitHost = getExitHost() {
publicKey = exitHost.publicKey
endpoint = Peer.endpoint(host: host.host, port: exitHost.multihopPort)
v2rayHost = exitHost
v2rayInboundIp = exitHost.host
}

if let ipv6 = host.ipv6, UserDefaults.shared.isIPv6 {
Expand All @@ -144,10 +146,12 @@ extension NETunnelProviderProtocol {
// If V2Ray is enabled
if (false) {
endpoint = Peer.endpoint(host: Config.v2rayHost, port: Config.v2rayPort)
if var v2rayPorts = V2RayPorts.load() {
v2rayPorts.port = v2rayPort
v2rayPorts.host = V2RayHost(host: v2rayHost.host, dnsName: v2rayHost.dnsName, v2ray: v2rayHost.v2ray)
v2rayPorts.save()
if var v2raySettings = V2RaySettings.load() {
v2raySettings.inboundIp = v2rayInboundIp
v2raySettings.outboundIp = v2rayOutboundIp
v2raySettings.outboundPort = v2rayOutboundPort
v2raySettings.dnsName = v2rayDnsName
v2raySettings.save()
}
}

Expand Down
4 changes: 2 additions & 2 deletions IVPNClient/Utilities/Extensions/UserDefaults+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extension UserDefaults {
static let isLoggedIn = "isLoggedIn"
static let antiTrackerDns = "antiTrackerDns"
static let disableLanAccess = "disableLanAccess"
static let v2RayPorts = "v2RayPorts"
static let v2raySettings = "v2raySettings"
}

@objc dynamic var wireguardTunnelProviderError: String {
Expand Down Expand Up @@ -277,7 +277,7 @@ extension UserDefaults {
shared.removeObject(forKey: Key.isLoggedIn)
shared.removeObject(forKey: Key.antiTrackerDns)
shared.removeObject(forKey: Key.disableLanAccess)
shared.removeObject(forKey: Key.v2RayPorts)
shared.removeObject(forKey: Key.v2raySettings)
standard.removeObject(forKey: Key.serviceStatus)
standard.removeObject(forKey: Key.selectedHost)
standard.removeObject(forKey: Key.selectedExitHost)
Expand Down

0 comments on commit f142cf0

Please sign in to comment.