yatwiki/AuthorHash.go
2017-07-11 19:14:26 +12:00

19 lines
442 B
Go

package yatwiki3
import (
"crypto/md5"
"encoding/hex"
"net/http"
"strings"
)
func RemoteAddrToIPAddress(remoteAddr string) string {
return strings.TrimRight(strings.TrimRight(remoteAddr, `0123456789`), `:`) // trim trailing port; IPv4 and IPv6-safe
}
func Author(r *http.Request) string {
userAgentHash := md5.Sum([]byte(r.UserAgent()))
return RemoteAddrToIPAddress(r.RemoteAddr) + "-" + hex.EncodeToString(userAgentHash[:])[:6]
}