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-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-10 13:56:42 +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:16:08 +00:00
|
|
|
"github.com/gorilla/mux"
|
2016-06-19 01:48:57 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2016-06-10 13:56:42 +00:00
|
|
|
)
|
|
|
|
|
2016-06-11 01:22:19 +00:00
|
|
|
const (
|
2016-06-19 04:36:00 +00:00
|
|
|
ADDRESS = "http://localhost:9900"
|
2016-06-19 01:48:57 +00:00
|
|
|
LENGTH = 6
|
|
|
|
TEXT = "$ <command> | curl -F 'p=<-' " + ADDRESS + "\n"
|
|
|
|
PORT = ":9900"
|
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 01:48:57 +00:00
|
|
|
db, err := sql.Open("sqlite3", "./database.db")
|
|
|
|
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-19 06:16:08 +00:00
|
|
|
func hash(paste []byte) string {
|
|
|
|
hasher := sha1.New()
|
|
|
|
hasher.Write(paste)
|
|
|
|
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
|
|
|
|
return sha
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(raw []byte) []string {
|
|
|
|
p := raw[86 : len(raw)-46]
|
2016-06-10 14:17:27 +00:00
|
|
|
|
2016-06-19 01:48:57 +00:00
|
|
|
db, err := sql.Open("sqlite3", "./database.db")
|
2016-06-10 14:17:27 +00:00
|
|
|
check(err)
|
2016-06-19 06:16:08 +00:00
|
|
|
|
|
|
|
sha := hash(p)
|
|
|
|
id := generateName()
|
|
|
|
url := ADDRESS + "/p/" + id
|
|
|
|
delKey := uniuri.NewLen(40)
|
|
|
|
paste := html.EscapeString(string(p))
|
|
|
|
|
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 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-11 02:22:22 +00:00
|
|
|
buf, err := ioutil.ReadAll(r.Body)
|
2016-06-11 03:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
2016-06-19 06:16:08 +00:00
|
|
|
|
|
|
|
values := save(buf)
|
|
|
|
|
|
|
|
switch output {
|
|
|
|
case "json":
|
|
|
|
b := &Response{
|
|
|
|
ID: values[0],
|
|
|
|
HASH: values[1],
|
|
|
|
URL: values[2],
|
|
|
|
SIZE: len(values[3]),
|
|
|
|
DELKEY: values[4],
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(b)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
io.WriteString(w, values[2]+"\n")
|
|
|
|
}
|
2016-06-19 04:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
io.WriteString(w, TEXT)
|
|
|
|
}
|
|
|
|
func langHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
paste := vars["pasteId"]
|
|
|
|
lang := vars["lang"]
|
|
|
|
s := getPaste(paste)
|
|
|
|
highlight := pygments.Highlight(html.UnescapeString(s), lang, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
|
|
|
|
io.WriteString(w, highlight)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPaste(paste string) string {
|
|
|
|
param1 := html.EscapeString(paste)
|
|
|
|
db, err := sql.Open("sqlite3", "./database.db")
|
|
|
|
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-19 04:36:00 +00:00
|
|
|
router := mux.NewRouter().StrictSlash(true)
|
|
|
|
router.HandleFunc("/", rootHandler)
|
|
|
|
router.HandleFunc("/p/{pasteId}", pasteHandler)
|
|
|
|
router.HandleFunc("/p/{pasteId}/{lang}", langHandler)
|
|
|
|
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-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
|
|
|
|
|
|
|
}
|