2016-06-10 13:56:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-06-19 06:16:08 +00:00
|
|
|
"crypto/sha1"
|
2016-06-19 01:48:57 +00:00
|
|
|
"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"
|
2016-06-19 01:48:57 +00:00
|
|
|
"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"
|
2016-06-10 13:56:42 +00:00
|
|
|
"net/http"
|
2016-06-19 00:17:35 +00:00
|
|
|
|
|
|
|
"github.com/dchest/uniuri"
|
|
|
|
"github.com/ewhal/pygments"
|
2016-06-19 06:27:54 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2016-06-19 06:16:08 +00:00
|
|
|
"github.com/gorilla/mux"
|
2016-06-10 13:56:42 +00:00
|
|
|
)
|
|
|
|
|
2016-06-11 01:22:19 +00:00
|
|
|
const (
|
2016-06-23 03:45:00 +00:00
|
|
|
ADDRESS = "http://localhost:9900"
|
|
|
|
LENGTH = 6
|
|
|
|
PORT = ":9900"
|
|
|
|
USERNAME = ""
|
|
|
|
PASS = ""
|
|
|
|
NAME = ""
|
|
|
|
DATABASE = USERNAME + ":" + PASS + "@/" + NAME + "?charset=utf8"
|
2016-06-11 01:22:19 +00:00
|
|
|
)
|
2016-06-10 14:29:22 +00:00
|
|
|
|
2016-06-19 06:16:08 +00:00
|
|
|
type Response struct {
|
|
|
|
ID string `json:"id"`
|
2016-06-24 05:24:15 +00:00
|
|
|
TITLE string `json:"title"`
|
2016-06-19 06:16:08 +00:00
|
|
|
HASH string `json:"hash"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
SIZE int `json:"size"`
|
|
|
|
DELKEY string `json:"delkey"`
|
|
|
|
}
|
|
|
|
|
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-06-11 03:33:29 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2016-06-10 13:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:27 +00:00
|
|
|
func generateName() string {
|
2016-06-11 02:31:03 +00:00
|
|
|
s := uniuri.NewLen(LENGTH)
|
2016-06-19 06:27:54 +00:00
|
|
|
db, err := sql.Open("mysql", DATABASE)
|
2016-06-19 01:48:57 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
query, err := db.Query("select id from pastebin")
|
|
|
|
for query.Next() {
|
|
|
|
var id string
|
|
|
|
err := query.Scan(&id)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
if id == s {
|
|
|
|
generateName()
|
|
|
|
}
|
2016-06-10 14:29:22 +00:00
|
|
|
}
|
2016-06-19 01:48:57 +00:00
|
|
|
db.Close()
|
2016-06-10 14:29:22 +00:00
|
|
|
|
2016-06-10 14:17:27 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
}
|
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-06-24 05:24:15 +00:00
|
|
|
func save(raw string, lang string, title string) []string {
|
2016-06-19 06:31:36 +00:00
|
|
|
db, err := sql.Open("mysql", DATABASE)
|
2016-06-10 14:17:27 +00:00
|
|
|
check(err)
|
2016-06-19 06:16:08 +00:00
|
|
|
|
2016-06-23 01:00:33 +00:00
|
|
|
sha := hash(raw)
|
2016-06-24 07:12:32 +00:00
|
|
|
query, err := db.Query("select id, title, hash, data, delkey from pastebin")
|
2016-06-19 06:44:46 +00:00
|
|
|
for query.Next() {
|
2016-06-24 07:11:49 +00:00
|
|
|
var id, title, hash, paste, delkey string
|
|
|
|
err := query.Scan(&id, &title, &hash, &paste, &delkey)
|
2016-06-19 06:44:46 +00:00
|
|
|
check(err)
|
|
|
|
if hash == sha {
|
2016-06-19 06:50:56 +00:00
|
|
|
url := ADDRESS + "/p/" + id
|
2016-06-24 07:11:49 +00:00
|
|
|
if title == "" {
|
|
|
|
return []string{id, title, hash, url, paste, delkey}
|
|
|
|
}
|
2016-06-19 06:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-19 06:16:08 +00:00
|
|
|
id := generateName()
|
2016-06-23 03:22:57 +00:00
|
|
|
var url string
|
2016-06-23 03:19:50 +00:00
|
|
|
if lang == "" {
|
2016-06-23 03:22:57 +00:00
|
|
|
url = ADDRESS + "/p/" + id
|
2016-06-23 03:19:50 +00:00
|
|
|
} else {
|
2016-06-23 03:22:57 +00:00
|
|
|
url = ADDRESS + "/p/" + id + "/" + lang
|
2016-06-23 03:19:50 +00:00
|
|
|
}
|
2016-06-19 06:16:08 +00:00
|
|
|
delKey := uniuri.NewLen(40)
|
2016-06-23 01:00:33 +00:00
|
|
|
paste := html.EscapeString(raw)
|
2016-06-19 06:16:08 +00:00
|
|
|
|
2016-06-24 05:24:15 +00:00
|
|
|
stmt, err := db.Prepare("INSERT INTO pastebin(id, title, hash, data, delkey) values(?,?,?,?,?)")
|
2016-06-19 01:48:57 +00:00
|
|
|
check(err)
|
2016-06-24 05:24:15 +00:00
|
|
|
if title == "" {
|
|
|
|
_, err = stmt.Exec(id, id, sha, paste, delKey)
|
|
|
|
check(err)
|
|
|
|
} else {
|
2016-06-24 05:45:52 +00:00
|
|
|
_, err = stmt.Exec(id, html.EscapeString(title), sha, paste, delKey)
|
2016-06-24 05:24:15 +00:00
|
|
|
check(err)
|
|
|
|
}
|
2016-06-19 01:48:57 +00:00
|
|
|
db.Close()
|
2016-06-24 05:24:15 +00:00
|
|
|
return []string{id, title, sha, url, paste, delKey}
|
2016-06-10 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
paste := vars["pasteId"]
|
|
|
|
delkey := vars["delKey"]
|
|
|
|
|
|
|
|
db, err := sql.Open("mysql", DATABASE)
|
|
|
|
check(err)
|
|
|
|
|
2016-06-23 03:58:15 +00:00
|
|
|
stmt, err := db.Prepare("delete from pastebin where delkey=?")
|
2016-06-19 06:59:19 +00:00
|
|
|
check(err)
|
|
|
|
|
2016-06-23 03:58:15 +00:00
|
|
|
res, err := stmt.Exec(html.EscapeString(delkey))
|
2016-06-19 06:59:19 +00:00
|
|
|
check(err)
|
|
|
|
|
2016-06-23 04:01:29 +00:00
|
|
|
_, err = res.RowsAffected()
|
2016-06-23 04:00:27 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
io.WriteString(w, "Error invalid paste")
|
|
|
|
} else {
|
|
|
|
io.WriteString(w, paste+" deleted")
|
|
|
|
}
|
2016-06-19 06:59:19 +00:00
|
|
|
db.Close()
|
|
|
|
|
2016-06-19 06:16:08 +00:00
|
|
|
}
|
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-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-06-24 05:24:15 +00:00
|
|
|
values := save(paste, lang, title)
|
2016-06-19 06:17:28 +00:00
|
|
|
b := &Response{
|
|
|
|
ID: values[0],
|
2016-06-24 05:24:15 +00:00
|
|
|
TITLE: values[1],
|
|
|
|
HASH: values[2],
|
|
|
|
URL: values[3],
|
|
|
|
SIZE: len(values[4]),
|
|
|
|
DELKEY: values[5],
|
2016-06-19 06:17:28 +00:00
|
|
|
}
|
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
|
|
|
|
2016-06-23 09:06:24 +00:00
|
|
|
case "redirect":
|
|
|
|
http.Redirect(w, r, b.URL, 301)
|
|
|
|
|
2016-06-19 06:16:08 +00:00
|
|
|
default:
|
2016-06-23 08:57:00 +00:00
|
|
|
w.Header().Set("Content-Type", "plain/text")
|
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-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-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)
|
2016-06-19 06:31:36 +00:00
|
|
|
db, err := sql.Open("mysql", DATABASE)
|
2016-06-24 05:32:20 +00:00
|
|
|
var title, s string
|
|
|
|
err = db.QueryRow("select title, data from pastebin where id=?", param1).Scan(&title, &s)
|
2016-06-19 04:36:00 +00:00
|
|
|
db.Close()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
2016-06-24 05:33:14 +00:00
|
|
|
return "Error invalid paste", ""
|
2016-06-19 04:36:00 +00:00
|
|
|
} else {
|
2016-06-23 10:41:45 +00:00
|
|
|
if lang == "" {
|
2016-06-24 05:45:52 +00:00
|
|
|
return html.UnescapeString(s), html.UnescapeString(title)
|
2016-06-23 10:41:45 +00:00
|
|
|
} else {
|
|
|
|
high, err := highlight(s, lang)
|
|
|
|
check(err)
|
2016-06-24 05:45:52 +00:00
|
|
|
return high, html.UnescapeString(title)
|
2016-06-23 10:41:45 +00:00
|
|
|
|
|
|
|
}
|
2016-06-10 14:29:22 +00:00
|
|
|
}
|
2016-06-19 04:36:00 +00:00
|
|
|
|
|
|
|
}
|
2016-06-23 08:57:00 +00:00
|
|
|
|
2016-06-24 02:59:29 +00:00
|
|
|
var templates = template.Must(template.ParseFiles("assets/paste.html", "assets/index.html", "assets/clone.html"))
|
2016-06-24 01:16:58 +00:00
|
|
|
var syntax, _ = ioutil.ReadFile("assets/syntax.html")
|
|
|
|
|
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)
|
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
|
2016-06-23 10:41:45 +00:00
|
|
|
if lang == "" {
|
|
|
|
p := &Page{
|
2016-06-24 05:32:20 +00:00
|
|
|
Title: title,
|
2016-06-24 02:17:23 +00:00
|
|
|
Body: []byte(s),
|
|
|
|
Raw: link,
|
|
|
|
Home: ADDRESS,
|
|
|
|
Download: download,
|
2016-06-24 02:59:29 +00:00
|
|
|
Clone: clone,
|
2016-06-23 10:41:45 +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-24 02:59:29 +00:00
|
|
|
fmt.Fprintf(w, string(syntax), paste, paste, s, ADDRESS, download, link, 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, "")
|
2016-06-24 02:59:29 +00:00
|
|
|
link := ADDRESS + "/raw/" + paste
|
|
|
|
download := ADDRESS + "/download/" + paste
|
|
|
|
clone := ADDRESS + "/clone/" + paste
|
|
|
|
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-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-06-19 04:36:00 +00:00
|
|
|
io.WriteString(w, s)
|
|
|
|
|
2016-06-10 13:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-06-23 01:46:50 +00:00
|
|
|
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("assets/"))))
|
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
|
|
|
}
|
2016-06-10 13:56:42 +00:00
|
|
|
|
|
|
|
}
|