SSL Handshake fail when trying to download a file from an FTPS - swift

What I'm doing?
I'm working on a iOS app which is going to download files from an FTPS server. For this purpose I'm using the library FilesProvider.
Error description
Until now I successfully achieve to login in the server, list files and search for files, but I'm getting the following error when trying to download one of the files:
File Provider <FilesProvider.FTPFileProvider: 0x283c23900> shouldDoOperation Copy with action Copying and destination file:///private/var/mobile/Containers/Data/Application/90AF4202-18C1-4A41-B461-4FB262FD39B9/tmp/B13A8110-C919-48E4-8BD9-E684929310C0.tmp
2020-05-27 14:35:39.372289+0200 MyApp[548:100799] [] nw_socket_handle_socket_event [C13:1] Socket SO_ERROR [54: Connection reset by peer]
2020-05-27 14:35:39.595959+0200 MyApp[548:99892] CFNetwork SSLHandshake failed (-9806)
2020-05-27 14:35:39.596380+0200 MyApp[548:99892] TCP Conn 0x28274f540 SSLHandshake failed (-9806)
File Provider <FilesProvider.FTPFileProvider: 0x283c23900> Failed for operation Copy with action Copying and destination file:///private/var/mobile/Containers/Data/Application/90AF4202-18C1-4A41-B461-4FB262FD39B9/tmp/B13A8110-C919-48E4-8BD9-E684929310C0.tmp
Throwing Error: Error Domain=NSOSStatusErrorDomain Code=-9806 "(null)" UserInfo={_kCFStreamErrorCodeKey=-9806, _kCFStreamErrorDomainKey=3}
FTPFileProvider is an object created with the library I've mention above that handles the FTP connection. That provider looks like:
guard let url = URL(string: "ftps://X.X.X.X") else { return } // I have to use an IP address instead of a domain
var provider = FTPFileProvider(baseURL: url, mode: .default, credential: credential, cache: .none)
provider.delegate = self
provider.fileOperationDelegate = self // This delegate is only for print the first line of the error
provider.serverTrustPolicy = .disableEvaluation
After creating the provider of the connection, I've been able to do login in the server, search some files and get the file list. I'm doing that with this function:
provider.searchFiles(path: remotePath, recursive: false, query: predicate, foundItemHandler: { (file) in print("File found with name: \(file.name)") }, completionHandler: { (list, error) in
if error != nil {
DispatchQueue.main.async {
onError(error!)
}
} else {
var files:[String] = []
for f in list {
(f.isRegularFile) ? files.append(f.name) : nil
}
DispatchQueue.main.async {
onSucess(files)
}
}
})
When running that search I get this warning:
2020-05-27 14:45:51.831812+0200 MyApp[555:102153] [] nw_socket_handle_socket_event [C4:1] Socket SO_ERROR [54: Connection reset by peer]
But I successfully get an output in onSuccess(files). The returned value for files is:
["20200527-093234-28346646454.pdf", "20200527-105409-28346646454.pdf"]
After that search, I try to download one of the files is when I get the error describe at the beginning of this post. For do the download I have the following function:
provider.copyItem(path: "\(remotePath)/\(file)", to: localPath.absoluteString, overwrite: true) { (error) in
if error != nil {
DispatchQueue.main.async {
onError(error!)
}
} else {
DispatchQueue.main.async {
onSuccess(localPath)
}
}
}
What I've try
As you could see above, the object who connect to the server has disabled the SSL certificate checks. That's why I can do the login and search for the files.
I've configure the Info.plist disabling ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key> <!-- Because I wan't to allow everything -->
<true/>
<key>NSAllowsLocalNetworking</key> <!-- Because seems like library uses AVFoundation framework -->
<true/>
<key>NSAllowsArbitraryLoadsForMedia</key> <!-- Because I'm using a public IP instead a domain -->
<true/>
</dict>
Neither of that options works.
Some facts
Domain DNS entry is not going to be created for now. I can't do anything about this.
Server do have a valid SSL certificate.
I can successfully login, list files and search for files. (I just can't download)
I didn't try to upload a file. App isn't going to do it.
Questions
Why login, listing or searching files works but I get that error when trying to download?
Any idea on how to fix it? Any workaround?

Related

How to handle connection loss uploading to Firebase iOS?

I have a frontend Swift application in which users are to upload large videos and photos to Firebase Storage. I am currently working on error handling. The documentation does a good job of explaining error handling from Google's sever side of things, however it does not cover how to deal with connection loss.
This is the error handling directly from the documentation:
...
// Upload file and metadata to the object 'images/mountains.jpg'
let uploadTask = storageRef.putFile(from: localFile, metadata: metadata)
uploadTask.observe(.failure) { snapshot in
if let error = snapshot.error as? NSError {
switch (StorageErrorCode(rawValue: error.code)!) {
case .objectNotFound:
// File doesn't exist
break
case .unauthorized:
// User doesn't have permission to access file
break
case .cancelled:
// User canceled the upload
break
/* ... */
case .unknown:
// Unknown error occurred, inspect the server response
break
default:
// A separate error occurred. This is a good place to retry the upload.
break
}
}
}
I have done some tests on my device where the upload begins with no network connection. The following gets automatically printed to the console every second or so after the network goes down:
2020-07-06 01:38:28.361559-0700 Rage[12281:1978025] Connection 9: failed to connect 1:50, reason -1
2020-07-06 01:38:28.361648-0700 Rage[12281:1978025] Connection 9: encountered error(1:50)
2020-07-06 01:38:28.366906-0700 Rage[12281:1978025] Task <320E9387-F725-4AEA-B3BE-76425F73F9F7>.<6> HTTP load failed, 0/0 bytes (error code: -1009 [1:50])
2020-07-06 01:38:28.368381-0700 Rage[12281:1978042] Task <320E9387-F725-4AEA-B3BE-76425F73F9F7>.<6> finished with error [-1009] Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x280a581e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <320E9387-F725-4AEA-B3BE-76425F73F9F7>.<6>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <320E9387-F725-4AEA-B3BE-76425F73F9F7>.<6>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://firebasestorage.googleapis.com/v0/b/rage-5940.appspot.com/o/event%2FBEC3B24F-5F97-4B6A-8FD3-5DC2F7D37AFB.mp4?uploadType=resumable&name=event%2FBEC3B24F-5F97-4B6A-8FD3-5DC2F7D37AFB.mp4, NSErrorFailingURLKey=https://firebasestorage.googleapis.com/v0/b/rage-5940.appspot.com/o/event%2FBEC3B24F-5F97-4B6A-8FD3-5DC2F7D37AFB.mp4?uploadType=resumable&name=event%2FBEC3B24F-5F97-4B6A-8FD3-5DC2F7D37AFB.mp4, _kCFStreamErrorDomainKey=1}
Is this Firebase throwing these errors? If so they are probably being thrown trying to create a storageRef before the uploadTask could even begin, and thus escape any provided error handling.
Is there anyway to catch these network errors?
Did you try this?
if error._code == NSURLErrorNetworkConnectionLost {
}
there are a couple of more you can check with them:
NSURLErrorTimedOut
NSURLErrorNotConnectedToInternet
NSURLErrorCannotConnectToHost

http APi request failed to connect error 1004

I have a problem with my http request from my custom Api using URLSession in Xcode with Swift. This is my code:
let forecast = self.forecastList[forecastIndex]
let url : URL = URL(string: "https:laundryireland.tk/getForecast?pwd=\(self.CustomApiKey)&temp=\(forecast.temp)&hum=\(forecast.humidity)&pres=\(forecast.pressure)&weat=\(forecast.weather)&wind=\(forecast.windspeed)")!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
do {
let scores = try JSONDecoder().decode(Scores.self, from: data)
// ...
} catch {
print("ops")
}
}
task.resume()
This code throw an error at the third line already:
2019-11-04 12:53:26.701212+0000 DIYP[2554:1119528] [] tcp_input [C6.1:3] flags=[R.] seq=0, ack=2949324956, win=0 state=SYN_SENT rcv_nxt=0, snd_una=2949324955
2019-11-04 12:53:26.702757+0000 DIYP[2554:1119528] Connection 6: received failure notification
2019-11-04 12:53:26.702843+0000 DIYP[2554:1119528] Connection 6: failed to connect 1:61, reason -1
2019-11-04 12:53:26.702897+0000 DIYP[2554:1119528] Connection 6: encountered error(1:61)
2019-11-04 12:53:26.706142+0000 DIYP[2554:1119528] Task <46A61430-630E-48F8-B121-82B1C7BEE5DF>.<2> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
I saw here: TIC TCP Conn Failed [4:0x604000360300]: 1:61 Err(61) <1> HTTP load failed (error code: -1004 that the error 1004 is something related to the device not able to resolve the host but this seems odd to me because my URL is on a registered domain running on a remote server which is accessible from everywhere (just type: http://laundryireland.tk in your browser to see that it's working).
How do I fix this issue?
So the problem was first of all a typo: my domain is http://laundryireland.tk while in the code I wrote https://laundryireland.tk .
I found out is still possible to change the app settings to allow http connections: right click on Info.plist, Open As > Source Code then copy paste the following code right after <dict> at the top:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

I would like my QuickBooks WebConnector (QBWC) to connect to my dev server node-soap

There are two servers, my development server and the server Quick Books is currently installed on.
This is my .QWC file that is saved on the Quick Books server.
<?xml version="1.0" encoding="utf-8"?>
<QBWCXML>
<AppName>QBWebService</AppName>
<AppID></AppID>
<AppURL>https://<url>/soap.js</AppURL>
<AppDescription>This is The App</AppDescription>
<AppSupport>https://<url></AppSupport>
<UserName>User</UserName>
<OwnerID>{GUID}</OwnerID>
<FileID>{GUID}</FileID>
<QBType>QBFS</QBType>
</QBWCXML>
And this below my code in the soap.js file on my dev server.
var soap = require('soap'),
fs = require('fs'),
xml = fs.readFileSync('wsdl.wsdl', 'utf8'),
https = require('https'),
options = {
key: fs.readFileSync('/path/to/key'),
cert: fs.readFileSync('/path/to/crt'),
ca: [
fs.readFileSync('/path/to/crt', 'utf8'),
fs.readFileSync('/path/to/crt', 'utf8'),
fs.readFileSync('/path/to/crt', 'utf8')
]
};
var server = https.createServer(options, function(request, response) {
response.end("404: Not Found: " + request.url);
});
var myService = {
'QBWebConnectorSvc': {
'QBWebConnectorSvcSoap': {
authenticate: function(args) {
return {
authenticateResult: { string: [guid(), {}]}
};
}
}
}
};
server.listen(443, function() {
console.log('Listening Jimbo');
});
soap.listen(server, '/wsdl', myService, xml);
var thisUrl = './wsdl/wsdl.wsdl';
soap.createClient(thisUrl, function(err, client) {
if(err) {
console.log('Error soap create client:');
console.log(err);
}
else {
console.log('In soap create client else');
console.log('Client:');
console.log(client);
console.log('This should be describe');
client.setSecurity(new soap.ClientSSLSecurity('/Path/to/Server/key', '/path/to/server/crt', function(err, secure) {
if(err) {
console.log('Error Not Secure:');
}
else {
console.log('Secure');
console.log(secure);
client.QBWebConnectorSvc.QBWebConnectorSvcSoap.authenticate(function(err, done) {
if(err) {
console.log('Err Auth:');
console.log(err);
}
else {
console.log('Auth Done:');
console.log(done);
}
});
}
}));
The problem is that the Web Connector keeps saying that it cannot verify my certs, "QBWC1048: QuickBooks Web Connector could not verify the web application server certificate.". I know the certs are good and the firewall is open between the two computers.
Below is from the WebConnector log file.
> 20160906.20:29:30 UTC : QBWebConnector.WebServiceManager.ReadQWC(QWCReader QWC) : Parsing application configuration xml file to load its content to variables
20160906.20:29:51 UTC : : QBWC1048: QuickBooks Web Connector could not verify the web application server certificate.Certificate URL:https://<url>/soap.jsStackTrace:at System.Net.HttpWebRequest.GetResponse()
at QBWebConnector.QWCReader.CheckCertURL()Message (description of theexception):Unable to connect to the remote server Source (name of application or object that caused the exception):SystemTargetSite (method that threw the exception):System.Net.WebResponse GetResponse()InnerException:System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond <IP>:443
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
atSystem.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,IAsyncResult asyncResult, Exception& exception)
20160906.20:29:58 UTC :QBWebConnector.WebServiceManager.ReadQWC(QWCReader QWC) : QBWC1048: QuickBooks Web Connector could not verify the web application server certificate.
QBWC1051: The new application was not added
20160906.20:29:58 UTC : : ~SingleInstanceHandler() - usingInstanceChannel = false. Returning without any Registry key delete or unmarshalling.
If you need any more information, please ask.
Any help would be much appreciated. Thank you.

Adding a websocket "put" request in the bootstrap.js file in sails : cannot find io

I need to call a socket request from the bootstrap.js file in sails.
The bootstrap.js file has some code checking if some game engine has updated some file. If so, it needs send a message with some updated data via socket to some defined route called "/update"... e.g.
io.socket.put('/update', {history:{sessions:[1,2,3,4]}},function gotResponse(body, response) {
console.log('Server sending request ot server ');
})
The problem is that it tells me that io is not recognised.
I tried to do npm install for both sails.io.js and socket.io-client and then write:
var io = require('sails.io.js')( require('socket.io-client') );
at the top.
Unfortunately, it gives me the following error message:
C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\socket.io-client\lib\url.js:29
if (null == uri) uri = loc.protocol + '//' + loc.host;
^
TypeError: Cannot read property 'protocol' of undefined
at url (C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\socket.io-client\lib\url.js:29:29)
at lookup (C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\socket.io-client\lib\index.js:44:16)
at goAheadAndActuallyConnect (C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\sails.io.js\sails.io.js:835:21)
at selfInvoking (C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\sails.io.js\sails.io.js:812:18)
at SailsSocket.SailsIOClient.SailsSocket._connect (C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\sails.io.js\sails.io.js:831:9)
at null._onTimeout (C:\Users\Evolver\Documents\programming\pipegame\game6\node_modules\sails.io.js\sails.io.js:1463:17)
at Timer.listOnTimeout (timers.js:92:15)
Any idea ?
Ok.
It now works, once npm install has been done for socket.io-client and sails.io.js if I do exactly the following:
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// Instantiate the socket client (`io`)
var io = sailsIOClient(socketIOClient);
io.sails.url = 'http://localhost:1337';
// then I send something via my socket
io.socket.put('/update', {history:{sessions:[1,2,3,4]}},function gotResponse(body, response) {
console.log('Server sending request ot server ');
})

fulljid is empty after connection to BOSH service with XMPHP

I am trying to pre-bind an XMPP session via XMPHP and pass the rid/sid/jid to a strophe client to attach to the session.
connection code here:
$conn = new CIRCUIT_BOSH('server.com', 7070, $username, $pass, $resource, 'server.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_VERBOSE);
$conn->autoSubscribe();
try{
$conn->connect('http://xmpp.server.com/http-bind', 1, true);
$log->lwrite('Connected!');
}catch(XMPPHP_Exception $e){
die($e->getMessage());
}
I am getting the rid and sid but the fulljid in the $conn object stays empty and I cant see a session started on my openfire admin console.
If I create the jid manually by using the given resource and passing jid/rid/sid to strophe to use in attach, I get the ATTACHED status and I see calls from the client to the BOSH ip but I still dont see a session and I cant use the connection.
Strophe Client Code:
Called on document ready:
var sid = $.cookie('sid');
var rid = $.cookie('rid');
var jid = $.cookie('jid');
$(document).trigger('attach', {
sid: sid,
rid: rid,
jid: jid,
});
$(document).bind('attach', function (ev, data) {
var conn = new Strophe.Connection(
"http://xmpp.server.com/http-bind");
conn.attach(data.jid, data.sid, data.rid, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
} else if (status === Strophe.Status.ATTACHED){
$(document).trigger('attached');
}
});
Object.connection = conn;
});
I think the problem starts on the XMPPHP side which is not creating the session properly.
'attached' is triggered but never 'connected', is status 'connected' supposed to be sent?
What am I missing?
Ok, solved, I saw that XMPPHP lib didn't create a session at all on the openfire server, so I wrote a simple test for the XMPP class which was good and created the session, and for the XMPP_BOSH class that didn't manage create one. Then I saw the issue report here: http://code.google.com/p/xmpphp/issues/detail?id=47 comment no.9 worked, it fixed the issue by copying the processUntil() function from the XMLStream.php to BOSH.php, still can't figure out why this is working. Then I found I had an overlapping bug also with some of the passwords set for users on the openfire server. These passwords contained these ! # % ^ characters, for some reason the XMPP_BOSH is sending the password corrupted or changed so I got Auth Failed exception. Changing the password fixed the issue and I can now attach to the session XMPPHP created with the Strophe.js library.