File size: 7,707 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/pinchtab/pinchtab/internal/cli"
	"github.com/pinchtab/pinchtab/internal/config"
)

// runSecurityWizard runs the interactive security setup wizard.
// isNew indicates a fresh install (full wizard) vs upgrade (migration notice).
// Returns true if the user completed setup, false if they cancelled.
func runSecurityWizard(cfg *config.FileConfig, configPath string, isNew bool) bool {
	interactive := isInteractiveTerminal()

	if !interactive {
		return runNonInteractiveSetup(cfg, configPath, isNew)
	}

	if isNew {
		return runFullWizard(cfg, configPath)
	}
	return runUpgradeNotice(cfg, configPath)
}

// runNonInteractiveSetup prints a summary and applies defaults silently.
func runNonInteractiveSetup(cfg *config.FileConfig, configPath string, isNew bool) bool {
	if isNew {
		fmt.Println()
		fmt.Println(cli.StyleStdout(cli.HeadingStyle, "πŸ›‘οΈ  Know your config"))
		fmt.Println()
		fmt.Println("   Guard: UP (maximum security)")
		fmt.Printf("   Allowed domains: %s\n", strings.Join(getAllowedDomains(cfg), ", "))
		if cfg.Server.Token != "" {
			fmt.Printf("   Token: %s\n", cfg.Server.Token[:min(12, len(cfg.Server.Token))]+"...")
		}
		fmt.Println()
		fmt.Println("   Run " + cli.StyleStdout(cli.CommandStyle, "pinchtab security") + " to review all settings.")
		fmt.Println()
	} else {
		fmt.Println()
		fmt.Println(cli.StyleStdout(cli.HeadingStyle, "πŸ›‘οΈ  Config updated to v"+config.CurrentConfigVersion))
		fmt.Println("   Run " + cli.StyleStdout(cli.CommandStyle, "pinchtab security") + " to review changes.")
		fmt.Println()
	}

	cfg.ConfigVersion = config.CurrentConfigVersion
	_ = config.SaveFileConfig(cfg, configPath)
	return true
}

// runFullWizard runs the interactive first-run wizard.
func runFullWizard(cfg *config.FileConfig, configPath string) bool {
	fmt.Println()
	fmt.Println(cli.StyleStdout(cli.HeadingStyle, "πŸ›‘οΈ  Know your config"))
	fmt.Println()
	fmt.Println("PinchTab ships with the strongest security defaults.")
	fmt.Println("Choose your security posture:")
	fmt.Println()
	printSeparator()

	// Guard Up
	fmt.Println()
	fmt.Println(cli.StyleStdout(cli.HeadingStyle, "1. Guard UP (recommended)"))
	fmt.Println(cli.StyleStdout(cli.MutedStyle, "Only sites running on this machine can be automated."))
	fmt.Println()
	printSetting("domains", cli.StyleStdout(cli.ValueStyle, strings.Join(getAllowedDomains(cfg), ", ")))
	printSetting("evaluate", cli.StyleStdout(cli.SuccessStyle, "disabled"))
	printSetting("download", cli.StyleStdout(cli.SuccessStyle, "disabled"))
	printSetting("upload", cli.StyleStdout(cli.SuccessStyle, "disabled"))
	printSetting("macros", cli.StyleStdout(cli.SuccessStyle, "disabled"))
	printSetting("screencast", cli.StyleStdout(cli.SuccessStyle, "disabled"))
	printSetting("IDPI", cli.StyleStdout(cli.SuccessStyle, "strict"))
	fmt.Println()

	// Guard Down
	fmt.Println(cli.StyleStdout(cli.HeadingStyle, "2. Guard DOWN (development)"))
	fmt.Println(cli.StyleStdout(cli.MutedStyle, "All features enabled, any site can be automated. Use for local dev only."))
	fmt.Println()
	printSetting("domains", cli.StyleStdout(cli.WarningStyle, "all"))
	printSetting("evaluate", cli.StyleStdout(cli.WarningStyle, "enabled"))
	printSetting("download", cli.StyleStdout(cli.WarningStyle, "enabled"))
	printSetting("upload", cli.StyleStdout(cli.WarningStyle, "enabled"))
	printSetting("macros", cli.StyleStdout(cli.WarningStyle, "enabled"))
	printSetting("screencast", cli.StyleStdout(cli.WarningStyle, "enabled"))
	printSetting("IDPI", cli.StyleStdout(cli.WarningStyle, "off"))
	fmt.Println()
	printSeparator()
	fmt.Println()

	picked, err := promptSelect("Security posture", []menuOption{
		{label: "Guard UP β€” maximum security", value: "up"},
		{label: "Guard DOWN β€” development mode", value: "down"},
	})
	if err != nil {
		return false
	}

	switch picked {
	case "up":
		applyGuardUp(cfg)
	case "down":
		applyGuardDown(cfg)
	}

	// Dashboard access
	printSeparator()
	fmt.Println()
	fmt.Println(cli.StyleStdout(cli.HeadingStyle, "Dashboard"))
	fmt.Println()
	loginURL := dashboardURL(cfg, "")
	fmt.Println(cli.StyleStdout(cli.CommandStyle, loginURL))
	if err := copyToClipboard(loginURL); err == nil {
		fmt.Println(cli.StyleStdout(cli.MutedStyle, "Copied to clipboard"))
	}
	fmt.Println()

	// Save
	cfg.ConfigVersion = config.CurrentConfigVersion
	if err := config.SaveFileConfig(cfg, configPath); err != nil {
		fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle, fmt.Sprintf("failed to save config: %v", err)))
		return false
	}

	fmt.Println(cli.StyleStdout(cli.SuccessStyle, "βœ“ Configuration complete β€” installing..."))
	fmt.Println(cli.StyleStdout(cli.MutedStyle, "For more configuration, visit the dashboard."))
	fmt.Println()
	return true
}

