fix: properly Set Real IP

This commit is contained in:
minoplhy 2024-04-11 20:27:57 +07:00
parent 897e0497d8
commit 14e819e09b
Signed by: minoplhy
GPG Key ID: 41D406044E2434BF
2 changed files with 38 additions and 2 deletions

10
main.go
View File

@ -88,7 +88,10 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
UploadHeaders := map[string]string{ UploadHeaders := map[string]string{
"X-Api-Key": API_Key, "X-Api-Key": API_Key,
"Content-Type": "application/json", "Content-Type": "application/json",
"X-Real-IP": r.RemoteAddr, }
if handler.IsInternalIP(r.RemoteAddr) {
UploadHeaders["X-Real-IP"] = r.RemoteAddr
} }
chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, UploadHeaders, PostData) chibisafe_post, err := handler.UploadPost(Chibisafe_basepath, UploadHeaders, PostData)
@ -129,7 +132,10 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
ProcessHeaders := map[string]string{ ProcessHeaders := map[string]string{
"X-Api-Key": API_Key, "X-Api-Key": API_Key,
"Content-Type": "application/json", "Content-Type": "application/json",
"X-Real-IP": r.RemoteAddr, }
if handler.IsInternalIP(r.RemoteAddr) {
ProcessHeaders["X-Real-IP"] = r.RemoteAddr
} }
PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, ProcessHeaders, PostProcessData) PostProcess, err := handler.UploadProcessPost(Chibisafe_basepath, ProcessHeaders, PostProcessData)

30
src/handler/net.go Normal file
View File

@ -0,0 +1,30 @@
package handler
import "net"
func IsInternalIP(ip string) bool {
// Parse the IP address
ipAddress := net.ParseIP(ip)
if ipAddress == nil {
return false
}
internalRanges := []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
}
for _, internalRange := range internalRanges {
_, ipNet, err := net.ParseCIDR(internalRange)
if err != nil {
return false
}
if ipNet.Contains(ipAddress) {
return true
}
}
return false
}