Skip to content

Commit

Permalink
Hardcode hungryserv URL
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 5, 2024
1 parent ff2d0b3 commit 759727b
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ used with the `--type` flag.
received events to the bridge via HTTP. Since the HTTP requests are all on
localhost, you don't need port forwarding or TLS certificates.

Note that the homeserver URL may change if you're moved to a different cluster.
In general, that shouldn't happen, but it's not impossible.
Note that the homeserver URL is not guaranteed to be stable forever, it has
changed in the past, and it may change again in the future.

You can use `--json` with `register` to get the whole response as JSON instead
of registration YAML and pretty-printed extra details. This may be useful if
Expand Down
10 changes: 8 additions & 2 deletions api/hungryapi/appservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hungryapi
import (
"context"
"net/http"
"net/url"
"time"

"go.mau.fi/util/jsontime"
Expand All @@ -16,8 +17,13 @@ type Client struct {
Username string
}

func NewClient(baseDomain, homeserverURL, username, accessToken string) *Client {
client, err := mautrix.NewClient(homeserverURL, id.NewUserID(username, baseDomain), accessToken)
func NewClient(baseDomain, username, accessToken string) *Client {
hungryURL := url.URL{
Scheme: "https",
Host: "matrix." + baseDomain,
Path: "/_hungryserv/" + username,
}
client, err := mautrix.NewClient(hungryURL.String(), id.NewUserID(username, baseDomain), accessToken)
if err != nil {
panic(err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/bbctl/authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type EnvConfig struct {
ClusterID string `json:"cluster_id"`
Username string `json:"username"`
AccessToken string `json:"access_token"`
HungryAddress string `json:"hungry_address"`
BridgeDataDir string `json:"bridge_data_dir"`
DatabaseDir string `json:"database_dir,omitempty"`
}
Expand Down
15 changes: 0 additions & 15 deletions cmd/bbctl/context.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package main

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/urfave/cli/v2"
"maunium.net/go/mautrix"

Expand Down Expand Up @@ -43,14 +39,3 @@ func GetHungryClient(ctx *cli.Context) *hungryapi.Client {
}
return val.(*hungryapi.Client)
}

func SaveHungryURL(ctx *cli.Context, newURL string) {
env := GetEnvConfig(ctx)
if env.HungryAddress != newURL && newURL != "" {
env.HungryAddress = newURL
err := GetConfig(ctx).Save()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, color.RedString("Failed to save config: "+err.Error()))
}
}
}
1 change: 0 additions & 1 deletion cmd/bbctl/login-email.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func doMatrixLogin(ctx *cli.Context, req *mautrix.ReqLogin, whoami *beeperapi.Re
envCfg := GetEnvConfig(ctx)
envCfg.ClusterID = whoami.UserInfo.BridgeClusterID
envCfg.Username = whoami.UserInfo.Username
envCfg.HungryAddress = whoami.UserInfo.HungryURL
envCfg.AccessToken = resp.AccessToken
envCfg.BridgeDataDir = filepath.Join(UserDataDir, "bbctl", ctx.String("env"))
err = cfg.Save()
Expand Down
6 changes: 2 additions & 4 deletions cmd/bbctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path"
"strings"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -83,16 +82,15 @@ func prepareApp(ctx *cli.Context) error {
ctx.Context = context.WithValue(ctx.Context, contextKeyConfig, cfg)
ctx.Context = context.WithValue(ctx.Context, contextKeyEnvConfig, envConfig)
if envConfig.HasCredentials() {
if envConfig.HungryAddress == "" || envConfig.ClusterID == "" || envConfig.Username == "" || !strings.Contains(envConfig.HungryAddress, "/_hungryserv") {
if envConfig.Username == "" {
log.Printf("Fetching whoami to fill missing env config details")
_, err = getCachedWhoami(ctx)
if err != nil {
return fmt.Errorf("failed to get whoami: %w", err)
}
}
homeserver := ctx.String("homeserver")
ctx.Context = context.WithValue(ctx.Context, contextKeyMatrixClient, NewMatrixAPI(homeserver, envConfig.Username, envConfig.AccessToken))
ctx.Context = context.WithValue(ctx.Context, contextKeyHungryClient, hungryapi.NewClient(homeserver, envConfig.HungryAddress, envConfig.Username, envConfig.AccessToken))
ctx.Context = context.WithValue(ctx.Context, contextKeyHungryClient, hungryapi.NewClient(homeserver, envConfig.Username, envConfig.AccessToken))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/bbctl/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func prepareAppserviceWebsocketProxy(ctx *cli.Context, as *appservice.AppService
}
return proxyWebsocketRequest(parsedURL, cmd)
})
_ = as.SetHomeserverURL(GetEnvConfig(ctx).HungryAddress)
_ = as.SetHomeserverURL(GetHungryClient(ctx).HomeserverURL.String())
}

type wsPingData struct {
Expand Down
10 changes: 0 additions & 10 deletions cmd/bbctl/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -172,15 +171,6 @@ func getCachedWhoami(ctx *cli.Context) (*beeperapi.RespWhoami, error) {
ec.ClusterID = resp.UserInfo.BridgeClusterID
changed = true
}
publicHungryURL := url.URL{
Scheme: "https",
Host: "matrix." + ctx.String("homeserver"),
Path: "/_hungryserv/" + resp.UserInfo.Username,
}
if ec.HungryAddress != publicHungryURL.String() {
ec.HungryAddress = publicHungryURL.String()
changed = true
}
if changed {
err = GetConfig(ctx).Save()
if err != nil {
Expand Down

0 comments on commit 759727b

Please sign in to comment.