pi-agent: Implement field Protocol Probe
This commit is contained in:
+40
-35
@@ -40,21 +40,21 @@ type Model struct {
|
||||
driftResolutionTargetID string
|
||||
picker profilePickerState
|
||||
form formState
|
||||
formSequence uint64
|
||||
help helpDialogState
|
||||
status string
|
||||
operation operationCoordinator
|
||||
detect func(context.Context, string) (probe.Capabilities, error)
|
||||
}
|
||||
|
||||
// probeDone carries one finished Protocol Probe back to the form. address is
|
||||
// always bare host:port so status text can never leak credentials. gen matches
|
||||
// the probe generation stored in formState so late results can be ignored after
|
||||
// the user leaves the form.
|
||||
// probeDone carries one finished Protocol Probe back to the form. Endpoint and
|
||||
// input text deliberately stay in formState: an asynchronous result cannot
|
||||
// introduce userinfo into status, and formID plus generation reject stale work.
|
||||
type probeDone struct {
|
||||
address string
|
||||
caps probe.Capabilities
|
||||
err error
|
||||
gen uint64
|
||||
caps probe.Capabilities
|
||||
err error
|
||||
formID uint64
|
||||
generation uint64
|
||||
}
|
||||
|
||||
type profileDeleteConfirmation struct {
|
||||
@@ -177,26 +177,30 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
return m, waitForOperation(progress.updates)
|
||||
}
|
||||
if done, ok := message.(probeDone); ok {
|
||||
if !m.form.probing || done.gen != m.form.probeGen {
|
||||
if m.page != formPage || !m.form.probe.active || done.formID != m.form.id || done.generation != m.form.probe.generation {
|
||||
return m, nil
|
||||
}
|
||||
m.form.probing = false
|
||||
m.form.probe.active = false
|
||||
address := m.form.probe.address
|
||||
switch {
|
||||
case done.err != nil:
|
||||
m.status = "Protocol Probe: no proxy detected at " + done.address + ": " + done.err.Error()
|
||||
// Detector errors are intentionally not rendered. Production errors are
|
||||
// based on the credential-free dial endpoint, but this boundary also
|
||||
// protects frames if a lower layer returns hostile credential text.
|
||||
m.form.probe.feedback = "Protocol Probe failed for " + address
|
||||
case !done.caps.HTTP && !done.caps.SOCKS5:
|
||||
m.status = "Protocol Probe: unrecognized protocol at " + done.address
|
||||
m.form.probe.feedback = "Protocol Probe: unrecognized protocol at " + address
|
||||
default:
|
||||
raw := m.form.profile.SingleURL
|
||||
if done.caps.HTTP && done.caps.SOCKS5 {
|
||||
httpURL, err := probe.URL(raw, probe.Capabilities{HTTP: true})
|
||||
if err != nil {
|
||||
m.status = "Protocol Probe: " + err.Error()
|
||||
m.form.probe.feedback = "Protocol Probe could not apply the detected capabilities"
|
||||
return m, nil
|
||||
}
|
||||
socksURL, err := probe.URL(raw, probe.Capabilities{SOCKS5: true})
|
||||
if err != nil {
|
||||
m.status = "Protocol Probe: " + err.Error()
|
||||
m.form.probe.feedback = "Protocol Probe could not apply the detected capabilities"
|
||||
return m, nil
|
||||
}
|
||||
m.form.profile.Mode = profile.AdvancedMode
|
||||
@@ -204,22 +208,24 @@ func (m Model) Update(message tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.form.profile.HTTPSURL = httpURL
|
||||
m.form.profile.AllURL = strings.Replace(socksURL, "socks5h://", "socks5://", 1)
|
||||
m.form.advancedInitialized = true
|
||||
m.form.probe.field = profile.HTTPURLField
|
||||
m.form.probe.feedback = "Protocol Probe: detected HTTP and SOCKS5 proxy at " + address
|
||||
m.form.rebuildInputs(1)
|
||||
m.status = "Protocol Probe: detected HTTP and SOCKS5 proxy at " + done.address
|
||||
return m, nil
|
||||
}
|
||||
rewritten, err := probe.URL(raw, done.caps)
|
||||
if err != nil {
|
||||
m.status = "Protocol Probe: " + err.Error()
|
||||
m.form.probe.feedback = "Protocol Probe could not apply the detected capabilities"
|
||||
return m, nil
|
||||
}
|
||||
m.form.profile.SingleURL = rewritten
|
||||
m.form.rebuildInputs(1)
|
||||
m.form.probe.field = profile.SingleURLField
|
||||
kind := "SOCKS5"
|
||||
if done.caps.HTTP {
|
||||
kind = "HTTP"
|
||||
}
|
||||
m.status = "Protocol Probe: detected " + kind + " proxy at " + done.address
|
||||
m.form.probe.feedback = "Protocol Probe: detected " + kind + " proxy at " + address
|
||||
m.form.rebuildInputs(1)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -495,7 +501,8 @@ func (m Model) updateProfiles(key tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
if !m.profileMutationAvailable("creating a Proxy Profile") {
|
||||
break
|
||||
}
|
||||
m.form = newFormState("", profile.Profile{Mode: profile.SingleMode})
|
||||
m.formSequence++
|
||||
m.form = newFormState(m.formSequence, "", profile.Profile{Mode: profile.SingleMode})
|
||||
m.page = formPage
|
||||
m.status = ""
|
||||
case "e", "enter":
|
||||
@@ -503,7 +510,8 @@ func (m Model) updateProfiles(key tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
break
|
||||
}
|
||||
if selected, ok := m.selectedProfile(); ok {
|
||||
m.form = newFormState(selected.Name, selected)
|
||||
m.formSequence++
|
||||
m.form = newFormState(m.formSequence, selected.Name, selected)
|
||||
m.page = formPage
|
||||
m.status = ""
|
||||
}
|
||||
@@ -567,12 +575,11 @@ func (m Model) updateForm(key tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
switch key.Type {
|
||||
case tea.KeyCtrlS:
|
||||
m.form.invalidateProbe()
|
||||
m.form.syncFocusedValue()
|
||||
m.form.refreshValidation()
|
||||
if err := m.form.validation.Err(); err != nil || m.form.nameConflict(m.snapshot.Profiles) != "" {
|
||||
m.status = "Fix the highlighted Profile fields before saving"
|
||||
m.form.probeGen++
|
||||
m.form.probing = false
|
||||
return m, nil
|
||||
}
|
||||
kind := app.CreateProfile
|
||||
@@ -583,34 +590,32 @@ func (m Model) updateForm(key tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
intent := app.Intent{Kind: kind, Name: m.form.originalName, Profile: normalized}
|
||||
result := m.applyProfileMutation(intent, normalized.Name)
|
||||
if result.Err != nil {
|
||||
m.form.probeGen++ // discard any in-flight probe result while staying on the form
|
||||
m.form.probing = false
|
||||
return m, nil
|
||||
}
|
||||
m.page = profilesPage
|
||||
m.form = formState{}
|
||||
return m, nil
|
||||
case tea.KeyCtrlD:
|
||||
if m.form.profile.Mode != profile.SingleMode {
|
||||
m.status = "Protocol Probe: available in Single mode only"
|
||||
return m, nil
|
||||
}
|
||||
if m.form.currentField() != profile.SingleURLField || m.form.probing {
|
||||
if m.form.profile.Mode != profile.SingleMode || m.form.currentField() != profile.SingleURLField || m.form.probe.active {
|
||||
return m, nil
|
||||
}
|
||||
m.form.syncFocusedValue()
|
||||
m.form.invalidateProbe()
|
||||
m.form.probe.field = profile.SingleURLField
|
||||
address, err := probeAddress(m.form.profile.SingleURL)
|
||||
if err != nil {
|
||||
m.status = "Protocol Probe: " + err.Error()
|
||||
m.form.probe.feedback = "Protocol Probe: " + err.Error()
|
||||
return m, nil
|
||||
}
|
||||
m.form.probing = true
|
||||
m.form.probeGen++
|
||||
m.status = "Protocol Probe: checking " + address + "…"
|
||||
m.form.probe.active = true
|
||||
m.form.probe.address = address
|
||||
m.form.probe.feedback = "Protocol Probe: checking " + address + "…"
|
||||
detect := m.detect
|
||||
gen := m.form.probeGen
|
||||
formID := m.form.id
|
||||
generation := m.form.probe.generation
|
||||
return m, func() tea.Msg {
|
||||
caps, err := detect(context.Background(), address)
|
||||
return probeDone{address: address, caps: caps, err: err, gen: gen}
|
||||
return probeDone{caps: caps, err: err, formID: formID, generation: generation}
|
||||
}
|
||||
case tea.KeyCtrlT:
|
||||
return m, m.form.toggleMode()
|
||||
|
||||
Reference in New Issue
Block a user