Skip to content

Commit

Permalink
feat(gui): feature of copying proxy command for git/ssh/http/https in…
Browse files Browse the repository at this point in the history
… fyne-based client

This commit is similar as the swiftui client (see PR #17). The difference is that this commit is for
the fyne-based client
  • Loading branch information
genshen committed Feb 7, 2024
1 parent ddc6660 commit 526c3d7
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion client-ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"fmt"
"net/url"
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
Expand All @@ -15,7 +18,6 @@ import (
pluginversion "github.com/genshen/wssocks-plugin-ustb/wssocks-ustb/version"
"github.com/genshen/wssocks/client"
"github.com/genshen/wssocks/version"
"net/url"
)

const (
Expand All @@ -33,6 +35,12 @@ const (
btnStopping
)

const (
ProxyCommandGit = iota
ProxyCommandHttp
ProxyCommandSsh
)

func newEntryWithText(text string) *widget.Entry {
entry := widget.NewEntry()
entry.SetText(text)
Expand Down Expand Up @@ -197,10 +205,28 @@ func main() {
),
)
tabs.SetTabLocation(container.TabLocationTop)
selectCopyProxyCommand := container.NewBorder(nil, nil, nil, nil,
NewWSelectWithCopyProxyCommand([]string{"git", "http/https", "ssh/sftp/scp"},
func(sel *widget.Select, value string) {
if value != "" {
sel.ClearSelected()
switch value {
case "git":
copyToClipboard(ProxyCommandGit, uiLocalAddr.Text, uiHttpLocalAddr.Text, w)
case "http/https":
copyToClipboard(ProxyCommandHttp, uiLocalAddr.Text, uiHttpLocalAddr.Text, w)
case "ssh/sftp/scp":
copyToClipboard(ProxyCommandSsh, uiLocalAddr.Text, uiHttpLocalAddr.Text, w)
}
}
},
),
)

w.SetContent(container.NewVBox(
widget.NewCard("Settings", "", tabs),
btnStart,
selectCopyProxyCommand,
&widget.Separator{},
container.NewGridWithColumns(2,
container.NewHBox(
Expand Down Expand Up @@ -244,3 +270,36 @@ func main() {
//w.SetOnClosed() todo
w.ShowAndRun()
}

// NewWSelectWithCopyProxyCommand is copied from widget.NewSelect.
func NewWSelectWithCopyProxyCommand(options []string, changed func(sel *widget.Select, val string)) *widget.Select {
s := &widget.Select{
Options: options,
PlaceHolder: "(copy proxy command)",
}
s.OnChanged = func(val string) {
changed(s, val)
}
s.ExtendBaseWidget(s)
return s
}

func copyToClipboard(category int, socksAddr string, httpAddr string, win fyne.Window) {
var text = ""
var nc = "nc -x" // darwin or linux
if runtime.GOOS == "windows" {
nc = "connect -S"
}
switch category {
case ProxyCommandGit:
text = fmt.Sprintf("export GIT_SSH_COMMAND=\"ssh -o ProxyCommand='%s %s %%h %%p' \"", nc, socksAddr)
break
case ProxyCommandHttp:
text = fmt.Sprintf("export https_proxy=http://%s http_proxy=http://%s", socksAddr, httpAddr)
break
case ProxyCommandSsh:
text = fmt.Sprintf("ssh -o ProxyCommand='%s %s %%h %%p'", nc, socksAddr)
break
}
win.Clipboard().SetContent(text)
}

0 comments on commit 526c3d7

Please sign in to comment.