Deny ip addresses?

Hi
Is there a way I can deny access from a specific ip address when they visit my glitch page?
Thanks

The incoming requests originating IP Address should be in the request’s “X-Forwarded-For” header, so you could check that for the IP you want to ban and if you see the one you don’t want you could then return an appropriate response. You could also use something like https://www.npmjs.com/package/express-ip-block if you’re using Express.

I’m using hello webpage though

That’s more complicated. hello-webpage uses https://www.npmjs.com/package/lws for its web server. I don’t think there’s anything built into lws that would handle this (the docs are at https://github.com/lwsjs/local-web-server/wiki) so I think you’re have to write your own middleware to handle it. You’d also have to have a custom config file to load that middleware, once it was in place.

Hey @SpeedyCraftah I thought this might be a fun little project and I’ve been meaning to put together a sample of a custom lws config, so I threw together a little example that should allow you to block an IP address on a hello-webpage-based project.

Take a look at https://glitch.com/~hello-webpage-custom-config and let me know if you have questions.

1 Like

Hi @cori, I do have a question. It doesn’t seem to work for me and I quite need it to. I tried to copy your project entirely at first, adding and deleting stuff that is not necessary etc. but it didn’t seem to block access to the page even if I straightforwardly did “ctx.response.status = 403” without any if statement, meaning just willing to block every ip. Then I tried remixing your project, leaving everything as it is and just trying to change ip to my own or doing what earlier - straightforwardly blocking every ip, but it didn’t work too. My 2 projects: ludicrous-galliform and octagonal-lynx. I’d appreciate help with this case.

Hey @supahero when I load https://ludicrous-galliform.glitch.me/ I get a “Forbidden” response just as I’d expect with the content of your middleware file (i.e. without the conditional):

image

What is it that you see when you go there?

When I load my sample project I see this:

image

but if I change the IP address at https://glitch.com/edit/#!/hello-webpage-custom-config?path=lws-mw-sample.js:6:21 to my current IP I see

image

as well. Do note, though, that your project needs to restart after changes to the config file; since that config is loaded at startup changes to that file aren’t loaded dynamically. You can force this by executing refresh in your project’s console.

1 Like

Oh yes, refreshing the project helped. I totally forgot about configs not loading dynamically. I was seeing the normal page all the times. Thanks.

1 Like