forked from lawl/dumpaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagearmory.go
159 lines (133 loc) · 3.4 KB
/
imagearmory.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"encoding/base64"
"fmt"
"github.com/DeftNerd/imagearmory/server"
"github.com/codegangsta/cli"
"github.com/codegangsta/negroni"
"github.com/nu7hatch/gouuid"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
const BUCKETNAME = "justatestingbucket2"
const DATAPATH = "data/"
const HTTPSTOREPATH = "/store"
const HTTPGETPATH = "/get/"
const RESOURCEPATH = "/c/"
const MAINPATH = "/"
const UIFILE = "client/index.html"
const UIPATH = "client/"
const UMASK = 0664
type webFunc func(http.ResponseWriter, *http.Request)
func GetId() string {
u4, err := uuid.NewV4()
if err != nil {
log.Fatalf("Unable to generate UUID: %v\n", err)
}
var byteData []byte = make([]byte, 16)
copy(byteData, u4[:])
// Base64 encode, remove ==s and replace slashes
res := strings.Trim(base64.StdEncoding.EncodeToString(byteData), "=")
return strings.Replace(res, "/", "x", -1)
}
func FileExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return false
}
return true
}
func StoreHandler(store server.ObjectStore) webFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := GetId()
err := store.Put(id, []byte(r.FormValue("r")))
if err != nil {
log.Printf("%v\n", err)
}
fmt.Fprintf(w, "OK:%s", id)
}
}
func GetHandler(store server.ObjectStore) webFunc {
return func(w http.ResponseWriter, r *http.Request) {
fname := r.URL.Path[len(HTTPGETPATH):]
if store.IsLocal() {
buffer, err := store.Get(fname)
if err == nil {
fmt.Fprint(w, string(buffer))
return
}
} else {
http.Redirect(w, r, store.GetURL(fname), http.StatusFound)
}
fmt.Fprint(w, "FAIL")
}
}
func Mainhandler(w http.ResponseWriter, r *http.Request) {
ui, err := ioutil.ReadFile(UIFILE)
if err != nil {
fmt.Fprint(w, "Couldn't read user interface")
return
}
fmt.Fprint(w, string(ui))
}
func Resourcehandler(w http.ResponseWriter, r *http.Request) {
fname := r.URL.Path[len(RESOURCEPATH):]
if fname == "" {
http.Error(w, "404 File not found", http.StatusNotFound)
return
}
if strings.Contains(fname, "..") || fname[:1] == "/" {
http.Error(w, "Nice try.", http.StatusForbidden)
return
}
fname = UIPATH + fname
if FileExists(fname) {
ui, err := ioutil.ReadFile(fname)
if err == nil {
fmt.Fprint(w, string(ui))
return
}
}
http.Error(w, "404 File not found", http.StatusNotFound)
}
func initializeStorage(c *cli.Context) (store server.ObjectStore) {
switch c.String("storage") {
case "local":
log.Fatal("Local storage not yet re-implemented!")
case "s3":
store = &server.S3Adapter{}
default:
log.Fatalf("Undefined storage class '%v'\n", c.String("storage"))
}
store.Init(c)
return
}
func main() {
app := cli.NewApp()
app.Name = "imagearmory"
app.Version = "0.1"
app.Usage = "Encrypted image server"
app.Flags = []cli.Flag{
cli.StringFlag{"port, p", "8080", "Server port"},
cli.StringFlag{"storage", "local", "Data storage backend (local, s3)"},
cli.StringFlag{"bucket", "", "Target S3 bucket"},
}
app.Action = func(c *cli.Context) {
store := initializeStorage(c)
mux := http.NewServeMux()
mux.HandleFunc(HTTPSTOREPATH, StoreHandler(store))
mux.HandleFunc(HTTPGETPATH, GetHandler(store))
mux.HandleFunc(RESOURCEPATH, Resourcehandler)
mux.HandleFunc(MAINPATH, Mainhandler)
static := negroni.NewStatic(http.Dir("client"))
static.Prefix = "/c"
n := negroni.Classic()
n.Use(static)
n.UseHandler(mux)
n.Run(":8080")
}
app.Run(os.Args)
}