Skip to content

Commit

Permalink
feat: create V2RayPorts.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Sep 18, 2023
1 parent b2ffbda commit 69fa660
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
4 changes: 4 additions & 0 deletions IVPNClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +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 */; };
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 @@ -508,6 +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>"; };
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 @@ -929,6 +931,7 @@
isa = PBXGroup;
children = (
8247C05F2A7CF54300A7C02F /* V2RayConfig.swift */,
82365E7E2AB86020006434C3 /* V2RayPorts.swift */,
821E35572A95DCD200AEE5C7 /* V2RayCore.swift */,
821E35592A95F77700AEE5C7 /* config.json */,
);
Expand Down Expand Up @@ -2284,6 +2287,7 @@
822563922431E03A00AE7F8D /* AccountView.swift in Sources */,
9CBFF02C21021D2100FE1757 /* ConnectionManager.swift in Sources */,
82D598C821A6ADF1000FABDE /* NetworkType.swift in Sources */,
82365E7F2AB86020006434C3 /* V2RayPorts.swift in Sources */,
9CC29DF81E1D59E90080F799 /* Alerts+Ext.swift in Sources */,
9CB2CE1F1DAA5258007A4D2D /* Authentication.swift in Sources */,
82DAB37B2457013900302F4C /* Service.swift in Sources */,
Expand Down
53 changes: 53 additions & 0 deletions IVPNClient/Models/V2Ray/V2RayPorts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// V2RayPorts.swift
// IVPN iOS app
// https://github.com/ivpn/ios-app
//
// Created by Juraj Hilje on 2023-09-18.
// Copyright (c) 2023 IVPN Limited.
//
// This file is part of the IVPN iOS app.
//
// The IVPN iOS app 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.
//
// The IVPN iOS app 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 the IVPN iOS app. If not, see <https://www.gnu.org/licenses/>.
//

import Foundation

struct V2RayPorts: Codable {

let id: String
let openvpn: [V2RayPort]
let wireguard: [V2RayPort]

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

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) {
return loaded
}
}

return nil
}

}

struct V2RayPort: Codable {
let type: String
let port: Int
}
18 changes: 17 additions & 1 deletion IVPNClient/Models/VPNServerList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class VPNServerList {
portRanges.append(PortRange(tunnelType: "OpenVPN", protocolType: "TCP", ranges: tcpRanges))
}
}

if let wireguard = portsObj["wireguard"] as? [[String: Any]] {
var ranges = [CountableClosedRange<Int>]()
for port in wireguard {
Expand All @@ -206,6 +207,20 @@ class VPNServerList {
portRanges.append(PortRange(tunnelType: "WireGuard", protocolType: "UDP", ranges: ranges))
}
}

if let v2ray = portsObj["v2ray"] as? [String: Any] {
if let wireguard = v2ray["wireguard"] as? [[String: Any]] {
var ports = [V2RayPort]()
for port in wireguard {
let type = port["type"] as? String ?? ""
let port = port["port"] as? Int ?? 0
ports.append(V2RayPort(type: type, port: port))
}
let id = v2ray["id"] as? String ?? ""
let v2rayPorts = V2RayPorts(id: id, openvpn: [], wireguard: ports)
v2rayPorts.save()
}
}
}
}
}
Expand Down Expand Up @@ -399,7 +414,8 @@ class VPNServerList {
publicKey: host["public_key"] as? String ?? "",
localIP: host["local_ip"] as? String ?? "",
multihopPort: host["multihop_port"] as? Int ?? 0,
load: host["load"] as? Double ?? 0
load: host["load"] as? Double ?? 0,
v2ray: host["v2ray"] as? String ?? ""
)

if let ipv6 = host["ipv6"] as? [String: Any] {
Expand Down
2 changes: 2 additions & 0 deletions IVPNClient/Utilities/Extensions/UserDefaults+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extension UserDefaults {
static let isLoggedIn = "isLoggedIn"
static let antiTrackerDns = "antiTrackerDns"
static let disableLanAccess = "disableLanAccess"
static let v2RayPorts = "v2RayPorts"
}

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

0 comments on commit 69fa660

Please sign in to comment.