Skip to content

Commit

Permalink
feat: advertise the low-bandwidth proxy in the /versions response
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorunome committed Oct 11, 2021
1 parent ff87b17 commit 4818998
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -165,6 +167,25 @@ func writeResponse(cfg *Config, res *http.Response, w http.ResponseWriter) []byt
}
}
}
if res.Request.URL.Path == "/_matrix/client/versions" {
dtlsPortParts := strings.Split(cfg.ListenDTLS, ":")
tuples := []struct {
Key string
Value interface{}
}{
{`org\.matrix\.msc3079\.low_bandwidth.dtls`, dtlsPortParts[len(dtlsPortParts) - 1]},
{`org\.matrix\.msc3079\.low_bandwidth.cbor_enum_version`, 1},
{`org\.matrix\.msc3079\.low_bandwidth.coap_enum_version`, 1},
}
for _, t := range tuples {
jsonBody2, err := sjson.SetBytes(jsonBody, t.Key, t.Value)
if err != nil {
logrus.WithError(err).Error("failed to advertise low-bandwidth support")
} else {
jsonBody = jsonBody2
}
}
}
if len(jsonBody) > 0 {
resBody, err = cfg.CBORCodec.JSONToCBOR(bytes.NewBuffer(jsonBody))
if err != nil {
Expand Down Expand Up @@ -291,6 +312,39 @@ func RunProxyServer(cfg *Config) error {
logrus.Infof("TCP proxy %v", req.URL.String())
req.Host = localURL.Host
},
ModifyResponse: func(res *http.Response) error {
if res.Request.URL.Path == "/_matrix/client/versions" {
logrus.Info("Got versions response....")
jsonBody, err := ioutil.ReadAll(res.Body)
if err != nil {
logrus.WithError(err).Error("failed to read the remote response body")
return nil
}
dtlsPortParts := strings.Split(cfg.ListenDTLS, ":")
tuples := []struct {
Key string
Value interface{}
}{
{`org\.matrix\.msc3079\.low_bandwidth.dtls`, dtlsPortParts[len(dtlsPortParts) - 1]},
{`org\.matrix\.msc3079\.low_bandwidth.cbor_enum_version`, 1},
{`org\.matrix\.msc3079\.low_bandwidth.coap_enum_version`, 1},
}
for _, t := range tuples {
jsonBody2, err := sjson.SetBytes(jsonBody, t.Key, t.Value)
if err != nil {
logrus.WithError(err).Error("failed to advertise low-bandwidth support")
} else {
jsonBody = jsonBody2
}
}
buf := bytes.NewBufferString("")
buf.Write(jsonBody)
res.Body = ioutil.NopCloser(buf)
res.Header["Content-Length"] = []string{fmt.Sprint(buf.Len())}
logrus.Info("Done modifying response")
}
return nil
},
}
if cfg.AdvertiseOnHTTPS {
tlsServer := &http.Server{
Expand Down

0 comments on commit 4818998

Please sign in to comment.