英文:
How to check if webpage is accessed from localhost or from outside?
问题
-
我可以通过什么方式在 Go 中检查网页是从本地访问还是外部访问?
-
我如何禁用某些功能对外部用户?
-
我如何隐藏整个网站,比如显示“没有内容,端口8080关闭,请继续前进”。
英文:
-
How can I check from Go whether a webpage is accessed from localhost or from outside?
-
How can I disable some functions for external users?
-
How can I hide the whole site, like "nope, nothing here, port 8080 is closed, move along".
答案1
得分: 6
-
要检查网站是否从外部访问,可以检查远程IP地址。如果不是来自127.0.0.1或::1(IPv6),则表示来自外部。可以使用函数
func (*IPConn) RemoteAddr
。 -
要禁用某些功能,请检查上述条件。
-
要隐藏整个网站,请将服务绑定到本地接口(127.0.0.1)上。
绑定方式:
net.Listen("tcp", "localhost:8080")
或者
net.Listen("tcp6", "ip6-localhost:8080")
使用http包:
http.ListenAndServe("localhost:8080", nil)
英文:
-
To check if website is accessed from outside, check remote IP address. If it is not from 127.0.0.1 or ::1 (IPv6) then it is outside. Use function
func (*IPConn) RemoteAddr
. -
To disable some functions check the above condition.
-
To hide the whole site, bind your service to the localhost interface (127.0.0.1) only.
Binding
net.Listen("tcp", "localhost:8080")
or
net.Listen("tcp6", "ip6-localhost:8080")
Using http package
http.ListenAndServe("localhost:8080", nil)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论