Skip to content

basic compatibility with the Xtream API #2

@GitHub666k

Description

@GitHub666k

Here’s how to add basic compatibility with the Xtream API to handle requests like get.php?username=&password=&type=m3u_plus&output=ts. The goal is to define and in a YAML configuration file and return the appropriate M3U stream on port 6078.

For example the steps to Implement:

Update Configuration Handling:
    Modify configuration file (e.g., config.yaml) to include username and password.
    Load these credentials when the application starts.

Add a HTTP Handler for Xtream API:
    Define a route in your HTTP server to handle get.php requests.
    Parse the query parameters to validate username and password.
    Return the M3U playlist if the credentials are correct.

Return the M3U Stream:
    Generate or fetch the M3U content based on the request parameters.
    Respond with the correct Content-Type.

Code Example:
Configuration (YAML File):

Example config.yaml:
xtream:
username: "exampleUser"
password: "examplePwd"
m3u_url: "http://localhost:6078/stream.m3u"

Go implentation proposal :

package main

import (
    "fmt"
    "net/http"
    "os"

    "gopkg.in/yaml.v2"
)

type Config struct {
    Xtream struct {
        Username string `yaml:"username"`
        Password string `yaml:"password"`
        M3UURL   string `yaml:"m3u_url"`
    } `yaml:"xtream"`
}

var config Config

func main() {
    // Load configuration
    err := loadConfig("config.yaml")
    if err != nil {
        fmt.Printf("Error loading config: %v\n", err)
        os.Exit(1)
    }

    // Start HTTP server
    http.HandleFunc("/get.php", handleXtreamAPI)
    fmt.Println("Server running on port 6078...")
    http.ListenAndServe(":6078", nil)
}

func loadConfig(filename string) error {
    file, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer file.Close()

    decoder := yaml.NewDecoder(file)
    return decoder.Decode(&config)
}

func handleXtreamAPI(w http.ResponseWriter, r *http.Request) {
    // Parse query parameters
    username := r.URL.Query().Get("username")
    password := r.URL.Query().Get("password")
    m3uType := r.URL.Query().Get("type")
    output := r.URL.Query().Get("output")

    // Validate parameters
    if username != config.Xtream.Username || password != config.Xtream.Password {
        http.Error(w, "Invalid credentials", http.StatusUnauthorized)
        return
    }
    if m3uType != "m3u_plus" || output != "ts" {
        http.Error(w, "Invalid request parameters", http.StatusBadRequest)
        return
    }

    // Return the M3U playlist
    w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
    fmt.Fprintf(w, "#EXTM3U\n#EXTINF:-1,Example Stream\n%s\n", config.Xtream.M3UURL)
}

Test the API:

test the server with a request like this:

curl "http://localhost:6078/get.php?username=exampleUser&password=examplePwd&type=m3u_plus&output=ts"

Expected response:

#EXTM3U
#EXTINF:-1,Example Stream
http://localhost:6078/stream.m3u

Thanks

Pierre

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions