R
Railwayβ€’13mo 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β€’13mo ago
Project ID: d1c85c02-8ca5-43c3-adc6-4a24cb066e33
macwilko
macwilkoβ€’13mo ago
d1c85c02-8ca5-43c3-adc6-4a24cb066e33
Solution
Brody
Brodyβ€’13mo ago
trust the X-Forwarded-For header https://utilities.up.railway.app/raw
macwilko
macwilkoβ€’13mo ago
sweet thx brody
macwilko
macwilkoβ€’13mo ago
adding this. Ruby Rails headers have HTTP_ prefix before the header
No description
Brody
Brodyβ€’13mo ago
you should split that header value by , and use the first value https://utilities.overr.net/raw
macwilko
macwilkoβ€’13mo ago
oh damn good spot thanks!
Brody
Brodyβ€’13mo ago
theres probably a middleware that would do all this for you
macwilko
macwilkoβ€’13mo ago
No description
macwilko
macwilkoβ€’13mo ago
my quick and dirty sneaky
Brody
Brodyβ€’13mo ago
that would just return the original string if there is no comma right?
macwilko
macwilkoβ€’13mo ago
No description
macwilko
macwilkoβ€’13mo ago
No description
Brody
Brodyβ€’13mo ago
perfect πŸ‘Œ
macwilko
macwilkoβ€’13mo ago
yeah ruby is nice like that super forgiving language lets you do bad stuff though πŸ˜‰
Brody
Brodyβ€’13mo ago
No description
macwilko
macwilkoβ€’13mo ago
i did not expect that of Go!
Brody
Brodyβ€’13mo ago
why not!? 😭 you can do that in almost every language
macwilko
macwilkoβ€’13mo 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β€’13mo ago
Off topic but what are you using for these screenshots? They're stunning
macwilko
macwilkoβ€’13mo ago
Xnapper! But I think a railway member (fp) has made an open source clone
Ashwin
Ashwinβ€’13mo ago
Awesome, thank you :D Do you by any chance have a link to the open source clone?
macwilko
macwilkoβ€’13mo ago
@Vin sorry to tag you here, but do you know?
MantisInABox
MantisInABoxβ€’13mo 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β€’13mo ago
it returns a slice with "" then selecting the zeroth index from that slice will just return an empty string value
MantisInABox
MantisInABoxβ€’13mo ago
Go is extremely forgiving, but the code analyzers and compiler prevent you from doing stupid things
macwilko
macwilkoβ€’13mo ago
that's really nice πŸ™‚
MantisInABox
MantisInABoxβ€’13mo 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β€’13mo ago
Such good code Brody
No description
Brody
Brodyβ€’13mo ago
in my head that sounds like sarcasm
macwilko
macwilkoβ€’13mo ago
hehe
MantisInABox
MantisInABoxβ€’13mo ago
Nope. It was sincere... You have seen my code... I still manage to do a lot of bad things with Go
Brody
Brodyβ€’13mo ago
aws shucks, thanks πŸ™‚
macwilko
macwilkoβ€’13mo ago
tbh if I ever do a re-write, I'de sincerely consider Go again
Brody
Brodyβ€’13mo 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
macwilkoβ€’13mo ago
i think one of the pains i had was GORM kinda sucked
Brody
Brodyβ€’13mo ago
gorm does suck
macwilko
macwilkoβ€’13mo ago
idk if there's better alterntatives now
Brody
Brodyβ€’13mo ago
there is
macwilko
macwilkoβ€’13mo ago
oo
MantisInABox
MantisInABoxβ€’13mo ago
Gorm is so good though
macwilko
macwilkoβ€’13mo 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β€’13mo ago
good times
macwilko
macwilkoβ€’13mo 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β€’13mo ago
ActiveRecord hurts my soul
macwilko
macwilkoβ€’13mo ago
oh well, future self problem >.<
Ashwin
Ashwinβ€’13mo ago
Thank you so much!!
Brody
Brodyβ€’13mo ago
I don't like him
macwilko
macwilkoβ€’13mo ago
This episode I think you’ll like
Brody
Brodyβ€’13mo ago
I don't think I will go is perfect
macwilko
macwilkoβ€’13mo ago
Go watch it (when you have time)
Brody
Brodyβ€’13mo ago
how long is it
macwilko
macwilkoβ€’13mo ago
25 mins
MantisInABox
MantisInABoxβ€’13mo ago
That's about 24 minutes too long for Brody! 🀣 If it isn't Fireship something in 100 seconds, its too long
Brody
Brodyβ€’13mo ago
oh hell no, fireships 100 second videos are already pushing the limits of my attention span
macwilko
macwilkoβ€’13mo ago
🧠
Brody
Brodyβ€’13mo ago
is there a brain emoji that doesn't have wrinkles?
macwilko
macwilkoβ€’13mo ago
You got the reference 🫢🀣
Want results from more Discord servers?
Add your server