Unable to make web request - Xcode v10.2.1
Closed this issue · 1 comments
I am using Xcode 10.2.1
I have successfully connected a Tor thread/circuit, initialised a TorController, authenticated the TorController and used TorController.getSessionConfiguration to convert the tor configuration to a URL session configuration as per the usage example, but when attempting to make a web request to a public V3 hidden service I get an error in the console of:
FWIW I definitely have an internet connection, I have tried other peoples code but get the same error, have tried over a VPN but get the same error.
2019-06-12 08:46:13.718157+0800 BitSense[3824:1682501] TIC TCP Conn Failed [1:0x282b5f540]: 1:50 Err(50)
2019-06-12 08:46:13.720342+0800 BitSense[3824:1682501] Task <74A66B62-7ECA-4A49-9320-05F005F88AB7>.<1> HTTP load failed (error code: -1009 [1:50])
2019-06-12 08:46:13.720697+0800 BitSense[3824:1682525] Task <74A66B62-7ECA-4A49-9320-05F005F88AB7>.<1> finished with error - code: -1009
response = nil
error = Optional(Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x2811a90e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://bwgmnddl3uwcy2bupa7qilod4ecd5dsncdfb6zmdrgsyxgoxe2hurxid.onion/, NSErrorFailingURLKey=http://bwgmnddl3uwcy2bupa7qilod4ecd5dsncdfb6zmdrgsyxgoxe2hurxid.onion/, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, NSLocalizedDescription=The Internet connection appears to be offline.})
And here is the code:
func startTor() {
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .cachesDirectory, in: .userDomainMask)
let docsDir = dirPaths[0].path
let dataDir = URL(fileURLWithPath: docsDir, isDirectory: true).appendingPathComponent("tor", isDirectory: true)
do {
try FileManager.default.createDirectory(atPath: dataDir.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print("error = \(error.localizedDescription))")
}
let hashedPassword = "16:872860B76453A77D60CA2BB8C1A7042072093276A3D701AD684053EC4C".data(using: .utf8)!
let conf = TorConfiguration()
conf.dataDirectory = dataDir
conf.arguments = ["--socksport", "39050",
"--controlport", "127.0.0.1:39060"]
conf.cookieAuthentication = false
let thread = TorThread.init(configuration: conf)
thread.start()
DispatchQueue.main.asyncAfter(deadline: .now() + 15, execute: {
let controller = TorController.init(socketHost: "127.0.0.1", port: 39060)
do {
try controller.connect()
print("connected")
} catch {
print("error connecting")
}
DispatchQueue.main.asyncAfter(deadline: .now() + 15, execute: {
if controller.isConnected {
print("controller is connected")
controller.authenticate(with: hashedPassword, completion: { (success, error) in
if error != nil {
print("error = \(error!.localizedDescription)")
} else {
print("success authenticating")
controller.getSessionConfiguration({ (configuration) in
print("configuration = \(configuration.debugDescription)")
let session = URLSession.init(configuration: configuration!)
let url = URL(string: "http://bwgmnddl3uwcy2bupa7qilod4ecd5dsncdfb6zmdrgsyxgoxe2hurxid.onion")!
let task = session.dataTask(with: url) {(data, response, error) in
print("response = \(String(describing: response))")
print("error = \(String(describing: error))")
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()
})
}
})
} else {
print("controller not connected")
}
})
})
}
I fixed the issue where I was unable to make a web request by using this code as a reference:
https://github.com/vergecurrency/vIOS/blob/master/VergeiOS/Http/TorClient.swift