2019-11-09 15:50:12 +01:00
|
|
|
package rushlink
|
2019-08-25 21:33:56 +02:00
|
|
|
|
|
|
|
import (
|
2019-09-01 01:41:01 +02:00
|
|
|
"crypto/subtle"
|
|
|
|
"encoding/base64"
|
2019-08-25 21:33:56 +02:00
|
|
|
"fmt"
|
2019-11-10 19:03:57 +01:00
|
|
|
"io"
|
2019-08-25 21:33:56 +02:00
|
|
|
"log"
|
2019-11-10 19:03:57 +01:00
|
|
|
"mime/multipart"
|
2019-08-25 21:33:56 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-11-10 19:03:57 +01:00
|
|
|
"os"
|
2019-08-25 21:33:56 +02:00
|
|
|
"time"
|
|
|
|
|
2019-11-10 19:03:57 +01:00
|
|
|
"github.com/google/uuid"
|
2019-08-29 23:40:24 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/pkg/errors"
|
2019-08-25 21:33:56 +02:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
type viewPaste uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ viewPaste = 1 << iota
|
|
|
|
viewNoRedirect
|
|
|
|
viewShowMeta
|
|
|
|
)
|
|
|
|
|
|
|
|
const CookieDeleteToken = "owner_token"
|
2019-09-01 01:41:01 +02:00
|
|
|
|
2019-09-01 12:04:43 +02:00
|
|
|
// These keys are designated reserved, and will not be randomly chosen
|
2019-09-15 22:54:07 +02:00
|
|
|
var ReservedPasteKeys = []string{"xd42", "example"}
|
2019-09-01 12:04:43 +02:00
|
|
|
|
2019-09-01 01:41:01 +02:00
|
|
|
// Base64 encoding and decoding
|
|
|
|
var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
2019-09-11 00:12:31 +02:00
|
|
|
var base64Encoder = base64.RawURLEncoding.WithPadding(base64.NoPadding)
|
2019-09-01 01:41:01 +02:00
|
|
|
|
2019-11-10 19:03:57 +01:00
|
|
|
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render(w, r, "index", map[string]interface{}{})
|
2019-09-01 01:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:03:57 +01:00
|
|
|
func uploadFileGetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
|
|
|
|
var fu *fileUpload
|
|
|
|
var badID bool
|
|
|
|
if err := DB.View(func(tx *bolt.Tx) error {
|
|
|
|
fuID, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
|
|
|
badID = true
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fu, err = getFileUpload(tx, fuID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
if badID {
|
|
|
|
renderError(w, r, http.StatusNotFound, "malformed file id")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-09-01 01:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:03:57 +01:00
|
|
|
filePath := fileStorePath(fu.ID, fu.FileName)
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("error: %v should exist according to the database, but it doesn't", filePath)
|
|
|
|
renderError(w, r, http.StatusNotFound, "file not found")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", fu.ContentType)
|
|
|
|
io.Copy(w, file)
|
2019-08-25 21:33:56 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
func viewPasteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
viewPasteHandlerInner(w, r, 0)
|
2019-08-31 00:02:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
func viewPasteHandlerNoRedirect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
viewPasteHandlerInner(w, r, viewNoRedirect)
|
2019-08-31 00:02:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
func viewPasteHandlerMeta(w http.ResponseWriter, r *http.Request) {
|
|
|
|
viewPasteHandlerInner(w, r, viewShowMeta)
|
2019-09-01 01:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
func viewPasteHandlerInner(w http.ResponseWriter, r *http.Request, flags viewPaste) {
|
2019-08-29 00:50:26 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
key := vars["key"]
|
2019-09-22 14:03:27 +02:00
|
|
|
var p *paste
|
2019-11-10 19:03:57 +01:00
|
|
|
var fuID *uuid.UUID
|
|
|
|
var fu *fileUpload
|
2019-11-09 15:50:12 +01:00
|
|
|
if err := DB.View(func(tx *bolt.Tx) error {
|
2019-08-29 00:50:26 +02:00
|
|
|
var err error
|
2019-09-22 14:03:27 +02:00
|
|
|
p, err = getPaste(tx, key)
|
2019-11-10 19:03:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p != nil && p.Type == pasteTypeFileUpload {
|
|
|
|
var id uuid.UUID
|
|
|
|
copy(id[:], p.Content)
|
|
|
|
fuID = &id
|
|
|
|
fu, err = getFileUpload(tx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-29 00:50:26 +02:00
|
|
|
}); err != nil {
|
2019-10-15 23:19:45 +02:00
|
|
|
panic(err)
|
2019-08-29 00:50:26 +02:00
|
|
|
}
|
2019-11-10 19:03:57 +01:00
|
|
|
|
2019-09-22 14:03:27 +02:00
|
|
|
if p == nil {
|
2019-09-19 21:42:01 +02:00
|
|
|
renderError(w, r, http.StatusNotFound, "url key not found in the database")
|
2019-09-01 01:41:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
if flags&viewShowMeta != 0 {
|
|
|
|
canDelete := struct {
|
|
|
|
Bool bool
|
|
|
|
String string
|
|
|
|
}{Bool: false}
|
2019-09-21 21:03:31 +02:00
|
|
|
deleteToken := getDeleteTokenFromRequest(r)
|
|
|
|
if deleteToken == "" {
|
2019-09-21 13:11:38 +02:00
|
|
|
canDelete.String = "undefined"
|
|
|
|
} else {
|
2019-09-22 14:03:27 +02:00
|
|
|
if subtle.ConstantTimeCompare([]byte(deleteToken), []byte(p.DeleteToken)) == 1 {
|
2019-09-21 13:11:38 +02:00
|
|
|
canDelete.Bool = true
|
|
|
|
canDelete.String = "correct"
|
|
|
|
} else {
|
|
|
|
canDelete.String = "invalid"
|
|
|
|
}
|
2019-09-01 01:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 22:54:07 +02:00
|
|
|
data := map[string]interface{}{
|
2019-09-22 14:03:27 +02:00
|
|
|
"Paste": p,
|
2019-09-21 13:11:38 +02:00
|
|
|
"CanDelete": canDelete,
|
2019-09-15 22:54:07 +02:00
|
|
|
}
|
2019-09-19 21:42:01 +02:00
|
|
|
render(w, r, "pasteMeta", data)
|
2019-08-31 00:02:01 +02:00
|
|
|
return
|
2019-08-29 00:50:26 +02:00
|
|
|
}
|
2019-09-01 01:41:01 +02:00
|
|
|
|
2019-09-22 14:03:27 +02:00
|
|
|
switch p.State {
|
2019-11-10 19:03:57 +01:00
|
|
|
case pasteStatePresent:
|
|
|
|
var location string
|
|
|
|
switch p.Type {
|
|
|
|
case pasteTypeFileUpload:
|
|
|
|
if fu == nil {
|
|
|
|
panic(fmt.Sprintf("file for id %v does not exist in database\n", fuID))
|
2019-08-31 00:02:01 +02:00
|
|
|
}
|
2019-11-10 19:03:57 +01:00
|
|
|
location = fu.url().String()
|
|
|
|
break
|
|
|
|
case pasteTypeRedirect:
|
|
|
|
location = p.redirectURL().String()
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
panic("paste type unsupported")
|
|
|
|
}
|
|
|
|
if flags&viewNoRedirect == 0 {
|
|
|
|
http.Redirect(w, r, location, http.StatusSeeOther)
|
2019-08-31 00:02:01 +02:00
|
|
|
}
|
2019-11-10 19:03:57 +01:00
|
|
|
fmt.Fprint(w, location)
|
|
|
|
case pasteStateDeleted:
|
|
|
|
renderError(w, r, http.StatusGone, "paste has been deleted\n")
|
2019-08-29 00:50:26 +02:00
|
|
|
default:
|
2019-10-15 23:19:45 +02:00
|
|
|
panic(errors.Errorf("invalid paste.State (%v) for key '%v'", p.State, p.Key))
|
2019-08-29 00:50:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 13:11:38 +02:00
|
|
|
func newPasteHandler(w http.ResponseWriter, r *http.Request) {
|
2019-11-22 18:41:54 +01:00
|
|
|
file, fileHeader, err := r.FormFile("file")
|
|
|
|
if err == nil {
|
|
|
|
newFileUploadPasteHandler(w, r, file, *fileHeader)
|
|
|
|
return
|
|
|
|
} else if err == http.ErrMissingFile {
|
|
|
|
// Fallthrough
|
|
|
|
} else {
|
|
|
|
msg := fmt.Sprintf("could not parse form: %v\n", err)
|
|
|
|
renderError(w, r, http.StatusBadRequest, msg)
|
2019-11-10 19:03:57 +01:00
|
|
|
return
|
2019-09-21 13:11:38 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:41:54 +01:00
|
|
|
shorten := r.FormValue("shorten")
|
|
|
|
if shorten != "" {
|
|
|
|
newRedirectPasteHandler(w, r, shorten)
|
2019-09-21 13:11:38 +02:00
|
|
|
return
|
|
|
|
}
|
2019-11-10 19:03:57 +01:00
|
|
|
|
2019-11-22 18:41:54 +01:00
|
|
|
renderError(w, r, http.StatusBadRequest, "no 'file' and no 'shorten' fields given in form\n")
|
2019-11-10 19:03:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:41:54 +01:00
|
|
|
func newFileUploadPasteHandler(w http.ResponseWriter, r *http.Request, file multipart.File, header multipart.FileHeader) {
|
2019-11-10 19:03:57 +01:00
|
|
|
var fu *fileUpload
|
|
|
|
var paste *paste
|
|
|
|
if err := DB.Update(func(tx *bolt.Tx) error {
|
|
|
|
var err error
|
|
|
|
// Create the fileUpload in the database
|
2019-11-22 18:41:54 +01:00
|
|
|
fu, err = newFileUpload(tx, file, header.Filename, header.Header.Get("Content-Type"))
|
2019-11-10 19:03:57 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "creating fileUpload"))
|
|
|
|
}
|
|
|
|
|
|
|
|
paste, err = shortenFileUploadID(tx, fu.ID)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
data := map[string]interface{}{"Paste": paste}
|
|
|
|
render(w, r, "newFileUploadPasteSuccess", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPasteHandlerURLEncoded(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
next(w, r)
|
2019-09-21 13:11:38 +02:00
|
|
|
return
|
|
|
|
}
|
2019-11-10 19:03:57 +01:00
|
|
|
shorten := r.PostFormValue("shorten")
|
|
|
|
if shorten == "" {
|
|
|
|
renderError(w, r, http.StatusBadRequest, "no 'shorten' param given\n")
|
2019-09-21 13:11:38 +02:00
|
|
|
return
|
|
|
|
}
|
2019-11-10 19:03:57 +01:00
|
|
|
newRedirectPasteHandler(w, r, shorten)
|
2019-09-21 13:11:38 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:41:54 +01:00
|
|
|
func newRedirectPasteHandler(w http.ResponseWriter, r *http.Request, rawurl string) {
|
2019-08-25 21:33:56 +02:00
|
|
|
userURL, err := url.ParseRequestURI(rawurl)
|
|
|
|
if err != nil {
|
2019-09-15 21:34:41 +02:00
|
|
|
msg := fmt.Sprintf("invalid url (%v): %v", err, rawurl)
|
2019-09-19 21:42:01 +02:00
|
|
|
renderError(w, r, http.StatusBadRequest, msg)
|
2019-08-25 21:33:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if userURL.Scheme == "" {
|
2019-11-10 19:03:57 +01:00
|
|
|
renderError(w, r, http.StatusBadRequest, "invalid url (unspecified scheme)\n")
|
2019-08-25 21:33:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if userURL.Host == "" {
|
2019-11-10 19:03:57 +01:00
|
|
|
renderError(w, r, http.StatusBadRequest, "invalid url (unspecified host)\n")
|
2019-08-25 21:33:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-22 14:03:27 +02:00
|
|
|
var paste *paste
|
2019-11-09 15:50:12 +01:00
|
|
|
if err := DB.Update(func(tx *bolt.Tx) error {
|
2019-09-21 21:03:31 +02:00
|
|
|
var err error
|
2019-09-22 14:03:27 +02:00
|
|
|
paste, err = shortenURL(tx, userURL)
|
2019-08-25 21:33:56 +02:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
2019-10-15 23:19:45 +02:00
|
|
|
panic(err)
|
2019-08-25 21:33:56 +02:00
|
|
|
}
|
2019-09-22 14:03:27 +02:00
|
|
|
data := map[string]interface{}{"Paste": paste}
|
2019-09-21 21:03:31 +02:00
|
|
|
render(w, r, "newRedirectPasteSuccess", data)
|
2019-09-21 13:11:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a URL from the database
|
|
|
|
func deletePasteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
key := vars["key"]
|
|
|
|
|
2019-09-21 21:03:31 +02:00
|
|
|
deleteToken := getDeleteTokenFromRequest(r)
|
|
|
|
if deleteToken == "" {
|
2019-11-10 19:03:57 +01:00
|
|
|
renderError(w, r, http.StatusBadRequest, "no delete token provided\n")
|
2019-09-21 13:11:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var errorCode int
|
2019-11-22 18:41:54 +01:00
|
|
|
var paste paste
|
2019-11-09 15:50:12 +01:00
|
|
|
if err := DB.Update(func(tx *bolt.Tx) error {
|
2019-09-22 14:03:27 +02:00
|
|
|
p, err := getPaste(tx, key)
|
2019-09-21 13:11:38 +02:00
|
|
|
if err != nil {
|
|
|
|
errorCode = http.StatusNotFound
|
|
|
|
return err
|
|
|
|
}
|
2019-11-22 18:41:54 +01:00
|
|
|
if p.State == pasteStateDeleted {
|
|
|
|
errorCode = http.StatusGone
|
|
|
|
return errors.New("already deleted")
|
2019-09-21 13:11:38 +02:00
|
|
|
}
|
2019-11-22 18:41:54 +01:00
|
|
|
if subtle.ConstantTimeCompare([]byte(deleteToken), []byte(p.DeleteToken)) == 0 {
|
|
|
|
errorCode = http.StatusForbidden
|
|
|
|
return errors.New("invalid delete token")
|
|
|
|
}
|
|
|
|
if err := p.delete(tx); err != nil {
|
|
|
|
errorCode = http.StatusInternalServerError
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paste = *p
|
|
|
|
return nil
|
2019-09-21 13:11:38 +02:00
|
|
|
}); err != nil {
|
|
|
|
log.Printf("error: %v\n", err)
|
2019-11-10 19:03:57 +01:00
|
|
|
renderError(w, r, errorCode, fmt.Sprintf("error: %v\n", err))
|
2019-09-21 13:11:38 +02:00
|
|
|
return
|
|
|
|
}
|
2019-11-22 18:41:54 +01:00
|
|
|
|
|
|
|
data := map[string]interface{}{"Paste": paste}
|
|
|
|
render(w, r, "deletePasteSuccess", data)
|
2019-08-29 00:50:26 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:03:57 +01:00
|
|
|
// Add a new fileUpload redirect to the database
|
|
|
|
//
|
|
|
|
// Returns the new paste key if the fileUpload was successfully added to the
|
|
|
|
// database
|
|
|
|
func shortenFileUploadID(tx *bolt.Tx, id uuid.UUID) (*paste, error) {
|
|
|
|
return shorten(tx, pasteTypeFileUpload, id[:])
|
|
|
|
}
|
|
|
|
|
2019-08-25 21:33:56 +02:00
|
|
|
// Add a new URL to the database
|
|
|
|
//
|
2019-11-10 19:03:57 +01:00
|
|
|
// Returns the new paste key if the url was successfully shortened
|
2019-09-22 14:03:27 +02:00
|
|
|
func shortenURL(tx *bolt.Tx, userURL *url.URL) (*paste, error) {
|
2019-11-10 19:03:57 +01:00
|
|
|
return shorten(tx, pasteTypeRedirect, []byte(userURL.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a paste (of any kind) to the database with arbitrary content.
|
|
|
|
func shorten(tx *bolt.Tx, ty pasteType, content []byte) (*paste, error) {
|
|
|
|
// Generate the paste key
|
2019-09-22 14:03:27 +02:00
|
|
|
pasteKey, err := generatePasteKey(tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "generating paste key")
|
2019-08-25 21:33:56 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 21:03:31 +02:00
|
|
|
// Also generate a deleteToken
|
|
|
|
deleteToken, err := generateDeleteToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "generating delete token")
|
|
|
|
}
|
|
|
|
|
2019-08-25 21:33:56 +02:00
|
|
|
// Store the new key
|
2019-09-22 14:03:27 +02:00
|
|
|
p := paste{
|
2019-11-10 19:03:57 +01:00
|
|
|
Type: ty,
|
|
|
|
State: pasteStatePresent,
|
|
|
|
Content: content,
|
2019-09-22 14:03:27 +02:00
|
|
|
Key: pasteKey,
|
2019-09-21 13:11:38 +02:00
|
|
|
DeleteToken: deleteToken,
|
2019-08-25 21:33:56 +02:00
|
|
|
TimeCreated: time.Now().UTC(),
|
|
|
|
}
|
2019-09-22 14:03:27 +02:00
|
|
|
if err := p.save(tx); err != nil {
|
2019-09-21 13:11:38 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-22 14:03:27 +02:00
|
|
|
return &p, nil
|
2019-09-01 01:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 21:03:31 +02:00
|
|
|
func getDeleteTokenFromRequest(r *http.Request) string {
|
|
|
|
return r.URL.Query().Get("deleteToken")
|
2019-09-01 01:41:01 +02:00
|
|
|
}
|