nat/main.go

292 lines
6.3 KiB
Go
Raw Normal View History

package main
import (
2016-06-24 02:05:14 +00:00
"bytes"
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 02:05:14 +00:00
"time"
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-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"`
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 {
Title string
2016-06-23 10:15:13 +00:00
Body []byte
2016-06-23 11:00:46 +00:00
Raw string
Home 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)
}
}
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)
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
}
db.Close()
2016-06-10 14:29:22 +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 03:19:50 +00:00
func save(raw string, lang string) []string {
2016-06-19 06:31:36 +00:00
db, err := sql.Open("mysql", DATABASE)
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
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()
var url string
2016-06-23 03:19:50 +00:00
if lang == "" {
url = ADDRESS + "/p/" + id
2016-06-23 03:19:50 +00:00
} else {
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-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)
check(err)
db.Close()
2016-06-19 06:16:08 +00:00
return []string{id, sha, url, paste, delKey}
}
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-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-23 03:19:50 +00:00
values := save(paste, lang)
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-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-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-23 09:21:37 +00:00
func getPaste(paste string, lang 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-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 {
2016-06-23 10:41:45 +00:00
if lang == "" {
return html.UnescapeString(s)
} else {
high, err := highlight(s, lang)
check(err)
return high
}
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 01:22:48 +00:00
var templates = template.Must(template.ParseFiles("assets/paste.html", "assets/index.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"]
s := getPaste(paste, lang)
2016-06-23 09:11:40 +00:00
link := ADDRESS + "/raw/" + paste
2016-06-23 10:41:45 +00:00
if lang == "" {
p := &Page{
Title: paste,
Body: []byte(s),
2016-06-23 10:59:12 +00:00
Raw: link,
Home: ADDRESS,
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 01:16:58 +00:00
fmt.Fprintf(w, string(syntax), paste, paste, s, ADDRESS, link)
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) {
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
2016-06-24 01:46:50 +00:00
vars := mux.Vars(r)
paste := vars["pasteId"]
s := getPaste(paste, "")
2016-06-24 02:05:14 +00:00
http.ServeContent(w, r, paste, time.Now(), bytes.NewReader([]byte(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-23 09:21:37 +00:00
s := getPaste(paste, "")
2016-06-19 04:36:00 +00:00
io.WriteString(w, s)
}
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
}
}