-
Notifications
You must be signed in to change notification settings - Fork 0
/
pastebin.go
55 lines (50 loc) · 1.34 KB
/
pastebin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/mux"
)
/*
This code creates a VERY SIMPLE paste bin on a server.
You can crete a random data and assign the token variable below.
The post request must be sent as form url encoded style.
The program stores the posted data on a file
There is no buffer protection in the code.
*/
var secret = "RANDOM_ACCESS_TOKEN"
var file *os.File
var fileName = "pastes"
func main() {
file, _ = os.OpenFile("fileName", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if file == nil {
panic("Something go wrong, cannot create the file")
}
router := mux.NewRouter()
router.HandleFunc("/paste", Paste).Methods("POST")
log.Fatal(http.ListenAndServe(":5192", router))
}
// Paste ...
func Paste(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Bad request!!!"))
return
}
token := r.Form.Get("token")
if token == secret {
data := r.Form.Get("data")
_, err := file.WriteString(data + "\n")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Something go wrong, could not save the data"))
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Content saved succefully"))
}
} else {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized Attempt!!!"))
}
}