// runUpgradeNotice shows a brief notice for config upgrades.
func runUpgradeNotice(cfg *config.FileConfig, configPath string) bool {
	fmt.Println()
	fmt.Println(cli.StyleStdout(cli.HeadingStyle, "πŸ›‘οΈ  Config update (v"+config.CurrentConfigVersion+")"))
	fmt.Println()

	oldVersion := cfg.ConfigVersion
	if oldVersion == "" {
		oldVersion = "pre-0.8.0"
	}
	fmt.Printf("   Upgraded: %s β†’ %s\n", oldVersion, config.CurrentConfigVersion)

	if cfg.Server.Token != "" {
		fmt.Printf("   Token: %s\n", cli.StyleStdout(cli.ValueStyle, cfg.Server.Token[:min(12, len(cfg.Server.Token))]+"..."))
	}

	fmt.Println()
	fmt.Println("   Run " + cli.StyleStdout(cli.CommandStyle, "pinchtab security") + " to review all settings.")
	fmt.Println()

	cfg.ConfigVersion = config.CurrentConfigVersion
	_ = config.SaveFileConfig(cfg, configPath)
	return true
}

// ─── Guard Presets ───────────────────────────────────────────────

func applyGuardUp(cfg *config.FileConfig) {
	f := false
	cfg.Security.AllowEvaluate = &f
	cfg.Security.AllowDownload = &f
	cfg.Security.AllowUpload = &f
	cfg.Security.AllowMacro = &f
	cfg.Security.AllowScreencast = &f
	cfg.Security.IDPI.Enabled = true
	cfg.Security.IDPI.StrictMode = true
	cfg.Security.IDPI.ScanContent = true
	cfg.Security.IDPI.WrapContent = true
	cfg.Security.IDPI.AllowedDomains = []string{"127.0.0.1", "localhost", "::1"}
	cfg.Server.Bind = "127.0.0.1"
}

func applyGuardDown(cfg *config.FileConfig) {
	t := true
	cfg.Security.AllowEvaluate = &t
	cfg.Security.AllowDownload = &t
	cfg.Security.AllowUpload = &t
	cfg.Security.AllowMacro = &t
	cfg.Security.AllowScreencast = &t
	cfg.Security.IDPI.Enabled = false
	cfg.Security.IDPI.StrictMode = false
	cfg.Security.IDPI.AllowedDomains = nil
}

// ─── Helpers ─────────────────────────────────────────────────────

func getAllowedDomains(cfg *config.FileConfig) []string {
	if len(cfg.Security.IDPI.AllowedDomains) > 0 {
		return cfg.Security.IDPI.AllowedDomains
	}
	return []string{"127.0.0.1", "localhost", "::1"}
}

func printSeparator() {
	fmt.Println(cli.StyleStdout(cli.MutedStyle, strings.Repeat("━", 50)))
}

func printSetting(name, value string) {
	fmt.Printf("  %-12s %s\n", name+":", value)
}

func boolPtrValue(p *bool) bool {
	if p == nil {
		return false
	}
	return *p
}

func dashboardURL(cfg *config.FileConfig, path string) string {
	host := orDefault(cfg.Server.Bind, "127.0.0.1")
	port := orDefault(cfg.Server.Port, "9867")
	url := fmt.Sprintf("http://%s:%s%s", host, port, path)
	if cfg.Server.Token != "" {
		url += "?token=" + cfg.Server.Token
	}
	return url
}

func orDefault(val, fallback string) string {
	if val == "" {
		return fallback
	}
	return val
}

// copyToClipboard is defined in cmd_config.go