Compare commits

6 Commits
master ... main

Author SHA1 Message Date
cf3ec88bb1 real remote addr 2025-10-26 13:14:31 +03:00
842444b70b telegram 2025-10-26 13:11:10 +03:00
e8353f7e28 telegram 2025-10-26 13:01:35 +03:00
Vladimir V Maksimov
2f9deb6997 files fix 2025-10-26 02:13:11 +03:00
Vladimir V Maksimov
565f4ce4fc Merge branch 'master' 2025-10-26 02:11:17 +03:00
bdb6574cf7 Initial commit 2025-10-25 22:46:26 +00:00
5 changed files with 86 additions and 34 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
*.yaml
bin/http_logger
bin/http_logger
*.out

18
LICENSE Normal file
View File

@@ -0,0 +1,18 @@
MIT License
Copyright (c) 2025 icewind
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
# http_logger

View File

@@ -1,10 +0,0 @@
2025/10/26 01:40:52 Ошибка загрузки конфига: open config.yaml: no such file or directory
2025/10/26 01:41:33 Ошибка загрузки конфига: open config.yaml: no such file or directory
2025/10/26 01:42:54 Запуск приложения
2025/10/26 01:42:54 Ошибка загрузки конфига: open config.yaml: no such file or directory
2025/10/26 01:43:36 Запуск приложения
2025/10/26 01:43:36 Слушаю 127.0.0.1:7001 ...
2025/10/26 01:43:50 Запуск приложения
2025/10/26 01:43:50 Слушаю 127.0.0.1:7001 ...
2025/10/26 01:43:54 Запуск приложения
2025/10/26 01:43:54 Слушаю 127.0.0.1:7001 ...

87
main.go
View File

@@ -4,8 +4,10 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"gopkg.in/yaml.v3"
@@ -25,6 +27,58 @@ type ConfigTelegraam struct {
GroupID int64 `yaml:"group_id"`
}
func GetRemoteAddr(r *http.Request) string {
// Сначала смотрим X-Forwarded-For
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
// X-Forwarded-For может содержать несколько IP через запятую
ips := strings.Split(xff, ",")
if len(ips) > 0 {
return strings.TrimSpace(ips[0])
}
}
// Потом X-Real-IP
if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
return realIP
}
// И в конце RemoteAddr
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}
func BuildMessage(r *http.Request) string {
t := time.Now()
var body []byte
if r.ContentLength > 1024 {
body = []byte("too long")
} else {
// Читаем тело запроса
body, _ = io.ReadAll(r.Body)
_ = r.Body.Close()
}
// Формируем содержимое
entry := fmt.Sprintf("[%s] %s %s%s\n", t.Format(time.RFC3339), r.Method, r.Host, r.URL.String())
entry += "RemoteAddr: " + GetRemoteAddr(r) + "\n"
entry += "Headers:\n"
for name, values := range r.Header {
for _, v := range values {
entry += fmt.Sprintf(" %s: %s\n", name, v)
}
}
entry += fmt.Sprintf("Body:\n%s\n", string(body))
entry += "----\n"
entry = "```" + entry + "```"
return entry
}
func main() {
log.Println("Запуск приложения")
// Загружаем конфиг
@@ -44,31 +98,18 @@ func main() {
// Общий обработчик
handler := func(w http.ResponseWriter, r *http.Request) {
t := time.Now()
var body []byte
if r.ContentLength > 1024 {
body = []byte("too long")
} else {
// Читаем тело запроса
body, _ = io.ReadAll(r.Body)
_ = r.Body.Close()
paths := strings.Split(r.URL.Path, "/")
switch {
case len(paths) < 2:
w.WriteHeader(http.StatusNotFound)
return
case paths[1] == "telegram":
default:
w.WriteHeader(http.StatusNotFound)
return
}
// Формируем содержимое
entry := fmt.Sprintf("[%s] %s %s%s\n", t.Format(time.RFC3339), r.Method, r.Host, r.URL.String())
entry += "Headers:\n"
for name, values := range r.Header {
for _, v := range values {
entry += fmt.Sprintf(" %s: %s\n", name, v)
}
}
entry += fmt.Sprintf("Body:\n%s\n", string(body))
entry += "----\n"
entry = "```" + entry + "```"
entry := BuildMessage(r)
_, err = tg.SendTextMessage(cfg.Telegram.GroupID, entry)
if err != nil {
log.Println(err)