diff --git a/client/platforms/ios/PacketTunnelProvider+Xray.swift b/client/platforms/ios/PacketTunnelProvider+Xray.swift index f618be59..277980d6 100644 --- a/client/platforms/ios/PacketTunnelProvider+Xray.swift +++ b/client/platforms/ios/PacketTunnelProvider+Xray.swift @@ -60,23 +60,50 @@ extension PacketTunnelProvider { let dns = ["8.8.4.4","1.1.1.1"] settings.dnsSettings = NEDNSSettings(servers: dns) - setTunnelNetworkSettings(settings) { [weak self] error in - if let error { - completionHandler(error) + do { + let port = 10808 + let address = "::1" + + let jsonDict = try JSONSerialization.jsonObject(with: xrayConfigData, + options: []) as? [String: Any] + + guard var jsonDict else { + xrayLog(.error, message: "Can't parse address and port for hevSocks") + completionHandler(XrayErrors.cantParseListenAndPort) return } - // Launch xray - self?.setupAndStartXray(configData: xrayConfigData) { xrayError in - if let xrayError { - completionHandler(xrayError) + if var inboundsArray = jsonDict["inbounds"] as? [[String: Any]], !inboundsArray.isEmpty { + inboundsArray[0]["port"] = port + inboundsArray[0]["listen"] = address + jsonDict["inbounds"] = inboundsArray + } + + let updatedData = try JSONSerialization.data(withJSONObject: jsonDict, options: []) + + setTunnelNetworkSettings(settings) { [weak self] error in + if let error { + completionHandler(error) return } - // Launch hevSocks - self?.setupAndRunTun2socks(configData: xrayConfigData, - completionHandler: completionHandler) + // Launch xray + self?.setupAndStartXray(configData: updatedData) { xrayError in + if let xrayError { + completionHandler(xrayError) + return + } + + // Launch hevSocks + self?.setupAndRunTun2socks(configData: updatedData, + address: address, + port: port, + completionHandler: completionHandler) + } } + } catch { + completionHandler(error) + return } } @@ -104,29 +131,9 @@ extension PacketTunnelProvider { } private func setupAndRunTun2socks(configData: Data, + address: String, + port: Int, completionHandler: @escaping (Error?) -> Void) { - var port = 10808 - var address = "::1" - - let jsonDict = try? JSONSerialization.jsonObject(with: configData, options: []) as? [String: Any] - - guard let jsonDict else { - xrayLog(.error, message: "Can't parse address and port for hevSocks") - completionHandler(XrayErrors.cantParseListenAndPort) - return - } - - // Xray listen and port should be the same as port and address in hevSocks - if let inbounds = jsonDict["inbounds"] as? [[String: Any]], let inbound = inbounds.first { - if let listen = inbound["listen"] as? String { - address = listen - address.removeAll { $0 == "[" || $0 == "]" } - } - if let portFromConfig = inbound["port"] as? Int { - port = portFromConfig - } - } - let config = """ tunnel: mtu: 9000 diff --git a/client/platforms/ios/WGConfig.swift b/client/platforms/ios/WGConfig.swift index 3dbdf7d0..e3b67efe 100644 --- a/client/platforms/ios/WGConfig.swift +++ b/client/platforms/ios/WGConfig.swift @@ -13,7 +13,7 @@ struct WGConfig: Decodable { let clientIP: String let clientPrivateKey: String let serverPublicKey: String - let presharedKey: String + let presharedKey: String? var allowedIPs: [String] var persistentKeepAlive: String let splitTunnelType: Int @@ -65,7 +65,7 @@ struct WGConfig: Decodable { \(settings) [Peer] PublicKey = \(serverPublicKey) - PresharedKey = \(presharedKey) + \(presharedKey == nil ? "" : "PresharedKey = \(presharedKey!)") AllowedIPs = \(allowedIPs.joined(separator: ", ")) Endpoint = \(hostName):\(port) PersistentKeepalive = \(persistentKeepAlive)