-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
306 lines (278 loc) · 6.87 KB
/
Copy pathconfig.go
File metadata and controls
306 lines (278 loc) · 6.87 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type appError string
func (err appError) Error() string {
return string(err)
}
func userError(message string) error {
return appError(message)
}
func intString(value int) string {
return strconv.Itoa(value)
}
func configuredPort() int {
if port := parsePort(os.Getenv("PORT")); port > 0 && portAvailable(port) {
return port
}
if value, ok := savedControlValue("port"); ok {
if port := parseSavedPort(value); port > 0 && portAvailable(port) {
return port
}
}
return 0
}
func configuredToken() string {
return strings.TrimSpace(os.Getenv("MOBILE_TYPER_TOKEN"))
}
func configuredRemoteKey() string {
if value := normalizeRemoteKey(os.Getenv("CODEX_REMOTE_KEY")); value != "" {
return value
}
if value, ok := savedControlValue("remoteKey"); ok {
if text, ok := value.(string); ok {
return normalizeRemoteKey(text)
}
}
return ""
}
func configuredRelayURL() string {
if value := normalizeNonEmpty(os.Getenv("CODEX_RELAY_URL")); value != "" {
return value
}
if value, ok := savedControlValue("relayUrl"); ok {
if text, ok := value.(string); ok {
return normalizeNonEmpty(text)
}
}
return ""
}
func saveControlConfig(payload map[string]any) (map[string]any, error) {
state := map[string]any{}
if data, err := os.ReadFile(codexStatePath()); err == nil && len(data) > 0 {
_ = json.Unmarshal(data, &state)
}
if state == nil {
state = map[string]any{}
}
config := normalizeControlConfigPayload(payload)
state["controlConfig"] = config
if _, ok := state["pinnedThreadIds"]; !ok {
state["pinnedThreadIds"] = []string{}
}
if _, ok := state["archivedThreadIds"]; !ok {
state["archivedThreadIds"] = []string{}
}
if _, ok := state["titleOverrides"]; !ok {
state["titleOverrides"] = map[string]any{}
}
if _, ok := state["guiFailureReports"]; !ok {
state["guiFailureReports"] = map[string]any{}
}
statePath := codexStatePath()
if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil {
return nil, err
}
data, err := json.MarshalIndent(state, "", " ")
if err != nil {
return nil, err
}
data = append(data, '\n')
if err := os.WriteFile(statePath, data, 0600); err != nil {
return nil, err
}
return config, nil
}
func normalizeControlConfigPayload(input map[string]any) map[string]any {
if input == nil {
input = map[string]any{}
}
port := parseSavedPort(input["port"])
var portValue any = ""
if port > 0 {
portValue = port
}
relayURL := stringFromAny(input["relayUrl"])
if len([]rune(relayURL)) > 240 {
relayURL = string([]rune(relayURL)[:240])
}
return map[string]any{
"port": portValue,
"relayUrl": relayURL,
"deviceId": defaultRelayDeviceId(),
"remoteKeyMode": "manual",
"remoteKey": normalizeRemoteKey(stringFromAny(input["remoteKey"])),
}
}
func publicDesktopControlConfig(config map[string]any) map[string]any {
remoteKey := stringFromAny(config["remoteKey"])
return map[string]any{
"port": config["port"],
"relayUrl": stringFromAny(config["relayUrl"]),
"deviceId": stringFromAny(config["deviceId"]),
"remoteKeyMode": "manual",
"remoteKey": remoteKey,
"remoteKeyConfigured": remoteKey != "",
"remoteKeyMasked": redactSecret(remoteKey),
}
}
func redactSecret(value string) string {
secret := strings.TrimSpace(value)
if secret == "" {
return ""
}
runes := []rune(secret)
if len(runes) <= 4 {
return "***"
}
return string(runes[:2]) + "***" + string(runes[len(runes)-2:])
}
func stringFromAny(value any) string {
switch typed := value.(type) {
case string:
return typed
case fmt.Stringer:
return typed.String()
case nil:
return ""
default:
return fmt.Sprintf("%v", typed)
}
}
func savedControlValue(key string) (any, bool) {
data, err := os.ReadFile(codexStatePath())
if err != nil {
return nil, false
}
var state struct {
ControlConfig map[string]any `json:"controlConfig"`
}
if err := json.Unmarshal(data, &state); err != nil {
return nil, false
}
value, ok := state.ControlConfig[key]
return value, ok
}
func codexStatePath() string {
if dir := os.Getenv("CODEX_STATE_DIR"); strings.TrimSpace(dir) != "" {
return filepath.Join(dir, "state.json")
}
return filepath.Join(homeDir(), ".codex", "state.json")
}
func homeDir() string {
if dir, err := os.UserHomeDir(); err == nil && dir != "" {
return dir
}
if dir := os.Getenv("USERPROFILE"); dir != "" {
return dir
}
if dir := os.Getenv("HOME"); dir != "" {
return dir
}
return "."
}
func parseSavedPort(value any) int {
switch typed := value.(type) {
case float64:
return parsePort(fmt.Sprintf("%.0f", typed))
case string:
return parsePort(typed)
default:
return 0
}
}
func parsePort(value string) int {
port, err := strconv.Atoi(strings.TrimSpace(value))
if err != nil || port <= 0 || port > 65535 {
return 0
}
return port
}
func normalizeNonEmpty(value string) string {
return strings.TrimRight(strings.TrimSpace(value), "/")
}
func defaultRelayDeviceId() string {
username := ""
if user := strings.TrimSpace(os.Getenv("USERNAME")); user != "" {
username = user
} else if user := strings.TrimSpace(os.Getenv("USER")); user != "" {
username = user
}
if username == "" {
username = strings.TrimSpace(os.Getenv("COMPUTERNAME"))
}
if username == "" {
if hostname, err := os.Hostname(); err == nil {
username = hostname
}
}
normalized := strings.Map(func(ch rune) rune {
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '.' || ch == '_' || ch == '-' {
return ch
}
return '-'
}, strings.TrimSpace(username))
normalized = strings.Trim(normalized, "-")
if len([]rune(normalized)) > 58 {
normalized = string([]rune(normalized)[:58])
}
if normalized == "" {
return "windows-pc"
}
return normalized
}
func normalizeRemoteKey(value string) string {
var builder strings.Builder
for _, ch := range strings.TrimSpace(value) {
if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
continue
}
if builder.Len() >= 80 {
break
}
builder.WriteRune(ch)
}
return builder.String()
}
func generateToken() string {
bytes := make([]byte, 18)
if _, err := rand.Read(bytes); err != nil {
return strconv.FormatInt(time.Now().UnixNano(), 16)
}
return hex.EncodeToString(bytes)
}
func findAvailablePort(start int, attempts int) int {
for port := start; port < start+attempts && port <= 65535; port++ {
if portAvailable(port) {
return port
}
}
return 0
}
func portAvailable(port int) bool {
listener, err := net.Listen("tcp", "127.0.0.1:"+intString(port))
if err != nil {
return false
}
_ = listener.Close()
return true
}
func waitForPortRelease(port int, timeout time.Duration) {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if portAvailable(port) {
return
}
time.Sleep(120 * time.Millisecond)
}
}