Skip to content

Commit

Permalink
Map participant attributes to SIP headers. (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc authored Nov 27, 2024
1 parent 4051190 commit 9e70d56
Show file tree
Hide file tree
Showing 13 changed files with 1,151 additions and 1,004 deletions.
6 changes: 6 additions & 0 deletions .changeset/slow-planes-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/protocol": minor
"github.com/livekit/protocol": minor
---

Map participant attributes to SIP headers.
1,003 changes: 525 additions & 478 deletions livekit/livekit_sip.pb.go

Large diffs are not rendered by default.

299 changes: 151 additions & 148 deletions livekit/livekit_sip.twirp.go

Large diffs are not rendered by default.

38 changes: 30 additions & 8 deletions livekit/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,27 @@ func (p *SIPOutboundTrunkInfo) AsTrunkInfo() *SIPTrunkInfo {
}
}

func validateHeaders(headers map[string]string) error {
func validateHeader(header string) error {
v := strings.ToLower(header)
if !strings.HasPrefix(v, "x-") {
return fmt.Errorf("only X-* headers are allowed: %s", header)
}
return nil
}

func validateHeaderKeys(headers map[string]string) error {
for k := range headers {
k = strings.ToLower(k)
if !strings.HasPrefix(k, "x-") {
return fmt.Errorf("only X-* headers are allowed: %s", k)
if err := validateHeader(k); err != nil {
return err
}
}
return nil
}

func validateHeaderValues(headers map[string]string) error {
for _, v := range headers {
if err := validateHeader(v); err != nil {
return err
}
}
return nil
Expand Down Expand Up @@ -151,10 +167,13 @@ func (p *SIPInboundTrunkInfo) Validate() error {
if !hasAuth && !hasCIDR && !hasNumbers {
return errors.New("for security, one of the fields must be set: AuthUsername+AuthPassword, AllowedAddresses or Numbers")
}
if err := validateHeaders(p.Headers); err != nil {
if err := validateHeaderKeys(p.Headers); err != nil {
return err
}
if err := validateHeaderKeys(p.HeadersToAttributes); err != nil {
return err
}
if err := validateHeaders(p.HeadersToAttributes); err != nil {
if err := validateHeaderValues(p.AttributesToHeaders); err != nil {
return err
}
return nil
Expand All @@ -171,10 +190,13 @@ func (p *SIPOutboundTrunkInfo) Validate() error {
} else if strings.ContainsAny(p.Address, "@;") || strings.HasPrefix(p.Address, "sip:") || strings.HasPrefix(p.Address, "sips:") {
return errors.New("trunk address should be a hostname or IP, not SIP URI")
}
if err := validateHeaders(p.Headers); err != nil {
if err := validateHeaderKeys(p.Headers); err != nil {
return err
}
if err := validateHeaderKeys(p.HeadersToAttributes); err != nil {
return err
}
if err := validateHeaders(p.HeadersToAttributes); err != nil {
if err := validateHeaderValues(p.AttributesToHeaders); err != nil {
return err
}
return nil
Expand Down
10 changes: 10 additions & 0 deletions protobufs/livekit_sip.proto
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,18 @@ message SIPInboundTrunkInfo {
map<string, string> headers = 9;
// Map SIP X-* headers from INVITE to SIP participant attributes.
map<string, string> headers_to_attributes = 10;
// Map LiveKit attributes to SIP X-* headers when sending BYE or REFER requests.
// Keys are the names of attributes and values are the names of X-* headers they will be mapped to.
map<string, string> attributes_to_headers = 14;

// Max time for the caller to wait for track subscription.
google.protobuf.Duration ringing_timeout = 11;
// Max call duration.
google.protobuf.Duration max_call_duration = 12;

bool krisp_enabled = 13;

// NEXT ID: 15
}

message CreateSIPOutboundTrunkRequest {
Expand Down Expand Up @@ -232,6 +237,11 @@ message SIPOutboundTrunkInfo {
// Map SIP X-* headers from 200 OK to SIP participant attributes.
// Keys are the names of X-* headers and values are the names of attributes they will be mapped to.
map<string, string> headers_to_attributes = 10;
// Map LiveKit attributes to SIP X-* headers when sending BYE or REFER requests.
// Keys are the names of attributes and values are the names of X-* headers they will be mapped to.
map<string, string> attributes_to_headers = 11;

// NEXT ID: 12
}

message GetSIPInboundTrunkRequest {
Expand Down
5 changes: 4 additions & 1 deletion protobufs/rpc/io.proto
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ message EvaluateSIPDispatchRulesResponse {
// Map SIP X-* headers from INVITE to SIP participant attributes.
// Keys are the names of X-* headers and values are the names of attributes they will be mapped to.
map<string, string> headers_to_attributes = 14;
// Map LiveKit attributes to SIP X-* headers when sending BYE or REFER requests.
// Keys are the names of attributes and values are the names of X-* headers they will be mapped to.
map<string, string> attributes_to_headers = 18;

repeated livekit.SIPFeature enabled_features = 15;

Expand All @@ -176,7 +179,7 @@ message EvaluateSIPDispatchRulesResponse {
// Max call duration.
google.protobuf.Duration max_call_duration = 17;

// NEXT ID: 18
// NEXT ID: 19
}

message UpdateSIPCallStateRequest {
Expand Down
5 changes: 4 additions & 1 deletion protobufs/rpc/sip.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ message InternalCreateSIPParticipantRequest {

map<string, string> headers = 21;
map<string, string> headers_to_attributes = 22;
// Map LiveKit attributes to SIP X-* headers when sending BYE or REFER requests.
// Keys are the names of attributes and values are the names of X-* headers they will be mapped to.
map<string, string> attributes_to_headers = 26;

repeated livekit.SIPFeature enabled_features = 25;

Expand All @@ -89,7 +92,7 @@ message InternalCreateSIPParticipantRequest {
// Max call duration.
google.protobuf.Duration max_call_duration = 24;

// NEXT ID: 26
// NEXT ID: 27
}

message InternalCreateSIPParticipantResponse {
Expand Down
Loading

0 comments on commit 9e70d56

Please sign in to comment.