server/cmd/noah-server/noah-server.go

97 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-09-20 05:47:09 +00:00
package main
import (
2021-10-04 10:06:42 +00:00
"io"
2021-09-20 05:47:09 +00:00
"fmt"
"net/http"
2021-10-04 10:06:42 +00:00
"strings"
"noahcloud/webdav"
2021-10-04 10:06:42 +00:00
"noahcloud/handlers/nextcloud"
//"noahcloud/handlers/proxy"
"net/http/httptest"
2021-09-20 05:47:09 +00:00
)
2021-10-04 10:06:42 +00:00
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
2021-09-20 05:47:09 +00:00
func main() {
2021-10-04 10:06:42 +00:00
nc_normal_handler := &nextcloud.Handler{}
//proxy_handler := &proxy.Handler{}
nc_webdav_handler := &webdav.Handler{
Prefix: "/remote.php/webdav",
FileSystem: webdav.Dir("/data/ngo/files/"),
}
webdav_handler := &webdav.Handler{
Prefix: "/webdav",
FileSystem: webdav.Dir("/data/ngo/files/"),
2021-09-20 05:47:09 +00:00
}
2021-10-04 10:06:42 +00:00
2021-09-20 05:47:09 +00:00
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method, r.URL.Path)
2021-10-04 10:06:42 +00:00
writer := httptest.NewRecorder()
//proxy_handler.ServeHTTP(writer, r)
//fmt.Printf("%d - %s", writer.Code, writer.Body.String())
//copyHeader(w.Header(), writer.Header)
//w.WriteHeader(writer.StatusCode)
//io.Copy(w, writer.Body)
//return
if strings.HasPrefix(r.URL.Path, "/remote.php/webdav") {
username, password, ok := r.BasicAuth()
fmt.Println(username, password)
if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
if username != "vscode" || password != "password" {
//http.Error(w, "WebDAV: need authorized!", http.StatusUnauthorized)
//return
}
nc_webdav_handler.ServeHTTP(writer, r)
copyHeader(w.Header(), writer.Header())
w.WriteHeader(writer.Code)
for k, vv := range writer.Header() {
for _, v := range vv {
fmt.Println(k, v)
}
}
fmt.Printf("%s\n", writer.Body.String())
io.Copy(w, writer.Body)
} else if strings.HasPrefix(r.URL.Path, "/webdav") {
username, password, ok := r.BasicAuth()
fmt.Println(username, password)
if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
w.WriteHeader(http.StatusUnauthorized)
return
}
if username != "vscode" || password != "password" {
http.Error(w, "WebDAV: need authorized!", http.StatusUnauthorized)
return
}
webdav_handler.ServeHTTP(w, r)
} else if r.URL.Path == "/status.php" {
nc_normal_handler.ServeHTTP(w, r)
} else if r.URL.Path == "/ocs/v2.php/cloud/user" {
nc_normal_handler.ServeHTTP(w, r)
} else if r.URL.Path == "/index.php/login/v2" { // POST
nc_normal_handler.ServeHTTP(w, r)
} else if r.URL.Path == "/index.php/login/flow" {
nc_normal_handler.ServeHTTP(w, r)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
2021-09-20 05:47:09 +00:00
}
})
fmt.Println("wait requests")
http.ListenAndServe(":8080", nil)
}