gpt4 book ai didi

ios - 如何限制 GCDWebServer 中允许的 IP 的连接

转载 作者:行者123 更新时间:2023-11-30 13:01:24 25 4
gpt4 key购买 nike

我正在使用 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] 连接已中止,套接字 13 上的状态代码为 500

谁能帮我解决这个问题吗?

附注以下也有效

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

最佳答案

参见GCDWebServer.h了解GCDWebServerProcessBlock:

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

关于ios - 如何限制 GCDWebServer 中允许的 IP 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39876563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com