Closed Cyberbolt closed 3 months ago
Could you please provide a sample code for iOS to call NetworkExtension to capture Tun traffic and transfer it to socks5://127.0.0.1:12345 through Tun2SocksKit? Thank you very much for your patient guidance!
Either switch to the latest version or write the config to a file and provide the path...
You've already opened another issue regarding this and I told you what to do...
It is the latest version, but it seems that the traffic is not successfully redirected to socks5://127.0.0.1:12345. What is the problem with the code?
IF IT WAS THE LATEST VERSION YOUR CODE WOULD THROW A COMPILE ERROR.
You're obviously not using the latest version.
The version you're using only accepts config file path as input write the string into a config.yml and pass the path to the library
Alright, thank you very much. On February 21st, I cloned the repository in advance during your update process, resulting in the source code not being the final version.
@EbrahimTahernejad Hello, I used the latest version of Tun2SocksKit and it ran successfully, but the traffic was not directed to socks5://127.0.0.1:12345. Could you please take a look at what is wrong with my code?
import os.log
import NetworkExtension
import Tun2SocksKit
class PacketTunnelProvider: NEPacketTunnelProvider {
override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
os_log("%{public}@", log: log, type: .error, "调用 startTunnel")
// 配置虚拟接口
let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "192.0.2.2")
settings.mtu = NSNumber(value: 1500)
let ipv4Settings = NEIPv4Settings(addresses: ["192.0.2.1"], subnetMasks: ["255.255.255.0"])
ipv4Settings.includedRoutes = [NEIPv4Route.default()]
settings.ipv4Settings = ipv4Settings
// 启动Tun2Socks
let socks5Config = """
tunnel:
mtu: 1500
socks5:
port: 12345
address: 127.0.0.1
udp: 'udp'
misc:
task-stack-size: 20480
connect-timeout: 5000
read-write-timeout: 60000
log-file: stderr
log-level: debug
limit-nofile: 65535
"""
Socks5Tunnel.run(withConfig: .string(content: socks5Config)) { code in
if code == 0 {
print("Tun2Socks started successfully")
} else {
print("Tun2Socks failed to start, error code: \(code)")
}
}
self.setTunnelNetworkSettings(settings) { error in
if let error = error {
completionHandler(error)
return
}
completionHandler(nil)
}
}
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
completionHandler()
}
}
I took a look and there should be a tun_fd parameter. How do I pass it in?
tun_fd capturing is handled automatically inside Tun2SocksKit
, you don't need to pass anything related to it
Use it like this:
import os.log
import NetworkExtension
import Tun2SocksKit
class PacketTunnelProvider: NEPacketTunnelProvider {
func getNetworkSettings(with mtu: Int, ipv6: Bool = true) -> NEPacketTunnelNetworkSettings {
let settings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "254.1.1.1")
settings.mtu = NSNumber(integerLiteral: mtu)
settings.ipv4Settings = {
let settings = NEIPv4Settings(addresses: ["198.18.0.1"], subnetMasks: ["255.255.0.0"])
settings.includedRoutes = [NEIPv4Route.default()]
return settings
}()
settings.ipv6Settings = { [ipv6] () in
guard ipv6 else {
return nil
}
let settings = NEIPv6Settings(addresses: ["fd6e:a81b:704f:1211::1"], networkPrefixLengths: [64])
settings.includedRoutes = [NEIPv6Route.default()]
return settings
}()
settings.dnsSettings = NEDNSSettings(servers: ["1.1.1.1", "8.8.8.8"])
return
}
override func startTunnel(options: [String : NSObject]?, completionHandler: @escaping (Error?) -> Void) {
os_log("%{public}@", log: log, type: .error, "调用 startTunnel")
// 启动Tun2Socks
let socks5Config = """
tunnel:
mtu: 1500
socks5:
port: 12345
address: 127.0.0.1
udp: 'udp'
misc:
task-stack-size: 20480
connect-timeout: 5000
read-write-timeout: 60000
log-file: stderr
log-level: debug
limit-nofile: 65535
"""
self.setTunnelNetworkSettings(getNetworkSettings(mtu: 1500, ipv6: false)) { [socks5Config] error in
if let error = error {
completionHandler(error)
return
}
Socks5Tunnel.run(withConfig: .string(content: socks5Config)) { code in
if code == 0 {
print("Tun2Socks started successfully")
} else {
print("Tun2Socks failed to start, error code: \(code)")
}
}
completionHandler(nil)
}
}
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
completionHandler()
}
}
Thank you very much, I have successfully run
Unable to redirect Tun traffic to port socks5://127.0.0.1:12345
How should I change the code? Thanks!