Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
delorean committed Nov 1, 2023
1 parent ae6356f commit 746058a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
webroot = "./www"
webroot = "www"
lport = "5000"
vhost = "hardfiles.org"
dbfile = "dbfile.db"
Expand Down
43 changes: 23 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ func LoadConf() {
}
}

func shred(path string) error {
log.Info().Msg("shredding file")
func Shred(path string) error {
fileinfo, err := os.Stat(path)
if err != nil {
return err
}
size := fileinfo.Size()
err = scramble(path, size)
err = Scramble(path, size)
if err != nil {
return err
}

err = zeros(path, size)
err = Zeros(path, size)
if err != nil {
return err
}
Expand All @@ -62,14 +61,15 @@ func shred(path string) error {
return nil
}

func scramble(path string, size int64) error {
func Scramble(path string, size int64) error {
var i int64
for i = 0; i < 7; i++ { // 7 iterations
file, err := os.OpenFile(path, os.O_RDWR, 0)
defer file.Close()
if err != nil {
return err
}
defer file.Close()

offset, err := file.Seek(0, 0)
if err != nil {
return err
Expand All @@ -82,12 +82,12 @@ func scramble(path string, size int64) error {
return nil
}

func zeros(path string, size int64) error {
func Zeros(path string, size int64) error {
file, err := os.OpenFile(path, os.O_RDWR, 0)
defer file.Close()
if err != nil {
return err
}
defer file.Close()

offset, err := file.Seek(0, 0)
if err != nil {
Expand Down Expand Up @@ -120,8 +120,7 @@ func CheckFile(name string) bool { // false if doesn't exist, true if exists

func UploadHandler(w http.ResponseWriter, r *http.Request) {
// expiry sanitize
twentyfour := int64(86400)
// twentyfour := int64(10)
twentyfour := int64(10)

file, _, err := r.FormFile("file")
if err != nil {
Expand Down Expand Up @@ -184,7 +183,7 @@ func Cull() {
continue
}
if time.Now().After(time.Unix(eol, 0)) {
if err := shred(conf.FileFolder + "/" + string(k)); err != nil {
if err := Shred(conf.FileFolder + "/" + string(k)); err != nil {
log.Error().Err(err).Msg("shredding failed")
} else {
removed += 1
Expand All @@ -195,7 +194,7 @@ func Cull() {
return nil
})
if removed >= 1 {
log.Info().Int("amount", removed).Msg("expired")
log.Info().Int("amount", removed).Msg("shredded")
}
time.Sleep(5 * time.Second)
}
Expand All @@ -206,7 +205,7 @@ func main() {
LoadConf()

err := landlock.V2.BestEffort().RestrictPaths(
landlock.RWDirs("./"+conf.FileFolder),
landlock.RWDirs(conf.FileFolder),
landlock.RWDirs(conf.Webroot),
landlock.RWFiles(conf.DBFile),
)
Expand Down Expand Up @@ -237,7 +236,7 @@ func main() {

r := mux.NewRouter()
r.HandleFunc("/", UploadHandler).Methods("POST")
r.HandleFunc("/uploads/{name}", func(w http.ResponseWriter, r *http.Request) { // upload hits
r.HandleFunc("/uploads/{name}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if !CheckFile(vars["name"]) {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -248,20 +247,24 @@ func main() {
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, conf.Webroot+"/index.html")
}).Methods("GET")
r.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, conf.Webroot+"/index.html")
}).Methods("GET")
r.HandleFunc("/fist.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, conf.Webroot+"/fist.ico")
}).Methods("GET")
http.Handle("/", r)

go Cull()

serv := &http.Server{
Addr: ":" + conf.LPort,
Handler: r,
ErrorLog: nil,
//ReadTimeout: 20 * time.Second,
//WriteTimeout: 20 * time.Second,
Addr: ":" + conf.LPort,
Handler: r,
ErrorLog: nil,
IdleTimeout: 20 * time.Second,
}

log.Info().Err(err).Msg("listening...")
log.Info().Err(err).Msg("listening on port " + conf.LPort + "...")

if err := serv.ListenAndServe(); err != nil {
log.Fatal().Err(err).Msg("error starting server")
Expand Down
2 changes: 1 addition & 1 deletion www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="https://i.imgur.com/FdC3YNu.png">
<link rel="icon" href="fist.ico">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Black+Ops+One&display=swap" rel="stylesheet">
Expand Down

0 comments on commit 746058a

Please sign in to comment.