nat/pastebin.go

402 lines
10 KiB
Go
Raw Normal View History

2016-07-04 02:31:01 +00:00
// Package pastebin is a simple modern and powerful pastebin service
2016-07-17 01:36:55 +00:00
package main
import (
2016-06-19 06:16:08 +00:00
"crypto/sha1"
"database/sql"
2016-06-19 06:16:08 +00:00
"encoding/base64"
"encoding/json"
2016-06-19 06:17:28 +00:00
"encoding/xml"
2016-06-11 03:33:29 +00:00
"fmt"
"html"
2016-06-23 08:57:00 +00:00
"html/template"
2016-06-11 01:22:19 +00:00
"io"
2016-06-23 10:41:45 +00:00
"io/ioutil"
2016-06-19 04:36:00 +00:00
"log"
"net/http"
2016-06-24 22:13:36 +00:00
"time"
2016-06-19 00:17:35 +00:00
2016-07-15 03:06:25 +00:00
duration "github.com/channelmeter/iso8601duration"
2016-07-04 00:19:57 +00:00
// uniuri is used for easy random string generation
2016-06-19 00:17:35 +00:00
"github.com/dchest/uniuri"
2016-07-04 00:19:57 +00:00
// pygments is used for syntax highlighting
2016-06-19 00:17:35 +00:00
"github.com/ewhal/pygments"
2016-07-04 00:19:57 +00:00
// mysql driver
2016-06-19 06:27:54 +00:00
_ "github.com/go-sql-driver/mysql"
2016-07-04 00:19:57 +00:00
// mux is used for url routing
2016-06-19 06:16:08 +00:00
"github.com/gorilla/mux"
)
2016-07-19 21:13:34 +00:00
type Configuration struct {
2016-07-04 00:19:57 +00:00
// ADDRESS that pastebin will return links for
2016-07-19 21:13:34 +00:00
Address string
2016-07-04 00:19:57 +00:00
// LENGTH of paste id
2016-07-19 21:13:34 +00:00
Length int
2016-07-04 00:19:57 +00:00
// PORT that pastebin will listen on
2016-07-19 21:13:34 +00:00
Port string
2016-07-04 02:29:59 +00:00
// USERNAME for database
2016-07-19 21:13:34 +00:00
Username string
2016-07-04 00:19:57 +00:00
// PASS database password
2016-07-19 21:13:34 +00:00
Pass string
2016-07-04 00:19:57 +00:00
// NAME database name
2016-07-19 21:13:34 +00:00
Name string
}
var configuration Configuration
// DATABASE connection String
var DATABASE = configuration.Username + ":" + configuration.Pass + "@/" + configuration.Name + "?charset=utf8"
2016-06-10 14:29:22 +00:00
2016-07-04 02:52:23 +00:00
// Template pages
var templates = template.Must(template.ParseFiles("assets/paste.html", "assets/index.html", "assets/clone.html"))
var syntax, _ = ioutil.ReadFile("assets/syntax.html")
2016-07-04 00:19:57 +00:00
// Response API struct
2016-06-19 06:16:08 +00:00
type Response struct {
2016-06-24 22:16:03 +00:00
ID string `json:"id"`
TITLE string `json:"title"`
2016-07-15 23:42:24 +00:00
SHA1 string `json:"sha1"`
2016-06-24 22:16:03 +00:00
URL string `json:"url"`
SIZE int `json:"size"`
DELKEY string `json:"delkey"`
2016-06-19 06:16:08 +00:00
}
2016-07-04 00:19:57 +00:00
// Page generation struct
2016-06-23 08:57:00 +00:00
type Page struct {
2016-06-24 02:17:23 +00:00
Title string
Body []byte
Raw string
Home string
Download string
2016-06-24 02:59:29 +00:00
Clone string
2016-06-23 08:57:00 +00:00
}
2016-07-04 00:19:57 +00:00
// check error handling function
2016-07-14 05:46:29 +00:00
func Check(err error) {
2016-06-11 03:33:29 +00:00
if err != nil {
2016-07-10 02:24:09 +00:00
log.Println(err)
}
}
2016-07-14 05:46:29 +00:00
// GenerateName uses uniuri to generate a random string that isn't in the
2016-07-04 00:19:57 +00:00
// database
2016-07-14 05:46:29 +00:00
func GenerateName() string {
2016-07-04 02:52:23 +00:00
// use uniuri to generate random string
2016-07-19 21:13:34 +00:00
id := uniuri.NewLen(configuration.Length)
2016-07-04 02:52:23 +00:00
db, err := sql.Open("mysql", DATABASE)
2016-07-14 05:46:29 +00:00
Check(err)
2016-07-04 00:03:22 +00:00
defer db.Close()
2016-07-04 02:52:23 +00:00
// query database if id exists and if it does call generateName again
2016-06-26 11:16:14 +00:00
query, err := db.Query("select id from pastebin where id=?", id)
if err != sql.ErrNoRows {
for query.Next() {
2016-07-14 05:46:29 +00:00
GenerateName()
}
2016-06-10 14:29:22 +00:00
}
2016-06-26 11:16:14 +00:00
return id
}
2016-07-04 00:19:57 +00:00
2016-07-15 00:19:45 +00:00
// Sha1 hashes paste into a sha1 hash
func Sha1(paste string) string {
2016-06-19 06:16:08 +00:00
hasher := sha1.New()
2016-06-23 01:00:33 +00:00
hasher.Write([]byte(paste))
2016-06-19 06:16:08 +00:00
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
return sha
}
2016-07-04 00:19:57 +00:00
2016-07-15 04:42:14 +00:00
// DurationFromExpiry takes the expiry in string format and returns the duration
// that the paste will exist for
func DurationFromExpiry(expiry string) time.Duration {
2016-07-15 04:42:51 +00:00
if expiry == "" {
expiry = "P20Y"
}
2016-07-15 04:42:14 +00:00
dura, err := duration.FromString(expiry) // dura is time.Duration type
Check(err)
duration := dura.ToDuration()
return duration
}
2016-07-14 05:46:29 +00:00
// Save function handles the saving of each paste.
2016-07-04 00:19:57 +00:00
// raw string is the raw paste input
// lang string is the user specified language for syntax highlighting
// title string user customized title
// expiry string duration that the paste will exist for
// Returns Response struct
2016-07-14 05:46:29 +00:00
func Save(raw string, lang string, title string, expiry string) Response {
2016-07-04 00:02:06 +00:00
db, err := sql.Open("mysql", DATABASE)
2016-07-14 05:46:29 +00:00
Check(err)
2016-07-04 00:03:22 +00:00
defer db.Close()
2016-06-19 06:16:08 +00:00
2016-07-04 02:52:23 +00:00
// hash paste data and query database to see if paste exists
2016-07-15 00:19:45 +00:00
sha := Sha1(raw)
2016-06-26 11:14:12 +00:00
query, err := db.Query("select id, title, hash, data, delkey from pastebin where hash=?", sha)
2016-07-04 02:52:23 +00:00
2016-06-26 11:14:12 +00:00
if err != sql.ErrNoRows {
for query.Next() {
var id, title, hash, paste, delkey string
err := query.Scan(&id, &title, &hash, &paste, &delkey)
2016-07-14 05:46:29 +00:00
Check(err)
2016-07-19 21:13:34 +00:00
url := configuration.Address + "/p/" + id
2016-07-04 00:04:37 +00:00
return Response{id, title, hash, url, len(paste), delkey}
2016-06-19 06:44:46 +00:00
}
}
2016-07-14 05:46:29 +00:00
id := GenerateName()
2016-07-19 21:13:34 +00:00
url := configuration.Address + "/p/" + id
2016-07-04 00:04:37 +00:00
if lang != "" {
url += "/" + lang
2016-06-23 03:19:50 +00:00
}
2016-07-04 00:04:37 +00:00
2016-07-03 23:59:31 +00:00
const timeFormat = "2006-01-02 15:04:05"
2016-07-15 04:42:14 +00:00
expiryTime := time.Now().Add(DurationFromExpiry(expiry)).Format(timeFormat)
2016-06-24 22:13:36 +00:00
2016-06-19 06:16:08 +00:00
delKey := uniuri.NewLen(40)
2016-06-27 22:10:20 +00:00
dataEscaped := html.EscapeString(raw)
2016-06-19 06:16:08 +00:00
2016-06-24 22:13:36 +00:00
stmt, err := db.Prepare("INSERT INTO pastebin(id, title, hash, data, delkey, expiry) values(?,?,?,?,?,?)")
2016-07-14 05:46:29 +00:00
Check(err)
2016-06-24 05:24:15 +00:00
if title == "" {
2016-07-04 00:38:12 +00:00
title = id
2016-06-24 05:24:15 +00:00
}
2016-07-04 00:38:12 +00:00
_, err = stmt.Exec(id, html.EscapeString(title), sha, dataEscaped, delKey, expiryTime)
2016-07-14 05:46:29 +00:00
Check(err)
2016-07-04 02:52:23 +00:00
2016-07-04 00:02:06 +00:00
return Response{id, title, sha, url, len(dataEscaped), delKey}
}
2016-07-14 05:46:29 +00:00
// DelHandler checks to see if delkey and pasteid exist in the database.
2016-07-04 00:19:57 +00:00
// if both exist and are correct the paste will be removed.
2016-07-14 05:46:29 +00:00
func DelHandler(w http.ResponseWriter, r *http.Request) {
2016-06-19 06:59:19 +00:00
vars := mux.Vars(r)
2016-06-27 22:10:20 +00:00
id := vars["pasteId"]
2016-06-19 06:59:19 +00:00
delkey := vars["delKey"]
db, err := sql.Open("mysql", DATABASE)
2016-07-14 05:46:29 +00:00
Check(err)
2016-07-04 00:03:22 +00:00
defer db.Close()
stmt, err := db.Prepare("delete from pastebin where delkey=? and id=?")
2016-07-14 05:46:29 +00:00
Check(err)
2016-06-19 06:59:19 +00:00
2016-06-27 22:10:20 +00:00
res, err := stmt.Exec(html.EscapeString(delkey), html.EscapeString(id))
2016-07-14 05:46:29 +00:00
Check(err)
2016-06-19 06:59:19 +00:00
2016-06-23 04:01:29 +00:00
_, err = res.RowsAffected()
2016-07-04 02:29:59 +00:00
if err != sql.ErrNoRows {
2016-06-27 22:10:20 +00:00
io.WriteString(w, id+" deleted")
2016-06-23 04:00:27 +00:00
}
2016-06-19 06:16:08 +00:00
}
2016-07-04 00:19:57 +00:00
2016-07-14 05:46:29 +00:00
// SaveHandler Handles saving pastes and outputing responses
func SaveHandler(w http.ResponseWriter, r *http.Request) {
2016-06-19 06:16:08 +00:00
vars := mux.Vars(r)
output := vars["output"]
2016-06-10 14:29:22 +00:00
switch r.Method {
case "POST":
2016-06-23 01:00:33 +00:00
paste := r.FormValue("p")
2016-06-23 03:19:50 +00:00
lang := r.FormValue("lang")
2016-06-24 05:24:15 +00:00
title := r.FormValue("title")
2016-06-24 22:13:36 +00:00
expiry := r.FormValue("expiry")
2016-06-23 01:00:33 +00:00
if paste == "" {
http.Error(w, "Empty paste", 500)
2016-06-23 04:57:07 +00:00
return
2016-06-11 03:33:29 +00:00
}
2016-07-14 05:46:29 +00:00
b := Save(paste, lang, title, expiry)
2016-06-19 06:16:08 +00:00
switch output {
case "json":
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2016-06-19 06:17:28 +00:00
case "xml":
x, err := xml.MarshalIndent(b, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml")
w.Write(x)
2016-06-23 08:57:00 +00:00
2016-06-23 03:45:00 +00:00
case "html":
w.Header().Set("Content-Type", "text/html")
2016-06-23 03:51:59 +00:00
io.WriteString(w, "<p><b>URL</b>: <a href='"+b.URL+"'>"+b.URL+"</a></p>")
2016-07-19 21:13:34 +00:00
io.WriteString(w, "<p><b>Delete Key</b>: <a href='"+configuration.Address+"/del/"+b.ID+"/"+b.DELKEY+"'>"+b.DELKEY+"</a></p>")
2016-06-19 06:16:08 +00:00
case "redirect":
http.Redirect(w, r, b.URL, 301)
2016-06-19 06:16:08 +00:00
default:
2016-07-09 04:44:48 +00:00
w.Header().Set("Content-Type", "text/plain; charset=UTF-8; imeanit=yes")
2016-06-19 06:27:54 +00:00
io.WriteString(w, b.URL+"\n")
io.WriteString(w, "delete key: "+b.DELKEY+"\n")
2016-06-19 06:16:08 +00:00
}
2016-06-19 04:36:00 +00:00
}
}
2016-07-14 05:46:29 +00:00
// Highlight uses user specified input to call pygments library to highlight the
2016-07-04 00:19:57 +00:00
// paste
2016-07-14 05:46:29 +00:00
func Highlight(s string, lang string) (string, error) {
2016-06-19 04:36:00 +00:00
2016-06-23 10:47:45 +00:00
highlight, err := pygments.Highlight(html.UnescapeString(s), html.EscapeString(lang), "html", "style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,noclasses=True,", "utf-8")
2016-06-22 23:55:08 +00:00
if err != nil {
2016-06-23 09:21:37 +00:00
return "", err
2016-06-22 23:55:08 +00:00
}
2016-06-23 09:21:37 +00:00
return highlight, nil
2016-06-19 04:36:00 +00:00
}
2016-07-14 05:46:29 +00:00
// GetPaste takes pasteid and language
2016-07-04 02:52:23 +00:00
// queries the database and returns paste data
2016-07-14 05:46:29 +00:00
func GetPaste(paste string, lang string) (string, string) {
2016-06-19 04:36:00 +00:00
param1 := html.EscapeString(paste)
db, err := sql.Open("mysql", DATABASE)
2016-07-14 05:46:29 +00:00
Check(err)
2016-07-04 00:03:22 +00:00
defer db.Close()
2016-06-24 22:33:56 +00:00
var title, s string
var expiry string
err = db.QueryRow("select title, data, expiry from pastebin where id=?", param1).Scan(&title, &s, &expiry)
2016-07-14 05:46:29 +00:00
Check(err)
2016-06-24 22:49:34 +00:00
if time.Now().Format("2006-01-02 15:04:05") > expiry {
2016-06-24 22:33:56 +00:00
stmt, err := db.Prepare("delete from pastebin where id=?")
2016-07-14 05:46:29 +00:00
Check(err)
2016-06-24 22:33:56 +00:00
_, err = stmt.Exec(param1)
2016-07-14 05:46:29 +00:00
Check(err)
2016-06-24 22:33:56 +00:00
return "Error invalid paste", ""
}
2016-06-19 04:36:00 +00:00
if err == sql.ErrNoRows {
2016-06-24 05:33:14 +00:00
return "Error invalid paste", ""
2016-06-10 14:29:22 +00:00
}
2016-07-04 02:29:59 +00:00
if lang != "" {
2016-07-14 05:46:29 +00:00
high, err := Highlight(s, lang)
Check(err)
2016-07-04 02:29:59 +00:00
return high, html.UnescapeString(title)
}
return html.UnescapeString(s), html.UnescapeString(title)
2016-06-19 04:36:00 +00:00
}
2016-06-23 08:57:00 +00:00
2016-07-14 05:46:29 +00:00
// PasteHandler handles the generation of paste pages with the links
func PasteHandler(w http.ResponseWriter, r *http.Request) {
2016-06-23 08:57:00 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-06-23 09:21:37 +00:00
lang := vars["lang"]
2016-07-14 05:46:29 +00:00
s, title := GetPaste(paste, lang)
// button links
2016-07-19 21:13:34 +00:00
link := configuration.Address + "/raw/" + paste
download := configuration.Address + "/download/" + paste
clone := configuration.Address + "/clone/" + paste
// Page struct
2016-06-25 20:45:54 +00:00
p := &Page{
Title: title,
Body: []byte(s),
Raw: link,
2016-07-19 21:13:34 +00:00
Home: configuration.Address,
2016-06-25 20:45:54 +00:00
Download: download,
Clone: clone,
}
2016-06-23 10:41:45 +00:00
if lang == "" {
2016-06-25 20:45:54 +00:00
2016-06-24 01:24:24 +00:00
err := templates.ExecuteTemplate(w, "paste.html", p)
2016-06-23 10:41:45 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
2016-06-25 20:45:54 +00:00
fmt.Fprintf(w, string(syntax), p.Title, p.Title, s, p.Home, p.Download, p.Raw, p.Clone)
2016-06-23 10:41:45 +00:00
2016-06-23 08:57:00 +00:00
}
2016-06-23 08:40:20 +00:00
}
2016-07-14 05:46:29 +00:00
// CloneHandler handles generating the clone pages
func CloneHandler(w http.ResponseWriter, r *http.Request) {
2016-06-24 02:59:29 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-07-14 05:46:29 +00:00
s, title := GetPaste(paste, "")
// Page links
2016-07-19 21:13:34 +00:00
link := configuration.Address + "/raw/" + paste
download := configuration.Address + "/download/" + paste
clone := configuration.Address + "/clone/" + paste
// Clone page struct
2016-06-24 02:59:29 +00:00
p := &Page{
2016-06-24 05:32:20 +00:00
Title: title,
2016-06-24 02:59:29 +00:00
Body: []byte(s),
Raw: link,
2016-07-19 21:13:34 +00:00
Home: configuration.Address,
2016-06-24 02:59:29 +00:00
Download: download,
Clone: clone,
}
err := templates.ExecuteTemplate(w, "clone.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2016-06-24 01:04:26 +00:00
}
2016-07-14 05:46:29 +00:00
// DownloadHandler forces downloads of selected pastes
func DownloadHandler(w http.ResponseWriter, r *http.Request) {
2016-06-24 01:46:50 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-07-14 05:46:29 +00:00
s, _ := GetPaste(paste, "")
2016-07-04 02:52:23 +00:00
// Set header to an attachment so browser will automatically download it
2016-06-24 02:10:54 +00:00
w.Header().Set("Content-Disposition", "attachment; filename="+paste)
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
io.WriteString(w, s)
2016-06-24 01:46:50 +00:00
2016-06-24 01:04:26 +00:00
}
2016-07-14 05:46:29 +00:00
// RawHandler displays the pastes in text/plain format
func RawHandler(w http.ResponseWriter, r *http.Request) {
2016-06-19 04:36:00 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-07-14 05:46:29 +00:00
s, _ := GetPaste(paste, "")
2016-07-09 04:40:48 +00:00
w.Header().Set("Content-Type", "text/plain; charset=UTF-8; imeanit=yes")
2016-07-04 02:52:23 +00:00
// simply write string to browser
2016-06-19 04:36:00 +00:00
io.WriteString(w, s)
}
2016-07-14 05:46:29 +00:00
// RootHandler handles generating the root page
func RootHandler(w http.ResponseWriter, r *http.Request) {
err := templates.ExecuteTemplate(w, "index.html", &Page{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
2016-06-23 01:25:12 +00:00
router := mux.NewRouter()
2016-07-14 05:46:29 +00:00
router.HandleFunc("/p/{pasteId}", PasteHandler).Methods("GET")
router.HandleFunc("/raw/{pasteId}", RawHandler).Methods("GET")
router.HandleFunc("/p/{pasteId}/{lang}", PasteHandler).Methods("GET")
router.HandleFunc("/clone/{pasteId}", CloneHandler).Methods("GET")
router.HandleFunc("/download/{pasteId}", DownloadHandler).Methods("GET")
router.HandleFunc("/p", SaveHandler).Methods("POST")
router.HandleFunc("/p/{output}", SaveHandler).Methods("POST")
router.HandleFunc("/p/{pasteId}/{delKey}", DelHandler).Methods("DELETE")
router.HandleFunc("/", RootHandler)
2016-07-19 21:13:34 +00:00
err := http.ListenAndServe(configuration.Port, router)
2016-06-11 03:33:29 +00:00
if err != nil {
2016-06-19 04:36:00 +00:00
log.Fatal(err)
2016-06-11 03:33:29 +00:00
}
}