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

New IP Retreival Method #178

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Carterpersall
Copy link
Contributor

  • Replaced the implementations of info_local_ip and info_public_ip with a DNS lookup
    • Using nslookup -type=a myip.opendns.com gives both the public IP and local IP
      • The implementation is in an if statement within both functions that check if the global variable exists, which allows for both functions to be executed in any order
      • After some formatting, the public and private IPs are set to global variables to allow for reuse between function calls
  • Added some new backup implementations that only return one of the IPs
    • info_local_ip:
      1. Creates and uses a network socket using System.Net.Sockets
      • Connects to 1.1.1.1 and gets the IP endpoint
        • Uses port 65530 but any port works in my testing
      • Executes in ~18ms (48ms on slow system)
        • ~2x Faster than the primary method on its own, but the speed gain of getting both IPs at once outweighs the speed gain of this method
        • Maybe: Find a way to check if info_public_ip is enabled
      1. Uses System.Net.Dns to perform a DNS lookup for myip.opendns.com
      • Most routers will process a DNS request for myip.opendns.com and return the sender's IP
        • Executes in 22ms (~58ms on slow system)
      1. Uses the old method of getting the IP information from the network adapter
    • info_public_ip:
      1. Uses the old method of querying http://ifconfig.me/ip
      • Replaced Invoke RestMethod with [System.Net.WebClient]::new().DownloadString()
        • ~10% Faster

Speed:

  • Powershell 5:
    • Slow:
      • 371ms -> 80ms
        • 4.63x Faster
    • Fast:
      • 118ms -> 33ms
        • 3.58x Faster
  • Powershell 7:
    • Slow:
      • 368ms -> 154ms
        • 2.39x Faster
    • Fast:
      • 129ms -> 45 ms
        • 2.87x Faster

- Replaced `Invoke RestMethod` with [System.Net.WebClient]::new().DownloadString()
  - Faster by ~10%
@rashil2000
Copy link
Member

Why not use Resolve-DnsName? It will help avoid needless text parsing and it supposedly faster too.

@Carterpersall
Copy link
Contributor Author

Why not use Resolve-DnsName? It will help avoid needless text parsing and it supposedly faster too.

For some reason, I was not aware of the CmdLet's existence. Though in initial testing, I'm seeing a few issues:

  1. It does not return the public IP like nslookup does
  2. Running Resolve-DnsName on a cold cache is about 5x slower than nslookup, while on subsequent runs Resolve-DnsName uses its cached values which only takes a few milliseconds

I'll definitely look a bit further into this though and see what I find (maybe reverse engineering it is possible)

@Carterpersall
Copy link
Contributor Author

Unfortunately, it seems that Resolve-DnsName is not in PowerShell Core yet, which means the source code is not available. And without the source code, there isn't much I can do to diagnose its speed issues.

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

Successfully merging this pull request may close these issues.

2 participants