Files
vpn-client/main.go
Vibe Kanban a58413d778 фикс для окна конфигураций (vibe-kanban b9621dd9)
Окно конфигураций не открывается:
PS C:\\Users\\hamy\\AppData\\Local\\Temp\\vibe-kanban\\worktrees\\a880-\\HamyVPNClient> go run .

panic: runtime error: invalid memory address or nil pointer dereference

[signal 0xc0000005 code=0x1 addr=0x38 pc=0x7ff69290cb17]

goroutine 1 [running, locked to thread]:

main.main.func4.2()

        C:/Users/hamy/AppData/Local/Temp/vibe-kanban/worktrees/a880-/HamyVPNClient/main.go:323 +0x177

main.main.func4()

        C:/Users/hamy/AppData/Local/Temp/vibe-kanban/worktrees/a880-/HamyVPNClient/main.go:328 +0x27a

fyne.io/fyne/v2/widget.(\*Button).Tapped(0xc00021e7e0, 0x7ff69328c860?)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/widget/button.go:192 +0x9f

fyne.io/fyne/v2/internal/driver/glfw.(\*window).mouseClickedHandleTapDoubleTap(0xc00011a000, {0x7ff6933cbac0, 0xc00021e7e0}, 0xc000c579a0)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/window.go:581 +0x182

fyne.io/fyne/v2/internal/driver/glfw.(\*window).processMouseClicked(0xc00011a000, 0x1, 0x0, 0x0)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/window.go:542 +0x718

fyne.io/fyne/v2/internal/driver/glfw.(\*window).mouseClicked(0xc00021ebd0?, 0xc000c3d9c8?, 0x7ff6928935a0?, 0x7ff6938e1480?, 0xc000c3d9a8?)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/window\_desktop.go:409 +0xaf

github.com/go-gl/glfw/v3.3/glfw.goMouseButtonCB(0xc0000021c0?, 0x0, 0x0, 0x0)

        C:/Users/hamy/go/pkg/mod/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20240506104042-037f3cc74f2a/input.go:333 +0x4e

github.com/go-gl/glfw/v3.3/glfw.\_Cfunc\_glfwPollEvents()

        \_cgo\_gotypes.go:1544 +0x45

github.com/go-gl/glfw/v3.3/glfw.PollEvents()

        C:/Users/hamy/go/pkg/mod/github.com/go-gl/glfw/v3.3/glfw@v0.0.0-20240506104042-037f3cc74f2a/window.go:931 +0x13

fyne.io/fyne/v2/internal/driver/glfw.(\*gLDriver).pollEvents(...)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/loop\_desktop.go:22

fyne.io/fyne/v2/internal/driver/glfw.(\*gLDriver).runGL(0xc00004bda8?)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/loop.go:154 +0x1aa

fyne.io/fyne/v2/internal/driver/glfw.(\*gLDriver).Run(0xc0000ea000)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/driver.go:162 +0x72

fyne.io/fyne/v2/app.(\*fyneApp).Run(0xc0000ea0b0)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/app/app.go:77 +0x102

fyne.io/fyne/v2/internal/driver/glfw.(\*window).ShowAndRun(0xc00011a000)

        C:/Users/hamy/go/pkg/mod/fyne.io/fyne/v2@v2.7.2/internal/driver/glfw/window.go:217 +0x64

main.main()

        C:/Users/hamy/AppData/Local/Temp/vibe-kanban/worktrees/a880-/HamyVPNClient/main.go:363 +0xa18

exit status 2

PS C:\\Users\\hamy\\AppData\\Local\\Temp\\vibe-kanban\\worktrees\\a880-\\HamyVPNClient>

также там была проблема с полем ввода - оно сжатое (отображается не во всю ширину окна)
2026-01-16 18:52:48 +03:00

407 lines
12 KiB
Go

