-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
428 lines (356 loc) · 10.1 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"context"
"crypto/tls"
"database/sql"
"embed"
"encoding/gob"
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"strings"
"time"
"log/slog"
"github.com/go-chi/chi/v5"
"github.com/rs/xid"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/jmoiron/sqlx"
)
var (
host, port, email, domain, certKey, cert, database_url string
dev bool
indexPage, deletePage []byte
decPageTmpl *template.Template
db *sqlx.DB
visitFile *os.File
visits = make(map[string]int)
//go:embed templates/*
tmpl embed.FS
// id content openned created_at updated_at
schema = `CREATE TABLE IF NOT EXISTS files (
id varchar(255) PRIMARY KEY,
content bytea,
openned integer DEFAULT 0,
created_at timestamp with time zone,
updated_at timestamp with time zone
);`
)
const (
ext = ".inc"
)
type RespError struct {
Status bool
Error string
}
func main() {
if err := run(); err != nil {
slog.Error("failed to run the application", "error", err)
os.Exit(1)
}
}
func run() error {
slog.Info("starting the application...")
setupFlags()
var err error
db, err = sqlx.Open("pgx", database_url)
if err != nil {
slog.Error("failed connect to the db", "error", err)
os.Exit(1)
}
defer db.Close()
err = db.Ping()
if err != nil {
return fmt.Errorf("failed to ping the db: %w", err)
}
db.MustExec(schema)
err = loadVisitCounts()
if err != nil {
return fmt.Errorf("failed to load openned: %w", err)
}
visitFile, err = os.OpenFile("counts.gob", os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("failed to open the visit file: %w", err)
}
defer visitFile.Close()
loadHTMLTemplates()
mux := newRouter()
srv, err := setupServer(mux)
if err != nil {
return err
}
go func() {
if dev {
slog.Info("server starting", "path", fmt.Sprintf("http://%s:%s", host, port))
}
err = srv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("failed to run server", "error", err)
}
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
<-sigCh
slog.Info("shutting down the server...")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
srv.Shutdown(ctx)
return nil
}
func loadVisitCounts() error {
res := []struct {
ID string
Openned int
}{}
err := db.Select(&res, "SELECT id, openned FROM files")
if err != nil {
return err
}
for _, v := range res {
visits[v.ID] = v.Openned
}
return nil
}
func saveCounts(v map[string]int) {
err := gob.NewEncoder(visitFile).Encode(v)
if err != nil {
slog.Error("gob.NewEncoder", "error", err)
}
}
func setupServer(mux http.Handler) (*http.Server, error) {
srv := http.Server{
Addr: fmt.Sprintf("%s:%s", host, port),
Handler: mux,
ReadTimeout: time.Second * 60,
ReadHeaderTimeout: time.Second * 30,
WriteTimeout: time.Second * 30,
MaxHeaderBytes: 1 << 12,
}
if cert != "" && certKey != "" {
cer, err := tls.LoadX509KeyPair(cert, certKey)
if err != nil {
slog.Error("tls.LoadX509KeyPair", err)
return nil, err
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cer},
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
srv.TLSConfig = tlsConfig
}
return &srv, nil
}
func newRouter() http.Handler {
mux := chi.NewRouter()
mux.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(indexPage)
})
mux.Post("/", func(w http.ResponseWriter, r *http.Request) {
type NewContent struct {
Data string `json:"data"`
Password string `json:"password"`
Submit string `json:"submit"`
}
var nc NewContent
//err = json.NewDecoder(r.Body).Decode(&nc)
//if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
//}
nc.Data = r.PostFormValue("data")
nc.Password = r.PostFormValue("password")
nc.Submit = r.PostFormValue("submit")
// iflen([]byte(nc.Password))!=32{}
if len([]byte(nc.Password)) != 32 {
http.Error(w, "password must be 32 char", http.StatusInternalServerError)
return
}
encd, err := EncryptAES([]byte(nc.Password), []byte(nc.Data))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
name := xid.New().String()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()
db.NamedExecContext(ctx, `INSERT INTO files (id, content, created_at) VALUES (:id, :content, NOW())`, map[string]interface{}{
"id": name,
"content": encd,
})
link := fmt.Sprintf("%s/%s.txt", domain, name)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `<a href="%s">%s</a>`, link, link)
})
mux.Get("/{file}.txt", func(w http.ResponseWriter, r *http.Request) {
// http://127.0.0.1:4430/ceid8micnjmqu3i77sf0.txt
// iflen([]byte(nc.Password))!=32{}
visitText := "This data has never been opened."
file := chi.URLParam(r, "file")
count := visits[file]
if count > 0 {
visitText = fmt.Sprintf("This data has been opened for %d time(s).", count)
}
data := struct {
Visit string
BasePath string
}{
Visit: visitText,
BasePath: r.RequestURI,
}
decPageTmpl.Execute(w, data)
//w.Write(decPage)
})
mux.Post("/{file}.txt", func(w http.ResponseWriter, r *http.Request) {
// @TODO: check the sumbit?
pass := r.PostFormValue("password")
file := chi.URLParam(r, "file")
var content []byte
err := db.Get(&content, "SELECT content FROM files WHERE id=$1", file)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
slog.Error("failed to db.Get", "error", err)
}
// don't return 404
http.Error(w, `{"message": "something went wrong"}`, http.StatusInternalServerError)
return
}
res, err := DecryptAES([]byte(pass), content)
if err != nil {
w.Header().Set("Content-Type", "application/json")
res := RespError{Error: err.Error()}
err = json.NewEncoder(w).Encode(res)
if err != nil {
slog.Error("failed to write the response on decryption failure", "error", err)
}
return
}
err = incVisit(file)
if err != nil {
slog.Error("failed to incVisit", "error", err)
w.Header().Set("Content-Type", "application/json")
e := RespError{Error: err.Error()}
err = json.NewEncoder(w).Encode(e)
if err != nil {
slog.Error("failed to write the response on incVisit failure", "error", err)
}
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write(res)
})
mux.Get("/{file}.txt/delete", func(w http.ResponseWriter, r *http.Request) {
w.Write(deletePage)
})
mux.Post("/{file}.txt/delete", func(w http.ResponseWriter, r *http.Request) {
// @TODO: check the sumbit?
pass := r.PostFormValue("password")
file := chi.URLParam(r, "file")
var content []byte
err := db.Get(&content, "SELECT content FROM files WHERE id=$1", file)
if err != nil {
slog.Error("failed to db.Get", "error", err)
http.Error(w, `{"message": "something went wrong"}`, http.StatusInternalServerError)
return
}
_, err = DecryptAES([]byte(pass), content)
if err != nil {
w.Header().Set("Content-Type", "application/json")
res := RespError{Error: err.Error()}
err = json.NewEncoder(w).Encode(res)
if err != nil {
slog.Error("failed to write the response on decryption failure", "error", err)
}
return
}
// key is valid
// delete the row
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()
_, err = db.ExecContext(ctx, "DELETE FROM files WHERE id=$1", file)
if err != nil {
slog.Error("failed to db.ExecContext", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
delete(visits, file)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "success"}`))
})
return mux
}
func incVisit(file string) error {
// inc db visits
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()
_, err := db.NamedExecContext(ctx, `UPDATE files SET openned=openned+1 WHERE id=:id`, map[string]interface{}{
"id": file,
})
if err != nil {
return fmt.Errorf("failed to increase the openned count: %w", err)
}
visits[file] += 1
return nil
}
func loadHTMLTemplates() {
var err error
indexPage, err = tmpl.ReadFile("templates/index.html")
if err != nil {
slog.Error("tmpl.ReadFile", err)
os.Exit(1)
}
decPage, err := tmpl.ReadFile("templates/decrypt.html")
if err != nil {
slog.Error("tmpl.ReadFile", err)
os.Exit(1)
}
decPageTmpl, err = template.New("decrypt").Parse(string(decPage))
if err != nil {
slog.Error("template.New", err)
os.Exit(1)
}
deletePage, err = tmpl.ReadFile("templates/delete.html")
if err != nil {
slog.Error("tmpl.ReadFile", err)
os.Exit(1)
}
}
func setupFlags() {
flag.BoolVar(&dev, "dev", true, "set true while in development")
flag.StringVar(&host, "host", "localhost", "host to run the server")
flag.StringVar(&port, "port", "80", "port to run the server")
flag.StringVar(&email, "email", "", "email to be used by autocert")
flag.StringVar(&domain, "domain", "http://127.0.0.1", "domain with schema (and port if needed) to be used for url generation")
flag.StringVar(&database_url, "database-url", "", "database url")
flag.StringVar(&certKey, "cert-key", "", "certificate key file")
flag.StringVar(&cert, "cert", "", "certificate file")
flag.Parse()
var err error
flag.VisitAll(func(f *flag.Flag) {
name := strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
if value, ok := os.LookupEnv(name); ok {
err2 := flag.Set(f.Name, value)
if err2 != nil {
err = fmt.Errorf("failed setting flag from environment: %w", err2)
}
}
})
if err != nil {
slog.Error("failed to load envs", "error", err)
}
if domain == "" {
domain = fmt.Sprintf("http://%s:%s", host, port)
}
}