-3

I want to add ip license in my application to do this, I get the user's ip address and compare it with the list of ip addresses on my website. However, my codes take all the ip addresses in the list, so the output always returns false.

I assigned the user's IP address to a variable and compared it to the ip address query result. It works this way, but I want to manage the ip address list from a web address.

var webClient = new System.Net.WebClient();

string dnsString = webClient.DownloadString("http://checkip.dyndns.org");
dnsString = (new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")).Match(dnsString).Value;

string userip = webClient.DownloadString("http://35.234.83.224/test.php");
userip = (new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")).Match(userip).Value;

webClient.Dispose();

(My Website) IP List:

192.168.1.1
192.168.1.2
192.168.1.3

My application output (User ip: 192.168.1.1):

Output: 192.168.1.1192.168.1.2192.168.1.3 - User IP Address did not match!

Capscroll
  • 3
  • 3
  • 1
    192.168.*.* are private addresses and not your public ips – Craig Beaumont Aug 03 '19 at 00:21
  • @CraigBeaumont I know, these are sample IP addresses. – Capscroll Aug 03 '19 at 00:22
  • Is your website behind the same router as the machine you're testing with? – Gabriel Luci Aug 03 '19 at 00:24
  • @GabrielLuci No, the two are on different servers. The person who wants to use my application gives me the IP address, and I save his ip address in a list. My application returns true if it checks the list and matches the user's ip address. – Capscroll Aug 03 '19 at 00:25
  • This seems like a very brittle system – TheGeneral Aug 03 '19 at 00:26
  • 1
    Stop trying to parse HTML with regular expressions, and use a proper DOM parser instead. Your current regex is matching all IP addresses on the page, and that's what's causing the problem you're having. Obligatory link: https://stackoverflow.com/a/1732454/62576 – Ken White Aug 03 '19 at 00:26
  • What Ken's saying definitely one possibility. But otherwise, we'd have to see the code you're using on your website to be of any more help. – Gabriel Luci Aug 03 '19 at 00:30
  • @TheGeneral I know, I just wanted to put a simple barrier. I don't want to deal with serial numbers. – Capscroll Aug 03 '19 at 00:30
  • @KenWhite You're right, my code is downloading the whole page and everything is complicated. What I want to do is make the application return true if the user's ip address is included in this list. – Capscroll Aug 03 '19 at 00:30
  • @GabrielLuci I didn't use a tag, just texts. [URL](http://35.234.83.224/test.php) – Capscroll Aug 03 '19 at 00:34
  • I know what you're trying to do, and I've pointed out why your current method isn't working. I've also explained that if you use a proper DOM parser, you can get the IP addresses separately to create a list that you can then check against. You're not going to do so easily with a regex, because HTML does not lend itself to parsing with regular expressions well. Did you follow the link I posted? – Ken White Aug 03 '19 at 00:37
  • That's not showing me any IP address at all. What I mean is that we need to see the code (your PHP code) that is deciding what IP address to send back. – Gabriel Luci Aug 03 '19 at 00:39
  • @KenWhite Yes, I'm currently investigating DOM Parser. – Capscroll Aug 03 '19 at 00:45
  • @GabrielLuci Reload page! – Capscroll Aug 03 '19 at 00:45
  • I'm not seeing any PHP code :) – Gabriel Luci Aug 03 '19 at 00:47
  • I get the user's IP address from the http://checkip.dyndns.org website and do ip adress validation from my website. – Capscroll Aug 03 '19 at 00:51
  • Right. So *how* are you doing the IP address validation from your website. – Gabriel Luci Aug 03 '19 at 01:11
  • Wait... or is your website just returning *every* IP that's allowed? – Gabriel Luci Aug 03 '19 at 01:15
  • @GabrielLuci The application goes to the page on my website and retrieves the data. If I add more than one IP address to the page, the validation fails. Because the output takes all the ip addresses in the list and compares them to the user's ip address. What I want to do is to return true if the user's ip address appears on the page. – Capscroll Aug 03 '19 at 01:16
  • Oh.... I get it now! Yeah, this is kind of an odd way to do this. Does your website really not put a space between the IP addresses? – Gabriel Luci Aug 03 '19 at 01:18
  • @GabrielLuci For example, if the output is 192.168.1.1 192.168.1.2 192.168.1.3 the application should find the user's ip address and return true. The codes I use don't work for this, unfortunately I don't know anything about what to do. – Capscroll Aug 03 '19 at 01:22
  • https://stackoverflow.com/a/1732454 rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ - – C. Augusto Proiete Aug 03 '19 at 01:49

1 Answers1

2

Your code does this:

  1. Download http://checkip.dyndns.org and take the first IP address.
  2. Download "MY IP LIST URL" and take the first IP address.

So, in step 2, if the client's IP address doesn't happen to be the first one in the list, then there won't be a match. In step 2, you need to look for the IP address you found in step 1 (but replace . with \.). Like this:

userip = (new Regex(@$"\b{dnsString.Replace(".","\\.")}\b")).Match(userip).Value;

But as others commented, it's not the greatest way. You control your website, so step 2 is unlikely to break on you, but you don't control checkip.dyndns.org, so if they change something, this could break on you. You're looking for an IP anywhere on the page, so even if they, for some reason, put an IP address in the <head> somewhere, which would never been seen if you visited the site in a browser, your code would break.

You might be better off using a web service like this, which does the same thing as checkip.dyndns.org, but returns the data in JSON format.

And your code too, you could return the list of IPs in JSON format instead of an HTML page. HTML is intended to be displayed to a human. If it's being read by a computer, there are better formats.

But also keep in mind that most residential ISP's use dynamic IPs. So the same person won't keep the same IP forever.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • Thanks for be interested. I will query the user's IP address from my web address. My application is quite simple, so I don't want to more security. A simple licensing system is enough for me. I chose the ip validation license because my application will run mostly on servers. My codes and methods may sound a bit ridiculous for you, but I'm new to C #, this kind of problems is quite normal for me xd – Capscroll Aug 03 '19 at 02:02