Исправления прокси сервера
Исправил важные ошибки и теперь правильно передаются данные ✅ $_POST - заполнен данными ✅ php://input - содержит raw body ✅ Content-Length - правильный размер ✅ $_SERVER['HTTP_X_REAL_IP'] - реальный IP клиента ✅ $_SERVER['HTTP_X_FORWARDED_FOR'] - реальный IP клиента ✅ $_SERVER['HTTP_X_FORWARDED_PROTO'] - протокол (http/https)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,4 +6,5 @@ WebServer/cert/*
|
|||||||
WebServer/tools/logs/*
|
WebServer/tools/logs/*
|
||||||
WebServer/soft/*
|
WebServer/soft/*
|
||||||
!WebServer/soft/soft.rar
|
!WebServer/soft/soft.rar
|
||||||
|
Backend/admin/frontend/wailsjs*
|
||||||
.cursorrules
|
.cursorrules
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package webserver
|
package webserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -71,9 +72,19 @@ func StartHandlerProxy(w http.ResponseWriter, r *http.Request) (valid bool) {
|
|||||||
protocol = "https"
|
protocol = "https"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Читаем тело запроса в буфер для корректной передачи POST данных
|
||||||
|
var bodyBuffer bytes.Buffer
|
||||||
|
if r.Body != nil {
|
||||||
|
if _, err := io.Copy(&bodyBuffer, r.Body); err != nil {
|
||||||
|
http.Error(w, "Ошибка чтения тела запроса", http.StatusInternalServerError)
|
||||||
|
return valid
|
||||||
|
}
|
||||||
|
r.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// Проксирование на локальный адрес
|
// Проксирование на локальный адрес
|
||||||
proxyURL := protocol + "://" + proxyConfig.LocalAddress + ":" + proxyConfig.LocalPort + r.URL.RequestURI()
|
proxyURL := protocol + "://" + proxyConfig.LocalAddress + ":" + proxyConfig.LocalPort + r.URL.RequestURI()
|
||||||
proxyReq, err := http.NewRequest(r.Method, proxyURL, r.Body)
|
proxyReq, err := http.NewRequest(r.Method, proxyURL, &bodyBuffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Ошибка создания прокси-запроса", http.StatusInternalServerError)
|
http.Error(w, "Ошибка создания прокси-запроса", http.StatusInternalServerError)
|
||||||
return valid
|
return valid
|
||||||
@@ -95,8 +106,19 @@ func StartHandlerProxy(w http.ResponseWriter, r *http.Request) (valid bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Прозрачная передача - никаких дополнительных заголовков
|
// Добавляем заголовки для передачи реального IP клиента
|
||||||
// Все заголовки уже скопированы выше "как есть"
|
clientIP := r.RemoteAddr
|
||||||
|
if colonIndex := strings.LastIndex(clientIP, ":"); colonIndex != -1 {
|
||||||
|
clientIP = clientIP[:colonIndex]
|
||||||
|
}
|
||||||
|
proxyReq.Header.Set("X-Real-IP", clientIP)
|
||||||
|
proxyReq.Header.Set("X-Forwarded-For", clientIP)
|
||||||
|
proxyReq.Header.Set("X-Forwarded-Proto", protocol)
|
||||||
|
|
||||||
|
// Устанавливаем правильный Content-Length для POST/PUT запросов
|
||||||
|
if bodyBuffer.Len() > 0 {
|
||||||
|
proxyReq.ContentLength = int64(bodyBuffer.Len())
|
||||||
|
}
|
||||||
|
|
||||||
// Выполняем прокси-запрос
|
// Выполняем прокси-запрос
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
|
|||||||
Reference in New Issue
Block a user