diff --git a/main b/main new file mode 100755 index 0000000..696f33a Binary files /dev/null and b/main differ diff --git a/main.go b/main.go index 35aea7a..6ea04cf 100644 --- a/main.go +++ b/main.go @@ -19,11 +19,9 @@ import ( "nxcaldav/internal/extra" ) - - func main() { // -- GET CONFIG - path := "config.yaml"; + path := "config.yaml" if len(os.Args) == 3 { if os.Args[1] == "-c" { path = os.Args[2] @@ -41,26 +39,26 @@ func main() { log.Fatalf("failed to initialize database backend: %v", err) } - // -- GET CONTEXT AND DB caldavHandler := &caldav.Handler{Backend: be} carddavHandler := &carddav.Handler{Backend: be} publicURL, _ := url.Parse(cfg.Server.PublicURL) - // -- DISCOVERIES http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - scheme := r.URL.Scheme - if scheme == "" { scheme = "http" } + scheme := r.URL.Scheme + if scheme == "" { + scheme = "http" + } // set host (because reverse proxies exist) if publicURL != nil && publicURL.Host != "" { r.Host = publicURL.Host r.URL.Host = publicURL.Host - + // prioritize X-Forwarded-Proto, then PublicURL (e.g. Cloudfalre proxy) - scheme := publicURL.Scheme + scheme = publicURL.Scheme if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" { scheme = proto } @@ -103,67 +101,68 @@ func main() { return } - log.Printf("[user: %s] %s %s", user, r.Method, r.URL.Path) + log.Printf("[user: %s] %s %s", user, r.Method, r.URL.Path) - principalPath := prefix + fmt.Sprintf("/%s/", user) - ctx := context.WithValue(r.Context(), "principal", principalPath) + principalPath := prefix + fmt.Sprintf("/%s/", user) + ctx := context.WithValue(r.Context(), "principal", principalPath) - // set header for carddav + // set header for carddav if slices.Contains([]string{ - "/.well-known/carddav", - prefix + "/.well-known/carddav", - principalPath, - strings.TrimSuffix(principalPath, "/"), + "/.well-known/carddav", + prefix + "/.well-known/carddav", + principalPath, + strings.TrimSuffix(principalPath, "/"), }, r.URL.Path) || strings.Contains(r.URL.Path, "/addressbooks/") { - w.Header().Add("DAV", "addressbook") + w.Header().Add("DAV", "addressbook") } - // for caldav discovery - if slices.Contains([]string{ - "/.well-known/caldav", - prefix+"/.well-known/caldav", - }, r.URL.Path) { - http.Redirect(w, r, fmt.Sprintf( "%s://%s%s", scheme, r.Host, principalPath), http.StatusMovedPermanently) - return - } + // for caldav discovery + if slices.Contains([]string{ + "/.well-known/caldav", + prefix + "/.well-known/caldav", + }, r.URL.Path) { + http.Redirect(w, r, fmt.Sprintf("%s://%s%s", scheme, r.Host, principalPath), http.StatusMovedPermanently) + return + } - // serve caldav - if strings.Contains(r.URL.Path, "/calendars/") { - if r.Method == "PROPFIND" { - // Calendar colors - // needed because color info is not RFC, so I hacked it in with regex, to look like Radicales response - extra.InjectColor(r, ctx, caldavHandler, w, be) - } else { - caldavHandler.ServeHTTP(w, r.WithContext(ctx)) - } + // serve caldav + if strings.Contains(r.URL.Path, "/calendars/") { + if r.Method == "PROPFIND" { + // Calendar colors + // needed because color info is not RFC, so I hacked it in with regex, to look like Radicales response + extra.InjectColor(r, ctx, caldavHandler, w, be) + } else { + caldavHandler.ServeHTTP(w, r.WithContext(ctx)) + } - // serve carddav - } else if strings.Contains(r.URL.Path, "/addressbooks/") { - carddavHandler.ServeHTTP(w, r.WithContext(ctx)) + // serve carddav + } else if strings.Contains(r.URL.Path, "/addressbooks/") { + carddavHandler.ServeHTTP(w, r.WithContext(ctx)) - // catch weird requests - } else { + // catch weird requests + } else { - if strings.HasSuffix(r.URL.Path, user+"/") || strings.HasSuffix(r.URL.Path, user) { - // For principal path, use merged discovery handler - if r.Method == "PROPFIND" { - extra.HandleDiscoveryPropfind(r, ctx, caldavHandler, w, be) - } else { - caldavHandler.ServeHTTP(w, r.WithContext(ctx)) - // Ensure DAV header includes addressbook for OPTIONS - if r.Method == "OPTIONS" { - dav := w.Header().Get("DAV") - if dav != "" && !strings.Contains(dav, "addressbook") { - w.Header().Set("DAV", dav+", addressbook") - } - } - } - } else { - http.NotFound(w, r) - } - } + if strings.HasSuffix(r.URL.Path, user+"/") || strings.HasSuffix(r.URL.Path, user) { + // For principal path, use merged discovery handler + if r.Method == "PROPFIND" { + extra.HandleDiscoveryPropfind(r, ctx, caldavHandler, w, be) + } else { + caldavHandler.ServeHTTP(w, r.WithContext(ctx)) + // Ensure DAV header includes addressbook for OPTIONS + if r.Method == "OPTIONS" { + dav := w.Header().Get("DAV") + if dav != "" && !strings.Contains(dav, "addressbook") { + w.Header().Set("DAV", dav+", addressbook") + } + } + } + } else { + http.NotFound(w, r) + } + } + }) - fmt.Printf("Starting CalDAV/CardDAV server on %s...\n", cfg.Server.BindAddress) + fmt.Printf("Starting CalDAV/CardDAV server on %s...\n", cfg.Server.BindAddress) server := &http.Server{ Addr: cfg.Server.BindAddress, ReadTimeout: 30 * time.Second,