package main
import (
"errors"
"fmt"
"image/color"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"fyne.io/fyne/v2"
)
// Config represents a VPN configuration
type Config struct {
Title string
URL string
}
var (
currentProcess *exec.Cmd
configs []Config // Store all configurations
activeConfig int // Index of the active config (-1 if none)
)
// checkSingBox checks if sing-box.exe exists in the bin folder
func checkSingBox() bool {
singBoxPath := filepath.Join("bin", "sing-box.exe")
if _, err := os.Stat(singBoxPath); os.IsNotExist(err) {
return false
}
return true
}
// isValidVLESS checks if the given URL is a valid VLESS URL
func isValidVLESS(vlessURL string) bool {
// Basic validation for VLESS URL format
if !strings.HasPrefix(vlessURL, "vless://") {
return false
}
u, err := url.Parse(vlessURL)
if err != nil {
return false
}
// Check if it's a valid VLESS link
matched, _ := regexp.MatchString(`^vless://[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}@.*`, vlessURL)
if !matched {
return false
}
// Additional validation checks can be added here
return u.Scheme == "vless" && u.Host != ""
}
// addConfig adds a new configuration to the list
func addConfig(title, url string) {
configs = append(configs, Config{Title: title, URL: url})
}
// updateConfig updates an existing configuration
func updateConfig(index int, title, url string) {
if index >= 0 && index < len(configs) {
configs[index].Title = title
configs[index].URL = url
}
}
// removeConfig removes a configuration by index
func removeConfig(index int) {
if index >= 0 && index < len(configs) {
configs = append(configs[:index], configs[index+1:]...)
if activeConfig >= index {
activeConfig--
}
if activeConfig >= len(configs) {
activeConfig = -1
}
}
}
// runSingBox starts the sing-box process with the given config path
func runSingBox(configPath string) error {
singBoxPath := filepath.Join("bin", "sing-box.exe")
if !checkSingBox() {
return nil
}
cmd := exec.Command(singBoxPath, "run", "-c", configPath)
// Make the process run as hidden
cmd.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
}
err := cmd.Start()
if err != nil {
return err
}
currentProcess = cmd
return nil
}
// killCurrentProcess terminates the current sing-box process if running
func killCurrentProcess() {
if currentProcess != nil && currentProcess.Process != nil {
currentProcess.Process.Kill()
currentProcess = nil
}
}
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Hamy VPN Client")
// Set the window to not resizable and fixed size
myWindow.SetFixedSize(true)
// Set the window size to 200x300 as requested
myWindow.Resize(fyne.NewSize(200, 300))
// Initialize active config to -1 (no config selected)
activeConfig = -1
// Connection state - true for connected, false for disconnected
var isConnected bool = false
// Update connection button text based on connection state
updateConnectionButtonText := func(button *widget.Button) {
if isConnected {
button.SetText("Отключить")
} else {
button.SetText("Подключить")
}
}
// Create program title with proper padding
titleText := canvas.NewText("Hamy VPN", nil)
titleText.TextStyle.Bold = true
titleText.TextSize = 16
titleText.Alignment = fyne.TextAlignLeading
// Create settings button (gear icon)
settingsButton := widget.NewButtonWithIcon("", theme.SettingsIcon(), func() {
// Show settings dialog in a separate fixed-size window
settingsWindow := myApp.NewWindow("Настройки")
settingsWindow.Resize(fyne.NewSize(400, 300))
settingsWindow.SetFixedSize(true)
// Add content to settings window (for now just a placeholder)
settingsLabel := widget.NewLabel("Настройки приложения")
settingsContent := container.NewVBox(
settingsLabel,
widget.NewLabel("Добавьте элементы настроек здесь"),
)
settingsWindow.SetContent(settingsContent)
settingsWindow.Show()
})
// Create HBox for title and settings button with proper padding
titleContainer := container.NewBorder(nil, nil, titleText, settingsButton, layout.NewSpacer())
// Status indicator (bold, smaller padding)
statusLabel := canvas.NewText("Отключено", color.RGBA{R: 128, G: 128, B: 128, A: 255}) // Gray color
statusLabel.Alignment = fyne.TextAlignCenter
statusLabel.TextSize = 14 // Small font
statusLabel.TextStyle.Bold = true // Make it bold
// Connect/Disconnect button (with dynamic text)
var connectButton *widget.Button
connectButton = widget.NewButton("Подключить", func() {
if activeConfig == -1 || len(configs) == 0 {
dialog.ShowError(errors.New("Выберите конфигурацию"), myWindow)
return
}
// Toggle connection state
isConnected = !isConnected
// Update button text
updateConnectionButtonText(connectButton)
// Update status text
if isConnected {
statusLabel.Text = "Подключено"
statusLabel.Color = color.RGBA{R: 0, G: 128, B: 0, A: 255} // Green color for connected
} else {
statusLabel.Text = "Отключено"
statusLabel.Color = color.RGBA{R: 128, G: 128, B: 128, A: 255} // Gray color for disconnected
}
statusLabel.Refresh()
// This functionality will be implemented later
dialog.ShowInformation("Info", fmt.Sprintf("Connect/Disconnect functionality will be implemented later using: %s", configs[activeConfig].Title), myWindow)
})
connectButton.Importance = widget.HighImportance
if len(configs) == 0 {
connectButton.Disable() // Disable initially until a config is selected
} else {
connectButton.Enable() // Enable if we have configs
}
// Configurations button - opens a separate window
configsButton := widget.NewButton("Конфигурации", func() {
// Create a separate fixed-size window for configurations
configWindow := myApp.NewWindow("Конфигурации")
configWindow.Resize(fyne.NewSize(400, 300))
configWindow.SetFixedSize(true)
// Declare variables before use
var listContainer *widget.List
var scrollContainer *container.Scroll
var refreshList func()
// Entry for importing new configuration - fixed to expand properly
importEntry := widget.NewEntry()
importEntry.PlaceHolder = "vless://..."
// Container for import field and button - use HBox to allow proper expansion
importButton := widget.NewButtonWithIcon("", theme.ContentAddIcon(), func() {
url := importEntry.Text
if url != "" && isValidVLESS(url) {
title := fmt.Sprintf("Конфиг %d", len(configs)+1)
addConfig(title, url)
dialog.ShowInformation("Успешно", "Конфиг добавлен", configWindow)
// Refresh the list
refreshList()
importEntry.SetText("")
} else if url != "" {
dialog.ShowError(errors.New("Невалидная ссылка"), configWindow)
}
})
// Fixed the order of operations to avoid nil reference
refreshList = func() {
// Create the list first
newListContainer := widget.NewList(
func() int {
return len(configs)
},
func() fyne.CanvasObject {
return container.NewBorder(nil, nil,
widget.NewLabel("Template"),
container.NewHBox(
widget.NewButtonWithIcon("", theme.DocumentCreateIcon(), func() {}),
widget.NewButtonWithIcon("", theme.DeleteIcon(), func() {}),
),
)
},
func(id widget.ListItemID, obj fyne.CanvasObject) {
box := obj.(*fyne.Container)
titleLabel := box.Objects[1].(*widget.Label)
buttonsContainer := box.Objects[2].(*fyne.Container)
editButton := buttonsContainer.Objects[0].(*widget.Button)
deleteButton := buttonsContainer.Objects[1].(*widget.Button)
titleLabel.SetText(configs[id].Title)
// Update visual indication for active config
if id == activeConfig {
titleLabel.SetText(fmt.Sprintf("● %s", configs[id].Title))
} else {
titleLabel.SetText(fmt.Sprintf("○ %s", configs[id].Title))
}
// Set up edit button
editButton.OnTapped = func() {
showEditDialog(configWindow, id)
}
// Set up delete button
deleteButton.OnTapped = func() {
dialog.ShowConfirm("Удалить конфигурацию",
fmt.Sprintf("Вы уверены, что хотите удалить конфигурацию '%s'?", configs[id].Title),
func(confirmed bool) {
if confirmed {
removeConfig(id)
dialog.ShowInformation("Удалено", "Конфигурация удалена", configWindow)
// Refresh the list
refreshList()
}
},
configWindow)
}
// Allow clicking on the whole row to select the config
selectButton := widget.NewButton("", func() {
activeConfig = id
dialog.ShowInformation("Config Selected", fmt.Sprintf("Active config: %s", configs[id].Title), configWindow)
configWindow.Close()
})
selectButton.Hidden = true
box.Objects[0] = container.NewStack(selectButton)
},
)
newListContainer.OnSelected = func(id widget.ListItemID) {
activeConfig = id
dialog.ShowInformation("Config Selected", fmt.Sprintf("Active config: %s", configs[id].Title), configWindow)
configWindow.Close()
}
// Assign to the variable before using it in the scroll container
listContainer = newListContainer
// Create scroll container only once
if scrollContainer == nil {
scrollContainer = container.NewScroll(listContainer)
} else {
scrollContainer.Content = listContainer
scrollContainer.Refresh()
}
}
// Call refreshList to initialize the list first
refreshList()
// Container for import field and button - use HBox to allow proper expansion
// Using container.NewBorder to make the entry expand properly
importContainer := container.NewBorder(nil, nil, nil, importButton, importEntry)
// Main content for the config window
configContent := container.NewVBox(
importContainer,
scrollContainer,
)
configWindow.SetContent(configContent)
configWindow.Show()
})
// Main content container with vertical layout
contentContainer := container.NewVBox(
titleContainer,
layout.NewSpacer(),
container.NewCenter(statusLabel),
layout.NewSpacer(),
container.NewCenter(connectButton),
layout.NewSpacer(),
container.NewCenter(configsButton),
layout.NewSpacer(),
)
// Create a top-level container
mainContainer := container.NewStack(contentContainer)
// Set the content and show the window
myWindow.SetContent(mainContainer)
myWindow.ShowAndRun()
}
// Function to show the edit dialog
func showEditDialog(parentWindow fyne.Window, configIndex int) {
// Create entry fields for title and URL with existing values
titleEntry := widget.NewEntry()
titleEntry.SetText(configs[configIndex].Title)
titleEntry.PlaceHolder = "Название конфигурации"
urlEntry := widget.NewEntry()
urlEntry.SetText(configs[configIndex].URL)
urlEntry.PlaceHolder = "vless://..."
// Create a custom dialog
editDialog := dialog.NewForm("Редактировать конфигурацию", "Сохранить", "Отмена",
[]*widget.FormItem{
widget.NewFormItem("Заголовок", titleEntry),
widget.NewFormItem("Ссылка", urlEntry),
},
func(ok bool) {
if ok {
title := titleEntry.Text
url := urlEntry.Text
if title == "" {
title = fmt.Sprintf("Конфиг %d", configIndex+1) // Default title
}
if isValidVLESS(url) {
updateConfig(configIndex, title, url)
dialog.ShowInformation("Успешно", "Конфиг обновлен", parentWindow)
} else {
dialog.ShowError(errors.New("Невалидная ссылка"), parentWindow)
}
}
},
parentWindow,
)
editDialog.Show()
}