我正在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
谁能帮我这个?
PS以下也不起作用
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);
})