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-11 01:22:19 +00:00
|
|
|
"io"
|
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-19 07:37:53 +00:00
|
|
|
ADDRESS = "http://localhost:9900"
|
|
|
|
LENGTH = 6
|
2016-06-22 23:58:28 +00:00
|
|
|
DELETE = ADDRESS + "/del/{PASTE}/{DELKEY}\n"
|
2016-06-19 07:37:53 +00:00
|
|
|
PASTEARGS = ADDRESS + "/p/{PASTE}/(python|language)\n"
|
2016-06-19 07:50:10 +00:00
|
|
|
URLARGS = ADDRESS + "/save/XML|JSON\n"
|
2016-06-19 07:37:53 +00:00
|
|
|
SOURCE = "Source: https://github.com/ewhal/Pastebin\n"
|
2016-06-20 12:30:21 +00:00
|
|
|
TEXT = "$ <command> | curl -F 'p=<-' " + ADDRESS + "/save" + "\n" + PASTEARGS + URLARGS + DELETE + SOURCE
|
2016-06-19 07:37:53 +00:00
|
|
|
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"`
|
|
|
|
HASH string `json:"hash"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
SIZE int `json:"size"`
|
|
|
|
DELKEY string `json:"delkey"`
|
|
|
|
}
|
|
|
|
|
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-23 01:00:33 +00:00
|
|
|
func save(raw 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-19 06:45:52 +00:00
|
|
|
query, err := db.Query("select id, hash, data, delkey from pastebin")
|
2016-06-19 06:44:46 +00:00
|
|
|
for query.Next() {
|
|
|
|
var id, hash, paste, delkey string
|
2016-06-19 06:47:10 +00:00
|
|
|
err := query.Scan(&id, &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-19 06:50:00 +00:00
|
|
|
return []string{id, hash, url, paste, delkey}
|
2016-06-19 06:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-19 06:16:08 +00:00
|
|
|
id := generateName()
|
|
|
|
url := ADDRESS + "/p/" + id
|
|
|
|
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-19 04:36:00 +00:00
|
|
|
stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)")
|
2016-06-19 06:16:08 +00:00
|
|
|
check(err)
|
|
|
|
_, err = stmt.Exec(id, sha, paste, delKey)
|
2016-06-19 01:48:57 +00:00
|
|
|
check(err)
|
|
|
|
db.Close()
|
2016-06-19 06:16:08 +00:00
|
|
|
return []string{id, 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)
|
|
|
|
|
|
|
|
stmt, err := db.Prepare("delete from pastebin where delkey=? id=?")
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
res, err := stmt.Exec(html.EscapeString(delkey), html.EscapeString(paste))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
affect, err := res.RowsAffected()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
io.WriteString(w, string(affect))
|
|
|
|
|
|
|
|
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")
|
|
|
|
if paste == "" {
|
|
|
|
http.Error(w, "Empty paste", 500)
|
2016-06-11 03:33:29 +00:00
|
|
|
}
|
2016-06-19 06:16:08 +00:00
|
|
|
|
2016-06-23 01:00:33 +00:00
|
|
|
values := save(paste)
|
2016-06-19 06:17:28 +00:00
|
|
|
b := &Response{
|
|
|
|
ID: values[0],
|
|
|
|
HASH: values[1],
|
|
|
|
URL: values[2],
|
|
|
|
SIZE: len(values[3]),
|
|
|
|
DELKEY: values[4],
|
|
|
|
}
|
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-19 06:16:08 +00:00
|
|
|
|
|
|
|
default:
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func langHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
paste := vars["pasteId"]
|
|
|
|
lang := vars["lang"]
|
|
|
|
s := getPaste(paste)
|
2016-06-22 23:55:08 +00:00
|
|
|
highlight, err := pygments.Highlight(html.UnescapeString(s), html.EscapeString(lang), "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-06-19 04:36:00 +00:00
|
|
|
io.WriteString(w, highlight)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPaste(paste string) string {
|
|
|
|
param1 := html.EscapeString(paste)
|
2016-06-19 06:31:36 +00:00
|
|
|
db, err := sql.Open("mysql", DATABASE)
|
2016-06-19 04:36:00 +00:00
|
|
|
var s string
|
|
|
|
err = db.QueryRow("select data from pastebin where id=?", param1).Scan(&s)
|
|
|
|
db.Close()
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "Error invalid paste"
|
|
|
|
} else {
|
|
|
|
return html.UnescapeString(s)
|
2016-06-10 14:29:22 +00:00
|
|
|
}
|
2016-06-19 04:36:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
paste := vars["pasteId"]
|
|
|
|
s := getPaste(paste)
|
|
|
|
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-22 23:58:28 +00:00
|
|
|
router.HandleFunc("/p/{pasteId}/{lang}", langHandler)
|
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:25:12 +00:00
|
|
|
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("static/"))))
|
2016-06-23 01:37:58 +00:00
|
|
|
router.PathPrefix("/css").Handler(http.StripPrefix("/", http.FileServer(http.Dir("static/css/"))))
|
|
|
|
router.PathPrefix("/js").Handler(http.StripPrefix("/", http.FileServer(http.Dir("static/js/"))))
|
|
|
|
router.PathPrefix("/fonts").Handler(http.StripPrefix("/", http.FileServer(http.Dir("static/fonts/"))))
|
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
|
|
|
|
|
|
|
}
|