app service unix socket support (#3022)

This is the last part of unix socket support to talk to app servers, go
based app services already support unix sockets:

5a68173fe3
```
appservice:
  # The address that the homeserver can use to connect to this appservice.
  address: unix:///var/snap/matrix/current/whatsapp.socket

  # The hostname and port where this appservice should listen.
  hostname: /var/snap/matrix/current/whatsapp.socket
  port: 0
```

### Pull Request Checklist

<!-- Please read
https://matrix-org.github.io/dendrite/development/contributing before
submitting your pull request -->

* [x] I have added Go unit tests or [Complement integration
tests](https://github.com/matrix-org/complement) for this PR _or_ I have
justified why this PR doesn't need tests
* [x] Pull request includes a [sign off below using a legally
identifiable
name](https://matrix-org.github.io/dendrite/development/contributing#sign-off)
_or_ I have already signed off privately

Signed-off-by: `Boris Rybalkin <ribalkin@gmail.com>`
This commit is contained in:
Boris Rybalkin 2023-04-04 08:42:46 +01:00 committed by GitHub
parent 682a7d0a66
commit 985298cfc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 189 additions and 61 deletions

View file

@ -15,16 +15,23 @@
package config
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const UnixSocketPrefix = "unix://"
type AppServiceAPI struct {
Matrix *Global `yaml:"-"`
Derived *Derived `yaml:"-"` // TODO: Nuke Derived from orbit
@ -80,7 +87,41 @@ type ApplicationService struct {
// Whether rate limiting is applied to each application service user
RateLimited bool `yaml:"rate_limited"`
// Any custom protocols that this application service provides (e.g. IRC)
Protocols []string `yaml:"protocols"`
Protocols []string `yaml:"protocols"`
HTTPClient *http.Client
isUnixSocket bool
unixSocket string
}
func (a *ApplicationService) CreateHTTPClient(insecureSkipVerify bool) {
client := &http.Client{
Timeout: time.Second * 30,
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
},
Proxy: http.ProxyFromEnvironment,
},
}
if strings.HasPrefix(a.URL, UnixSocketPrefix) {
a.isUnixSocket = true
a.unixSocket = "http://unix"
client.Transport = &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", strings.TrimPrefix(a.URL, UnixSocketPrefix))
},
}
}
a.HTTPClient = client
}
func (a *ApplicationService) RequestUrl() string {
if a.isUnixSocket {
return a.unixSocket
} else {
return a.URL
}
}
// IsInterestedInRoomID returns a bool on whether an application service's
@ -152,7 +193,7 @@ func (a *ApplicationService) IsInterestedInRoomAlias(
func loadAppServices(config *AppServiceAPI, derived *Derived) error {
for _, configPath := range config.ConfigFiles {
// Create a new application service with default options
appservice := ApplicationService{
appservice := &ApplicationService{
RateLimited: true,
}
@ -169,13 +210,13 @@ func loadAppServices(config *AppServiceAPI, derived *Derived) error {
}
// Load the config data into our struct
if err = yaml.Unmarshal(configData, &appservice); err != nil {
if err = yaml.Unmarshal(configData, appservice); err != nil {
return err
}
appservice.CreateHTTPClient(config.DisableTLSValidation)
// Append the parsed application service to the global config
derived.ApplicationServices = append(
derived.ApplicationServices, appservice,
derived.ApplicationServices, *appservice,
)
}