Skip to content

Commit

Permalink
add a button to do netisolation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 12, 2024
1 parent 6885ea3 commit 7e90c03
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions ui/gui/popups/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package popups

import (
"fmt"
"os/exec"
"runtime"

"gioui.org/layout"
"gioui.org/widget"
Expand All @@ -24,7 +26,8 @@ type ConnectPopup struct {
ListenIP string
ListenPort int

connectButton widget.Clickable
connectButton widget.Clickable
netIsolationButton widget.Clickable
}

func NewConnect(ui ui.UI, ListenIP string, ListenPort int) Popup {
Expand All @@ -38,9 +41,38 @@ func (p *ConnectPopup) ID() string {
return "connect"
}

func fixCheckNetIsolation() error {
command := `CheckNetIsolation LoopbackExempt -a -n=\"Microsoft.MinecraftUWP_8wekyb3d8bbwe\"; Write-Host -NoNewLine \"Done, Enter to Close\"; $null = $Host.UI.RawUI.ReadKey(\"NoEcho,IncludeKeyDown\");`
psCommand := fmt.Sprintf(`Start-Process powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command "%s"' -Verb RunAs`, command)
cmd := exec.Command("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", psCommand)

output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error: %s\nOutput: %s\n", err, string(output))
return err
}

fmt.Println(string(output))
return nil
}

func (p *ConnectPopup) Layout(gtx C, th *material.Theme) D {
if p.connectButton.Clicked(gtx) {
utils.OpenUrl(fmt.Sprintf("minecraft://connect/?serverUrl=%s&serverPort=%d", p.ListenIP, p.ListenPort))
go utils.OpenUrl(fmt.Sprintf("minecraft://connect/?serverUrl=%s&serverPort=%d", p.ListenIP, p.ListenPort))
}

if p.netIsolationButton.Clicked(gtx) {
go func() {
err := fixCheckNetIsolation()
if err != nil {
messages.Router.Handle(&messages.Message{
Source: p.ID(),
Target: "ui",
Data: messages.Error(err),
})
}
}()

}

if p.close.Clicked(gtx) {
Expand Down Expand Up @@ -86,7 +118,7 @@ func (p *ConnectPopup) Layout(gtx C, th *material.Theme) D {
return layout.Flex{
Axis: layout.Horizontal,
Spacing: layout.SpaceBetween,
Alignment: layout.Middle,
Alignment: layout.End,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
b := material.Button(th, &p.close, "Close")
Expand All @@ -95,9 +127,28 @@ func (p *ConnectPopup) Layout(gtx C, th *material.Theme) D {
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if p.state == "listening" {
b := material.Button(th, &p.connectButton, "Connect Minecraft")
b.CornerRadius = 8
return b.Layout(gtx)
var elems []layout.FlexChild = []layout.FlexChild{
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
b := material.Button(th, &p.connectButton, "Open Minecraft")
b.CornerRadius = 8
return b.Layout(gtx)
}),
}
if runtime.GOOS == "windows" {
elems = append(elems, []layout.FlexChild{
layout.Rigid(layout.Spacer{Width: 8}.Layout),
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
b := material.Button(th, &p.netIsolationButton, "LoopbackExempt")
b.CornerRadius = 8
return b.Layout(gtx)
}),
}...)
}

return layout.Flex{
Axis: layout.Horizontal,
Alignment: layout.Middle,
}.Layout(gtx, elems...)
}
return layout.Dimensions{}
}),
Expand Down

0 comments on commit 7e90c03

Please sign in to comment.