2017-07-04 08:37:59 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-04-20 13:20:39 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2017-07-04 08:37:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
contentType = "application/x-www-form-urlencoded"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type Client struct {
|
|
|
|
url string
|
|
|
|
insecure bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient ...
|
|
|
|
func NewClient(url string, insecure bool) *Client {
|
|
|
|
return &Client{url: url, insecure: insecure}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paste ...
|
|
|
|
func (c *Client) Paste(body io.Reader) error {
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: !c.insecure},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
|
2021-04-20 13:20:39 +00:00
|
|
|
buf := new(strings.Builder)
|
|
|
|
_, err := io.Copy(buf, body)
|
|
|
|
// check errors
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error reading in file: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("blob", buf.String())
|
|
|
|
res, err := client.PostForm(c.url, v)
|
2017-07-04 08:37:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error pasting to %s: %s", c.url, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-20 13:20:39 +00:00
|
|
|
if res.StatusCode != 200 && res.StatusCode != 301 {
|
2017-07-04 08:37:59 +00:00
|
|
|
log.Printf("unexpected response from %s: %d", c.url, res.StatusCode)
|
|
|
|
return errors.New("unexpected response")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s", res.Request.URL.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|