From 09d4d49fcbbb08926105d358949d5d409cb00f63 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Wed, 24 Feb 2021 18:20:32 +0100 Subject: [PATCH] remove hard-coded hostnames --- README.md | 2 +- internal/http/http.go | 6 +++--- internal/matrix/events.go | 15 +++++++++------ main.go | 7 ++++++- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 65c9ead..f1da9e5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Usage ``` -INSECURE_TLS=no LISTEN_ADDR=0.0.0.0:8888 ACCESS_TOKEN=hackme go run . +INSECURE_TLS=no BASE_URL=https://matrix.server.com LISTEN_ADDR=0.0.0.0:8888 ACCESS_TOKEN=hackme go run . ``` ## Licence diff --git a/internal/http/http.go b/internal/http/http.go index 05a2f3c..10f0bc5 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -37,17 +37,17 @@ func (h *handler) handleTransactions(w http.ResponseWriter, r *http.Request) { w.Write([]byte("{}")) } -func ListenAndServe(addr, accessToken string, insecureTLS bool) error { +func ListenAndServe(listenAddr, baseURL, accessToken string, insecureTLS bool) error { http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureTLS} logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) h := handler{ logger: logger, - ep: matrix.NewProcessor(accessToken, logger), + ep: matrix.NewProcessor(baseURL, accessToken, logger), } mux := http.NewServeMux() mux.HandleFunc("/transactions/", h.handleTransactions) - return http.ListenAndServe(addr, loggerMiddleware(logger, mux)) + return http.ListenAndServe(listenAddr, loggerMiddleware(logger, mux)) } diff --git a/internal/matrix/events.go b/internal/matrix/events.go index 0489ea8..86e2ff9 100644 --- a/internal/matrix/events.go +++ b/internal/matrix/events.go @@ -18,14 +18,16 @@ type EventProcessor interface { } type processor struct { + baseURL string accessToken string httpclient *http.Client processed map[string]bool logger zerolog.Logger } -func NewProcessor(accessToken string, logger zerolog.Logger) EventProcessor { +func NewProcessor(baseURL, accessToken string, logger zerolog.Logger) EventProcessor { return &processor{ + baseURL: baseURL, accessToken: accessToken, httpclient: &http.Client{}, processed: make(map[string]bool), @@ -76,7 +78,7 @@ func (p *processor) ProcessEvents(events []*RawEvent) error { } func (p *processor) acceptInvite(e *RawEvent) error { - url := fmt.Sprintf("https://synapse.local/_matrix/client/r0/rooms/%s/join", e.RoomId) + url := fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/join", p.baseURL, e.RoomId) req, _ := p.buildRequest("POST", url, bytes.NewReader([]byte("{}"))) resp, err := p.httpclient.Do(req) if err != nil { @@ -101,7 +103,7 @@ func (p *processor) handleVerb(e *RawEvent, cmd, arg string) error { return fmt.Errorf("error encoding message: %v", err) } - url := fmt.Sprintf("https://synapse.local/_matrix/client/r0/rooms/%s/send/m.room.message", e.RoomId) + url := fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", p.baseURL, e.RoomId) req, _ := p.buildRequest("POST", url, bytes.NewReader(encodedBody)) resp, err := p.httpclient.Do(req) if err != nil { @@ -126,11 +128,12 @@ func (p *processor) handleErrorResponse(e *RawEvent, resp *http.Response) error return nil } p.logger.Error().Str("errcode", er.Code).Str("error", er.Error).Msg("error sending message") - return fmt.Errorf("synapse error: %v", er.Error) + return fmt.Errorf("server error: %v", er.Error) } -func (p *processor) buildRequest(url, method string, body io.Reader) (*http.Request, error) { - req, err := http.NewRequest(url, method, body) +func (p *processor) buildRequest(method, url string, body io.Reader) (*http.Request, error) { + p.logger.Debug().Str("method", method).Str("url", url).Msg("build HTTP req") + req, err := http.NewRequest(method, url, body) if err != nil { return nil, err } diff --git a/main.go b/main.go index 0e1de08..310b18d 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,12 @@ func main() { log.Fatal("missing LISTEN_ADDR") } + baseURL := os.Getenv("BASE_URL") + if baseURL == "" { + log.Fatal("missing BASE_URL") + } + insecureTLS := os.Getenv("INSECURE_TLS") == "yes" - log.Fatal(http.ListenAndServe(listenAddr, accessToken, insecureTLS)) + log.Fatal(http.ListenAndServe(listenAddr, baseURL, accessToken, insecureTLS)) }