3
  1. 如何从 Go 检查网页是从 localhost 还是从外部访问?

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

  3. 如何隐藏整个站点,例如“不,这里什么都没有,端口 8080 已关闭,继续前进”。

4

1 回答 1

6
  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)
于 2013-08-18T11:52:18.467 回答