-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
213 lines (187 loc) · 5.08 KB
/
main.go
File metadata and controls
213 lines (187 loc) · 5.08 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
const version = "0.1.0"
func main() {
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
switch os.Args[1] {
case "setup":
cmdSetup()
case "serve":
cmdServe()
case "revoke":
cmdRevoke()
case "install":
cmdInstall()
case "uninstall":
cmdUninstall()
case "status":
cmdStatus()
case "version":
fmt.Printf("claude-remote %s\n", version)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", os.Args[1])
printUsage()
os.Exit(1)
}
}
func printUsage() {
fmt.Println(`Usage: claude-remote <command>
Commands:
setup Generate secret + QR code for first-time auth
serve Start server (foreground)
revoke Regenerate secret, invalidate all sessions
install Install launchd plist + load
uninstall Unload + remove launchd plist
status Show running state
version Print version`)
}
func getConfig() *Config {
home, _ := os.UserHomeDir()
dataDir := filepath.Join(home, ".claude-remote")
cfg, err := LoadConfig(dataDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err)
os.Exit(1)
}
return cfg
}
func cmdSetup() {
cfg := getConfig()
cfg.Save()
auth := NewAuth(cfg.SecretPath())
if err := auth.GenerateSecret(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
token := auth.GenerateToken()
hostname := detectTailscaleHost()
// Detect if TLS certs are available to choose protocol
proto := "http"
home, _ := os.UserHomeDir()
for _, dir := range []string{cfg.DataDir, home + "/Desktop", "/var/run/tailscale"} {
matches, _ := filepath.Glob(filepath.Join(dir, "*.crt"))
if len(matches) > 0 {
proto = "https"
break
}
}
url := fmt.Sprintf("%s://%s:%d/auth/scan?token=%s", proto, hostname, cfg.Port, token)
fmt.Println("Claude Remote Setup")
fmt.Println("===================")
fmt.Printf("Protocol: %s\n", strings.ToUpper(proto))
auth.PrintQR(url)
fmt.Println("\nScan this QR code with your phone to connect.")
fmt.Println("The server must be running: claude-remote serve")
os.WriteFile(filepath.Join(cfg.DataDir, ".pending-token"), []byte(token), 0600)
}
func cmdServe() {
cfg := getConfig()
auth := NewAuth(cfg.SecretPath())
if err := auth.LoadSecret(); err != nil {
fmt.Fprintf(os.Stderr, "No secret found. Run 'claude-remote setup' first.\n")
os.Exit(1)
}
tokenPath := filepath.Join(cfg.DataDir, ".pending-token")
if data, err := os.ReadFile(tokenPath); err == nil {
auth.mu.Lock()
auth.pendingToken = string(data)
auth.mu.Unlock()
os.Remove(tokenPath)
}
server := NewServer(cfg, auth)
if err := server.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Server error: %v\n", err)
os.Exit(1)
}
}
func cmdRevoke() {
cfg := getConfig()
auth := NewAuth(cfg.SecretPath())
if err := auth.GenerateSecret(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
os.Remove(cfg.SessionsPath())
fmt.Println("All sessions revoked. New secret generated.")
fmt.Println("Run 'claude-remote setup' to generate a new QR code.")
}
func cmdInstall() {
home, _ := os.UserHomeDir()
plistDir := filepath.Join(home, "Library", "LaunchAgents")
os.MkdirAll(plistDir, 0755)
plistPath := filepath.Join(plistDir, "com.claude-remote.plist")
binPath, err := os.Executable()
if err != nil {
binPath = "/usr/local/bin/claude-remote"
}
tmpl := template.Must(template.New("plist").Parse(plistTemplate))
f, err := os.Create(plistPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer f.Close()
tmpl.Execute(f, map[string]string{
"BinPath": binPath,
"Home": home,
})
exec.Command("launchctl", "load", plistPath).Run()
fmt.Printf("Installed and loaded: %s\n", plistPath)
}
func cmdUninstall() {
home, _ := os.UserHomeDir()
plistPath := filepath.Join(home, "Library", "LaunchAgents", "com.claude-remote.plist")
exec.Command("launchctl", "unload", plistPath).Run()
os.Remove(plistPath)
fmt.Println("Uninstalled.")
}
func cmdStatus() {
cfg := getConfig()
fmt.Printf("Claude Remote v%s\n", version)
fmt.Printf("Config dir: %s\n", cfg.DataDir)
fmt.Printf("Port: %d\n", cfg.Port)
if _, err := os.Stat(cfg.SecretPath()); err == nil {
fmt.Println("Secret: configured")
} else {
fmt.Println("Secret: not configured (run setup)")
}
hostname := detectTailscaleHost()
fmt.Printf("Tailscale: %s\n", hostname)
}
const plistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.claude-remote</string>
<key>ProgramArguments</key>
<array>
<string>{{.BinPath}}</string>
<string>serve</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>{{.Home}}/.claude-remote/server.log</string>
<key>StandardErrorPath</key>
<string>{{.Home}}/.claude-remote/server.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin</string>
</dict>
</dict>
</plist>
`