Skip to content

Commit

Permalink
Add quite mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wanetty committed Aug 9, 2024
1 parent fcf8e05 commit 6719a65
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ go build
### Docker

```bash
podman build . -t upgopher
podman run --name upgopher -p 9090:9090 upgopher
docker build . -t upgopher
docker run --name upgopher -p 9090:9090 upgopher
```

## Usage
Expand All @@ -70,6 +70,7 @@ Usage of ./upgopher:
use HTTPS on port 443 by default. (If you don't put cert and key, it will generate a self-signed certificate)
-user string
username for authentication
-q quite mode
```
## License
Expand Down
55 changes: 42 additions & 13 deletions upgopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
//go:embed static/favicon.ico
var favicon embed.FS

// global var to quite mode
var quite bool = false

// Handlers //////////////////////////////////////////////////
func fileHandlerWithDir(dir string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -47,26 +50,33 @@ func rawHandler(dir string) http.HandlerFunc {
if err != nil || !isSafe {
http.Error(w, "Bad path", http.StatusForbidden)
return_code = "403"
log.Printf("[%s - %s] %s %s\n", r.Method, return_code, r.URL.Path, r.RemoteAddr)
if !quite {
log.Printf("[%s - %s] %s %s\n", r.Method, return_code, r.URL.Path, r.RemoteAddr)
}
return
}

fileInfo, err := os.Stat(fullPath)
if os.IsNotExist(err) || fileInfo.IsDir() {
http.Error(w, "File not found", http.StatusNotFound)
return_code = "404"
log.Printf("[%s - %s] %s %s\n", r.Method, return_code, r.URL.Path, r.RemoteAddr)
if !quite {
log.Printf("[%s - %s] %s %s\n", r.Method, return_code, r.URL.Path, r.RemoteAddr)
}
return
}

log.Printf("[%s - %s] %s %s\n", r.Method, return_code, r.URL.Path, r.RemoteAddr)
if !quite {
log.Printf("[%s - %s] %s %s\n", r.Method, return_code, r.URL.Path, r.RemoteAddr)
}
http.ServeFile(w, r, fullPath)
}
}

func uploadHandler(dir string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] %s%s %s\n", r.Method, "/download/", r.URL.String(), r.RemoteAddr)
if !quite {
log.Printf("[%s] %s%s %s\n", r.Method, "/download/", r.URL.String(), r.RemoteAddr)
}

encodedFilePath := r.URL.Query().Get("path")
decodedFilePath, err := base64.StdEncoding.DecodeString(encodedFilePath)
Expand All @@ -79,7 +89,9 @@ func uploadHandler(dir string) http.HandlerFunc {
isSafe, err := isSafePath(dir, fullFilePath)
if err != nil || !isSafe {
http.Error(w, "Bad path", http.StatusForbidden)
log.Printf("[%s - %s] %s %s\n", r.Method, "403", r.URL.Path, r.RemoteAddr)
if !quite {
log.Printf("[%s] %s%s %s\n", r.Method, "/download/", r.URL.String(), r.RemoteAddr)
}
return
}

Expand All @@ -96,7 +108,9 @@ func uploadHandler(dir string) http.HandlerFunc {

func deleteHandler(dir string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] %s%s %s\n", r.Method, "/delete/", r.URL.String(), r.RemoteAddr)
if !quite {
log.Printf("[%s] %s%s %s\n", r.Method, "/delete/", r.URL.String(), r.RemoteAddr)
}

encodedFilePath := r.URL.Query().Get("path")
decodedFilePath, err := base64.StdEncoding.DecodeString(encodedFilePath)
Expand All @@ -109,7 +123,9 @@ func deleteHandler(dir string) http.HandlerFunc {
isSafe, err := isSafePath(dir, fullFilePath)
if err != nil || !isSafe {
http.Error(w, "Bad path", http.StatusForbidden)
log.Printf("[%s - %s] %s %s\n", r.Method, "403", r.URL.Path, r.RemoteAddr)
if !quite {
log.Printf("[%s] %s%s %s\n", r.Method, "/delete/", r.URL.String(), r.RemoteAddr)
}
return
}

Expand Down Expand Up @@ -174,8 +190,13 @@ func main() {
useTLS := flag.Bool("ssl", false, "use HTTPS on port 443 by default. (If you don't put cert and key, it will generate a self-signed certificate)")
certFile := flag.String("cert", "", "HTTPS certificate")
keyFile := flag.String("key", "", "private key for HTTPS")
quitearg := flag.Bool("q", false, "quite mode")
flag.Parse()
log.Printf("Executin version v1.6.0")
quite = *quitearg

if !quite {
log.Printf("Executing version v1.6.1")
}

if _, err := os.Stat(*dir); os.IsNotExist(err) {
os.MkdirAll(*dir, 0755)
Expand Down Expand Up @@ -244,12 +265,16 @@ func startServer(addr string, useTLS bool, certFile, keyFile string, port int) {
},
}

log.Printf("Starting HTTPS server on %s", addr)
if !quite {
log.Printf("Starting HTTPS server on %s", addr)
}
if err := server.ListenAndServeTLS("", ""); err != nil {
log.Fatalf("Error starting HTTPS server: %v", err)
}
} else {
log.Printf("Starting HTTP server on %s", addr)
if !quite {
log.Printf("Starting HTTP server on %s", addr)
}
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatalf("Error starting HTTP server: %v", err)
}
Expand Down Expand Up @@ -317,7 +342,9 @@ func basicAuth(handler http.HandlerFunc, username, password []byte) http.Handler
}

func fileHandler(w http.ResponseWriter, r *http.Request, dir string) {
log.Printf("[%s] %s %s\n", r.Method, r.URL.String(), r.RemoteAddr)
if !quite {
log.Printf("[%s] %s %s\n", r.Method, r.URL.String(), r.RemoteAddr)
}
currentPath := r.URL.Query().Get("path")
if currentPath != "" {
decodedPath, err := base64.StdEncoding.DecodeString(currentPath)
Expand All @@ -329,7 +356,9 @@ func fileHandler(w http.ResponseWriter, r *http.Request, dir string) {
isSafe, err := isSafePath(dir, newdir)
if err != nil || !isSafe {
http.Error(w, "Bad path", http.StatusForbidden)
log.Printf("[%s - %s] %s %s\n", r.Method, "403", r.URL.Path, r.RemoteAddr)
if !quite {
log.Printf("[%s - %s] %s %s\n", r.Method, "403", r.URL.Path, r.RemoteAddr)
}
return
} else {
dir = newdir
Expand Down

0 comments on commit 6719a65

Please sign in to comment.