|
@@ -1,13 +1,15 @@
|
|
|
package webdav
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"fmt"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
- "imuslab.com/bokofs/bokofsd/mod/bokoworker"
|
|
|
)
|
|
|
|
|
|
/*
|
|
@@ -25,16 +27,24 @@ type Options struct {
|
|
|
}
|
|
|
|
|
|
type Server struct {
|
|
|
- LoadedWorkers *bokoworker.Worker
|
|
|
+ LoadedWorkers sync.Map //Storing uuid to bokoworker pointer (*bokoworker.Worker)
|
|
|
+ RouterDir *RouterDir
|
|
|
+ Options *Options
|
|
|
}
|
|
|
|
|
|
-func NewWebdavInterfaceServer() *Server {
|
|
|
- return &Server{}
|
|
|
+/* NewWebdavInterfaceServer creates a new WebDAV server instance */
|
|
|
+func NewWebdavInterfaceServer(options Options) *Server {
|
|
|
+ newRouter := &RouterDir{}
|
|
|
+ return &Server{
|
|
|
+ LoadedWorkers: sync.Map{},
|
|
|
+ RouterDir: newRouter,
|
|
|
+ Options: &options,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func (s *Server) Start() error {
|
|
|
srv := &webdav.Handler{
|
|
|
- FileSystem: webdav.Dir(dir),
|
|
|
+ FileSystem: s.RouterDir,
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
Logger: func(r *http.Request, err error) {
|
|
|
if err != nil {
|
|
@@ -45,19 +55,44 @@ func (s *Server) Start() error {
|
|
|
},
|
|
|
}
|
|
|
http.Handle("/", srv)
|
|
|
- if *serveSecure == true {
|
|
|
- if _, err := os.Stat("./cert.pem"); err != nil {
|
|
|
- fmt.Println("[x] No cert.pem in current directory. Please provide a valid cert")
|
|
|
- return
|
|
|
+
|
|
|
+ if s.Options.SecureServe {
|
|
|
+ if _, err := os.Stat(s.Options.PublicKeyPath); err != nil {
|
|
|
+ fmt.Println("Public ket not found")
|
|
|
+ return err
|
|
|
}
|
|
|
- if _, er := os.Stat("./key.pem"); er != nil {
|
|
|
- fmt.Println("[x] No key.pem in current directory. Please provide a valid cert")
|
|
|
- return
|
|
|
+ if _, err := os.Stat(s.Options.PrivateKeyPath); err != nil {
|
|
|
+ fmt.Println("Private key not found")
|
|
|
+ return err
|
|
|
}
|
|
|
-
|
|
|
- go http.ListenAndServeTLS(fmt.Sprintf(":%d", *httpsPort), "cert.pem", "key.pem", nil)
|
|
|
}
|
|
|
- if err := http.ListenAndServe(fmt.Sprintf(":%d", *httpPort), nil); err != nil {
|
|
|
- log.Fatalf("Error with WebDAV server: %v", err)
|
|
|
+
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
+ defer cancel()
|
|
|
+ done := make(chan error)
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ if s.Options.SecureServe {
|
|
|
+ if err := http.ListenAndServeTLS(s.Options.ListeningAddress, s.Options.PublicKeyPath, s.Options.PrivateKeyPath, nil); err != nil {
|
|
|
+ done <- err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if err := http.ListenAndServe(s.Options.ListeningAddress, nil); err != nil {
|
|
|
+ log.Fatalf("Error with WebDAV server: %v", err)
|
|
|
+ done <- err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ select {
|
|
|
+ case <-ctx.Done():
|
|
|
+ //No error in 3 seconds. Assume all green
|
|
|
+ return nil
|
|
|
+ case success := <-done:
|
|
|
+ if success != nil {
|
|
|
+ log.Fatalf("Error with WebDAV server")
|
|
|
+ return success
|
|
|
+ }
|
|
|
}
|
|
|
+ return nil
|
|
|
}
|