2017-07-12 06:43:11 +00:00
|
|
|
package yatwiki
|
2017-07-09 06:45:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-07-11 07:14:26 +00:00
|
|
|
func RemoteAddrToIPAddress(remoteAddr string) string {
|
|
|
|
return strings.TrimRight(strings.TrimRight(remoteAddr, `0123456789`), `:`) // trim trailing port; IPv4 and IPv6-safe
|
|
|
|
}
|
|
|
|
|
2017-08-13 05:32:54 +00:00
|
|
|
func Author(r *http.Request, trustXForwardedFor bool) string {
|
2017-07-09 06:45:28 +00:00
|
|
|
userAgentHash := md5.Sum([]byte(r.UserAgent()))
|
|
|
|
|
2017-08-13 05:32:54 +00:00
|
|
|
ipAddress := RemoteAddrToIPAddress(r.RemoteAddr)
|
|
|
|
|
|
|
|
if trustXForwardedFor {
|
|
|
|
if xff := r.Header.Get("X-Forwarded-For"); len(xff) > 0 {
|
|
|
|
ipAddress = xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipAddress + "-" + hex.EncodeToString(userAgentHash[:])[:6]
|
2017-07-09 06:45:28 +00:00
|
|
|
}
|