48 lines
1013 B
Go
48 lines
1013 B
Go
package ari
|
|
|
|
import (
|
|
"net/url"
|
|
|
|
"ai-operator/internal/config"
|
|
)
|
|
|
|
type WSAuthMode string
|
|
|
|
const (
|
|
WSAuthBasic WSAuthMode = "basic"
|
|
WSAuthQueryAPIKey WSAuthMode = "query_api_key"
|
|
WSAuthAuto WSAuthMode = "auto"
|
|
)
|
|
|
|
type WebSocketURL struct {
|
|
URL string
|
|
Sanitized string
|
|
AuthMode WSAuthMode
|
|
}
|
|
|
|
func BuildWebSocketURL(cfg config.AsteriskConfig, mode WSAuthMode) (WebSocketURL, error) {
|
|
if mode == "" {
|
|
mode = WSAuthMode(cfg.ARIWSAuthMode)
|
|
}
|
|
if mode == "" {
|
|
mode = WSAuthBasic
|
|
}
|
|
parsed, err := url.Parse(cfg.ARIWSURL)
|
|
if err != nil {
|
|
return WebSocketURL{}, err
|
|
}
|
|
q := parsed.Query()
|
|
q.Set("app", cfg.ARIApp)
|
|
if mode == WSAuthQueryAPIKey {
|
|
q.Set("api_key", cfg.ARIUser+":"+cfg.ARIPassword)
|
|
}
|
|
parsed.RawQuery = q.Encode()
|
|
sanitized := *parsed
|
|
if mode == WSAuthQueryAPIKey {
|
|
sq := sanitized.Query()
|
|
sq.Set("api_key", "***MASKED***")
|
|
sanitized.RawQuery = sq.Encode()
|
|
}
|
|
return WebSocketURL{URL: parsed.String(), Sanitized: sanitized.String(), AuthMode: mode}, nil
|
|
}
|