mirror of
https://github.com/jeessy2/ddns-go.git
synced 2025-10-20 23:43:45 +08:00
* feat: Add a login page * feat: Modify save rules, more secure * remove remoteAddr == "localhost" * "登录失败次数过多,请等待 %d 分钟后再试 * cookie remove secure * set cookie expires time by `NotAllowWanAccess` * prettier * fix: rename * feat: auto login if unfilled * feat: auto login if there is no username/password * auto login if no username/password
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package util
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// IsPrivateNetwork 是否为私有地址
|
|
// https://en.wikipedia.org/wiki/Private_network
|
|
func IsPrivateNetwork(remoteAddr string) bool {
|
|
// removing optional port from remoteAddr
|
|
if strings.HasPrefix(remoteAddr, "[") { // ipv6
|
|
if index := strings.LastIndex(remoteAddr, "]"); index != -1 {
|
|
remoteAddr = remoteAddr[1:index]
|
|
} else {
|
|
return false
|
|
}
|
|
} else { // ipv4
|
|
if index := strings.LastIndex(remoteAddr, ":"); index != -1 {
|
|
remoteAddr = remoteAddr[:index]
|
|
}
|
|
}
|
|
|
|
if ip := net.ParseIP(remoteAddr); ip != nil {
|
|
return ip.IsLoopback() || // 127/8, ::1
|
|
ip.IsPrivate() || // 10/8, 172.16/12, 192.168/16, fc00::/7
|
|
ip.IsLinkLocalUnicast() // 169.254/16, fe80::/10
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// GetRequestIPStr get IP string from request
|
|
func GetRequestIPStr(r *http.Request) (addr string) {
|
|
addr = "Remote: " + r.RemoteAddr
|
|
if r.Header.Get("X-Real-IP") != "" {
|
|
addr = addr + " ,Real-IP: " + r.Header.Get("X-Real-IP")
|
|
}
|
|
if r.Header.Get("X-Forwarded-For") != "" {
|
|
addr = addr + " ,Forwarded-For: " + r.Header.Get("X-Forwarded-For")
|
|
}
|
|
return addr
|
|
}
|