Removed cli, mux cleanup, docs update

This commit is contained in:
David Zhao
2021-05-07 01:16:33 -07:00
parent 933bdb9a2f
commit d399a57a7b
21 changed files with 963 additions and 738 deletions
+117
View File
@@ -0,0 +1,117 @@
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/utils"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
func generateKeys(c *cli.Context) error {
apiKey := utils.NewGuid(utils.APIKeyPrefix)
secret := utils.RandomSecret()
fmt.Println("API Key: ", apiKey)
fmt.Println("Secret Key: ", secret)
return nil
}
func printPorts(c *cli.Context) error {
conf, err := getConfig(c)
if err != nil {
return err
}
udpPorts := make([]string, 0)
tcpPorts := make([]string, 0)
tcpPorts = append(tcpPorts, fmt.Sprintf("%d - HTTP service", conf.Port))
if conf.RTC.TCPPort != 0 {
tcpPorts = append(tcpPorts, fmt.Sprintf("%d - ICE/TCP", conf.RTC.TCPPort))
}
if conf.RTC.UDPPort != 0 {
udpPorts = append(udpPorts, fmt.Sprintf("%d - ICE/UDP", conf.RTC.UDPPort))
} else {
udpPorts = append(udpPorts, fmt.Sprintf("%d-%d - ICE/UDP range", conf.RTC.ICEPortRangeStart, conf.RTC.ICEPortRangeEnd))
}
if conf.TURN.Enabled {
udpPorts = append(udpPorts, fmt.Sprintf("%d-%d", conf.TURN.PortRangeStart, conf.TURN.PortRangeEnd))
udpPorts = append(udpPorts, strconv.Itoa(conf.TURN.TCPPort))
tcpPorts = append(tcpPorts, strconv.Itoa(conf.TURN.TCPPort))
}
fmt.Println("TCP Ports")
for _, p := range tcpPorts {
fmt.Println(p)
}
fmt.Println("UDP Ports")
for _, p := range udpPorts {
fmt.Println(p)
}
return nil
}
func createToken(c *cli.Context) error {
room := c.String("room")
identity := c.String("identity")
conf, err := getConfig(c)
if err != nil {
return err
}
// use the first API key from config
if len(conf.Keys) == 0 {
// try to load from file
if _, err := os.Stat(conf.KeyFile); err != nil {
return err
}
f, err := os.Open(conf.KeyFile)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
decoder := yaml.NewDecoder(f)
if err = decoder.Decode(conf.Keys); err != nil {
return err
}
if len(conf.Keys) == 0 {
return fmt.Errorf("keys are not configured")
}
}
var apiKey string
var apiSecret string
for k, v := range conf.Keys {
apiKey = k
apiSecret = v
break
}
grant := &auth.VideoGrant{
RoomJoin: true,
Room: room,
}
at := auth.NewAccessToken(apiKey, apiSecret).
AddGrant(grant).
SetIdentity(identity).
SetValidFor(30 * 24 * time.Hour)
token, err := at.ToJWT()
if err != nil {
return err
}
fmt.Println("Token:", token)
return nil
}
+38 -56
View File
@@ -9,7 +9,6 @@ import (
"os/signal"
"runtime"
"runtime/pprof"
"strconv"
"syscall"
"time"
@@ -23,7 +22,6 @@ import (
"github.com/livekit/livekit-server/pkg/service"
"github.com/livekit/livekit-server/version"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/utils"
)
func main() {
@@ -83,6 +81,23 @@ func main() {
Usage: "print ports that server is configured to use",
Action: printPorts,
},
{
Name: "create-join-token",
Usage: "create a room join token for development use",
Action: createToken,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "room",
Usage: "name of room to join",
Required: true,
},
&cli.StringFlag{
Name: "identity",
Usage: "identity of participant that holds the token",
Required: true,
},
},
},
},
Version: version.Version,
}
@@ -92,26 +107,34 @@ func main() {
}
}
func getConfig(c *cli.Context) (*config.Config, error) {
confString, err := getConfigString(c)
if err != nil {
return nil, err
}
conf, err := config.NewConfig(confString)
if err != nil {
return nil, err
}
if err = conf.UpdateFromCLI(c); err != nil {
return nil, err
}
return conf, nil
}
func startServer(c *cli.Context) error {
rand.Seed(time.Now().UnixNano())
cpuProfile := c.String("cpuprofile")
memProfile := c.String("memprofile")
confString, err := getConfigString(c)
conf, err := getConfig(c)
if err != nil {
return err
}
conf, err := config.NewConfig(confString)
if err != nil {
return err
}
if err = conf.UpdateFromCLI(c); err != nil {
return err
}
if conf.Development {
logger.InitDevelopment(conf.LogLevel)
} else {
@@ -216,7 +239,9 @@ func createKeyProvider(conf *config.Config) (auth.KeyProvider, error) {
if err != nil {
return nil, err
}
defer f.Close()
defer func() {
_ = f.Close()
}()
return auth.NewFileBasedKeyProviderFromReader(f)
}
@@ -241,46 +266,3 @@ func getConfigString(c *cli.Context) (string, error) {
}
return configBody, nil
}
func generateKeys(c *cli.Context) error {
apiKey := utils.NewGuid(utils.APIKeyPrefix)
secret := utils.RandomSecret()
fmt.Println("API Key: ", apiKey)
fmt.Println("Secret Key: ", secret)
return nil
}
func printPorts(c *cli.Context) error {
confString, err := getConfigString(c)
if err != nil {
return err
}
conf, err := config.NewConfig(confString)
if err != nil {
return err
}
udpPorts := make([]string, 0)
tcpPorts := make([]string, 0)
tcpPorts = append(tcpPorts, strconv.Itoa(int(conf.Port)))
udpPorts = append(udpPorts, fmt.Sprintf("%d-%d", conf.RTC.ICEPortRangeStart, conf.RTC.ICEPortRangeEnd))
if conf.TURN.Enabled {
udpPorts = append(udpPorts, fmt.Sprintf("%d-%d", conf.TURN.PortRangeStart, conf.TURN.PortRangeEnd))
udpPorts = append(udpPorts, strconv.Itoa(conf.TURN.TCPPort))
tcpPorts = append(tcpPorts, strconv.Itoa(conf.TURN.TCPPort))
}
fmt.Println("TCP Ports")
for _, p := range tcpPorts {
fmt.Println(p)
}
fmt.Println("UDP Ports")
for _, p := range udpPorts {
fmt.Println(p)
}
return nil
}