remove hard-coded hostnames

This commit is contained in:
Rob Watson 2021-02-24 18:20:32 +01:00
parent 60dbb8c5b2
commit 09d4d49fcb
4 changed files with 19 additions and 11 deletions

View File

@ -3,7 +3,7 @@
## Usage ## 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 ## Licence

View File

@ -37,17 +37,17 @@ func (h *handler) handleTransactions(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{}")) 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} http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureTLS}
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
h := handler{ h := handler{
logger: logger, logger: logger,
ep: matrix.NewProcessor(accessToken, logger), ep: matrix.NewProcessor(baseURL, accessToken, logger),
} }
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/transactions/", h.handleTransactions) mux.HandleFunc("/transactions/", h.handleTransactions)
return http.ListenAndServe(addr, loggerMiddleware(logger, mux)) return http.ListenAndServe(listenAddr, loggerMiddleware(logger, mux))
} }

View File

@ -18,14 +18,16 @@ type EventProcessor interface {
} }
type processor struct { type processor struct {
baseURL string
accessToken string accessToken string
httpclient *http.Client httpclient *http.Client
processed map[string]bool processed map[string]bool
logger zerolog.Logger logger zerolog.Logger
} }
func NewProcessor(accessToken string, logger zerolog.Logger) EventProcessor { func NewProcessor(baseURL, accessToken string, logger zerolog.Logger) EventProcessor {
return &processor{ return &processor{
baseURL: baseURL,
accessToken: accessToken, accessToken: accessToken,
httpclient: &http.Client{}, httpclient: &http.Client{},
processed: make(map[string]bool), processed: make(map[string]bool),
@ -76,7 +78,7 @@ func (p *processor) ProcessEvents(events []*RawEvent) error {
} }
func (p *processor) acceptInvite(e *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("{}"))) req, _ := p.buildRequest("POST", url, bytes.NewReader([]byte("{}")))
resp, err := p.httpclient.Do(req) resp, err := p.httpclient.Do(req)
if err != nil { 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) 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)) req, _ := p.buildRequest("POST", url, bytes.NewReader(encodedBody))
resp, err := p.httpclient.Do(req) resp, err := p.httpclient.Do(req)
if err != nil { if err != nil {
@ -126,11 +128,12 @@ func (p *processor) handleErrorResponse(e *RawEvent, resp *http.Response) error
return nil return nil
} }
p.logger.Error().Str("errcode", er.Code).Str("error", er.Error).Msg("error sending message") 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) { func (p *processor) buildRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(url, method, body) p.logger.Debug().Str("method", method).Str("url", url).Msg("build HTTP req")
req, err := http.NewRequest(method, url, body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,7 +18,12 @@ func main() {
log.Fatal("missing LISTEN_ADDR") log.Fatal("missing LISTEN_ADDR")
} }
baseURL := os.Getenv("BASE_URL")
if baseURL == "" {
log.Fatal("missing BASE_URL")
}
insecureTLS := os.Getenv("INSECURE_TLS") == "yes" insecureTLS := os.Getenv("INSECURE_TLS") == "yes"
log.Fatal(http.ListenAndServe(listenAddr, accessToken, insecureTLS)) log.Fatal(http.ListenAndServe(listenAddr, baseURL, accessToken, insecureTLS))
} }