-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (117 loc) · 3.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"log"
"net/url"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"github.com/Jigsaw-Code/outline-sdk/x/sysproxy"
)
var selectedItemID int
type AppState struct {
CurrentPage string
}
type NavEvent struct {
TargetPage string
}
type AppSettings struct {
Domain string `json:"domain"`
ResolverHost string `json:"resolverHost"`
Tcp bool `json:"tcp"`
Udp bool `json:"udp"`
ReporterURL string `json:"reporter"`
LocalAddress string `json:"localAddress"`
Configs []Config `json:"configs"`
SmartConfig []byte `json:"smartConfig"`
SmartConfigURL url.URL `json:"smartConfigURL"`
BlockedDomains []string `json:"blockedDomains"`
}
type Config struct {
Transport string `json:"transport"`
ConfigFile []byte `json:"configFile"`
TestReports []*connectivityReport `json:"testReport"`
Health int `json:"health"`
}
// 0: healthly, 1: some tests failed, 2: all tests failed
type AppContext struct {
Window fyne.Window
Preferences fyne.Preferences
Settings *AppSettings
}
var proxy *runningProxy
func main() {
defer sysproxy.DisableWebProxy()
ProxyApp := app.NewWithID("com.amirgh.fyneproxy")
if meta := ProxyApp.Metadata(); meta.Name == "" {
// App not packaged, probably from `go run`.
meta.Name = "Proxy App"
app.SetMetadata(meta)
}
ProxyApp.Settings().SetTheme(newAppTheme())
mainWin := ProxyApp.NewWindow(ProxyApp.Metadata().Name)
mainWin.Resize(fyne.NewSize(200, 300))
log.Println("Setting the context")
ctx := &AppContext{
Window: mainWin,
Preferences: ProxyApp.Preferences(),
Settings: &AppSettings{},
}
// Load settings from preferences
loadSettings(ctx)
printSettings(ctx)
// State variable
state := &AppState{CurrentPage: "main"}
// Channel for navigation events
navChannel := make(chan NavEvent)
log.Println("Starting the app")
// Start listening to navigation events
go func() {
for event := range navChannel {
state.CurrentPage = event.TargetPage
mainWin.SetContent(makePageContent(ctx, state, navChannel))
}
}()
// Set initial content
mainWin.SetContent(makePageContent(ctx, state, navChannel))
mainWin.ShowAndRun()
}
func loadSettings(ctx *AppContext) {
// Create your settings content here
var settings AppSettings
settingsJSON := ctx.Preferences.String("AppSettings")
if settingsJSON != "" {
err := json.Unmarshal([]byte(settingsJSON), &settings)
if err != nil {
log.Println("Error loading settings:", err)
}
ctx.Settings = &settings
} else {
// Set default settings if no saved settings are found
ctx.Settings = &AppSettings{
Domain: "example.com",
ResolverHost: "8.8.8.8",
Tcp: true,
Udp: true,
LocalAddress: "localhost:8080",
}
}
}
func updateSettings(ctx *AppContext) {
// Serialize settings to JSON
settingsJSON, err := json.Marshal(ctx.Settings)
if err != nil {
log.Println("Error marshaling settings:", err)
return
}
// Save JSON string to app preferences
ctx.Preferences.SetString("AppSettings", string(settingsJSON))
}
func printSettings(ctx *AppContext) {
// Serialize settings to JSON
settingsJSON, err := json.Marshal(ctx.Settings)
if err != nil {
log.Println("Error marshaling settings:", err)
return
}
log.Printf("Settings: %v", string(settingsJSON))
}