cleanups
This commit is contained in:
161
main.go
161
main.go
@@ -1,27 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-webdav/caldav"
|
||||
"github.com/emersion/go-webdav/carddav"
|
||||
"nxcaldav/internal/backend"
|
||||
|
||||
"nxcaldav/internal/extra"
|
||||
"nxcaldav/internal/backend"
|
||||
"nxcaldav/internal/config"
|
||||
"nxcaldav/internal/extra"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func main() {
|
||||
// -- GET CONFIG
|
||||
path := "config.yaml";
|
||||
if len(os.Args) == 3 {
|
||||
if os.Args[1] == "-c" {
|
||||
@@ -33,51 +34,39 @@ func main() {
|
||||
log.Fatalf("failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// -- GET CONTEXT AND DB
|
||||
ctx := context.Background()
|
||||
be, err := backend.NewDBBackend(ctx, cfg)
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
http.HandleFunc("/respond", func(w http.ResponseWriter, r *http.Request) {
|
||||
p := r.URL.Query().Get("path")
|
||||
attendee := r.URL.Query().Get("attendee")
|
||||
status := r.URL.Query().Get("status")
|
||||
|
||||
if p == "" || attendee == "" || status == "" {
|
||||
http.Error(w, "Missing parameters", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := be.RespondToInvitation(r.Context(), p, attendee, status)
|
||||
if err != nil {
|
||||
log.Printf("[email] Error handling response: %v", err)
|
||||
http.Error(w, fmt.Sprintf("Error: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
fmt.Fprintf(w, "<h1>Response recorded</h1><p>You have %s the invitation for %s.</p>", strings.ToLower(status), attendee)
|
||||
})
|
||||
|
||||
// -- DISCOVERIES
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Proxy-aware normalization:
|
||||
|
||||
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
|
||||
|
||||
// Detect scheme: prioritize X-Forwarded-Proto, then PublicURL
|
||||
// prioritize X-Forwarded-Proto, then PublicURL (e.g. Cloudfalre proxy)
|
||||
scheme := publicURL.Scheme
|
||||
if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" {
|
||||
scheme = proto
|
||||
}
|
||||
r.URL.Scheme = scheme
|
||||
|
||||
// Also rewrite WebDAV Destination header (used for MOVE/COPY)
|
||||
// Also rewrite WebDAV Destination header (for MOVE/COPY)
|
||||
if dest := r.Header.Get("Destination"); dest != "" {
|
||||
destURL, err := url.Parse(dest)
|
||||
if err == nil {
|
||||
@@ -95,92 +84,84 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// caldav access needs auth
|
||||
// DAV needs auth
|
||||
user, password, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="CalDAV Server"`)
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify via Database (bcrypt)
|
||||
valid, err := be.VerifyUser(r.Context(), user, password)
|
||||
if err != nil {
|
||||
log.Printf("auth error for %s: %v", user, err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !valid {
|
||||
log.Printf("auth failed for %s", user)
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
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)
|
||||
principalPath := prefix + fmt.Sprintf("/%s/", user)
|
||||
ctx := context.WithValue(r.Context(), "principal", principalPath)
|
||||
|
||||
// Add addressbook to DAV header for discovery/access paths
|
||||
// This includes principal path and .well-known
|
||||
isCardDAVPath := strings.Contains(r.URL.Path, "/addressbooks/") ||
|
||||
r.URL.Path == "/.well-known/carddav" ||
|
||||
r.URL.Path == prefix+"/.well-known/carddav" ||
|
||||
r.URL.Path == principalPath ||
|
||||
r.URL.Path == strings.TrimSuffix(principalPath, "/")
|
||||
|
||||
if isCardDAVPath {
|
||||
w.Header().Add("DAV", "addressbook")
|
||||
}
|
||||
// set header for carddav
|
||||
if slices.Contains([]string{
|
||||
"/.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")
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), "principal", principalPath)
|
||||
// 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
|
||||
}
|
||||
|
||||
if r.URL.Path == "/.well-known/caldav" || r.URL.Path == prefix+"/.well-known/caldav" ||
|
||||
r.URL.Path == "/.well-known/carddav" || r.URL.Path == prefix+"/.well-known/carddav" {
|
||||
// If normalized request, use normalized host/scheme for redirect
|
||||
if publicURL != nil && publicURL.Host != "" {
|
||||
scheme := r.URL.Scheme
|
||||
if scheme == "" {
|
||||
scheme = "http"
|
||||
}
|
||||
target := fmt.Sprintf("%s://%s%s", scheme, r.Host, principalPath)
|
||||
http.Redirect(w, r, target, http.StatusMovedPermanently)
|
||||
} else {
|
||||
http.Redirect(w, r, 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))
|
||||
}
|
||||
|
||||
// needed because color info is not RFC, so regex hack
|
||||
if strings.Contains(r.URL.Path, "/calendars/") {
|
||||
if r.Method == "PROPFIND" {
|
||||
extra.AddColorToCalendarPropfind(r, ctx, caldavHandler, w, be)
|
||||
} else {
|
||||
caldavHandler.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
} 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 {
|
||||
|
||||
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 {
|
||||
// Fallback: try both or default to caldav for principal path etc.
|
||||
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)
|
||||
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)
|
||||
server := &http.Server{
|
||||
|
||||
Reference in New Issue
Block a user