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