feat: links redirect forward client ip

This commit is contained in:
sjdonado
2025-03-16 10:20:01 +01:00
parent ce2f73dfe3
commit d26aa2f18a
2 changed files with 14 additions and 35 deletions
+13 -13
View File
@@ -51,15 +51,20 @@ module App::Controllers::Link
link = Database.get_by(Link, slug: slug)
raise App::NotFoundException.new(env) if !link
client_ip = env.request.remote_address.try &.to_s || "Unknown"
user_agent_str = env.request.headers["User-Agent"]? || "Unknown"
user_agent = user_agent_str != "Unknown" ? UserAgent.new(user_agent_str) : nil
language_header = env.request.headers["Accept-Language"]? || "Unknown"
language = language_header.split(',').first.split(';').first
referer = env.request.headers["Referer"]?
env.response.status_code = 301
env.response.headers["Location"] = link.url!
env.response.headers["X-Forwarded-For"] = client_ip
env.response.headers["X-Forwarded-User-Agent"] = user_agent_str
spawn do
user_agent_str = env.request.headers["User-Agent"]? || "Unknown"
user_agent = user_agent_str != "Unknown" ? UserAgent.new(user_agent_str) : nil
language_header = env.request.headers["Accept-Language"]? || "Unknown"
language = language_header.split(',').first.split(';').first
referer = env.request.headers["Referer"]?
click = Click.new
click.id = UUID.v4.to_s
click.link = link
@@ -74,11 +79,6 @@ module App::Controllers::Link
Log.error { "Logging click event failed: #{changeset.errors}" }
end
end
env.response.status_code = 301
env.response.headers["Location"] = link.url!
env.response.headers["Content-Type"] = "text/html"
env.response.print("Redirecting...")
end
end
+1 -22
View File
@@ -14,29 +14,8 @@
2. **Redirect by Slug**
- Endpoint: `GET /:slug`
- Headers: `X-Api-Key`
- Payload: None
- Response Example
```json
{
"data": {
"id": "84f0c7a4-8c4e-4665-b676-cb9c5e40f1db",
"refer": "http://localhost:4000/3wP4BQ",
"origin": "https://monocuco.donado.co",
"clicks": [
{
"id": "730e2202-58f9-478c-a24c-f1c561df6716",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:127.0) Gecko/20100101 Firefox/127.0",
"language": "en-US",
"browser": "Firefox",
"os": "Mac OS X",
"source": "Unknown",
"created_at": "2024-07-12T19:25:22Z"
}
]
}
}
```
- Response 301
3. **List All Links**