summaryrefslogtreecommitdiff
path: root/internal/state/state.go
blob: 97d2d47d304f419ae1430486d12ca6f331af60ce (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package state

import (
	"encoding/json"
	"os"
	"path/filepath"
	"sync"
	"time"
)

// AppState holds persistent client state.
type AppState struct {
	SelectedServer      string    `json:"selected_server"`
	SelectedMode        string    `json:"selected_mode"`
	LastSync            time.Time `json:"last_sync"`
	AutoConnect         bool      `json:"auto_connect"`
	LocalProxyPort      int       `json:"local_proxy_port,omitempty"`
	EnabledRuleSets     map[string]bool `json:"enabled_rule_sets,omitempty"`
	CustomBypass        []string  `json:"custom_bypass_processes,omitempty"`
	RecommendedServer   string    `json:"recommended_server,omitempty"`
	RecommendedNodeID   string    `json:"recommended_node_id,omitempty"`
	LastRecommendation  time.Time `json:"last_recommendation,omitempty"`
	RecommendationFetched bool    `json:"recommendation_fetched,omitempty"` // true if recommendation was ever fetched
}

// Store manages persistent state on disk.
type Store struct {
	mu      sync.Mutex
	path    string
	dataDir string
	data    AppState
}

// NewStore creates a state store at the given path.
func NewStore(dataDir string) *Store {
	return &Store{
		path:    filepath.Join(dataDir, "state.json"),
		dataDir: dataDir,
		data: AppState{
			SelectedMode: "Комбо (приложения + Re-filter)",
			AutoConnect:  false,
		},
	}
}

// DataDir returns the data directory path.
func (s *Store) DataDir() string {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.dataDir
}

// Load reads state from disk. Returns default state if file doesn't exist.
func (s *Store) Load() error {
	s.mu.Lock()
	defer s.mu.Unlock()

	data, err := os.ReadFile(s.path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return err
	}
	return json.Unmarshal(data, &s.data)
}

// Save writes state to disk.
func (s *Store) Save() error {
	s.mu.Lock()
	defer s.mu.Unlock()

	if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil {
		return err
	}

	data, err := json.MarshalIndent(s.data, "", "  ")
	if err != nil {
		return err
	}
	return os.WriteFile(s.path, data, 0o644)
}

// Get returns a copy of the current state.
func (s *Store) Get() AppState {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.data
}

// SetServer updates the selected server.
func (s *Store) SetServer(tag string) {
	s.mu.Lock()
	s.data.SelectedServer = tag
	s.mu.Unlock()
}

// SetMode updates the selected routing mode.
func (s *Store) SetMode(mode string) {
	s.mu.Lock()
	s.data.SelectedMode = mode
	s.mu.Unlock()
}

// SetLastSync records the last sync time.
func (s *Store) SetLastSync(t time.Time) {
	s.mu.Lock()
	s.data.LastSync = t
	s.mu.Unlock()
}

// SetAutoConnect updates the auto-connect setting.
func (s *Store) SetAutoConnect(v bool) {
	s.mu.Lock()
	s.data.AutoConnect = v
	s.mu.Unlock()
}

func (s *Store) SetLocalProxyPort(port int) {
	s.mu.Lock()
	s.data.LocalProxyPort = port
	s.mu.Unlock()
}

// SetRuleSetEnabled enables/disables an optional rule-set.
func (s *Store) SetRuleSetEnabled(tag string, enabled bool) {
	s.mu.Lock()
	if s.data.EnabledRuleSets == nil {
		s.data.EnabledRuleSets = make(map[string]bool)
	}
	s.data.EnabledRuleSets[tag] = enabled
	s.mu.Unlock()
}

// IsRuleSetEnabled checks if a rule-set is enabled.
func (s *Store) IsRuleSetEnabled(tag string) bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	if s.data.EnabledRuleSets == nil {
		return false
	}
	return s.data.EnabledRuleSets[tag]
}

// SetCustomBypass sets custom bypass processes.
func (s *Store) SetCustomBypass(processes []string) {
	s.mu.Lock()
	s.data.CustomBypass = processes
	s.mu.Unlock()
}

// GetCustomBypass returns custom bypass processes.
func (s *Store) GetCustomBypass() []string {
	s.mu.Lock()
	defer s.mu.Unlock()
	return append([]string{}, s.data.CustomBypass...)
}

// SetRecommendation saves the server recommendation.
func (s *Store) SetRecommendation(serverIP, nodeID string) {
	s.mu.Lock()
	s.data.RecommendedServer = serverIP
	s.data.RecommendedNodeID = nodeID
	s.data.LastRecommendation = time.Now()
	s.data.RecommendationFetched = true
	s.mu.Unlock()
}

// GetRecommendation returns the current recommendation.
func (s *Store) GetRecommendation() (serverIP, nodeID string, at time.Time) {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.data.RecommendedServer, s.data.RecommendedNodeID, s.data.LastRecommendation
}