shayne / go-wsl2-host

Automatically update your Windows hosts file with the WSL2 VM IP address
1.65k stars 140 forks source link

[ADD] add hyper-v vm'ip to name translation #88

Open JoJoJoinme opened 1 month ago

JoJoJoinme commented 1 month ago

Description

[Provide a brief description of the new feature you're adding.] support hyper-v vm's ipv4 addr to domain name. adn the domain-name format is <-your-vmName->.example.com

Motivation and Context

[Explain why this feature is needed and what problem it solves.] I meet same problem. 'the ip address of vm inside hyper-v changed everytime when system restart' Inspired by this project. I write a domain-name <---> ip addr translation function

How Has This Been Tested?

[Describe the tests you've run to verify your changes. Provide instructions so we can reproduce.] Proof of Concept: there are hyper-v api which you can get vm info(ip addr, vnName, etc..)managed by hyper-v

  1. get all runing vm info Get-VM | Where-Object {$_.State -eq 'Running'} | Select-Object-ExpandProperty Name
  2. get ip addr of running vm Get-VMNetworkAdapter -VMName <-put-your-vmName-> | Select-Object -ExpandProperty IPAddresses This must be run by Administrator

I solve the problem by writing a function to do the same thing

Screenshots (if appropriate):

[Add screenshots to help explain your feature, if applicable.]

No sceenshots but there are logs..

PS D:\Code\go-wsl2-host> .\wsl2host.exe debug wsl2host.info(1): starting wsl2host service Get running vm names [Fedora-39-2 win10-home] vm:Fedora-39-2, ipInfo:ipv4:[172.17.116.89], ipv6:[fe80::215:5dff:fe00:104] vm:win10-home, ipInfo:ipv4:[172.17.112.60], ipv6:[fe80::d7bc:1e4f:3ca0:e1a0] old hapi entry info: map[Fedora-39-2.exmaple.com:id:23, ip:172.17.116.89,hostname:Fedora-39-2.exmaple.com, comment:managed by api - hyper-vm win10-home.exmaple.com:id:24, ip:172.17.112.60, hostname:win10-home.exmaple.com, comment:managed by api - hyper-vm] upsert entry:id:0, ip:172.17.116.89, hostname:Fedora-39-2.exmaple.com, comment:managed by api - hyper-vm upsert entry:id:0, ip:172.17.112.60, hostname:win10-home.exmaple.com, comment:managed by api - hyper-vm

Types of changes

Checklist:

Additional Notes

[Add any additional information that might be helpful for reviewers.] Additional. I fix a bug of GetHostIP func which get gateway addr of wsl, and write a new func GetHostIPv2 which get result from api directly