diff --git a/bridgev2/bridgeconfig/config.go b/bridgev2/bridgeconfig/config.go index 448c2f89..d7ef575b 100644 --- a/bridgev2/bridgeconfig/config.go +++ b/bridgev2/bridgeconfig/config.go @@ -10,6 +10,8 @@ import ( "go.mau.fi/util/dbutil" "go.mau.fi/zeroconfig" "gopkg.in/yaml.v3" + + "maunium.net/go/mautrix/mediaproxy" ) type Config struct { @@ -48,11 +50,8 @@ type ProvisioningConfig struct { } type DirectMediaConfig struct { - Enabled bool `yaml:"enabled"` - AllowProxy bool `yaml:"allow_proxy"` - ServerName string `yaml:"server_name"` - WellKnownResponse string `yaml:"well_known_response"` - ServerKey string `yaml:"server_key"` + Enabled bool `yaml:"enabled"` + mediaproxy.BasicConfig `yaml:",inline"` } type DoublePuppetConfig struct { diff --git a/bridgev2/matrix/directmedia.go b/bridgev2/matrix/directmedia.go index e4090ea0..58b461bb 100644 --- a/bridgev2/matrix/directmedia.go +++ b/bridgev2/matrix/directmedia.go @@ -35,16 +35,10 @@ func (br *Connector) initDirectMedia() error { return fmt.Errorf("direct media is enabled in config, but the network connector does not support it") } var err error - br.MediaProxy, err = mediaproxy.New(br.Config.DirectMedia.ServerName, br.Config.DirectMedia.ServerKey, br.getDirectMedia) + br.MediaProxy, err = mediaproxy.NewFromConfig(br.Config.DirectMedia.BasicConfig, br.getDirectMedia) if err != nil { return fmt.Errorf("failed to initialize media proxy: %w", err) } - if br.Config.DirectMedia.WellKnownResponse != "" { - br.MediaProxy.KeyServer.WellKnownTarget = br.Config.DirectMedia.WellKnownResponse - } - if !br.Config.DirectMedia.AllowProxy { - br.MediaProxy.DisallowProxying() - } br.MediaProxy.RegisterRoutes(br.AS.Router) br.dmaSigKey = sha256.Sum256(br.MediaProxy.GetServerKey().Priv.Seed()) dmn.SetUseDirectMedia() diff --git a/mediaproxy/mediaproxy.go b/mediaproxy/mediaproxy.go index 702910fa..f56bad5b 100644 --- a/mediaproxy/mediaproxy.go +++ b/mediaproxy/mediaproxy.go @@ -94,6 +94,38 @@ func New(serverName string, serverKey string, getMedia GetMediaFunc) (*MediaProx }, nil } +type BasicConfig struct { + ServerName string `yaml:"server_name" json:"server_name"` + ServerKey string `yaml:"server_key" json:"server_key"` + AllowProxy bool `yaml:"allow_proxy" json:"allow_proxy"` + WellKnownResponse string `yaml:"well_known_response" json:"well_known_response"` +} + +func NewFromConfig(cfg BasicConfig, getMedia GetMediaFunc) (*MediaProxy, error) { + mp, err := New(cfg.ServerName, cfg.ServerKey, getMedia) + if err != nil { + return nil, err + } + if !cfg.AllowProxy { + mp.DisallowProxying() + } + if cfg.WellKnownResponse != "" { + mp.KeyServer.WellKnownTarget = cfg.WellKnownResponse + } + return mp, nil +} + +type ServerConfig struct { + Hostname string `yaml:"hostname" json:"hostname"` + Port uint16 `yaml:"port" json:"port"` +} + +func (mp *MediaProxy) Listen(cfg ServerConfig) error { + router := mux.NewRouter() + mp.RegisterRoutes(router) + return http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Hostname, cfg.Port), router) +} + func (mp *MediaProxy) GetServerName() string { return mp.serverName }