nat/pastebin.go

399 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
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-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-06-11 01:22:19 +00:00
const (
2016-07-04 00:19:57 +00:00
// ADDRESS that pastebin will return links for
ADDRESS = "http://localhost:9900"
// LENGTH of paste id
LENGTH = 6
// PORT that pastebin will listen on
PORT = ":9900"
2016-07-04 02:29:59 +00:00
// USERNAME for database
2016-06-23 03:45:00 +00:00
USERNAME = ""
2016-07-04 00:19:57 +00:00
// PASS database password
PASS = ""
// NAME database name
NAME = ""
// DATABASE connection String
2016-06-23 03:45:00 +00:00
DATABASE = USERNAME + ":" + PASS + "@/" + NAME + "?charset=utf8"
2016-06-11 01:22:19 +00:00
)
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"`
HASH string `json:"hash"`
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-06-11 03:33:29 +00:00
func check(err error) {
if err != nil {
fmt.Println(err)
}
}
2016-07-04 00:19:57 +00:00
// generateName uses uniuri to generate a random string that isn't in the
// database
func generateName() string {
2016-07-04 02:52:23 +00:00
// use uniuri to generate random string
2016-06-26 11:16:14 +00:00
id := uniuri.NewLen(LENGTH)
2016-07-04 02:52:23 +00:00
db, err := sql.Open("mysql", DATABASE)
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() {
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
// hash hashes paste into a sha1 hash
2016-06-23 01:00:33 +00:00
func hash(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
// durationFromExpiry takes the expiry in string format and returns the duration
// that the paste will exist for
2016-07-03 23:59:31 +00:00
func durationFromExpiry(expiry string) time.Duration {
switch expiry {
case "5 minutes":
return time.Minute * 5
case "1 hour":
return time.Hour + 1 // XXX: did you mean '*'?
case "1 day":
return time.Hour * 24
case "1 week":
return time.Hour * 24 * 7
case "1 month":
return time.Hour * 24 * 30
case "1 year":
return time.Hour * 24 * 365
case "forever":
return time.Hour * 24 * (365 * 20)
}
return time.Hour * 24 * (365 * 20)
}
2016-06-19 06:16:08 +00:00
2016-07-04 00:19:57 +00:00
// save function handles the saving of each paste.
// 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-04 00:02:06 +00:00
func save(raw string, lang string, title string, expiry string) Response {
db, err := sql.Open("mysql", DATABASE)
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-06-23 01:00:33 +00:00
sha := hash(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)
check(err)
2016-06-19 06:50:56 +00:00
url := 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-06-19 06:16:08 +00:00
id := generateName()
2016-07-04 00:04:37 +00:00
url := ADDRESS + "/p/" + id
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"
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(?,?,?,?,?,?)")
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)
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-04 00:19:57 +00:00
// delHandler checks to see if delkey and pasteid exist in the database.
// if both exist and are correct the paste will be removed.
2016-06-19 06:16:08 +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)
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-06-19 06:59:19 +00:00
check(err)
2016-06-27 22:10:20 +00:00
res, err := stmt.Exec(html.EscapeString(delkey), html.EscapeString(id))
2016-06-19 06:59:19 +00:00
check(err)
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
// saveHandler
2016-06-19 04:36:00 +00:00
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-04 00:02:06 +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-06-23 03:55:09 +00:00
io.WriteString(w, "<p><b>Delete Key</b>: <a href='"+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-04 00:19:57 +00:00
// highlight uses user specified input to call pygments library to highlight the
// paste
2016-06-23 09:21:37 +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-04 02:52:23 +00:00
// getPaste takes pasteid and language
// queries the database and returns paste data
2016-06-24 05:32:20 +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-04 00:03:22 +00:00
check(err)
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-06-19 04:36:00 +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=?")
check(err)
_, err = stmt.Exec(param1)
check(err)
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 != "" {
high, err := highlight(s, lang)
check(err)
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-06-19 04:36:00 +00:00
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-06-24 05:32:20 +00:00
s, title := getPaste(paste, lang)
// button links
2016-06-23 09:11:40 +00:00
link := ADDRESS + "/raw/" + paste
2016-06-24 02:17:23 +00:00
download := ADDRESS + "/download/" + paste
2016-06-24 02:59:29 +00:00
clone := ADDRESS + "/clone/" + paste
// Page struct
2016-06-25 20:45:54 +00:00
p := &Page{
Title: title,
Body: []byte(s),
Raw: link,
Home: ADDRESS,
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-06-24 01:04:26 +00:00
func cloneHandler(w http.ResponseWriter, r *http.Request) {
2016-06-24 02:59:29 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-06-24 05:32:20 +00:00
s, title := getPaste(paste, "")
// Page links
2016-06-24 02:59:29 +00:00
link := ADDRESS + "/raw/" + paste
download := ADDRESS + "/download/" + paste
clone := 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,
Home: ADDRESS,
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
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
2016-06-24 01:46:50 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-06-24 05:32:20 +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-06-23 08:40:20 +00:00
func rawHandler(w http.ResponseWriter, r *http.Request) {
2016-06-19 04:36:00 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
2016-06-24 05:32:20 +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)
}
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-06-19 04:36:00 +00:00
router.HandleFunc("/p/{pasteId}", pasteHandler)
2016-06-23 08:40:20 +00:00
router.HandleFunc("/raw/{pasteId}", rawHandler)
2016-06-23 09:21:37 +00:00
router.HandleFunc("/p/{pasteId}/{lang}", pasteHandler)
2016-06-24 01:46:50 +00:00
router.HandleFunc("/clone/{pasteId}", cloneHandler)
router.HandleFunc("/download/{pasteId}", downloadHandler)
2016-06-19 04:36:00 +00:00
router.HandleFunc("/save", saveHandler)
2016-06-19 06:16:08 +00:00
router.HandleFunc("/save/{output}", saveHandler)
router.HandleFunc("/del/{pasteId}/{delKey}", delHandler)
router.HandleFunc("/", rootHandler)
2016-06-19 04:36:00 +00:00
err := http.ListenAndServe(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
}
}