如何在GCDWebServer中限制与允许的IP的连接

时间:2016-10-05 14:21:58

标签: ios swift gcdwebserver

我正在玩GCDWebServer类(很棒!)并且想知道是否可以通过远程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);

})

我希望上面会有效,但不是,我的意思是当IP不同时它会发送403 Forbidden,否则它会以最终结果:

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

任何人都可以帮我吗?

P.S。以下也是最重要的工作

    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);
})

1 个答案:

答案 0 :(得分:0)

GCDWebServer.hGCDWebServerProcessBlock

  

该块必须在出错时返回GCDWebServerResponse或nil,这将导致返回给客户端的500 HTTP状态代码。