Skip to content

Commit

Permalink
Merge pull request #371 from ivpn/feature/v2ray
Browse files Browse the repository at this point in the history
Obfuscation with V2Ray
  • Loading branch information
jurajhilje authored Nov 12, 2023
2 parents 88b1440 + d0450f0 commit e2ce503
Show file tree
Hide file tree
Showing 44 changed files with 1,787 additions and 290 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ OpenVPNConf.swift
# Fastlane
fastlane/test_output
fastlane/report.xml
fastlane/Appfile
fastlane/Appfile

# Frameworks
Frameworks
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ file_length:
ignore_comment_only_lines: true
cyclomatic_complexity:
warning: 15
error: 35
error: 45
reporter: "xcode"
69 changes: 47 additions & 22 deletions IVPNClient.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions IVPNClient/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ struct Config {
static let wgKeyExpirationDays = 30
static let wgKeyRegenerationRate = 1

// MARK: V2Ray

static let v2rayHost = "127.0.0.1"
static let v2rayPort = 16661

// MARK: ENV variables

static var Environment: String {
Expand Down
16 changes: 7 additions & 9 deletions IVPNClient/Enums/AddressType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,22 @@
// along with the IVPN iOS app. If not, see <https://www.gnu.org/licenses/>.
//

import Network

enum AddressType {

case IPv6
case IPv4
case other

static func validateIpAddress(ipToValidate: String) -> AddressType {
var sin = sockaddr_in()
if ipToValidate.withCString({ cstring in inet_pton(AF_INET, cstring, &sin.sin_addr) }) == 1 {
static func validateIpAddress(_ address: String) -> AddressType {
if let _ = IPv4Address(address) {
return .IPv4
}

var sin6 = sockaddr_in6()
if ipToValidate.withCString({ cstring in inet_pton(AF_INET6, cstring, &sin6.sin6_addr) }) == 1 {
} else if let _ = IPv6Address(address) {
return .IPv6
} else {
return .other
}

return .other
}

}
42 changes: 37 additions & 5 deletions IVPNClient/Enums/ConnectionSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ enum ConnectionSettings {
return "OpenVPN, UDP \(port)"
}
case .wireguard(_, let port):
return "WireGuard, UDP \(port)"
return "WireGuard, \(wireguardProtocol()) \(port)"
}
}

Expand All @@ -60,8 +60,12 @@ enum ConnectionSettings {
case .udp:
return "OpenVPN, UDP"
}
case .wireguard:
return "WireGuard, UDP"
case .wireguard(_, let port):
if UserDefaults.shared.isV2ray {
return "WireGuard, \(wireguardProtocol()) \(port)"
}

return "WireGuard, \(wireguardProtocol())"
}
}

Expand Down Expand Up @@ -104,7 +108,27 @@ enum ConnectionSettings {
return "UDP \(port)"
}
case .wireguard(_, let port):
return "UDP \(port)"
return "\(wireguardProtocol()) \(port)"
}
}

func formatProtocolMultiHop() -> String {
switch self {
case .ipsec:
return "IKEv2"
case .openvpn(let proto, _):
switch proto {
case .tcp:
return "TCP"
case .udp:
return "UDP"
}
case .wireguard(_, let port):
if UserDefaults.shared.isV2ray {
return "\(wireguardProtocol()) \(port)"
}

return "\(wireguardProtocol())"
}
}

Expand Down Expand Up @@ -227,8 +251,16 @@ enum ConnectionSettings {
return "UDP"
}
case .wireguard:
return "UDP"
return wireguardProtocol()
}
}

func wireguardProtocol() -> String {
if UserDefaults.shared.isV2ray && UserDefaults.shared.v2rayProtocol == "tcp" {
return "TCP"
}

return "UDP"
}

static func == (lhs: ConnectionSettings, rhs: ConnectionSettings) -> Bool {
Expand Down
1 change: 0 additions & 1 deletion IVPNClient/IVPNClient-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#include "../WireGuardKitC/WireGuardKitC.h"
#include "Utilities/Logging/ringlogger.h"
#include "liboqs/include/oqs/oqs.h"
2 changes: 2 additions & 0 deletions IVPNClient/IVPNClient.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<string>NSFileProtectionCompleteUntilFirstUserAuthentication</string>
<key>com.apple.developer.networking.networkextension</key>
<array>
<string>app-proxy-provider</string>
<string>content-filter-provider</string>
<string>packet-tunnel-provider</string>
</array>
<key>com.apple.developer.networking.vpn.api</key>
Expand Down
30 changes: 30 additions & 0 deletions IVPNClient/Managers/ConnectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ class ConnectionManager {
self.evaluateCloseApp()
}
}
DispatchQueue.delay(2.5) {
if UserDefaults.shared.isV2ray && !V2RayCore.shared.reconnectWithV2ray {
V2RayCore.shared.reconnectWithV2ray = true
self.reconnect()
} else {
V2RayCore.shared.reconnectWithV2ray = false
}
}
} else {
self.connected = false
}
Expand Down Expand Up @@ -247,6 +255,17 @@ class ConnectionManager {
return
}

if UserDefaults.shared.isV2ray && V2RayCore.shared.reconnectWithV2ray {
DispatchQueue.global(qos: .userInitiated).async {
let error = V2RayCore.shared.start()
if error != nil {
log(.error, message: error?.localizedDescription ?? "")
} else {
log(.info, message: "V2Ray start OK")
}
}
}

self.vpnManager.connect(tunnelType: self.settings.connectionProtocol.tunnelType())
}
}
Expand All @@ -263,6 +282,17 @@ class ConnectionManager {
}
}
}

if UserDefaults.shared.isV2ray {
DispatchQueue.global(qos: .userInitiated).async {
let error = V2RayCore.shared.close()
if error != nil {
log(.error, message: error?.localizedDescription ?? "")
} else {
log(.info, message: "V2Ray stop OK")
}
}
}
}

func installOnDemandRules() {
Expand Down
1 change: 1 addition & 0 deletions IVPNClient/Models/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct Host: Codable {
var ipv6: IPv6?
var multihopPort: Int
var load: Double
var v2ray: String

func localIPAddress() -> String {
if let range = localIP.range(of: "/", options: .backwards, range: nil, locale: nil) {
Expand Down
Loading

0 comments on commit e2ce503

Please sign in to comment.