mediaproxy: add basic config structs

This commit is contained in:
Tulir Asokan 2024-06-19 11:46:03 +03:00
commit 3e302fb46f
3 changed files with 37 additions and 12 deletions

View file

@ -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 {

View file

@ -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()

View file

@ -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
}