| 1 |
// Copyright 2013 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
// Package scrape provides a client for interacting with GitHub using screen |
| 7 |
// scraping. |
| 8 |
// |
| 9 |
// This is intended to be used as a supplement to the standard go-github |
| 10 |
// library to access data that is not currently exposed by either the official |
| 11 |
// REST or GraphQL APIs. |
| 12 |
// |
| 13 |
// Because of the nature of screen scraping, this package should be treated as |
| 14 |
// HIGHLY EXPERIMENTAL, and potentially unstable. We make no promises relating |
| 15 |
// to compatibility or stability of the exported API. Even though this package |
| 16 |
// is distributed as part of the go-github library, it is explicitly exempt |
| 17 |
// from any stability promises that my be implied by the library version |
| 18 |
// number. |
| 19 |
package scrape |
| 20 |
|
| 21 |
import ( |
| 22 |
"bytes" |
| 23 |
"encoding/gob" |
| 24 |
"fmt" |
| 25 |
"log" |
| 26 |
"net/http" |
| 27 |
"net/http/cookiejar" |
| 28 |
"net/url" |
| 29 |
"strings" |
| 30 |
|
| 31 |
"github.com/PuerkitoBio/goquery" |
| 32 |
"github.com/xlzd/gotp" |
| 33 |
"golang.org/x/net/publicsuffix" |
| 34 |
) |
| 35 |
|
| 36 |
var defaultBaseURL = "https://github.com/" |
| 37 |
|
| 38 |
// Client is a GitHub scraping client. |
| 39 |
type Client struct { |
| 40 |
*http.Client |
| 41 |
|
| 42 |
// base URL for github.com pages. Exposed primarily for testing. Also |
| 43 |
// used for saving and restoring cookies on the Client. |
| 44 |
baseURL *url.URL |
| 45 |
} |
| 46 |
|
| 47 |
// NewClient constructs a new Client. If transport is nil, a default transport is used. |
| 48 |
func NewClient(transport http.RoundTripper) *Client { |
| 49 |
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) |
| 50 |
if err != nil { |
| 51 |
log.Fatalf("error creating cookiejar: %v", err) |
| 52 |
} |
| 53 |
baseURL, _ := url.Parse(defaultBaseURL) |
| 54 |
|
| 55 |
return &Client{ |
| 56 |
Client: &http.Client{ |
| 57 |
Transport: transport, |
| 58 |
Jar: jar, |
| 59 |
}, |
| 60 |
baseURL: baseURL, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// SaveCookies returns an encoded form of the github.com cookies set on this |
| 65 |
// client. If Authenticate() has been called, this should include the |
| 66 |
// github.com session cookie. These cookies can be loaded onto a new client by |
| 67 |
// calling LoadCookies. |
| 68 |
// |
| 69 |
// GitHub session cookies are bearer tokens that are not tied to any particular |
| 70 |
// client, so should be treated with the same sensitivity as the account |
| 71 |
// credentials. |
| 72 |
func (c *Client) SaveCookies() ([]byte, error) { |
| 73 |
cookies := c.Client.Jar.Cookies(c.baseURL) |
| 74 |
|
| 75 |
var b bytes.Buffer |
| 76 |
err := gob.NewEncoder(&b).Encode(cookies) |
| 77 |
return b.Bytes(), err |
| 78 |
} |
| 79 |
|
| 80 |
// LoadCookies loads the provided cookies for github.com. |
| 81 |
func (c *Client) LoadCookies(v []byte) error { |
| 82 |
var cookies []*http.Cookie |
| 83 |
r := bytes.NewReader(v) |
| 84 |
err := gob.NewDecoder(r).Decode(&cookies) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
|
| 89 |
c.Client.Jar.SetCookies(c.baseURL, cookies) |
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
// get fetches a urlStr (a GitHub URL relative to the client's baseURL), and |
| 94 |
// returns the parsed response document. |
| 95 |
func (c *Client) get(urlStr string, a ...interface{}) (*goquery.Document, error) { |
| 96 |
u, err := c.baseURL.Parse(fmt.Sprintf(urlStr, a...)) |
| 97 |
if err != nil { |
| 98 |
return nil, fmt.Errorf("error parsing URL: %q: %v", urlStr, err) |
| 99 |
} |
| 100 |
|
| 101 |
resp, err := c.Client.Get(u.String()) |
| 102 |
if err != nil { |
| 103 |
return nil, fmt.Errorf("error fetching url %q: %v", u, err) |
| 104 |
} |
| 105 |
defer resp.Body.Close() |
| 106 |
|
| 107 |
if resp.StatusCode == http.StatusNotFound { |
| 108 |
return nil, fmt.Errorf("received %v response fetching URL %q", resp.StatusCode, u) |
| 109 |
} |
| 110 |
|
| 111 |
doc, err := goquery.NewDocumentFromReader(resp.Body) |
| 112 |
if err != nil { |
| 113 |
return nil, fmt.Errorf("error parsing response: %v", err) |
| 114 |
} |
| 115 |
|
| 116 |
return doc, nil |
| 117 |
} |
| 118 |
|
| 119 |
// Authenticate client to GitHub with the provided username, password, and if |
| 120 |
// two-factor auth is enabled for the account, otpseed. |
| 121 |
// |
| 122 |
// otpseed is the OTP Secret provided from GitHub as part of two-factor |
| 123 |
// application enrollment. When registering the application, click the "enter |
| 124 |
// this text code" link on the QR Code page to see the raw OTP Secret. |
| 125 |
func (c *Client) Authenticate(username, password, otpseed string) error { |
| 126 |
setPassword := func(values url.Values) { |
| 127 |
values.Set("login", username) |
| 128 |
values.Set("password", password) |
| 129 |
} |
| 130 |
resp, err := fetchAndSubmitForm(c.Client, "https://github.com/login", setPassword) |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
if resp.StatusCode != http.StatusOK { |
| 135 |
return fmt.Errorf("received %v response submitting login form", resp.StatusCode) |
| 136 |
} |
| 137 |
|
| 138 |
if otpseed == "" { |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
setOTP := func(values url.Values) { |
| 143 |
otp := gotp.NewDefaultTOTP(strings.ToUpper(otpseed)).Now() |
| 144 |
values.Set("otp", otp) |
| 145 |
} |
| 146 |
resp, err = fetchAndSubmitForm(c.Client, "https://github.com/sessions/two-factor", setOTP) |
| 147 |
if err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
if resp.StatusCode != http.StatusOK { |
| 151 |
return fmt.Errorf("received %v response submitting otp form", resp.StatusCode) |
| 152 |
} |
| 153 |
|
| 154 |
return nil |
| 155 |
} |