Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: advertise the low-bandwidth proxy in the /versions response #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions cmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -165,6 +168,31 @@ func writeResponse(cfg *Config, res *http.Response, w http.ResponseWriter) []byt
}
}
}
if res.Request.URL.Path == "/_matrix/client/versions" {
dtlsPortParts := strings.Split(cfg.ListenDTLS, ":")
dtlsPortInt, err := strconv.Atoi(dtlsPortParts[len(dtlsPortParts) - 1])
if err != nil {
logrus.WithError(err).Error("failed to parse port")
} else {
tuples := []struct {
Key string
Value interface{}
}{
{`org\.matrix\.msc3079\.low_bandwidth.dtls`, dtlsPortInt},
{`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
}
}
logrus.Info("Modified versions response")
}
}
if len(jsonBody) > 0 {
resBody, err = cfg.CBORCodec.JSONToCBOR(bytes.NewBuffer(jsonBody))
if err != nil {
Expand Down Expand Up @@ -291,6 +319,43 @@ 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" {
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, ":")
dtlsPortInt, err := strconv.Atoi(dtlsPortParts[len(dtlsPortParts) - 1])
if err != nil {
logrus.WithError(err).Error("failed to parse port")
} else {
tuples := []struct {
Key string
Value interface{}
}{
{`org\.matrix\.msc3079\.low_bandwidth.dtls`, dtlsPortInt},
{`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("Modified versions response")
}
}
return nil
},
}
if cfg.AdvertiseOnHTTPS {
tlsServer := &http.Server{
Expand Down