parent
760f935965
commit
2bceb9f7ba
2 changed files with 41 additions and 34 deletions
|
@ -60,6 +60,27 @@ extension PacketTunnelProvider {
|
|||
let dns = ["8.8.4.4","1.1.1.1"]
|
||||
settings.dnsSettings = NEDNSSettings(servers: dns)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -67,17 +88,23 @@ extension PacketTunnelProvider {
|
|||
}
|
||||
|
||||
// Launch xray
|
||||
self?.setupAndStartXray(configData: xrayConfigData) { xrayError in
|
||||
self?.setupAndStartXray(configData: updatedData) { xrayError in
|
||||
if let xrayError {
|
||||
completionHandler(xrayError)
|
||||
return
|
||||
}
|
||||
|
||||
// Launch hevSocks
|
||||
self?.setupAndRunTun2socks(configData: xrayConfigData,
|
||||
self?.setupAndRunTun2socks(configData: updatedData,
|
||||
address: address,
|
||||
port: port,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
completionHandler(error)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func stopXray(completionHandler: () -> Void) {
|
||||
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue