Only accept HTTP connections from Localhost in Go?

I have a simple HTTP server standing in the Golang:

h := http.NewServeMux()
h.Handle("/somepath", MyHandler)

s := &http.Server{
    Addr:    "1234",
    Handler: h,
}   

s.ListenAndServe();

What is the best way to drop connections when the caller is not localhost? I am currently considering checking for basic connection information and ensuring that the IP address is there 127.0.0.1, but it takes a lot of resources (and goes through a whole chain of Go code) before eventually disconnecting the connection. Ideally, I can get the Golang server to remove the original TCP SYN packet based on the IP address, and not create a TCP connection at all (or show that this port is listening).

What is the cleanest way here?

+4
1

VonC .

, host:port http.Server.Addr http.ListenAndServe.

net.Listen .

net.Listen:

TCP UDP laddr - "host: port", "127.0.0.1:8080". , ": 8080", , .

+8

Source: https://habr.com/ru/post/1663173/


All Articles