Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
package webfinger
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/owncast/owncast/utils"
)
// isValidRedirectURL reports whether host is safe to send an outbound request
// to: it must not resolve to an internal (loopback or private) address. The
// name is intentional so CodeQL's request-forgery analysis recognizes this as
// a URL sanitizer and treats the internal-host check as a barrier on the
// outbound webfinger request.
func isValidRedirectURL(host string) bool {
return !utils.IsHostnameInternal(host)
}
// GetWebfingerLinks will return webfinger data for an account.
func GetWebfingerLinks(account string) ([]map[string]interface{}, error) {
type webfingerResponse struct {
Links []map[string]interface{} `json:"links"`
}
account = strings.TrimLeft(strings.TrimSpace(account), "@") // remove any leading @
accountComponents := strings.Split(account, "@")
if len(accountComponents) != 2 || accountComponents[0] == "" || accountComponents[1] == "" {
return nil, errors.New("fediverse account must be in user@server format")
}
fediverseServer := accountComponents[1]
// Reject any requests to our internal network or loopback.
if !isValidRedirectURL(fediverseServer) {
return nil, errors.New("unable to use provided host as a valid fediverse server")
}
// HTTPS is required.
requestURL, err := url.Parse("https://" + fediverseServer)
if err != nil {
return nil, fmt.Errorf("unable to parse fediverse server host %s", fediverseServer)
}
requestURL.Path = "/.well-known/webfinger"
query := requestURL.Query()
query.Add("resource", fmt.Sprintf("acct:%s", account))
requestURL.RawQuery = query.Encode()
// Follow redirects (e.g. subdomain -> canonical domain, as Mastodon does).
// GetRetryableHTTPClient re-validates each hop against the internal-host
// check to prevent SSRF and caps the redirect count.
client := utils.GetRetryableHTTPClient()
req, err := http.NewRequest("GET", requestURL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
response, err := client.Do(req)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, errors.New("webfinger request returned bad status code: " + http.StatusText(response.StatusCode) + ", check account details")
}
defer response.Body.Close()
var links webfingerResponse
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&links); err != nil {
return nil, fmt.Errorf("error decoding webfinger response: %s", err)
}
return links.Links, nil
}