0

I'm playing with GCDWebServer class (it is great!) and was wondering if it is possible to limit allowed connections by remote IP.

server.addDefaultHandlerForMethod("GET", requestClass: GCDWebServerRequest.self, processBlock: {request in

    let remote = request.remoteAddressString.substringToIndex(request.remoteAddressString.lastIndexOf(":")!)
    let local =  request.localAddressString.substringToIndex(request.localAddressString.lastIndexOf(":")!)

    print("\(remote) vs \(local)")
    if remote == local {
        return nil
    }

    //Forbidden
    return GCDWebServerResponse(statusCode: 403);

})

I hoped above will work but it isnt, I mean it sends 403 Forbidden when IP's are different but otherwise it ends up with:

[DEBUG] Connection aborted with status code 500 on socket 13

Can anyone help me with this?

P.S. following also doenst work

    server.addDefaultHandlerForMethod("GET",
                                      requestClass: GCDWebServerRequest.self,
                                      processBlock: {request in

    print(request.remoteAddressString)
    print(request.localAddressString)

    let remote = request.remoteAddressString.substringToIndex(request.remoteAddressString.lastIndexOf(":")!)
    let local =  request.localAddressString.substringToIndex(request.localAddressString.lastIndexOf(":")!)

    print("\(remote) vs \(local)")
    if remote == local {
        return GCDWebServerResponse(statusCode: 200)
    }
    //Forbidden
    return GCDWebServerResponse(statusCode: 403);
})
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79

1 Answers1

0

See GCDWebServer.h for GCDWebServerProcessBlock:

The block must return a GCDWebServerResponse or nil on error, which will result in a 500 HTTP status code returned to the client.

Pol
  • 3,848
  • 1
  • 38
  • 55
  • Are you saying that I should on success (e.g. valid ip) not return `nil` but e.g. `GCDWebServerResponse(statusCode: 200)`? – Lukasz 'Severiaan' Grela Nov 01 '16 at 15:17
  • Yes, you should return a response. Returning nil will result in a 500 error response, which is not what you want. – Pol Nov 01 '16 at 19:39
  • I did as suggested but nothing appears on screen, the page is not loaded, when I remove this `server.addDefaultHandlerForMethod` then it works (but from any IP) :( – Lukasz 'Severiaan' Grela Nov 02 '16 at 10:36
  • Your IP check will not work as expected if you use multiple handlers. I think you were expecting that returning nil from a handler's process block would have the next handler be called. That's not how it works in GCDWebServer. If the IP checking code is in the process block of the handler, then you need to have this code in every handler of your app. – Pol Nov 03 '16 at 16:27
  • Could you show me an example on how to stop network comm from disallowed IP? – Lukasz 'Severiaan' Grela Nov 03 '16 at 16:31
  • Your code in your question is already doing it right: just return a real response instead of just `GCDWebServerResponse(statusCode: 200)`. – Pol Nov 04 '16 at 17:49