Skip to content

Commit

Permalink
fix: hide WSL2 menu when Hyper-V disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
buptczq committed Nov 20, 2020
1 parent f9de056 commit 37dd280
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
40 changes: 26 additions & 14 deletions app/vsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ func (s *VSock) Run(ctx context.Context, handler func(conn io.ReadWriteCloser))
if isHV {
return nil
}

if !utils.CheckHvSocket() {
return nil
}

if !utils.CheckHVService() {
return nil
}

agentSrvGUID := winio.VsockServiceID(utils.ServicePort)
pipe, err := winio.ListenHvsock(&winio.HvsockAddr{
VMID: vmWildCard,
Expand Down Expand Up @@ -163,43 +172,46 @@ func (*VSock) AppId() AppId {
}

func (s *VSock) Menu(register func(id AppId, name string, handler func())) {
if !utils.CheckHvSocket() {
return
}
wsl2 := AppId(APP_WSL2)
register(wsl2, "Show WSL2 / Linux On Hyper-V Settings", s.onClick)
register(s.AppId(), "Check Hyper-V Agent Status", s.onCheckClick)
}

func (s *VSock) onClick() {
if s.running {
help := "socat UNIX-LISTEN:/tmp/wincrypt-hv.sock,fork,mode=777 SOCKET-CONNECT:40:0:x0000x33332222x02000000x00000000,forever,interval=5 &\n"
help += "export SSH_AUTH_SOCK=/tmp/wincrypt-hv.sock\n"
if utils.MessageBox(s.AppId().FullName()+" (OK to copy):", help, utils.MB_OKCANCEL) == utils.IDOK {
utils.SetClipBoard(help)
}
} else {
if !utils.CheckHVService() {
if utils.MessageBox(s.AppId().FullName()+":", s.AppId().String()+" agent is not working! Don you want to enable it?", utils.MB_OKCANCEL) == utils.IDOK {
if err := utils.RunMeElevatedWithArgs("-i"); err != nil {
utils.MessageBox("Install Service Error:", err.Error(), utils.MB_ICONERROR)
return
}
} else {
return
}
} else {
utils.MessageBox("Error:", s.AppId().String()+" agent doesn't work!", utils.MB_ICONWARNING)
}
help := "socat UNIX-LISTEN:/tmp/wincrypt-hv.sock,fork,mode=777 SOCKET-CONNECT:40:0:x0000x33332222x02000000x00000000,forever,interval=5 &\n"
help += "export SSH_AUTH_SOCK=/tmp/wincrypt-hv.sock\n"
if utils.MessageBox(s.AppId().FullName()+" (OK to copy):", help, utils.MB_OKCANCEL) == utils.IDOK {
utils.SetClipBoard(help)
}
} else {
utils.MessageBox("Error:", s.AppId().String()+" agent doesn't work!", utils.MB_ICONWARNING)
}
}

func (s *VSock) onCheckClick() {
if s.running {
utils.MessageBox(s.AppId().FullName()+":", s.AppId().String()+" agent is working!", 0)
} else {
if !utils.CheckHVService() {
if utils.MessageBox(s.AppId().FullName()+":", s.AppId().String()+" agent is not working! Don you want to enable it?", utils.MB_OKCANCEL) == utils.IDOK {
utils.RunMeElevatedWithArgs("-i")
if err := utils.RunMeElevatedWithArgs("-i"); err != nil {
utils.MessageBox("Install Service Error:", err.Error(), utils.MB_ICONERROR)
}
}
} else {
utils.MessageBox(s.AppId().FullName()+":", s.AppId().String()+" agent is working!", 0)
utils.MessageBox("Error:", s.AppId().String()+" agent doesn't work!", utils.MB_ICONWARNING)
}
} else {
utils.MessageBox("Error:", s.AppId().String()+" agent doesn't work!", utils.MB_ICONWARNING)
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func installService() {
})
if err != nil {
utils.MessageBox("Install Service Error:", err.Error(), utils.MB_ICONERROR)
} else {
utils.MessageBox("Install Service Success:", "Please reboot your computer to take effect!", utils.MB_ICONINFORMATION)
}
return

Expand Down
13 changes: 13 additions & 0 deletions utils/wsl2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"github.com/StackExchange/wmi"
"golang.org/x/sys/windows/registry"
"strings"
"syscall"
)

const afHvSock = 34 // AF_HYPERV

func CheckHVService() bool {
gcs, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices`, registry.READ)
if err != nil {
Expand Down Expand Up @@ -50,3 +53,13 @@ func GetVMID() []string {
}
return results
}

func CheckHvSocket() bool {
fd, err := syscall.Socket(afHvSock, syscall.SOCK_STREAM, 1)
if err != nil {
println(err.Error())
return false
}
syscall.Close(fd)
return true
}
6 changes: 3 additions & 3 deletions versioninfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"FileVersion": {
"Major": 1,
"Minor": 1,
"Patch": 1,
"Patch": 2,
"Build": 0
},
"ProductVersion": {
"Major": 1,
"Minor": 1,
"Patch": 1,
"Patch": 2,
"Build": 0
},
"FileFlagsMask": "3f",
Expand All @@ -29,7 +29,7 @@
"OriginalFilename": "WinCryptSSHAgent.exe",
"PrivateBuild": "",
"ProductName": "WinCrypt SSH Agent",
"ProductVersion": "v1.1.1",
"ProductVersion": "v1.1.2",
"SpecialBuild": ""
},
"VarFileInfo": {
Expand Down

0 comments on commit 37dd280

Please sign in to comment.