Need help for lua pattern matching

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Need help for lua pattern matching

Thomas Schmiedl
Hello,

I hope someone could help me. I use the xupnpd2 mediaserver
(https://github.com/clark15b/xupnpd2) on my router to display some
hls-streams on the TV. I try to display the streams from
https://www.mall.tv/zive. xupnpd2 doesn't support https and the playlist
handling has to be fixed (I'm not a developer to fix it and the original
author doesn't reply).

My idea is to use haproxy to receive the streams via https and
"redirect" to xupnpd2 via http. This works without problems.

The "faulty" playlist-handling displays the streams from
https://www.mall.tv/zive with a timeshift (e.g. this stream
https://www.mall.tv/planespotting is 4 hours behind the actual time). My
idea is to use a lua-script in haproxy to receive the playlist
"manually" and return only the latest .ts-segments (the last 3 in this
case) to xupnpd2.

The playlist.lua contains pattern that works, but sometimes the return
from tcp:receive("*a") doesn't match at the end (I used ........ at the
end, which is not very flexible). But I'm only a beginner and need help.

Best regards,
Thomas

test.cfg (using: haproxy -f test.cfg)
-------------------------------------------------
global
        lua-load /home/user/haproxy-1.9.4/playlist.lua

frontend main
        mode http
        bind *:8080
        acl is_index_m3u8 path -m end /index.m3u8
        http-request use-service lua.playlist if is_index_m3u8
        default_backend forward

backend forward
        mode http
        server gjirafa puma.gjirafa.com:443 ssl verify none
-------------------------------------------------

playlist.lua
-------------------------------------------------
core.register_service("playlist", "http", function(applet)
        local tcp = core.tcp()
        tcp:connect_ssl("217.182.199.209", 443)
        tcp:send("GET ".. applet.path .." HTTP/1.1\r\nConnection:
Close\r\nHost: puma.gjirafa.com\r\n\r\n")
        local body = tcp:receive("*a")

        local result = string.match(body,"^.*(#EXTM3U.-)#EXTINF")
        result = result ..
string.match(body,"(...............%d+.ts................%d+.ts................%d+.ts)........$")

        applet:set_status(200)
        applet:add_header("Content-Type", "application/x-mpegURL")
        applet:add_header("content-length", string.len(result))
        applet:add_header("Connection", "close")
        applet:start_response()
        applet:send(result)
end)
-------------------------------------------------

example response from tcp:receive("*a")
-------------------------------------------------
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Mon, 11 Mar 2019 19:20:01 GMT
Content-Type: application/x-mpegURL
Transfer-Encoding: chunked
Connection: close
Last-Modified: Mon, 11 Mar 2019 19:20:00 GMT
ETag: W/"5c86b4e0-3844b"
Expires: Mon, 11 Mar 2019 19:20:00 GMT
X-Backend-G: 1s
X-Backend-S: 1s
X-Backend: gjlenc3
Vary: Accept-Encoding
X-Varnish: 157712007
Age: 0
Via: 1.1 varnish-v4
Access-Control-Allow-Origin:
Access-Control-Allow-Methods: GET,OPTIONS,POST
Access-Control-Allow-Headers: Range
Accept-Ranges: bytes
Cache-Control: no-cache

1e00
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:146
#EXT-X-TARGETDURATION:2
#EXTINF:2.000,
1552317596599.ts
(...)
#EXTINF:2.000,
1552331994339.ts
#EXTINF:2.000,
1552331996364.ts
#EXTINF:2.000,
1552331998371.ts

0

-------------------------------------------------