Exemplo n.º 1
0
        public BackendHub(ref SQLDatabase db, Network.Backend backend)
        {
            channel = new ChannelProvider(this);
            channel.ChannelProviderResponse += (sender, e) =>
            {
                var evArgs = new BackendHubResponseEventArgs();
                evArgs.Response = e.Response;
                evArgs.Context  = e.context;
                evArgs.Target   = BackendTarget.Channel;

                switch (e.Action)
                {
                case BackendAction.Create:
                    MakeRequest(BackendTarget.Channel, BackendAction.Import, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Import:
                    MakeRequest(BackendTarget.Epg, BackendAction.Download, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Remove:
                    MakeRequest(BackendTarget.Epg, BackendAction.Remove, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Update:
                    MakeRequest(BackendTarget.Epg, e.Action, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Download:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Download, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                default:
                    break;
                }
            };

            playlist = new PlayListProvider(ref db, this);
            playlist.PlayListProviderResponse += (sender, e) =>
            {
                var evArgs = new BackendHubResponseEventArgs();
                evArgs.Response = e.Response;
                evArgs.Context  = e.Context;
                evArgs.Target   = BackendTarget.Playlist;

                switch (e.Action)
                {
                case BackendAction.Create:
                    if (e.Context != null)
                    {
                        BackendHubResponse?.Invoke(this, evArgs);
                    }
                    break;

                case BackendAction.Download:
                    MakeRequest(BackendTarget.Channel, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Import:
                    MakeRequest(BackendTarget.Channel, BackendAction.Import, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Remove:
                    BackendHubResponse?.Invoke(this, evArgs);
                    break;

                case BackendAction.Update:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                default:
                    break;
                }
            };

            epg = new EPGProvider(ref db, this);
            epg.EPGProviderResponse += (sender, e) =>
            {
                var evArgs = new BackendHubResponseEventArgs();
                evArgs.Response = e.Response;
                evArgs.Context  = e.Context;
                evArgs.Target   = BackendTarget.Epg;

                switch (e.Action)
                {
                case BackendAction.Create:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Import:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Update, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Remove:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Remove, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Update:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Update, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Download:
                    MakeRequest(BackendTarget.Epg, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                default:
                    break;
                }
            };

            backend.BackendHubRequest += (sender, e) =>
            {
                MakeRequest(e.Target, e.Action, e.Context, e.Parameters, e.Response, 0);
            };

            var providers = db.SQLQuery <uint>("SELECT * FROM providers WHERE id !='0' AND skip !='1'").Result;

            if (providers.Count != 0)
            {
                for (var i = uint.MinValue; i < providers.Count; i++)
                {
                    var x = new Dictionary <string, string>();
                    x.Add("chanlist", providers[i]["channel_list"]);
                    x.Add("logolist", providers[i]["logo_list"]);
                    x.Add("epglist", providers[i]["epg_list"]);
                    x.Add("playlists", providers[i]["playlist_urls"]);

                    var clist    = providers[i]["channel_list"];
                    var provider = int.Parse(providers[i]["id"]);

                    Task.Run(() => MakeRequest((clist != "0") ? BackendTarget.Channel :
                                               BackendTarget.Playlist, BackendAction.Download, null, x, string.Empty, provider));
                }
            }
            else
            {
                Task.Run(() => MakeRequest(BackendTarget.Playlist, BackendAction.Download,
                                           null, new Dictionary <string, string>(), string.Empty, 0));
            }
        }
Exemplo n.º 2
0
        public Backend(int port = 81)
        {
            db          = new SQLDatabase("channels.db");
            HTTPSockets = new Dictionary <string, HTTPSocket>();

            HTTPSockets.Add("backend", new HTTPSocket("*", port, string.Empty));
            HTTPSockets.Add("website", new HTTPSocket("*", port + 1, string.Empty));
            HTTPSockets.Add("tsproxy", new HTTPSocket("*", port + 2, string.Empty));

            backend = new Network.Backend(ref db, this);
            backend.BackendResponse += (sender, e) =>
            {
                if (e.Context == null)
                {
                    return;
                }

                switch (e.Target)
                {
                case BackendTarget.Playlist:
                    e.Context.Response.StatusCode        = 200;
                    e.Context.Response.StatusDescription = "OK";
                    e.Context.Response.ContentType       = "application/x-mpegURL";
                    e.Context.Response.Headers.Add("Content-Disposition",
                                                   "attachment; filename=playlist.m3u8");
                    break;

                case BackendTarget.Epg:
                    e.Context.Response.StatusCode        = 200;
                    e.Context.Response.StatusDescription = "OK";
                    e.Context.Response.ContentType       = "application/gzip";
                    break;

                case BackendTarget.Channel:
                    break;

                case BackendTarget.TSProxy:
                    e.Context.Response.StatusCode        = 200;
                    e.Context.Response.StatusDescription = "OK";
                    e.Context.Response.ContentType       = "video/mp2t";
                    break;

                case BackendTarget.WebSite:
                case BackendTarget.Unknown:
                default:
                    break;
                }

                Task.Run(() => HTTPSockets["backend"].Send(
                             Encoding.UTF8.GetBytes(e.Response), e.Context)
                         );
            };

            HTTPSockets["tsproxy"].DataReceived += (sender, e) =>
            {
                using (var tsp = new Formats.TSDownloader())
                    tsp.Request(new Uri(e.Parameters["url"]), ref e.Context);
            };

            HTTPSockets["backend"].DataReceived += (sender, e) =>
            {
                switch (e.Target)
                {
                case BackendTarget.Playlist:
                    var evArgs = new BackendRequestEventArgs();
                    evArgs.Context    = e.Context;
                    evArgs.Target     = e.Target;
                    evArgs.Parameters = e.Parameters;

                    BackendRequest?.Invoke(this, evArgs);
                    break;

                case BackendTarget.Epg:
                    using (var fs = new Filesystem(""))
                    {
                        if (fs.Exists("epgdata.xml.gz"))
                        {
                            var data = fs.Read("epgdata.xml.gz").Result;

                            e.Context.Response.ContentType = "application/gzip";
                            e.Context.Response.Headers.Add("Content-Disposition",
                                                           "attachment; filename=epgdata.xml.gz");

                            e.Context.Response.StatusCode        = 200;
                            e.Context.Response.StatusDescription = "OK";

                            Task.Run(() => HTTPSockets["backend"].Send(data, e.Context));
                        }
                        else
                        {
                            e.Context.Response.StatusCode        = 503;
                            e.Context.Response.StatusDescription = "Service Unavailable";

                            Task.Run(() => HTTPSockets["backend"].Send(new byte[0], e.Context));
                        }
                    }

                    break;

                case BackendTarget.WebSite:
                case BackendTarget.Unknown:
                default:
                    e.Context.Response.StatusCode        = 404;
                    e.Context.Response.StatusDescription = "Not Found";
                    Task.Run(() => HTTPSockets["backend"].Send(e.Context.Response.StatusDescription, e.Context));
                    break;
                }
            };

            website = new WebSite(this);
            website.WebSiteResponse += (sender, e) =>
            {
                Task.Run(() => HTTPSockets["website"].Send(Encoding.UTF8.
                                                           GetBytes(e.Response), e.Context));
            };

            HTTPSockets["website"].DataReceived += (sender, e) =>
            {
                if (e.UserAgent.Contains("Kodi"))
                {
                    e.Context.Response.StatusCode        = 403;
                    e.Context.Response.StatusDescription = "Forbidden";

                    Task.Run(() => HTTPSockets["website"].Send(new byte[0], e.Context));

                    return;
                }

                var evArgs = new BackendRequestEventArgs();
                evArgs.Context    = e.Context;
                evArgs.Parameters = e.Parameters;
                evArgs.Target     = e.Target;

                WebSiteRequest?.Invoke(this, evArgs);
            };

            running = true;
            var t = new Thread(HeartBeat);

            t.IsBackground = true;

            t.Start();
        }