[to #1] support Nextcloud IOS

This commit is contained in:
2021-10-04 06:06:42 -04:00
parent 8497c9375c
commit a1ebe0a9ed
6 changed files with 533 additions and 73 deletions

View File

@@ -0,0 +1,77 @@
package nextcloud
import (
"net/http"
"encoding/json"
"io/ioutil"
)
type Handler struct {
}
type Status struct {
Installed bool `json:"installed"`
Maintenance bool `json:"maintenance"`
NeedsDbUpgrade bool `json:"needsDbUpgrade"`
Version string `json:"version"`
VersionString string `json:"versionstring"`
Edition string `json:"edition"`
ProductName string `json:"productname"`
ExtendedSupport bool `json:"extendedSupport"`
}
type PollMsg struct {
Endpoint string `json:"endpoint"`
Token string `json:"token"`
}
type TokenMsg struct {
Login string `json:"login"`
Poll PollMsg `json:"poll"`
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/status.php" {
status := Status{
Installed: true,
Maintenance: false,
NeedsDbUpgrade: false,
Version: "21.0.1.1",
VersionString: "21.0.1",
Edition: "",
ProductName: "Nextcloud",
ExtendedSupport: false,
}
js, _ := json.Marshal(status)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
} else if r.URL.Path == "/index.php/login/v2" { // #1 POST to start login session
tokenMsg := TokenMsg{
Login: "http://" + r.Host + "/login/v2/flow/mqNBzQ3AY1QFfOIzPIn5hZYeNZlZ0RDbT32b42cpmdkFjgGvqjSTsKh6OG7floh7z2JHKmWlZnl7kBFxNEQYcDnmYArkOFjXeBsAB5w3fbOcyJBmi5JBS1k1jYGL8V1D",
Poll: PollMsg {
Endpoint: "http://" + r.Host + "/login/v2/poll",
Token: "zbF2EOo2de8gNOOePERCHQgLp1FQHB8zcSQ5t1KQhtNH0fRqV85Nh5UGRm9ZqpHmigH52jCYuxLSVZpu4FjHq9j8va233OXEHqQnBnMGr4p3BoRcc957jvJEjynPvJcB",
},
}
js, _ := json.Marshal(tokenMsg)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
} else if r.URL.Path == "/index.php/login/flow" { // # goto login page
var NewUrl string = "nc://login/server:http://" + r.Host + "&user:vscode&password:TEwNL0Sbkeln0TKdlcmNukkhQaOc6is0GBWlXIoO1YOByPweiSpoISuN5UI4fSGRBOgGE61I"
http.Redirect(w, r, NewUrl, http.StatusSeeOther)
} else if r.URL.Path == "/login/flow/grant" { // #3 redirect to grant page
} else if r.URL.Path == "/ocs/v2.php/cloud/user" { // #4 retreive user metadata
dat, _ := ioutil.ReadFile("/data/ngo/conf/ocs_v2.php_cloud_user.xml")
js := []byte(string(dat))
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.Write(js)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
}

88
handlers/proxy/proxy.go Normal file
View File

@@ -0,0 +1,88 @@
package proxy
import (
"net/http"
"net/http/httputil"
"io/ioutil"
"fmt"
"strings"
"bytes"
"io"
)
type Handler struct {
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// we need to buffer the body if we want to read it here and send it
// in the request.
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("request body--start")
fmt.Println(string(body))
fmt.Println("request body--stop")
// you can reassign the body if you need to parse it as multipart
r.Body = ioutil.NopCloser(bytes.NewReader(body))
// create a new url from the raw RequestURI sent by the client
url := fmt.Sprintf("%s://%s%s", "http", r.Host, r.RequestURI)
fmt.Println("-----Request-----", r.RequestURI)
fmt.Println(r.Method, url)
proxyReq, err := http.NewRequest(r.Method, url, bytes.NewReader(body))
// We may want to filter some headers, otherwise we could just use a shallow copy
// proxyReq.Header = r.Header
proxyReq.Header = make(http.Header)
for h, val := range r.Header {
tmp := []string{}
for _, v := range val{
if h == "Accept-Encoding" && strings.Contains(v, "gzip") {
continue
}
tmp = append(tmp, v)
}
proxyReq.Header[h] = tmp
fmt.Println(h, ":", tmp)
}
httpClient := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := httpClient.Do(proxyReq)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
responseData, err := httputil.DumpResponse(resp, true)
if err != nil {
fmt.Println("err", err)
}
responseString := string(responseData)
fmt.Println("-----Response-----", r.RequestURI)
fmt.Println(responseString)
io.Copy(w, resp.Body)
defer resp.Body.Close()
// legacy code
}