Skip to content

Commit

Permalink
Support HTTP header X-Real-Host. v5.11.10
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Nov 4, 2023
1 parent 3810a5c commit 05d755b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,13 @@ but also has extra improvements for we can do more after proxy the API.
1. If `NAME_LOOKUP` (default is `on`) isn't `off`, try to resolve the candidate from `Host` of HTTP API by SRS Stack.
1. If access SRS Stack by `localhost` for debugging or run in localhost.
1. If `PLATFORM_DOCKER` is `off`, such as directly run in host, not in docker, use the private ip of SRS Stack.
1. Use `127.0.0.1` for OBS WHIP or native client to access SRS Stack by localhost.
1. If not set `CANDIDATE`, use `127.0.0.1` for OBS WHIP or native client to access SRS Stack by localhost.
1. Use `Host` if it's a valid IP address, for example, to access SRS Stack by public ip address.
1. Use DNS lookup if `Host` is a domain, for example, to access SRS Stack by domain name.
1. If no candidate, use docker IP address discovered by SRS.

> Note: Client can also set the header `X-Real-Host` to set the candidate.
> Note: Never use `host.docker.internal` because it's only available in docker, not in host server.
## Docker Allocated Ports
Expand Down
5 changes: 5 additions & 0 deletions platform/candidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (v *CandidateWorker) Resolve(host string) (net.IP, error) {
return conf.ipv4, nil
}

// If already set CANDIDATE, ignore lo.
if os.Getenv("CANDIDATE") != "" {
return nil, nil
}

// Return lo for OBS WHIP or native client to access it.
return net.IPv4(127, 0, 0, 1), nil
}
Expand Down
20 changes: 14 additions & 6 deletions platform/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,29 @@ func handleHTTPService(ctx context.Context, handler *http.ServeMux) error {

// Proxy to SRS RTC API, by /rtc/ prefix.
if strings.HasPrefix(r.URL.Path, "/rtc/") {
if eip := r.URL.Query().Get("eip"); eip != "" {
q := r.URL.Query()
if eip := q.Get("eip"); eip != "" {
logger.Tf(ctx, "Proxy %v to backend 1985, eip=%v, query is %v",
r.URL.Path, eip, r.URL.RawQuery)
} else {
// Allow test to mock and overwrite the host.
host := r.Header.Get("X-Real-Host")
if host == "" {
host = r.Host
}

// Resolve the host to ip.
starttime := time.Now()
if ip, err := candidateWorker.Resolve(r.Host); err != nil {
logger.Ef(ctx, "Proxy %v to backend 1985, resolve %v failed, cost=%v, err is %v",
r.URL.Path, r.Host, time.Now().Sub(starttime), err)
if ip, err := candidateWorker.Resolve(host); err != nil {
logger.Ef(ctx, "Proxy %v to backend 1985, resolve %v/%v failed, cost=%v, err is %v",
r.URL.Path, r.Host, host, time.Now().Sub(starttime), err)
ohttp.WriteError(ctx, w, r, err)
return
} else if ip != nil {
eip = ip.String()
r.URL.RawQuery += fmt.Sprintf("&eip=%v", eip)
logger.Tf(ctx, "Proxy %v to backend 1985, host=%v, resolved ip=%v, cost=%v, query is %v",
r.URL.Path, r.Host, eip, time.Now().Sub(starttime), r.URL.RawQuery)
logger.Tf(ctx, "Proxy %v to backend 1985, host=%v/%v, resolved ip=%v, cost=%v, query is %v",
r.URL.Path, r.Host, host, eip, time.Now().Sub(starttime), r.URL.RawQuery)
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func TestSystem_CandidateByHostIp(t *testing.T) {
var answer string
if err := NewApi(func(v *testApi) {
v.InjectRequest = func(req *http.Request) {
req.Host = eip
req.Header.Set("X-Real-Host", eip)
}
}).NoAuth(ctx, fmt.Sprintf("/rtc/v1/whip/?app=live&stream=%v&secret=%v", streamID, pubSecret), offer, &answer); err != nil {
r0 = errors.Wrapf(err, "should ok for rtc publish api")
Expand Down

0 comments on commit 05d755b

Please sign in to comment.