tomasen / realip

a golang library that can get client's real public ip address from http request headers
MIT License
221 stars 47 forks source link

Add some comments and use standard method when possible #3

Closed RadhiFadlillah closed 6 years ago

RadhiFadlillah commented 6 years ago

Hi @tomasen, thanks for this package. I've made some changes which I explain below :

Add some comments

When I saw list of lancidrs for the first time, I was wondering why are those addresses are included as private address :

lancidrs := []string{
    "127.0.0.1/8", "10.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7",
}

To prevent confusion from the future contributor, I added some comments and Wikipedia links that explain about those address.

maxCidrBlocks := []string{
    "127.0.0.1/8",    // localhost
    "10.0.0.0/8",     // 24-bit block
    "172.16.0.0/12",  // 20-bit block
    "192.168.0.0/16", // 16-bit block
    "169.254.0.0/16", // link local address
    "::1/128",        // localhost IPv6
    "fc00::/7",       // unique local address IPv6
    "fe80::/10",      // link local address IPv6
}

Remove log.Fatal from init()

I think a library must not make a project panic when the library is only imported by the project. That's why I removed these lines :

if err != nil {
    log.Fatalf("ParseCIDR error: %v", err) // assuming I did it right above
}

Besides, those lines are not needed anyway, because the submitted value is fixed, which make there are no possibility that error happened when calling ParseCIDR.

Use standard method for splitting IP address and port number in r.RemoteAddr

According to the documentation about RemoteAddr, it said that :

RemoteAddr allows HTTP servers and other software to record the network address that sent the request, usually for logging. This field is not filled in by ReadRequest and has no defined format. The HTTP server in this package sets RemoteAddr to an "IP:port" address before invoking a handler.

Now, since RemoteAddr is always in "IP:port" format, we can simply use net.SplitHostPort.

Clean up the test file

In TestRealIP, I added name field to the struct, to make it easier to see which test scenario that failed.

Rename the function's name

When updating readme, I found it's quite weird to write realip.RealIP(r), so I changed it into realip.FromRequest(r).