mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-06-22 02:01:08 +07:00
fix: tun2socks auth settings (#2456)
* add parser auth/pass & fix port * fix generateRandomHex * remove hardcore port ios * add generated random port * fix sin6_port * fixed inbound * add error message * add std::runtime_error & fixed random generator * remove loop --------- Co-authored-by: Yaumenau Pavel <yaumenau.pavel@planetvpn.dev>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import Darwin
|
||||
import Foundation
|
||||
import NetworkExtension
|
||||
|
||||
@@ -6,6 +7,7 @@ enum XrayErrors: Error {
|
||||
case xrayConfigIsWrong
|
||||
case cantSaveXrayConfig
|
||||
case cantParseListenAndPort
|
||||
case cantAcquireLocalPort
|
||||
case cantSaveHevSocksConfig
|
||||
}
|
||||
|
||||
@@ -21,6 +23,42 @@ extension Constants {
|
||||
}
|
||||
|
||||
extension PacketTunnelProvider {
|
||||
/// TCP port chosen by the OS on IPv6 loopback (::1), matching inbound listen address.
|
||||
private func acquireFreeLocalPort() throws -> Int {
|
||||
let fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)
|
||||
guard fd != -1 else {
|
||||
throw XrayErrors.cantAcquireLocalPort
|
||||
}
|
||||
defer { close(fd) }
|
||||
var reuse: Int32 = 1
|
||||
_ = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, socklen_t(MemoryLayout<Int32>.size))
|
||||
var addr = sockaddr_in6()
|
||||
addr.sin6_len = UInt8(MemoryLayout<sockaddr_in6>.size)
|
||||
addr.sin6_family = sa_family_t(AF_INET6)
|
||||
addr.sin6_port = in_port_t(0).bigEndian
|
||||
addr.sin6_addr = in6addr_loopback
|
||||
addr.sin6_scope_id = 0
|
||||
let bindResult = withUnsafePointer(to: &addr) { ptr in
|
||||
ptr.withMemoryRebound(to: sockaddr.self, capacity: 1) { p in
|
||||
bind(fd, p, socklen_t(MemoryLayout<sockaddr_in6>.size))
|
||||
}
|
||||
}
|
||||
guard bindResult == 0 else {
|
||||
throw XrayErrors.cantAcquireLocalPort
|
||||
}
|
||||
var bound = sockaddr_in6()
|
||||
var len = socklen_t(MemoryLayout<sockaddr_in6>.size)
|
||||
let gr = withUnsafeMutablePointer(to: &bound) { p in
|
||||
p.withMemoryRebound(to: sockaddr.self, capacity: 1) { bp in
|
||||
getsockname(fd, bp, &len)
|
||||
}
|
||||
}
|
||||
guard gr == 0 else {
|
||||
throw XrayErrors.cantAcquireLocalPort
|
||||
}
|
||||
return Int(bound.sin6_port.byteSwapped)
|
||||
}
|
||||
|
||||
private func applyXraySplitTunnel(_ xrayConfig: XrayConfig,
|
||||
settings: NEPacketTunnelNetworkSettings) {
|
||||
guard let splitTunnelType = xrayConfig.splitTunnelType else {
|
||||
@@ -129,14 +167,11 @@ extension PacketTunnelProvider {
|
||||
return
|
||||
}
|
||||
|
||||
let port = 10808
|
||||
let port = try acquireFreeLocalPort()
|
||||
let address = "::1"
|
||||
|
||||
if var inboundsArray = jsonDict["inbounds"] as? [[String: Any]], !inboundsArray.isEmpty {
|
||||
inboundsArray[0]["port"] = port
|
||||
inboundsArray[0]["listen"] = address
|
||||
jsonDict["inbounds"] = inboundsArray
|
||||
}
|
||||
// Extract existing SOCKS5 credentials or generate new ones per session.
|
||||
let socksCredentials = ensureInboundAuth(jsonDict: &jsonDict, port: port, address: address)
|
||||
|
||||
let updatedData = try JSONSerialization.data(withJSONObject: jsonDict, options: [])
|
||||
|
||||
@@ -159,6 +194,8 @@ extension PacketTunnelProvider {
|
||||
self?.setupAndRunTun2socks(configData: updatedData,
|
||||
address: address,
|
||||
port: port,
|
||||
username: socksCredentials.username,
|
||||
password: socksCredentials.password,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
@@ -183,6 +220,62 @@ extension PacketTunnelProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private struct SocksCredentials {
|
||||
let username: String
|
||||
let password: String
|
||||
}
|
||||
|
||||
private func indexOfSocksInbound(in inboundsArray: [[String: Any]]) -> Int? {
|
||||
for (i, inbound) in inboundsArray.enumerated() {
|
||||
guard let proto = inbound["protocol"] as? String else { continue }
|
||||
if proto.caseInsensitiveCompare("socks") == .orderedSame {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns existing SOCKS5 credentials from the inbound config, or generates and injects
|
||||
// new random ones. Also sets port and address on the socks inbound entry.
|
||||
private func ensureInboundAuth(jsonDict: inout [String: Any], port: Int, address: String) -> SocksCredentials {
|
||||
var inboundsArray = jsonDict["inbounds"] as? [[String: Any]] ?? []
|
||||
|
||||
if let socksIdx = indexOfSocksInbound(in: inboundsArray) {
|
||||
var inbound = inboundsArray[socksIdx]
|
||||
inbound["port"] = port
|
||||
inbound["listen"] = address
|
||||
|
||||
var settings = inbound["settings"] as? [String: Any] ?? [:]
|
||||
if let accounts = settings["accounts"] as? [[String: Any]],
|
||||
let first = accounts.first,
|
||||
let user = first["user"] as? String, !user.isEmpty,
|
||||
let pass = first["pass"] as? String, !pass.isEmpty {
|
||||
// Re-use existing credentials, but always enforce auth mode in case the
|
||||
// imported config had accounts but auth: "noauth" (or no auth field).
|
||||
settings["auth"] = "password"
|
||||
inbound["settings"] = settings
|
||||
inboundsArray[socksIdx] = inbound
|
||||
jsonDict["inbounds"] = inboundsArray
|
||||
return SocksCredentials(username: user, password: pass)
|
||||
}
|
||||
|
||||
// Generate new random credentials for this session
|
||||
let user = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased().prefix(16)
|
||||
let pass = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
|
||||
settings["auth"] = "password"
|
||||
settings["accounts"] = [["user": String(user), "pass": pass]]
|
||||
inbound["settings"] = settings
|
||||
inboundsArray[socksIdx] = inbound
|
||||
jsonDict["inbounds"] = inboundsArray
|
||||
return SocksCredentials(username: String(user), password: pass)
|
||||
}
|
||||
|
||||
// Fallback: no socks inbound — generate credentials but can't inject
|
||||
let user = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased().prefix(16)
|
||||
let pass = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
|
||||
return SocksCredentials(username: String(user), password: pass)
|
||||
}
|
||||
|
||||
private func setupAndStartXray(configData: Data,
|
||||
completionHandler: @escaping (Error?) -> Void) {
|
||||
let path = Constants.cachesDirectory.appendingPathComponent("config.json", isDirectory: false).path
|
||||
@@ -214,6 +307,8 @@ extension PacketTunnelProvider {
|
||||
private func setupAndRunTun2socks(configData: Data,
|
||||
address: String,
|
||||
port: Int,
|
||||
username: String,
|
||||
password: String,
|
||||
completionHandler: @escaping (Error?) -> Void) {
|
||||
let config = """
|
||||
tunnel:
|
||||
@@ -221,6 +316,8 @@ extension PacketTunnelProvider {
|
||||
socks5:
|
||||
port: \(port)
|
||||
address: \(address)
|
||||
username: \(username)
|
||||
password: \(password)
|
||||
udp: 'udp'
|
||||
misc:
|
||||
task-stack-size: 20480
|
||||
|
||||
Reference in New Issue
Block a user