2018-05-24 07:54:42 -05:00
|
|
|
// Copyright 2018 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package appservice
|
|
|
|
|
|
|
|
import (
|
2018-07-17 11:31:40 -05:00
|
|
|
"context"
|
2021-03-05 10:40:32 -06:00
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2018-07-05 11:34:59 -05:00
|
|
|
"sync"
|
2021-03-05 10:40:32 -06:00
|
|
|
"time"
|
2018-07-05 11:34:59 -05:00
|
|
|
|
2020-06-08 09:51:07 -05:00
|
|
|
"github.com/gorilla/mux"
|
2018-07-17 09:36:04 -05:00
|
|
|
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
2018-05-24 07:54:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/appservice/consumers"
|
2020-06-04 09:43:07 -05:00
|
|
|
"github.com/matrix-org/dendrite/appservice/inthttp"
|
2018-07-17 09:36:04 -05:00
|
|
|
"github.com/matrix-org/dendrite/appservice/query"
|
2018-07-05 11:34:59 -05:00
|
|
|
"github.com/matrix-org/dendrite/appservice/storage"
|
|
|
|
"github.com/matrix-org/dendrite/appservice/types"
|
|
|
|
"github.com/matrix-org/dendrite/appservice/workers"
|
2018-07-17 09:36:04 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2021-11-24 04:45:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2021-07-14 07:34:42 -05:00
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2020-06-16 11:39:56 -05:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2018-05-24 07:54:42 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-06-08 09:51:07 -05:00
|
|
|
// AddInternalRoutes registers HTTP handlers for internal API calls
|
|
|
|
func AddInternalRoutes(router *mux.Router, queryAPI appserviceAPI.AppServiceQueryAPI) {
|
|
|
|
inthttp.AddRoutes(queryAPI, router)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInternalAPI returns a concerete implementation of the internal API. Callers
|
|
|
|
// can call functions directly on the returned API or via an HTTP interface using AddInternalRoutes.
|
|
|
|
func NewInternalAPI(
|
2021-11-24 04:45:23 -06:00
|
|
|
base *base.BaseDendrite,
|
2020-06-16 11:39:56 -05:00
|
|
|
userAPI userapi.UserInternalAPI,
|
2020-05-01 04:48:17 -05:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI,
|
2018-07-17 09:36:04 -05:00
|
|
|
) appserviceAPI.AppServiceQueryAPI {
|
2021-03-05 10:40:32 -06:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 30,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: base.Cfg.AppServiceAPI.DisableTLSValidation,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-11-03 09:28:40 -05:00
|
|
|
js, _, _ := jetstream.Prepare(&base.Cfg.Global.JetStream)
|
2020-10-15 07:27:13 -05:00
|
|
|
|
2018-07-05 11:34:59 -05:00
|
|
|
// Create a connection to the appservice postgres DB
|
2020-08-10 08:18:04 -05:00
|
|
|
appserviceDB, err := storage.NewDatabase(&base.Cfg.AppServiceAPI.Database)
|
2018-07-05 11:34:59 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to connect to appservice db")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap application services in a type that relates the application service and
|
|
|
|
// a sync.Cond object that can be used to notify workers when there are new
|
|
|
|
// events to be sent out.
|
|
|
|
workerStates := make([]types.ApplicationServiceWorkerState, len(base.Cfg.Derived.ApplicationServices))
|
|
|
|
for i, appservice := range base.Cfg.Derived.ApplicationServices {
|
|
|
|
m := sync.Mutex{}
|
|
|
|
ws := types.ApplicationServiceWorkerState{
|
|
|
|
AppService: appservice,
|
|
|
|
Cond: sync.NewCond(&m),
|
|
|
|
}
|
|
|
|
workerStates[i] = ws
|
2018-07-17 11:31:40 -05:00
|
|
|
|
|
|
|
// Create bot account for this AS if it doesn't already exist
|
2020-06-16 11:39:56 -05:00
|
|
|
if err = generateAppServiceAccount(userAPI, appservice); err != nil {
|
2018-07-17 11:31:40 -05:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"appservice": appservice.ID,
|
|
|
|
}).WithError(err).Panicf("failed to generate bot account for appservice")
|
|
|
|
}
|
2018-07-05 11:34:59 -05:00
|
|
|
}
|
|
|
|
|
2018-08-08 10:17:09 -05:00
|
|
|
// Create appserivce query API with an HTTP client that will be used for all
|
|
|
|
// outbound and inbound requests (inbound only for the internal API)
|
2020-06-04 09:43:07 -05:00
|
|
|
appserviceQueryAPI := &query.AppServiceQueryAPI{
|
2021-03-05 04:40:27 -06:00
|
|
|
HTTPClient: client,
|
|
|
|
Cfg: base.Cfg,
|
2018-07-17 09:36:04 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 09:11:33 -05:00
|
|
|
// Only consume if we actually have ASes to track, else we'll just chew cycles needlessly.
|
|
|
|
// We can't add ASes at runtime so this is safe to do.
|
|
|
|
if len(workerStates) > 0 {
|
|
|
|
consumer := consumers.NewOutputRoomEventConsumer(
|
2021-11-03 09:28:40 -05:00
|
|
|
base.ProcessContext, base.Cfg, js, appserviceDB,
|
2020-06-12 09:11:33 -05:00
|
|
|
rsAPI, workerStates,
|
|
|
|
)
|
|
|
|
if err := consumer.Start(); err != nil {
|
|
|
|
logrus.WithError(err).Panicf("failed to start appservice roomserver consumer")
|
|
|
|
}
|
2018-05-24 07:54:42 -05:00
|
|
|
}
|
|
|
|
|
2018-07-05 11:34:59 -05:00
|
|
|
// Create application service transaction workers
|
2021-03-05 04:40:27 -06:00
|
|
|
if err := workers.SetupTransactionWorkers(client, appserviceDB, workerStates); err != nil {
|
2018-07-05 11:34:59 -05:00
|
|
|
logrus.WithError(err).Panicf("failed to start app service transaction workers")
|
|
|
|
}
|
2020-06-04 09:43:07 -05:00
|
|
|
return appserviceQueryAPI
|
2018-05-24 07:54:42 -05:00
|
|
|
}
|
2018-07-17 11:31:40 -05:00
|
|
|
|
|
|
|
// generateAppServiceAccounts creates a dummy account based off the
|
|
|
|
// `sender_localpart` field of each application service if it doesn't
|
|
|
|
// exist already
|
|
|
|
func generateAppServiceAccount(
|
2020-06-16 11:39:56 -05:00
|
|
|
userAPI userapi.UserInternalAPI,
|
2018-07-17 11:31:40 -05:00
|
|
|
as config.ApplicationService,
|
|
|
|
) error {
|
2020-06-16 11:39:56 -05:00
|
|
|
var accRes userapi.PerformAccountCreationResponse
|
|
|
|
err := userAPI.PerformAccountCreation(context.Background(), &userapi.PerformAccountCreationRequest{
|
2020-06-17 05:22:26 -05:00
|
|
|
AccountType: userapi.AccountTypeUser,
|
2020-06-16 11:39:56 -05:00
|
|
|
Localpart: as.SenderLocalpart,
|
|
|
|
AppServiceID: as.ID,
|
|
|
|
OnConflict: userapi.ConflictUpdate,
|
|
|
|
}, &accRes)
|
2018-07-17 11:31:40 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-16 11:39:56 -05:00
|
|
|
var devRes userapi.PerformDeviceCreationResponse
|
|
|
|
err = userAPI.PerformDeviceCreation(context.Background(), &userapi.PerformDeviceCreationRequest{
|
|
|
|
Localpart: as.SenderLocalpart,
|
|
|
|
AccessToken: as.ASToken,
|
|
|
|
DeviceID: &as.SenderLocalpart,
|
|
|
|
DeviceDisplayName: &as.SenderLocalpart,
|
|
|
|
}, &devRes)
|
2018-07-17 11:31:40 -05:00
|
|
|
return err
|
|
|
|
}
|