-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
336 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
syntax = "proto3"; | ||
|
||
package livekit; | ||
option go_package = "github.com/livekit/livekit-server/proto/livekit"; | ||
|
||
import "livekit_models.proto"; | ||
|
||
message WebhookEvent { | ||
// one of room_started, room_ended, participant_joined, participant_left, recording_started, recording_finished | ||
string type = 1; | ||
|
||
// JWT token signed with the first API key pair | ||
string token = 2; | ||
|
||
Room room = 3; | ||
|
||
// set when event is participant_* | ||
ParticipantInfo participant = 4; | ||
|
||
// set when event is recording_* | ||
string recording_id = 5; | ||
// oneof recording_output { | ||
// string file = 7; | ||
// RecordingS3Output s3 = 8; | ||
// string rtmp = 9; | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package webhook | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/livekit/protocol/auth" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
const authHeader = "Authorization" | ||
|
||
type Notifier struct { | ||
apiKey string | ||
apiSecret string | ||
urls []string | ||
Logger logr.Logger | ||
} | ||
|
||
func NewNotifier(apiKey, apiSecret string, urls []string) *Notifier { | ||
return &Notifier{ | ||
apiKey: apiKey, | ||
apiSecret: apiSecret, | ||
urls: urls, | ||
Logger: logr.Discard(), | ||
} | ||
} | ||
|
||
func (n *Notifier) Notify(payload interface{}) error { | ||
var encoded []byte | ||
var err error | ||
if message, ok := payload.(proto.Message); ok { | ||
// use proto marshaler to ensure lowerCaseCamel | ||
encoded, err = protojson.Marshal(message) | ||
} else { | ||
// encode as JSON | ||
encoded, err = json.Marshal(payload) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// sign payload | ||
sum := sha256.Sum256(encoded) | ||
b64 := base64.StdEncoding.EncodeToString(sum[:]) | ||
|
||
at := auth.NewAccessToken(n.apiKey, n.apiSecret). | ||
SetValidFor(5 * time.Minute). | ||
SetSha256(b64) | ||
token, err := at.ToJWT() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, url := range n.urls { | ||
r, err := http.NewRequest("POST", url, bytes.NewReader(encoded)) | ||
if err != nil { | ||
// ignore and continue | ||
n.Logger.Error(err, "could not create request", "url", url) | ||
continue | ||
} | ||
r.Header.Set(authHeader, token) | ||
_, err = http.DefaultClient.Do(r) | ||
if err != nil { | ||
n.Logger.Error(err, "could not post to webhook", "url", url) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.