R
Railwayβ€’14mo ago
macwilko

IP address of caller

I need to put some security based on the IP address of the client calling an API. Is there a way I can know the original IP address of the caller from the request?
Solution:
trust the X-Forwarded-For header https://utilities.up.railway.app/raw...
Jump to solution
60 Replies
Percy
Percyβ€’14mo ago
Project ID: d1c85c02-8ca5-43c3-adc6-4a24cb066e33
macwilko
macwilkoOPβ€’14mo ago
d1c85c02-8ca5-43c3-adc6-4a24cb066e33
Solution
Brody
Brodyβ€’14mo ago
trust the X-Forwarded-For header https://utilities.up.railway.app/raw
macwilko
macwilkoOPβ€’14mo ago
sweet thx brody
macwilko
macwilkoOPβ€’14mo ago
adding this. Ruby Rails headers have HTTP_ prefix before the header
No description
Brody
Brodyβ€’14mo ago
you should split that header value by , and use the first value https://utilities.overr.net/raw
macwilko
macwilkoOPβ€’14mo ago
oh damn good spot thanks!
Brody
Brodyβ€’14mo ago
theres probably a middleware that would do all this for you
macwilko
macwilkoOPβ€’14mo ago
No description
macwilko
macwilkoOPβ€’14mo ago
my quick and dirty sneaky
Brody
Brodyβ€’14mo ago
that would just return the original string if there is no comma right?
macwilko
macwilkoOPβ€’14mo ago
No description
macwilko
macwilkoOPβ€’14mo ago
No description
Brody
Brodyβ€’14mo ago
perfect πŸ‘Œ
macwilko
macwilkoOPβ€’14mo ago
yeah ruby is nice like that super forgiving language lets you do bad stuff though πŸ˜‰
Brody
Brodyβ€’14mo ago
No description
macwilko
macwilkoOPβ€’14mo ago
i did not expect that of Go!
Brody
Brodyβ€’14mo ago
why not!? 😭 you can do that in almost every language
macwilko
macwilkoOPβ€’14mo ago
idk, i think some languages crash if you do [][0] there's no items in that array ruby is fine, it says, whatever man, nil peace what happens if the string is "" in this Go code?
Ashwin
Ashwinβ€’14mo ago
Off topic but what are you using for these screenshots? They're stunning
macwilko
macwilkoOPβ€’14mo ago
Xnapper! But I think a railway member (fp) has made an open source clone
Ashwin
Ashwinβ€’14mo ago
Awesome, thank you :D Do you by any chance have a link to the open source clone?
macwilko
macwilkoOPβ€’14mo ago
@Vin sorry to tag you here, but do you know?
MantisInABox
MantisInABoxβ€’14mo ago
Yeah, the source for FPs project is here https://github.com/FarazPatankar/snip-snap/
GitHub
GitHub - FarazPatankar/snip-snap: A simple but powerful tool to mak...
A simple but powerful tool to make your screenshots prettier. - GitHub - FarazPatankar/snip-snap: A simple but powerful tool to make your screenshots prettier.
Brody
Brodyβ€’14mo ago
it returns a slice with "" then selecting the zeroth index from that slice will just return an empty string value
MantisInABox
MantisInABoxβ€’14mo ago
Go is extremely forgiving, but the code analyzers and compiler prevent you from doing stupid things
macwilko
macwilkoOPβ€’14mo ago
that's really nice πŸ™‚
MantisInABox
MantisInABoxβ€’14mo ago
That's one of the reasons why I have been using Go for literally everything lately... Prevents me from doing the normal stupid things I tend to do
MantisInABox
MantisInABoxβ€’14mo ago
Such good code Brody
No description
Brody
Brodyβ€’14mo ago
in my head that sounds like sarcasm
macwilko
macwilkoOPβ€’14mo ago
hehe
MantisInABox
MantisInABoxβ€’14mo ago
Nope. It was sincere... You have seen my code... I still manage to do a lot of bad things with Go
Brody
Brodyβ€’14mo ago
aws shucks, thanks πŸ™‚
macwilko
macwilkoOPβ€’14mo ago
tbh if I ever do a re-write, I'de sincerely consider Go again
Brody
Brodyβ€’14mo ago
though now that im looking at it, why didnt i just do this
func getRealIP(headers http.Header) string {
for _, proxyHeader := range proxyIPHeaders {
if value := headers.Get(proxyHeader); value != "" {
return strings.SplitN(value, ",", 2)[0]
}
}
}
func getRealIP(headers http.Header) string {
for _, proxyHeader := range proxyIPHeaders {
if value := headers.Get(proxyHeader); value != "" {
return strings.SplitN(value, ",", 2)[0]
}
}
}
macwilko
macwilkoOPβ€’14mo ago
i think one of the pains i had was GORM kinda sucked
Brody
Brodyβ€’14mo ago
gorm does suck
macwilko
macwilkoOPβ€’14mo ago
idk if there's better alterntatives now
Brody
Brodyβ€’14mo ago
there is
macwilko
macwilkoOPβ€’14mo ago
oo
MantisInABox
MantisInABoxβ€’14mo ago
Gorm is so good though
macwilko
macwilkoOPβ€’14mo ago
that's good i mean back when I used GORM it didn't even have good migration files it just auto migrated everything
Brody
Brodyβ€’14mo ago
good times
macwilko
macwilkoOPβ€’14mo ago
but yeah now all my slow areas of code are to do with the damn database layer being slow >.< ActiveRecord is also very bloated
MantisInABox
MantisInABoxβ€’14mo ago
ActiveRecord hurts my soul
macwilko
macwilkoOPβ€’14mo ago
oh well, future self problem >.<
Ashwin
Ashwinβ€’14mo ago
Thank you so much!!
Brody
Brodyβ€’14mo ago
I don't like him
macwilko
macwilkoOPβ€’14mo ago
This episode I think you’ll like
Brody
Brodyβ€’14mo ago
I don't think I will go is perfect
macwilko
macwilkoOPβ€’14mo ago
Go watch it (when you have time)
Brody
Brodyβ€’14mo ago
how long is it
macwilko
macwilkoOPβ€’14mo ago
25 mins
MantisInABox
MantisInABoxβ€’14mo ago
That's about 24 minutes too long for Brody! 🀣 If it isn't Fireship something in 100 seconds, its too long
Brody
Brodyβ€’14mo ago
oh hell no, fireships 100 second videos are already pushing the limits of my attention span
macwilko
macwilkoOPβ€’14mo ago
🧠
Brody
Brodyβ€’14mo ago
is there a brain emoji that doesn't have wrinkles?
macwilko
macwilkoOPβ€’14mo ago
You got the reference 🫢🀣
Want results from more Discord servers?
Add your server