small refactor (#104)
This commit is contained in:
+1
-35
@@ -11,8 +11,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/livekit/protocol/auth"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
@@ -187,19 +185,12 @@ func startServer(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// require a key provider
|
|
||||||
keyProvider, err := createKeyProvider(conf)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
logger.Infow("configured key provider", "numKeys", keyProvider.NumKeys())
|
|
||||||
|
|
||||||
currentNode, err := routing.NewLocalNode(conf)
|
currentNode, err := routing.NewLocalNode(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
server, err := service.InitializeServer(conf, keyProvider, currentNode)
|
server, err := service.InitializeServer(conf, currentNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -216,31 +207,6 @@ func startServer(c *cli.Context) error {
|
|||||||
return server.Start()
|
return server.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func createKeyProvider(conf *config.Config) (auth.KeyProvider, error) {
|
|
||||||
// prefer keyfile if set
|
|
||||||
if conf.KeyFile != "" {
|
|
||||||
if st, err := os.Stat(conf.KeyFile); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if st.Mode().Perm() != 0600 {
|
|
||||||
return nil, fmt.Errorf("key file must have permission set to 600")
|
|
||||||
}
|
|
||||||
f, err := os.Open(conf.KeyFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
_ = f.Close()
|
|
||||||
}()
|
|
||||||
return auth.NewFileBasedKeyProviderFromReader(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(conf.Keys) == 0 {
|
|
||||||
return nil, errors.New("one of key-file or keys must be provided in order to support a secure installation")
|
|
||||||
}
|
|
||||||
|
|
||||||
return auth.NewFileBasedKeyProviderFromMap(conf.Keys), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getConfigString(c *cli.Context) (string, error) {
|
func getConfigString(c *cli.Context) (string, error) {
|
||||||
configFile := c.String("config")
|
configFile := c.String("config")
|
||||||
configBody := c.String("config-body")
|
configBody := c.String("config-body")
|
||||||
|
|||||||
@@ -143,8 +143,8 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
|
|||||||
EnabledCodecs: []CodecSpec{
|
EnabledCodecs: []CodecSpec{
|
||||||
{Mime: webrtc.MimeTypeOpus},
|
{Mime: webrtc.MimeTypeOpus},
|
||||||
{Mime: webrtc.MimeTypeVP8},
|
{Mime: webrtc.MimeTypeVP8},
|
||||||
//{Mime: webrtc.MimeTypeH264},
|
// {Mime: webrtc.MimeTypeH264},
|
||||||
//{Mime: webrtc.MimeTypeVP9},
|
// {Mime: webrtc.MimeTypeVP9},
|
||||||
},
|
},
|
||||||
EmptyTimeout: 5 * 60,
|
EmptyTimeout: 5 * 60,
|
||||||
},
|
},
|
||||||
|
|||||||
+54
-26
@@ -2,7 +2,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
@@ -21,8 +23,9 @@ var ServiceSet = wire.NewSet(
|
|||||||
createRedisClient,
|
createRedisClient,
|
||||||
createRouter,
|
createRouter,
|
||||||
createStore,
|
createStore,
|
||||||
createWebhookNotifier,
|
CreateKeyProvider,
|
||||||
nodeSelectorFromConfig,
|
CreateWebhookNotifier,
|
||||||
|
CreateNodeSelector,
|
||||||
NewRecordingService,
|
NewRecordingService,
|
||||||
NewRoomService,
|
NewRoomService,
|
||||||
NewRTCService,
|
NewRTCService,
|
||||||
@@ -35,6 +38,55 @@ var ServiceSet = wire.NewSet(
|
|||||||
wire.Bind(new(livekit.RoomService), new(*RoomService)),
|
wire.Bind(new(livekit.RoomService), new(*RoomService)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func CreateKeyProvider(conf *config.Config) (auth.KeyProvider, error) {
|
||||||
|
// prefer keyfile if set
|
||||||
|
if conf.KeyFile != "" {
|
||||||
|
if st, err := os.Stat(conf.KeyFile); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if st.Mode().Perm() != 0600 {
|
||||||
|
return nil, fmt.Errorf("key file must have permission set to 600")
|
||||||
|
}
|
||||||
|
f, err := os.Open(conf.KeyFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = f.Close()
|
||||||
|
}()
|
||||||
|
return auth.NewFileBasedKeyProviderFromReader(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(conf.Keys) == 0 {
|
||||||
|
return nil, errors.New("one of key-file or keys must be provided in order to support a secure installation")
|
||||||
|
}
|
||||||
|
|
||||||
|
return auth.NewFileBasedKeyProviderFromMap(conf.Keys), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateWebhookNotifier(conf *config.Config, provider auth.KeyProvider) (*webhook.Notifier, error) {
|
||||||
|
wc := conf.WebHook
|
||||||
|
if len(wc.URLs) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
secret := provider.GetSecret(wc.APIKey)
|
||||||
|
if secret == "" {
|
||||||
|
return nil, ErrWebHookMissingAPIKey
|
||||||
|
}
|
||||||
|
|
||||||
|
return webhook.NewNotifier(wc.APIKey, secret, wc.URLs), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateNodeSelector(conf *config.Config) routing.NodeSelector {
|
||||||
|
switch conf.NodeSelector.Kind {
|
||||||
|
case "sysload":
|
||||||
|
return &routing.SystemLoadSelector{
|
||||||
|
SysloadLimit: conf.NodeSelector.SysloadLimit,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return &routing.RandomSelector{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createRedisClient(conf *config.Config) (*redis.Client, error) {
|
func createRedisClient(conf *config.Config) (*redis.Client, error) {
|
||||||
if !conf.HasRedis() {
|
if !conf.HasRedis() {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@@ -72,30 +124,6 @@ func createStore(rc *redis.Client) RoomStore {
|
|||||||
return NewLocalRoomStore()
|
return NewLocalRoomStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWebhookNotifier(conf *config.Config, provider auth.KeyProvider) (*webhook.Notifier, error) {
|
|
||||||
wc := conf.WebHook
|
|
||||||
if len(wc.URLs) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
secret := provider.GetSecret(wc.APIKey)
|
|
||||||
if secret == "" {
|
|
||||||
return nil, ErrWebHookMissingAPIKey
|
|
||||||
}
|
|
||||||
|
|
||||||
return webhook.NewNotifier(wc.APIKey, secret, wc.URLs), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func nodeSelectorFromConfig(conf *config.Config) routing.NodeSelector {
|
|
||||||
switch conf.NodeSelector.Kind {
|
|
||||||
case "sysload":
|
|
||||||
return &routing.SystemLoadSelector{
|
|
||||||
SysloadLimit: conf.NodeSelector.SysloadLimit,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return &routing.RandomSelector{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleError(w http.ResponseWriter, status int, msg string) {
|
func handleError(w http.ResponseWriter, status int, msg string) {
|
||||||
// GetLogger already with extra depth 1
|
// GetLogger already with extra depth 1
|
||||||
logger.GetLogger().V(1).Info("error handling request", "error", msg, "status", status)
|
logger.GetLogger().V(1).Info("error handling request", "error", msg, "status", status)
|
||||||
|
|||||||
+1
-3
@@ -5,13 +5,11 @@ package service
|
|||||||
import (
|
import (
|
||||||
"github.com/google/wire"
|
"github.com/google/wire"
|
||||||
|
|
||||||
"github.com/livekit/protocol/auth"
|
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
"github.com/livekit/livekit-server/pkg/routing"
|
"github.com/livekit/livekit-server/pkg/routing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, currentNode routing.LocalNode) (*LivekitServer, error) {
|
func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*LivekitServer, error) {
|
||||||
wire.Build(
|
wire.Build(
|
||||||
ServiceSet,
|
ServiceSet,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -8,20 +8,23 @@ package service
|
|||||||
import (
|
import (
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
"github.com/livekit/livekit-server/pkg/routing"
|
"github.com/livekit/livekit-server/pkg/routing"
|
||||||
"github.com/livekit/protocol/auth"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Injectors from wire.go:
|
// Injectors from wire.go:
|
||||||
|
|
||||||
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, currentNode routing.LocalNode) (*LivekitServer, error) {
|
func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*LivekitServer, error) {
|
||||||
client, err := createRedisClient(conf)
|
client, err := createRedisClient(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
roomStore := createStore(client)
|
roomStore := createStore(client)
|
||||||
router := createRouter(client, currentNode)
|
router := createRouter(client, currentNode)
|
||||||
nodeSelector := nodeSelectorFromConfig(conf)
|
nodeSelector := CreateNodeSelector(conf)
|
||||||
notifier, err := createWebhookNotifier(conf, keyProvider)
|
keyProvider, err := CreateKeyProvider(conf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
notifier, err := CreateWebhookNotifier(conf, keyProvider)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ func createSingleNodeServer() *service.LivekitServer {
|
|||||||
panic(fmt.Sprintf("could not create config: %v", err))
|
panic(fmt.Sprintf("could not create config: %v", err))
|
||||||
}
|
}
|
||||||
conf.Development = true
|
conf.Development = true
|
||||||
|
conf.Keys = map[string]string{testApiKey: testApiSecret}
|
||||||
|
|
||||||
currentNode, err := routing.NewLocalNode(conf)
|
currentNode, err := routing.NewLocalNode(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -145,7 +146,7 @@ func createSingleNodeServer() *service.LivekitServer {
|
|||||||
}
|
}
|
||||||
currentNode.Id = utils.NewGuid(nodeId1)
|
currentNode.Id = utils.NewGuid(nodeId1)
|
||||||
|
|
||||||
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, currentNode)
|
s, err := service.InitializeServer(conf, currentNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("could not create server: %v", err))
|
panic(fmt.Sprintf("could not create server: %v", err))
|
||||||
}
|
}
|
||||||
@@ -165,6 +166,7 @@ func createMultiNodeServer(nodeId string, port uint32) *service.LivekitServer {
|
|||||||
conf.RTC.TCPPort = port + 2
|
conf.RTC.TCPPort = port + 2
|
||||||
conf.Redis.Address = "localhost:6379"
|
conf.Redis.Address = "localhost:6379"
|
||||||
conf.Development = true
|
conf.Development = true
|
||||||
|
conf.Keys = map[string]string{testApiKey: testApiSecret}
|
||||||
|
|
||||||
currentNode, err := routing.NewLocalNode(conf)
|
currentNode, err := routing.NewLocalNode(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -173,7 +175,7 @@ func createMultiNodeServer(nodeId string, port uint32) *service.LivekitServer {
|
|||||||
currentNode.Id = nodeId
|
currentNode.Id = nodeId
|
||||||
|
|
||||||
// redis routing and store
|
// redis routing and store
|
||||||
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, currentNode)
|
s, err := service.InitializeServer(conf, currentNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("could not create server: %v", err))
|
panic(fmt.Sprintf("could not create server: %v", err))
|
||||||
}
|
}
|
||||||
@@ -239,17 +241,3 @@ func stopClients(clients ...*testclient.RTCClient) {
|
|||||||
c.Stop()
|
c.Stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type StaticKeyProvider struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *StaticKeyProvider) NumKeys() int {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *StaticKeyProvider) GetSecret(key string) string {
|
|
||||||
if key == testApiKey {
|
|
||||||
return testApiSecret
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|||||||
+2
-1
@@ -21,13 +21,14 @@ func testTurnServer(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conf.TURN.Enabled = true
|
conf.TURN.Enabled = true
|
||||||
|
conf.Keys = map[string]string{testApiKey: testApiSecret}
|
||||||
|
|
||||||
currentNode, err := routing.NewLocalNode(conf)
|
currentNode, err := routing.NewLocalNode(conf)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
currentNode.Id = utils.NewGuid(nodeId1)
|
currentNode.Id = utils.NewGuid(nodeId1)
|
||||||
|
|
||||||
// local routing and store
|
// local routing and store
|
||||||
s, err := service.InitializeServer(conf, &StaticKeyProvider{}, currentNode)
|
s, err := service.InitializeServer(conf, currentNode)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
go s.Start()
|
go s.Start()
|
||||||
waitForServerToStart(s)
|
waitForServerToStart(s)
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ func setupServerWithWebhook() (server *service.LivekitServer, testServer *webook
|
|||||||
conf.WebHook.URLs = []string{"http://localhost:7890"}
|
conf.WebHook.URLs = []string{"http://localhost:7890"}
|
||||||
conf.WebHook.APIKey = testApiKey
|
conf.WebHook.APIKey = testApiKey
|
||||||
conf.Development = true
|
conf.Development = true
|
||||||
|
conf.Keys = map[string]string{testApiKey: testApiSecret}
|
||||||
|
|
||||||
testServer = newTestServer(":7890")
|
testServer = newTestServer(":7890")
|
||||||
if err = testServer.Start(); err != nil {
|
if err = testServer.Start(); err != nil {
|
||||||
@@ -104,7 +105,7 @@ func setupServerWithWebhook() (server *service.LivekitServer, testServer *webook
|
|||||||
}
|
}
|
||||||
currentNode.Id = utils.NewGuid(nodeId1)
|
currentNode.Id = utils.NewGuid(nodeId1)
|
||||||
|
|
||||||
server, err = service.InitializeServer(conf, &StaticKeyProvider{}, currentNode)
|
server, err = service.InitializeServer(conf, currentNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -134,7 +135,7 @@ type webookTestServer struct {
|
|||||||
func newTestServer(addr string) *webookTestServer {
|
func newTestServer(addr string) *webookTestServer {
|
||||||
s := &webookTestServer{
|
s := &webookTestServer{
|
||||||
events: make(map[string]*livekit.WebhookEvent),
|
events: make(map[string]*livekit.WebhookEvent),
|
||||||
provider: &StaticKeyProvider{},
|
provider: auth.NewFileBasedKeyProviderFromMap(map[string]string{testApiKey: testApiSecret}),
|
||||||
}
|
}
|
||||||
s.server = &http.Server{
|
s.server = &http.Server{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
|
|||||||
Reference in New Issue
Block a user