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

Implement Faster IPv4 Address Retrieval Method #159

Merged
merged 2 commits into from
Oct 10, 2022

Conversation

Carterpersall
Copy link
Contributor

@Carterpersall Carterpersall commented Oct 4, 2022

  • Implemented a new method to find the user's actual IPv4 address using a recursive mess of .NET witchcraft that somehow ends up being much faster
    • Uses a combination of C# scripts at https://stackoverflow.com/questions/8089685/c-sharp-finding-my-machines-local-ip-address-and-not-the-vms/25553311 which I converted to PowerShell
    • Gets all network adapters using [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
      • Checks if each adapter is online, has a gateway address, and is not a loopback adapter
        • Then checks if the adapter is a WiFi or Ethernet adapter using NetworkInterfaceType
          • Then iterates through each Unicast address, and checks if the address is for InterNetwork communication
            • Then checks if the $local_ip variable has already been set, and if not, sets it to the IP returned
              • This is because, under some circumstances, multiple IPs can be returned
                • It grabbed the ZeroTier IP address on my PC, which shouldn't occur
                • This bug also exists with the previous implementation, and I'm not sure if there is an easy way to fix it without manually filtering out specific adapter names

Speed:

  • PowerShell 5:
    • 400ms → 43ms
      • 9.3x Faster
  • PowerShell 7:
    • 459ms → 56ms
      • 8.2x Faster

Bottlenecked Speed:

  • PowerShell 5:
    • 1446ms → 321ms
      • 4.5x Faster
  • PowerShell 7:
    • 1673ms → 313ms
      • 6.3x Faster

- Implemented new method to find user's real IPv4 address using a recursive mess of .NET witchcraft that somehow ends up being much faster
  - Gets all network adapters using `[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()`
    - Checks if each adapter is online, has a gateway address, and is not a loopback adapter
      - Then checks if adapter is a WiFi or Ethernet adapter using `NetworkInterfaceType`
        - Then iterates through each Unicast address, and checks if the address is for InterNetwork communication
          - Then checks if the $local_ip variable has already been set, and if not, sets it to the IP returned
            - This is because under some circumstances, multiple IP's can be returned
              - It grabbed the ZeroTier IP address on my PC, which shouldn't occur
              - This bug also exists with the previous implementation, and I'm not sure if there is an easy way to fix it without manually filtering out certain adapter names

Speed:
- PowerShell 5:
  - 400ms -> 43ms
    - 9.3x Faster
- PowerShell 7:
  - 459ms -> 56ms
    - 8.2x Faster

Possible Extra Checks:
- Check `$ni.GetIPProperties().UnicastAddresses.DhcpLeaseLifetime` for if time is less than 24 hours
@Carterpersall Carterpersall changed the title New Method Working Implement Faster IPv4 Address Retrieval Method Oct 4, 2022
@Carterpersall Carterpersall mentioned this pull request Oct 4, 2022
16 tasks
winfetch.ps1 Outdated Show resolved Hide resolved
winfetch.ps1 Outdated Show resolved Hide resolved
winfetch.ps1 Outdated Show resolved Hide resolved
winfetch.ps1 Outdated Show resolved Hide resolved
@rashil2000
Copy link
Member

LGTM

Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com>
@rashil2000 rashil2000 merged commit 5918b3f into lptstr:master Oct 10, 2022
@Carterpersall Carterpersall deleted the Optimize-local_ip branch October 10, 2022 21:04
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.

None yet

2 participants