You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.2 KiB

  1. // maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
  2. // Copyright (C) 2024 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "context"
  19. "flag"
  20. "fmt"
  21. "os"
  22. "regexp"
  23. "time"
  24. "go.mau.fi/util/exerrors"
  25. "gopkg.in/yaml.v3"
  26. "maunium.net/go/mautrix/federation"
  27. "maunium.net/go/mautrix/mediaproxy"
  28. )
  29. type Config struct {
  30. mediaproxy.BasicConfig `yaml:",inline"`
  31. mediaproxy.ServerConfig `yaml:",inline"`
  32. }
  33. var configPath = flag.String("config", "config.yaml", "config file path")
  34. var generateServerKey = flag.Bool("generate-key", false, "generate a new server key and exit")
  35. var giphyIDRegex = regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
  36. func main() {
  37. flag.Parse()
  38. if *generateServerKey {
  39. fmt.Println(federation.GenerateSigningKey().SynapseString())
  40. } else {
  41. cfgFile := exerrors.Must(os.ReadFile(*configPath))
  42. var cfg Config
  43. exerrors.PanicIfNotNil(yaml.Unmarshal(cfgFile, &cfg))
  44. mp := exerrors.Must(mediaproxy.NewFromConfig(cfg.BasicConfig, getMedia))
  45. mp.KeyServer.Version.Name = "maunium-stickerpicker giphy proxy"
  46. mp.ForceProxyLegacyFederation = true
  47. exerrors.PanicIfNotNil(mp.Listen(cfg.ServerConfig))
  48. }
  49. }
  50. func getMedia(_ context.Context, id string) (response mediaproxy.GetMediaResponse, err error) {
  51. // This is not related to giphy, but random cats are always fun
  52. if id == "cat" {
  53. return &mediaproxy.GetMediaResponseURL{
  54. URL: "https://cataas.com/cat",
  55. ExpiresAt: time.Now(),
  56. }, nil
  57. }
  58. if !giphyIDRegex.MatchString(id) {
  59. return nil, mediaproxy.ErrInvalidMediaIDSyntax
  60. }
  61. return &mediaproxy.GetMediaResponseURL{
  62. URL: fmt.Sprintf("https://i.giphy.com/%s.webp", id),
  63. }, nil
  64. }