nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

how to connect to a server with self-signed certs? #93

Closed jauyou closed 8 years ago

jauyou commented 8 years ago

I'm trying to connect to my server using Websockets.

The CA and certificate has already installed in my iPhone.

my server use TLS 1.2 so I add secure to my options.

NSString *url = [NSString stringWithFormat:@"%@:55004", [SocketConnectConfig getServerBaseUrl]];

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

socket = [[SocketIOClient alloc] initWithSocketURL:url options:@{@"log": @YES, @"forceWebsockets": @YES, @"secure": @YES}];

[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
        NSLog(@"socket connected");

then I get those errors 2015-11-19 09:38:21.996 socketConnect[1411:556211] Log SocketEngine: Handshaking 2015-11-19 09:38:22.039 socketConnect[1411:559665] CFNetwork SSLHandshake failed (-9807) 2015-11-19 09:38:22.046 socketConnect[1411:559665] ERROR SocketIOClient: The operation couldn’t be completed. (OSStatus error -9807.) 2015-11-19 09:38:22.047 socketConnect[1411:559665] Log SocketIOClient: Handling event: error with data: ( "The operation couldn\U2019t be completed. (OSStatus error -9807.)" )

Then I set selfSignedSSL in WebSocket.swift

public var selfSignedSSL = true

the results are as follows 2015-11-20 09:45:30.758 socketConnect[624:134808] Log SocketIOClient: Adding handler for event: connect 2015-11-20 09:45:30.760 socketConnect[624:134808] Log SocketIOClient: Adding engine 2015-11-20 09:45:30.760 socketConnect[624:134808] Log SocketEngine: Starting engine 2015-11-20 09:45:30.761 socketConnect[624:134808] Log SocketEngine: Handshaking

2015-11-20 09:51:57.669 socketConnect[633:136912] ERROR SocketIOClient: The operation couldn’t be completed. Operation timed out 2015-11-20 09:51:57.670 socketConnect[633:136912] Log SocketIOClient: Handling event: error with data: ( "The operation couldn\U2019t be completed. Operation timed out" )

Is it correct to set selfSignedSSL for self-signed certs? Does the client use wrong way to connect server? Any suggestion will be appreciated.

nuclearace commented 8 years ago

You are trying to connect to a socket.io server correct?

jauyou commented 8 years ago

I create a socket.io server listen on port 55004

var cipher = require('../../common/cipher');
cipher.unlock(cipher.k, '../../cert/.application.keystore', function cb (err, obj) {
    if (!err) {
        try {
           httpsServer = https.createServer({
                pfx: fs.readFileSync('../../cert/certificate.pfx'),
                passphrase: ""
            }, app).listen(config.basicServer.sport); 
        } catch (e) {
            err = e;
            httpsServer = null;
        }
        console.log('registerSocketIO');
        registerSocketIO(); 
    }
    if (err) {
        console.log('Failed to setup secured server:', err);
        console.error('Failed to setup secured server:', err);
        return process.exit();
    }
});

function registerSocketIO() {
    if(io) return;

    //socket.io 0.9.16
    if(httpServer) {
        io = socketio.listen(httpServer, {log: false});
    }
    if(httpsServer) {
        io = socketio.listen(httpsServer, {log: false});
    }
    io.set('log level', 0);
    io.set('authorization', socketio_authorize); 
    io.on('connection', function (socket) {
        console.log('connected', socket.id);
        ...
    } 
}
jauyou commented 8 years ago

@nuclearace The problem is socket.io version

I found server side version is 0.9.16 This framework support 1.0+

Thanks.