Beispiel #1
0
        internal static void Handle(RawBrowserMessageEnvelop envelop)
        {
            //Log.Debug("Type: " + envelop.MessageType);
            if (envelop.MessageType == "videoIds")
            {
                foreach (var item in envelop.VideoIds)
                {
                    ApplicationContext.VideoTracker.AddVideoDownload(item);
                }
                return;
            }

            if (envelop.MessageType == "clear")
            {
                ApplicationContext.VideoTracker.ClearVideoList();
                return;
            }

            if (envelop.MessageType == "sync")
            {
                return;
            }

            if (envelop.MessageType == "custom")
            {
                var args = ArgsProcessor.ParseArgs(envelop.CustomData.Split('\r'));
                ArgsProcessor.Process(args);
                return;
            }

            var rawMessage = envelop.Message;

            if (rawMessage == null && envelop.Messages == null)
            {
                Log.Debug("Raw message/messages is null");
                return;
            }
            ;

            switch (envelop.MessageType)
            {
            case "download":
            {
                var message = Parse(rawMessage);
                if (!(Helpers.IsBlockedHost(message.Url) || Helpers.IsCompressedJSorCSS(message.Url)))
                {
                    ApplicationContext.CoreService.AddDownload(message);
                }
                break;
            }

            case "links":
            {
                var messages = new List <Message>(envelop.Messages.Length);
                foreach (var msg in envelop.Messages)
                {
                    var message = Parse(msg);
                    messages.Add(message);
                }
                ApplicationContext.CoreService.AddBatchLinks(messages);
                break;
            }

            case "video":
            {
                var message     = Parse(rawMessage);
                var contentType = message.GetResponseHeaderFirstValue("Content-Type");

                if (VideoUrlHelper.IsYtFormat(contentType))
                {
                    VideoUrlHelper.ProcessPostYtFormats(message);
                }

                //if (VideoUrlHelper.IsFBFormat(contentType, message.Url))
                //{
                //    VideoUrlHelper.ProcessPostFBFormats(message, ApplicationContext.Core);
                //}

                if (VideoUrlHelper.IsHLS(contentType))
                {
                    VideoUrlHelper.ProcessHLSVideo(message);
                }

                if (VideoUrlHelper.IsDASH(contentType))
                {
                    VideoUrlHelper.ProcessDashVideo(message);
                }

                if (!VideoUrlHelper.ProcessYtDashSegment(message))
                {
                    if (VideoUrlHelper.IsNormalVideo(contentType, message.Url, message.GetContentLength()))
                    {
                        VideoUrlHelper.ProcessNormalVideo(message);
                    }
                }
                break;
            }
            }
        }
Beispiel #2
0
        public void HandleRequest(RequestContext context)
        {
            if (context.RequestPath == "/204")
            {
                context.ResponseStatus = new ResponseStatus
                {
                    StatusCode    = 204,
                    StatusMessage = "No Content"
                };
                context.AddResponseHeader("Cache-Control", "max-age=0, no-cache, must-revalidate");
                context.SendResponse();
                return;
            }

            try
            {
                switch (context.RequestPath)
                {
                case "/download":
                {
                    var text = Encoding.UTF8.GetString(context.RequestBody !);
                    Log.Debug(text);
                    var message = Message.ParseMessage(text);
                    if (!(Helpers.IsBlockedHost(message.Url) || Helpers.IsCompressedJSorCSS(message.Url)))
                    {
                        ApplicationContext.CoreService.AddDownload(message);
                    }
                    break;
                }

                case "/video":
                {
                    var text = Encoding.UTF8.GetString(context.RequestBody !);
                    Log.Debug(text);
                    var message2    = Message.ParseMessage(Encoding.UTF8.GetString(context.RequestBody !));
                    var contentType = message2.GetResponseHeaderFirstValue("Content-Type")?.ToLowerInvariant() ?? string.Empty;
                    if (VideoUrlHelper.IsHLS(contentType))
                    {
                        VideoUrlHelper.ProcessHLSVideo(message2);
                    }
                    if (VideoUrlHelper.IsDASH(contentType))
                    {
                        VideoUrlHelper.ProcessDashVideo(message2);
                    }
                    if (!VideoUrlHelper.ProcessYtDashSegment(message2))
                    {
                        if (contentType != null && !(contentType.Contains("f4f") ||
                                                     contentType.Contains("m4s") ||
                                                     contentType.Contains("mp2t") || message2.Url.Contains("abst") ||
                                                     message2.Url.Contains("f4x") || message2.Url.Contains(".fbcdn") ||
                                                     message2.Url.Contains("http://127.0.0.1:9614")))
                        {
                            VideoUrlHelper.ProcessNormalVideo(message2);
                        }
                    }
                    break;
                }

                case "/links":
                {
                    var text = Encoding.UTF8.GetString(context.RequestBody !);
                    Log.Debug(text);
                    var arr = text.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    ApplicationContext.CoreService.AddBatchLinks(arr.Select(str => Message.ParseMessage(str.Trim())).ToList());
                    break;
                }

                case "/item":
                {
                    foreach (var item in Encoding.UTF8.GetString(context.RequestBody !).Split(new char[] { '\r', '\n' }))
                    {
                        ApplicationContext.VideoTracker.AddVideoDownload(item);
                    }
                    break;
                }

                case "/clear":
                    ApplicationContext.VideoTracker.ClearVideoList();
                    break;
                }
            }
            finally
            {
                SendSyncResponse(context);
            }
        }