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

go, network-programming

Solution

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)

Problem

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".

Original source