diff --git a/api/custom/federation.go b/api/custom/federation.go index 78458c61ef2ff4253426bb8679d620162a313ed7..80921ff0dad260de7a280d897bf372eebb113a3a 100644 --- a/api/custom/federation.go +++ b/api/custom/federation.go @@ -2,7 +2,6 @@ package custom import ( "encoding/json" - "io" "net/http" "github.com/getsentry/sentry-go" @@ -41,15 +40,9 @@ func GetFederationInfo(r *http.Request, rctx rcontext.RequestContext, user _apim return _responses.InternalServerError(err.Error()) } - c, err := io.ReadAll(versionResponse.Body) - if err != nil { - rctx.Log.Error(err) - sentry.CaptureException(err) - return _responses.InternalServerError(err.Error()) - } - + decoder := json.NewDecoder(versionResponse.Body) out := make(map[string]interface{}) - err = json.Unmarshal(c, &out) + err = decoder.Decode(&out) if err != nil { rctx.Log.Error(err) sentry.CaptureException(err) diff --git a/common/assets/process.go b/common/assets/process.go index e1de6a7fcab0dab480365c179c169211ff6e7951..5e1bdc51f29129eebace1e50d8e2087960e505e1 100644 --- a/common/assets/process.go +++ b/common/assets/process.go @@ -89,29 +89,29 @@ func extractPrefixTo(pathName string, destination string) { continue } - logrus.Infof("Decoding %s", f) b, err := base64.StdEncoding.DecodeString(b64) if err != nil { panic(err) } - logrus.Infof("Decompressing %s", f) gr, err := gzip.NewReader(bytes.NewBuffer(b)) if err != nil { panic(err) } - //noinspection GoDeferInLoop,GoUnhandledErrorResult - defer gr.Close() - uncompressedBytes, err := io.ReadAll(gr) + + dest := path.Join(destination, filepath.Base(f)) + logrus.Debugf("Writing %s to %s", f, dest) + file, err := os.Create(dest) if err != nil { panic(err) } - dest := path.Join(destination, filepath.Base(f)) - logrus.Infof("Writing %s to %s", f, dest) - err = os.WriteFile(dest, uncompressedBytes, 0644) + _, err = io.Copy(file, gr) if err != nil { panic(err) } + + _ = gr.Close() + file.Close() } } diff --git a/common/config/access.go b/common/config/access.go index 89d09308fa0663453f00c6542c81bbb22ef569f1..8822a73d11c85bce1f36a43529e766eb0c90757f 100644 --- a/common/config/access.go +++ b/common/config/access.go @@ -9,7 +9,6 @@ import ( "sync" "github.com/sirupsen/logrus" - "github.com/turt2live/matrix-media-repo/util/stream_util" "gopkg.in/yaml.v3" ) @@ -109,7 +108,8 @@ func reloadConfig() (*MainRepoConfig, map[string]*DomainRepoConfig, error) { if err != nil { return nil, nil, err } - defer stream_util.DumpAndCloseStream(f) + //goland:noinspection GoDeferInLoop + defer f.Close() buffer, err := io.ReadAll(f) if err != nil { diff --git a/matrix/client_server.go b/matrix/client_server.go index 3588ebc9928ab913c0241ddc246ad4e70bdc1d7e..640094ad62feee07ca4251bee3dbbe3a583b90d6 100644 --- a/matrix/client_server.go +++ b/matrix/client_server.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/turt2live/matrix-media-repo/common/rcontext" - "github.com/turt2live/matrix-media-repo/util/stream_util" ) // Based in part on https://github.com/matrix-org/gomatrix/blob/072b39f7fa6b40257b4eead8c958d71985c28bdd/client.go#L180-L243 @@ -48,7 +47,7 @@ func doRequest(ctx rcontext.RequestContext, method string, urlStr string, body i if err != nil { return err } - defer stream_util.DumpAndCloseStream(res.Body) + defer res.Body.Close() contents, err := io.ReadAll(res.Body) if err != nil { diff --git a/matrix/federation.go b/matrix/federation.go index bca37918deedc3cccd5155aff24e4c2aa809e2cf..e76c58b3cdd3d4e7a070231d0792dc9dd5dd90c0 100644 --- a/matrix/federation.go +++ b/matrix/federation.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "net" "net/http" "os" @@ -105,64 +104,62 @@ func GetServerApiUrl(hostname string) (string, string, error) { r, err := http.Get(fmt.Sprintf("https://%s/.well-known/matrix/server", h)) if err == nil && r.StatusCode == http.StatusOK { // Try parsing .well-known - c, err2 := io.ReadAll(r.Body) - if err2 == nil { - wk := &wellknownServerResponse{} - err3 := json.Unmarshal(c, wk) - if err3 == nil && wk.ServerAddr != "" { - wkHost, wkPort, err4 := net.SplitHostPort(wk.ServerAddr) - wkDefPort := false - if err4 != nil && strings.HasSuffix(err4.Error(), "missing port in address") { - wkHost, wkPort, err4 = net.SplitHostPort(wk.ServerAddr + ":8448") - wkDefPort = true + decoder := json.NewDecoder(r.Body) + wk := &wellknownServerResponse{} + err3 := decoder.Decode(&wk) + if err3 == nil && wk.ServerAddr != "" { + wkHost, wkPort, err4 := net.SplitHostPort(wk.ServerAddr) + wkDefPort := false + if err4 != nil && strings.HasSuffix(err4.Error(), "missing port in address") { + wkHost, wkPort, err4 = net.SplitHostPort(wk.ServerAddr + ":8448") + wkDefPort = true + } + if err4 == nil { + // Step 3a: if the delegated host is an IP address, use that (regardless of port) + logrus.Debug("Checking if WK host is an IP: " + wkHost) + if is.IP(wkHost) { + url := fmt.Sprintf("https://%s", net.JoinHostPort(wkHost, wkPort)) + server := cachedServer{url, wk.ServerAddr} + apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) + logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; IP address)") + return url, wk.ServerAddr, nil } - if err4 == nil { - // Step 3a: if the delegated host is an IP address, use that (regardless of port) - logrus.Debug("Checking if WK host is an IP: " + wkHost) - if is.IP(wkHost) { - url := fmt.Sprintf("https://%s", net.JoinHostPort(wkHost, wkPort)) - server := cachedServer{url, wk.ServerAddr} - apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) - logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; IP address)") - return url, wk.ServerAddr, nil - } - // Step 3b: if the delegated host is not an IP and an explicit port is given, use that - logrus.Debug("Checking if WK is using default port? ", wkDefPort) - if !wkDefPort { - wkHost = net.JoinHostPort(wkHost, wkPort) - url := fmt.Sprintf("https://%s", wkHost) - server := cachedServer{url, wkHost} - apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) - logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; explicit port)") - return url, wkHost, nil - } + // Step 3b: if the delegated host is not an IP and an explicit port is given, use that + logrus.Debug("Checking if WK is using default port? ", wkDefPort) + if !wkDefPort { + wkHost = net.JoinHostPort(wkHost, wkPort) + url := fmt.Sprintf("https://%s", wkHost) + server := cachedServer{url, wkHost} + apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) + logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; explicit port)") + return url, wkHost, nil + } - // Step 3c: if the delegated host is not an IP and doesn't have a port, start a SRV lookup and use it - // Note: we ignore errors here because the hostname will fail elsewhere. - logrus.Debug("Doing SRV on WK host ", wkHost) - _, addrs, _ := net.LookupSRV("matrix", "tcp", wkHost) - if len(addrs) > 0 { - // Trim off the trailing period if there is one (golang doesn't like this) - realAddr := addrs[0].Target - if realAddr[len(realAddr)-1:] == "." { - realAddr = realAddr[0 : len(realAddr)-1] - } - url := fmt.Sprintf("https://%s", net.JoinHostPort(realAddr, strconv.Itoa(int(addrs[0].Port)))) - server := cachedServer{url, wkHost} - apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) - logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; SRV)") - return url, wkHost, nil + // Step 3c: if the delegated host is not an IP and doesn't have a port, start a SRV lookup and use it + // Note: we ignore errors here because the hostname will fail elsewhere. + logrus.Debug("Doing SRV on WK host ", wkHost) + _, addrs, _ := net.LookupSRV("matrix", "tcp", wkHost) + if len(addrs) > 0 { + // Trim off the trailing period if there is one (golang doesn't like this) + realAddr := addrs[0].Target + if realAddr[len(realAddr)-1:] == "." { + realAddr = realAddr[0 : len(realAddr)-1] } - - // Step 3d: use the delegated host as-is - logrus.Debug("Using .well-known as-is for ", wkHost) - url := fmt.Sprintf("https://%s", net.JoinHostPort(wkHost, wkPort)) + url := fmt.Sprintf("https://%s", net.JoinHostPort(realAddr, strconv.Itoa(int(addrs[0].Port)))) server := cachedServer{url, wkHost} apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) - logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; fallback)") + logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; SRV)") return url, wkHost, nil } + + // Step 3d: use the delegated host as-is + logrus.Debug("Using .well-known as-is for ", wkHost) + url := fmt.Sprintf("https://%s", net.JoinHostPort(wkHost, wkPort)) + server := cachedServer{url, wkHost} + apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration) + logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; fallback)") + return url, wkHost, nil } } } diff --git a/util/files.go b/util/LEGACY_files.go similarity index 100% rename from util/files.go rename to util/LEGACY_files.go