如何检查网页是从本地主机访问还是从外部访问?

huangapple go评论92阅读模式
英文:

How to check if webpage is accessed from localhost or from outside?

问题

  1. 我可以通过什么方式在 Go 中检查网页是从本地访问还是外部访问?

  2. 我如何禁用某些功能对外部用户?

  3. 我如何隐藏整个网站,比如显示“没有内容,端口8080关闭,请继续前进”。

英文:
  1. How can I check from Go whether a webpage is accessed from localhost or from outside?

  2. How can I disable some functions for external users?

  3. How can I hide the whole site, like "nope, nothing here, port 8080 is closed, move along".

答案1

得分: 6

  1. 要检查网站是否从外部访问,可以检查远程IP地址。如果不是来自127.0.0.1或::1(IPv6),则表示来自外部。可以使用函数func (*IPConn) RemoteAddr

  2. 要禁用某些功能,请检查上述条件。

  3. 要隐藏整个网站,请将服务绑定到本地接口(127.0.0.1)上。

绑定方式:

net.Listen("tcp", "localhost:8080")

或者

net.Listen("tcp6", "ip6-localhost:8080")

使用http包:

http.ListenAndServe("localhost:8080", nil)
英文:
  1. 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.

  2. To disable some functions check the above condition.

  3. 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)

huangapple
  • 本文由 发表于 2013年8月18日 18:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/18298223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定