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

Add feature of copying proxy command of git/ssh/http/https for fyne-based client #23

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading