Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Function to check if request came from localhost #1629

Closed
GarryGaller opened this issue Nov 22, 2021 · 4 comments · Fixed by #1671
Closed

🚀 Function to check if request came from localhost #1629

GarryGaller opened this issue Nov 22, 2021 · 4 comments · Fixed by #1671

Comments

@GarryGaller
Copy link

Could you build into the library a simple function that guarantees that the request came from a local host?
That would be handy than writing your own bike every time, which often isn't quite right enough yet.
Let's say this option doesn't work correctly.

func GetIPs(c *fiber.Ctx) []string {
    ips := c.IPs()
    if len(ips) == 0 {
        ips = append(ips, c.IP())
    }
    return ips
}

func NextIfLocal(c *fiber.Ctx) bool {
    return GetIPs(c)[0] == "127.0.0.1"
}

This one's better:

func IsLocalHost(address string) bool {
	/*
		fmt.Println(IsLocalHost("::1"))
		fmt.Println(IsLocalHost("::1:3001"))
		fmt.Println(IsLocalHost("127.0.0.1:80"))
		fmt.Println(IsLocalHost("0.0.0.0"))
		fmt.Println(IsLocalHost("178.10.10.10"))  // false
	*/

	localHosts := []string{"localhost", "127.0.0.1", "0.0.0.0", "::1"}
	for _, h := range localHosts {
		if strings.Contains(address, h) {
			return true
		}
	}
	return false
}

func NextIfLocal(c *fiber.Ctx) bool {
	return IsLocalHost(GetIPs(c)[0])
}
@hi019 hi019 changed the title 🚀 🚀 Function to check if request came from localhost Nov 26, 2021
@muratmirgun
Copy link

Maybe I can take this issue

@hi019
Copy link
Contributor

hi019 commented Dec 9, 2021

Sure @muratmirgun

@vecpeng
Copy link
Contributor

vecpeng commented Dec 27, 2021

Maybe I can take this issue

Are you still working on it?
If you are busy, maybe I can take it. @muratmirgun

@muratmirgun
Copy link

@vecpeng Yes I am still busy you can take this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants