Skip to content

shafat-96/go-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go HLS Proxy

A minimal Go HTTP server that proxies HLS playlists and segments with:

  • Robust playlist rewriting to local endpoints
  • Automatic, domain-based upstream headers (via templates.go)
  • Segment streaming with Range passthrough
  • CORS support with allowlist
  • No caching (no Cache-Control set or forwarded)

Directory: e:/do/serrver/no/proxy/go-proxy/

Endpoints

  • GET /proxy?url={m3u8_url}&headers={optional_headers}
    • Fetches an m3u8, rewrites child URLs to proxy endpoints, returns playlist.
  • GET /ts-proxy?url={segment_url}&headers={optional_headers}
    • Streams media segments (TS/MP4/etc), forwards key headers, supports Range.
  • GET /healthz200 ok

Query Parameters

  • url (required): Percent-encoded absolute URL to the upstream resource.
  • headers (optional): Percent-encoded JSON object of extra headers to merge on top of auto-headers.

Example headers JSON:

{"referer":"https://example.com","origin":"https://example.com"}

Encode with encodeURIComponent before sending in the query string.

Auto Headers (Domain Templates)

File: templates.go

  • GenerateHeadersForDomain(rawURL, additionalHeaders) picks a template by domain (regex) and returns default headers merged with additionalHeaders.
  • Used in both handlers and during playlist rewriting so each rewritten child link carries the correct headers for its own domain.
  • You can add/modify templates by editing domainTemplates in templates.go.

CORS

  • Allowlist comes from WHITELIST_DOMAINS env (comma-separated). If * present, all origins allowed.
  • Preflight OPTIONS handled automatically.
  • Exposed headers: Content-Length, Content-Range, Content-Type, Accept-Ranges.

No Caching

  • The server does not set Cache-Control, and does not forward upstream Cache-Control.
  • If you want to force-disable all caching, add Cache-Control: no-store to responses (not enabled by default).

Requirements

  • Go 1.20+

Run

The server loads environment variables from a local .env file automatically.

  1. Prepare .env in e:/do/serrver/no/proxy/go-proxy/:
WHITELIST_DOMAINS="*"
PORT="3000"
  1. Install modules and run (PowerShell, run inside go-proxy/):
go mod tidy
go run .

Server logs:

Go proxy server running on http://localhost:3000

Usage Examples

  • Playlist:
GET http://localhost:3000/proxy?url={ENCODED_M3U8_URL}
  • Playlist with extra headers:
GET http://localhost:3000/proxy?url={ENCODED_M3U8_URL}&headers={ENCODED_JSON}
  • Segment (normally produced by rewritten playlists):
GET http://localhost:3000/ts-proxy?url={ENCODED_SEGMENT_URL}

Where ENCODED_JSON is encodeURIComponent(JSON.stringify({ referer: "https://example.com" })).

Behavior Details

  • Playlist rewriting:
    • #EXT-X-STREAM-INF → next line rewritten to /proxy with per-link headers.
    • #EXTINF → next line rewritten to /ts-proxy with per-link headers.
    • #EXT-X-MAP URI=... → rewritten to /ts-proxy with per-link headers.
    • #EXT-X-MEDIA URI=... → rewritten to /proxy with per-link headers.
  • Segment streaming:
    • Forwards status code and headers: Content-Type, Content-Length, Accept-Ranges, Content-Range.
    • Passes through Range from client.
  • User-Agent:
    • A browser-like UA is ensured if not provided by templates or headers param.

Security Notes

  • This is an open proxy for any URL. Consider adding an allowlist for target hostnames if needed.
  • Only a subset of headers is recommended to be passed via headers for safety (e.g., referer, origin, user-agent).

Build

go build -o hls-proxy.exe

Run the built binary:

./hls-proxy.exe

Troubleshooting

  • If upstream requests fail, check:
    • The domain has a matching template in templates.go.
    • The headers JSON is valid and percent-encoded.
    • CORS origin allowed by WHITELIST_DOMAINS.
  • Use /healthz to verify server is running.

About

hls proxy

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published