diff --git a/appservice/appservice.go b/appservice/appservice.go index 258dca92..d352c2d5 100644 --- a/appservice/appservice.go +++ b/appservice/appservice.go @@ -12,13 +12,16 @@ import ( "html/template" "io/ioutil" "net/http" + "net/http/cookiejar" "os" "path/filepath" "strings" "sync" + "time" "github.com/gorilla/mux" "github.com/gorilla/websocket" + "golang.org/x/net/publicsuffix" "gopkg.in/yaml.v2" "maunium.net/go/maulogger/v2" @@ -33,10 +36,12 @@ var EventChannelSize = 64 // Create a blank appservice instance. func Create() *AppService { + jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) return &AppService{ LogConfig: CreateLogConfig(), clients: make(map[id.UserID]*mautrix.Client), intents: make(map[id.UserID]*IntentAPI), + HTTPClient: &http.Client{Timeout: 180 * time.Second, Jar: jar}, StateStore: NewBasicStateStore(), Router: mux.NewRouter(), UserAgent: mautrix.DefaultUserAgent, @@ -93,11 +98,12 @@ type AppService struct { QueryHandler QueryHandler `yaml:"-"` StateStore StateStore `yaml:"-"` - Router *mux.Router `yaml:"-"` - UserAgent string `yaml:"-"` - server *http.Server - botClient *mautrix.Client - botIntent *IntentAPI + Router *mux.Router `yaml:"-"` + UserAgent string `yaml:"-"` + server *http.Server + HTTPClient *http.Client + botClient *mautrix.Client + botIntent *IntentAPI clients map[id.UserID]*mautrix.Client clientsLock sync.RWMutex @@ -180,8 +186,7 @@ func (as *AppService) Intent(userID id.UserID) *IntentAPI { func (as *AppService) BotIntent() *IntentAPI { if as.botIntent == nil { - as.botIntent = as.NewIntentAPI(as.Registration.SenderLocalpart) - as.botIntent.Logger = as.Log.Sub(string(as.botIntent.UserID)) + as.botIntent = as.makeIntent(as.BotMXID()) } return as.botIntent } @@ -205,6 +210,7 @@ func (as *AppService) makeClient(userID id.UserID) *mautrix.Client { client.Store = nil client.AppServiceUserID = userID client.Logger = as.Log.Sub(string(userID)) + client.Client = as.HTTPClient as.clients[userID] = client return client } @@ -221,17 +227,8 @@ func (as *AppService) Client(userID id.UserID) *mautrix.Client { func (as *AppService) BotClient() *mautrix.Client { if as.botClient == nil { - var err error - as.botClient, err = mautrix.NewClient(as.HomeserverURL, as.BotMXID(), as.Registration.AppToken) - if err != nil { - as.Log.Fatalln("Failed to create gomatrix instance:", err) - return nil - } - as.botClient.UserAgent = as.UserAgent - as.botClient.Syncer = nil - as.botClient.Store = nil + as.botClient = as.makeClient(as.BotMXID()) as.botClient.Logger = as.Log.Sub("Bot") - as.botClient.AppServiceUserID = as.BotMXID() } return as.botClient } diff --git a/go.mod b/go.mod index 07ab8955..8caad13e 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/tidwall/gjson v1.6.8 github.com/tidwall/sjson v1.1.5 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 - golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d + golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 gopkg.in/yaml.v2 v2.3.0 maunium.net/go/maulogger/v2 v2.2.4 ) diff --git a/go.sum b/go.sum index c25c9411..b4822e9a 100644 --- a/go.sum +++ b/go.sum @@ -65,6 +65,8 @@ golang.org/x/net v0.0.0-20201026091529-146b70c837a4 h1:awiuzyrRjJDb+OXi9ceHO3SDx golang.org/x/net v0.0.0-20201026091529-146b70c837a4/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d h1:1aflnvSoWWLI2k/dMUAl5lvU1YO4Mb4hz0gh+1rjcxU= golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -73,6 +75,7 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/version.go b/version.go index 7e45667a..946c4f22 100644 --- a/version.go +++ b/version.go @@ -1,5 +1,5 @@ package mautrix -const Version = "v0.9.3" +const Version = "v0.9.4" var DefaultUserAgent = "mautrix-go/" + Version