Skip to content

Commit

Permalink
Map participant attributes to SIP headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Nov 25, 2024
1 parent 7924953 commit ec73e82
Show file tree
Hide file tree
Showing 12 changed files with 1,129 additions and 990 deletions.
983 changes: 515 additions & 468 deletions livekit/livekit_sip.pb.go

Large diffs are not rendered by default.

291 changes: 147 additions & 144 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 @@ -142,10 +158,13 @@ func (p *SIPInboundTrunkInfo) Validate() error {
if len(p.Numbers) == 0 {
return errors.New("no trunk numbers specified")
}
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 @@ -160,10 +179,13 @@ func (p *SIPOutboundTrunkInfo) Validate() error {
} else if strings.Contains(p.Address, "@") {
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
6 changes: 6 additions & 0 deletions protobufs/livekit_sip.proto
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ 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;
Expand Down Expand Up @@ -232,6 +235,9 @@ 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;
}

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 @@ -169,6 +169,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 SIPFeature enabled_features = 15;

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

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

message UpdateSIPCallStateRequest {
Expand Down
3 changes: 3 additions & 0 deletions 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 SIPFeature enabled_features = 25;

Expand Down
290 changes: 157 additions & 133 deletions rpc/io.pb.go

Large diffs are not rendered by default.

180 changes: 91 additions & 89 deletions rpc/io.psrpc.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rpc/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func NewCreateSIPParticipantRequest(
PlayDialtone: req.PlayRingtone || req.PlayDialtone,
Headers: trunk.Headers,
HeadersToAttributes: trunk.HeadersToAttributes,
AttributesToHeaders: trunk.AttributesToHeaders,
EnabledFeatures: features,
RingingTimeout: req.RingingTimeout,
MaxCallDuration: req.MaxCallDuration,
Expand Down
Loading

0 comments on commit ec73e82

Please sign in to comment.