| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "net/netip" |
| 5 | "regexp" |
| 6 | ) |
| 7 | |
| 8 | // How the person API shows Research (slice R3; slice R5 renders it): the |
| 9 | // site inventory card and a Research step's result on the task. These |
| 10 | // are read shapes only; nothing here travels to a runner. |
| 11 | |
| 12 | // CardInventoryView is what a research_inventory card shows: the primary |
| 13 | // it is about and every address Equipo detected for it (the addresses |
| 14 | // its interfaces report and the one the instance saw it connect from). |
| 15 | // Confirm records them, so Research never fetches from them. |
| 16 | type CardInventoryView struct { |
| 17 | RunnerID string `json:"runner_id"` |
| 18 | Addresses []string `json:"addresses"` |
| 19 | } |
| 20 | |
| 21 | // StepResearch is a Research step's result on the task's read shape: the |
| 22 | // findings of its latest done attempt (or the "ended without findings" |
| 23 | // gap), its fetch log, and the images it kept. Each image is served at |
| 24 | // GET /api/v1/tasks/{task}/files/{id}. |
| 25 | type StepResearch struct { |
| 26 | Attempt int64 `json:"attempt"` |
| 27 | Findings ResearchFindings `json:"findings"` |
| 28 | Fetches []ResearchFetch `json:"fetches"` |
| 29 | Images []StepResearchImage `json:"images"` |
| 30 | } |
| 31 | |
| 32 | // StepResearchImage is one kept image: ID is the task file's id, ImageID |
| 33 | // the id the findings cite. |
| 34 | type StepResearchImage struct { |
| 35 | ID string `json:"id"` |
| 36 | ImageID string `json:"image_id"` |
| 37 | SourceID string `json:"source_id"` |
| 38 | Role string `json:"role"` |
| 39 | Description string `json:"description"` |
| 40 | Size int64 `json:"size"` |
| 41 | Width int `json:"width"` |
| 42 | Height int `json:"height"` |
| 43 | } |
| 44 | |
| 45 | // ResearchOriginExpanded is a fetch log line's origin for a page or image |
| 46 | // the fetcher reached from an approved source (Parent names it). |
| 47 | const ResearchOriginExpanded = "expanded" |
| 48 | |
| 49 | // MaxKeptSide bounds a kept image's width and height, as |
| 50 | // ValidResearchImage does. |
| 51 | const MaxKeptSide = 2048 |
| 52 | |
| 53 | // ResearchNoFindingsGap is the gap a Research step's findings hold when |
| 54 | // it ended without valid findings (Research spec §5). |
| 55 | const ResearchNoFindingsGap = "Research ended without findings" |
| 56 | |
| 57 | // NoFindings is the findings document of a Research step that ended |
| 58 | // without valid findings. |
| 59 | func NoFindings() ResearchFindings { |
| 60 | return ResearchFindings{Facts: []ResearchFact{}, Images: []string{}, Gaps: []string{ResearchNoFindingsGap}} |
| 61 | } |
| 62 | |
| 63 | // ValidSHA256 is a lowercase hex SHA-256. |
| 64 | func ValidSHA256(s string) bool { return sha256Pattern.MatchString(s) } |
| 65 | |
| 66 | var fetchWord = regexp.MustCompile(`^[a-z][a-z0-9_]{0,31}$`) |
| 67 | |
| 68 | // ValidFetchWord is a fetch log line's origin or reason: a short code. |
| 69 | func ValidFetchWord(s string) bool { return fetchWord.MatchString(s) } |
| 70 | |
| 71 | // ResearchPrefixes are the site inventory prefixes an owner's Confirm |
| 72 | // records for one detected address: the address itself (/32 or /128), |
| 73 | // and for IPv6 its /64 too. Nil for anything ValidResearchAddress |
| 74 | // refuses. |
| 75 | func ResearchPrefixes(addr string) []string { |
| 76 | if !ValidResearchAddress(addr) { |
| 77 | return nil |
| 78 | } |
| 79 | a := netip.MustParseAddr(addr) |
| 80 | out := []string{netip.PrefixFrom(a, a.BitLen()).String()} |
| 81 | if a.Is6() { |
| 82 | p, _ := a.Prefix(64) |
| 83 | out = append(out, p.String()) |
| 84 | } |
| 85 | return out |
| 86 | } |
| 87 | |