| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "html" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/http/cookiejar" |
| 11 | "net/url" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | "sync" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/rastrillo/rastrillo/pow" |
| 18 | ) |
| 19 | |
| 20 | type capture struct { |
| 21 | mu sync.Mutex |
| 22 | link string |
| 23 | } |
| 24 | |
| 25 | func (m *capture) Send(_ context.Context, _, _, body string) error { |
| 26 | m.mu.Lock() |
| 27 | defer m.mu.Unlock() |
| 28 | for _, word := range strings.Fields(body) { |
| 29 | if strings.Contains(word, "/auth/verify?") { |
| 30 | m.link = word |
| 31 | return nil |
| 32 | } |
| 33 | } |
| 34 | return errors.New("sign-in email has no link") |
| 35 | } |
| 36 | func (m *capture) last() string { m.mu.Lock(); defer m.mu.Unlock(); return m.link } |
| 37 | func newClient() *http.Client { |
| 38 | jar, _ := cookiejar.New(nil) |
| 39 | return &http.Client{Jar: jar, Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} |
| 40 | } |
| 41 | |
| 42 | var forms = regexp.MustCompile(`(?s)<form\b[^>]*>.*?</form>`) |
| 43 | var inputs = regexp.MustCompile(`<input\b[^>]*>`) |
| 44 | var attributes = regexp.MustCompile(`([a-z_-]+)="([^"]*)"`) |
| 45 | |
| 46 | func fields(c *http.Client, target, action string) (url.Values, error) { |
| 47 | res, err := c.Get(target) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | defer res.Body.Close() |
| 52 | body, err := io.ReadAll(res.Body) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | if res.StatusCode != 200 { |
| 57 | return nil, fmt.Errorf("GET %s: %d", res.Request.URL.Path, res.StatusCode) |
| 58 | } |
| 59 | for _, form := range forms.FindAllString(string(body), -1) { |
| 60 | if !strings.Contains(form, `action="`+action+`"`) { |
| 61 | continue |
| 62 | } |
| 63 | values := url.Values{} |
| 64 | for _, input := range inputs.FindAllString(form, -1) { |
| 65 | attrs := map[string]string{} |
| 66 | for _, pair := range attributes.FindAllStringSubmatch(input, -1) { |
| 67 | attrs[pair[1]] = html.UnescapeString(pair[2]) |
| 68 | } |
| 69 | if attrs["name"] != "" { |
| 70 | values.Set(attrs["name"], attrs["value"]) |
| 71 | } |
| 72 | } |
| 73 | return values, nil |
| 74 | } |
| 75 | return nil, fmt.Errorf("missing form: %s", action) |
| 76 | } |
| 77 | func post(ctx context.Context, c *http.Client, origin, path string, values url.Values) (string, error) { |
| 78 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, origin+path, strings.NewReader(values.Encode())) |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 83 | req.Header.Set("Origin", origin) |
| 84 | res, err := c.Do(req) |
| 85 | if err != nil { |
| 86 | return "", err |
| 87 | } |
| 88 | defer res.Body.Close() |
| 89 | if res.StatusCode != 303 { |
| 90 | return "", fmt.Errorf("POST %s: %d", path, res.StatusCode) |
| 91 | } |
| 92 | return res.Header.Get("Location"), nil |
| 93 | } |
| 94 | |
| 95 | type challenge struct { |
| 96 | values url.Values |
| 97 | ready time.Time |
| 98 | } |
| 99 | |
| 100 | func prepare(_ context.Context, c *http.Client, origin string) (challenge, error) { |
| 101 | values, err := fields(c, origin+"/signin?force=1", "/auth/begin") |
| 102 | if err != nil { |
| 103 | return challenge{}, err |
| 104 | } |
| 105 | values.Set("address", owner) |
| 106 | return challenge{values, time.Now().Add(pow.DefaultMinAge + 50*time.Millisecond)}, nil |
| 107 | } |
| 108 | func begin(ctx context.Context, c *http.Client, origin string, ch challenge, m *capture) (string, error) { |
| 109 | timer := time.NewTimer(time.Until(ch.ready)) |
| 110 | defer timer.Stop() |
| 111 | select { |
| 112 | case <-ctx.Done(): |
| 113 | return "", ctx.Err() |
| 114 | case <-timer.C: |
| 115 | } |
| 116 | location, err := post(ctx, c, origin, "/auth/begin", ch.values) |
| 117 | if err != nil { |
| 118 | return "", err |
| 119 | } |
| 120 | if !strings.Contains(location, "sent=1") { |
| 121 | return "", errors.New("sign-in request refused") |
| 122 | } |
| 123 | return m.last(), nil |
| 124 | } |
| 125 | func confirm(c *http.Client, link string) error { |
| 126 | u, err := url.Parse(link) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | values, err := fields(c, link, "/auth/verify") |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | location, err := post(context.Background(), c, u.Scheme+"://"+u.Host, "/auth/verify", values) |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | if location != "/" { |
| 139 | return errors.New("sign-in confirmation refused") |
| 140 | } |
| 141 | return nil |
| 142 | } |
| 143 | func seed(ctx context.Context, c *http.Client, origin string) error { |
| 144 | _, err := post(ctx, c, origin, "/accounts", url.Values{"id": {"0"}, "revision": {"0"}, "label": {"Drive account"}}) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | for _, kind := range []string{"app", "website", "repository"} { |
| 149 | v, err := fields(c, origin+"/projects/new", "/projects") |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | v.Set("account_id", "1") |
| 154 | v.Set("name", "Drive "+kind) |
| 155 | v.Set("kind", kind) |
| 156 | v.Set("repository", "amadan.net/drive/"+kind) |
| 157 | if kind != "repository" { |
| 158 | v.Set("app_name", "drive-"+kind) |
| 159 | } |
| 160 | v.Set("default_branch", "main") |
| 161 | v.Set("owner_work", owner) |
| 162 | v.Set("owner_merge", owner) |
| 163 | location, err := post(ctx, c, origin, "/projects", v) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if !strings.HasPrefix(location, "/projects/") { |
| 168 | return errors.New("project was not saved") |
| 169 | } |
| 170 | } |
| 171 | return nil |
| 172 | } |
| 173 | |