2016-06-10 13:56:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-06-19 01:48:57 +00:00
|
|
|
"database/sql"
|
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"
|
|
|
|
"net/http"
|
2016-06-19 00:17:35 +00:00
|
|
|
|
|
|
|
"github.com/dchest/uniuri"
|
|
|
|
"github.com/ewhal/pygments"
|
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 01:48:57 +00:00
|
|
|
ADDRESS = "https://p.pantsu.cat/"
|
|
|
|
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-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-11 02:31:03 +00:00
|
|
|
func save(raw []byte) string {
|
2016-06-19 01:48:57 +00:00
|
|
|
paste := raw[86 : len(raw)-46]
|
2016-06-10 14:17:27 +00:00
|
|
|
|
|
|
|
s := generateName()
|
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 01:48:57 +00:00
|
|
|
stmt, err := db.Prepare("INSERT INTO pastebin(id, data) values(?,?)")
|
|
|
|
_, err = stmt.Exec(s, html.EscapeString(string(paste)))
|
|
|
|
check(err)
|
|
|
|
db.Close()
|
2016-06-10 14:17:27 +00:00
|
|
|
|
2016-06-11 01:22:19 +00:00
|
|
|
return s
|
2016-06-10 14:17:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 14:29:22 +00:00
|
|
|
func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
2016-06-19 01:48:57 +00:00
|
|
|
param1 := html.EscapeString(r.URL.Query().Get("p"))
|
|
|
|
param2 := html.EscapeString(r.URL.Query().Get("lang"))
|
|
|
|
db, err := sql.Open("sqlite3", "./database.db")
|
|
|
|
var s string
|
|
|
|
err = db.QueryRow("select data from pastebin where id=?", param1).Scan(&s)
|
|
|
|
db.Close()
|
2016-06-19 01:57:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
io.WriteString(w, "Error invalid paste")
|
|
|
|
} else {
|
|
|
|
check(err)
|
|
|
|
}
|
2016-06-11 03:09:28 +00:00
|
|
|
|
2016-06-19 01:48:57 +00:00
|
|
|
if param1 != "" {
|
2016-06-15 04:56:51 +00:00
|
|
|
if param2 != "" {
|
2016-06-19 01:48:57 +00:00
|
|
|
highlight := pygments.Highlight(html.UnescapeString(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
|
|
|
|
io.WriteString(w, highlight)
|
2016-06-11 03:33:29 +00:00
|
|
|
|
2016-06-11 03:09:28 +00:00
|
|
|
} else {
|
2016-06-19 01:48:57 +00:00
|
|
|
io.WriteString(w, html.UnescapeString(s))
|
2016-06-11 03:09:28 +00:00
|
|
|
}
|
2016-06-11 02:13:37 +00:00
|
|
|
} else {
|
2016-06-11 02:31:03 +00:00
|
|
|
io.WriteString(w, TEXT)
|
2016-06-11 02:13:37 +00:00
|
|
|
}
|
2016-06-10 14:29:22 +00:00
|
|
|
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 01:48:57 +00:00
|
|
|
name := save(buf)
|
|
|
|
io.WriteString(w, ADDRESS+"?p="+name+"\n")
|
2016-06-10 14:29:22 +00:00
|
|
|
case "DELETE":
|
|
|
|
// Remove the record.
|
|
|
|
}
|
2016-06-10 13:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", pasteHandler)
|
2016-06-11 02:31:03 +00:00
|
|
|
err := http.ListenAndServe(PORT, nil)
|
2016-06-11 03:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-06-10 13:56:42 +00:00
|
|
|
|
|
|
|
}
|