feat: vendor, makefiles, build settings
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
# lint
|
||||
- go get github.com/golang/lint/golint
|
||||
|
||||
# code complexity
|
||||
- go get github.com/on99/gocyclo
|
||||
|
||||
# test
|
||||
- go get github.com/smartystreets/goconvey/convey
|
||||
|
||||
script:
|
||||
- golint ./...
|
||||
- go vet ./...
|
||||
- gocyclo -skip-godeps=true -top 10 -over 10 . # over 10 is bad code
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 SHEN SHENG
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
# RealIP
|
||||
|
||||
[](http://godoc.org/github.com/tomasen/realip)
|
||||
|
||||
Go package that can be used to get client's real public IP, which usually useful for logging HTTP server.
|
||||
|
||||
### Feature
|
||||
|
||||
* Follows the rule of X-Real-IP
|
||||
* Follows the rule of X-Forwarded-For
|
||||
* Exclude local or private address
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/tomasen/realip"
|
||||
|
||||
func (h *Handler) ServeIndexPage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
clientIP := realip.FromRequest(r)
|
||||
log.Println("GET / from", clientIP)
|
||||
}
|
||||
```
|
||||
|
||||
## Developing
|
||||
|
||||
Commited code must pass:
|
||||
|
||||
* [golint](https://github.com/golang/lint)
|
||||
* [go vet](https://godoc.org/golang.org/x/tools/cmd/vet)
|
||||
* [gofmt](https://golang.org/cmd/gofmt)
|
||||
* [go test](https://golang.org/cmd/go/#hdr-Test_packages):
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package realip
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var cidrs []*net.IPNet
|
||||
|
||||
func init() {
|
||||
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
|
||||
}
|
||||
|
||||
cidrs = make([]*net.IPNet, len(maxCidrBlocks))
|
||||
for i, maxCidrBlock := range maxCidrBlocks {
|
||||
_, cidr, _ := net.ParseCIDR(maxCidrBlock)
|
||||
cidrs[i] = cidr
|
||||
}
|
||||
}
|
||||
|
||||
// isLocalAddress works by checking if the address is under private CIDR blocks.
|
||||
// List of private CIDR blocks can be seen on :
|
||||
//
|
||||
// https://en.wikipedia.org/wiki/Private_network
|
||||
//
|
||||
// https://en.wikipedia.org/wiki/Link-local_address
|
||||
func isPrivateAddress(address string) (bool, error) {
|
||||
ipAddress := net.ParseIP(address)
|
||||
if ipAddress == nil {
|
||||
return false, errors.New("address is not valid")
|
||||
}
|
||||
|
||||
for i := range cidrs {
|
||||
if cidrs[i].Contains(ipAddress) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// FromRequest return client's real public IP address from http request headers.
|
||||
func FromRequest(r *http.Request) string {
|
||||
// Fetch header value
|
||||
xRealIP := r.Header.Get("X-Real-Ip")
|
||||
xForwardedFor := r.Header.Get("X-Forwarded-For")
|
||||
|
||||
// If both empty, return IP from remote address
|
||||
if xRealIP == "" && xForwardedFor == "" {
|
||||
var remoteIP string
|
||||
|
||||
// If there are colon in remote address, remove the port number
|
||||
// otherwise, return remote address as is
|
||||
if strings.ContainsRune(r.RemoteAddr, ':') {
|
||||
remoteIP, _, _ = net.SplitHostPort(r.RemoteAddr)
|
||||
} else {
|
||||
remoteIP = r.RemoteAddr
|
||||
}
|
||||
|
||||
return remoteIP
|
||||
}
|
||||
|
||||
// Check list of IP in X-Forwarded-For and return the first global address
|
||||
for _, address := range strings.Split(xForwardedFor, ",") {
|
||||
address = strings.TrimSpace(address)
|
||||
isPrivate, err := isPrivateAddress(address)
|
||||
if !isPrivate && err == nil {
|
||||
return address
|
||||
}
|
||||
}
|
||||
|
||||
// If nothing succeed, return X-Real-IP
|
||||
return xRealIP
|
||||
}
|
||||
|
||||
// RealIP is depreciated, use FromRequest instead
|
||||
func RealIP(r *http.Request) string {
|
||||
return FromRequest(r)
|
||||
}
|
||||
Reference in New Issue
Block a user