public async Task OnRequest(object sender, SessionEventArgs e)
        {
            if (e.WebSession.Request.Url.Contains("music.163.com/eapi/song/enhance") || e.WebSession.Request.Url.Contains("music.163.com/eapi/song/like"))
            {
                Console.WriteLine("从代理服务器获取:" + e.WebSession.Request.Url);
                var proxy = proxySelector.GetTop();
                var st    = new Stopwatch();
                st.Start();
                try
                {
                    byte[] ret = null;
                    using (var wc = new ImpatientWebClient())
                    {
                        wc.Proxy = new WebProxy(proxy.host, proxy.port);
                        foreach (var aheader in e.WebSession.Request.RequestHeaders)
                        {
                            var str = aheader.Name.ToLower();
                            if (str == "host" || str == "content-length" || str == "accept" || str == "user-agent" || str == "connection")
                            {
                                continue;
                            }
                            wc.Headers.Add(aheader.Name, aheader.Value);
                        }
                        ret = wc.UploadData(e.WebSession.Request.Url.Replace("https://", "http://"), await e.GetRequestBody());
                    }
                    st.Stop();
                    await e.Ok(ret);

                    Console.WriteLine("修改完成,用时 " + st.ElapsedMilliseconds + " ms");
                }
                catch (Exception ex) { Console.WriteLine(ex); }
            }
        }
Esempio n. 2
0
 public async Task OnRequest(object sender, SessionEventArgs e)
 {
     if (proxiedAddresses.Any(str => e.WebSession.Request.Url.Contains(str)))
     {
         Console.WriteLine("从代理服务器获取:" + e.WebSession.Request.Url);
         var proxy = proxySelector.GetTop();
         var st    = new Stopwatch();
         st.Start();
         try
         {
             using (var wc = new ImpatientWebClient())
             {
                 wc.Proxy = new WebProxy(proxy.host, proxy.port);
                 foreach (var aheader in e.WebSession.Request.RequestHeaders)
                 {
                     var str = aheader.Name.ToLower();
                     if (skipRequestHeaders.Contains(str))
                     {
                         continue;
                     }
                     wc.Headers.Add(aheader.Name, aheader.Value);
                 }
                 var body    = wc.UploadData(e.WebSession.Request.Url, await e.GetRequestBody());
                 var headers = new Dictionary <string, HttpHeader>();
                 foreach (var key in wc.ResponseHeaders.AllKeys)
                 {
                     headers.Add(key, new HttpHeader(key, wc.ResponseHeaders[key]));
                 }
                 await e.Ok(body, headers);
             }
             st.Stop();
             Console.WriteLine("修改完成,用时 " + st.ElapsedMilliseconds + " ms");
         }
         catch (Exception ex) { Console.WriteLine(ex); }
     }
 }
Esempio n. 3
0
        //Modify response
        public async Task OnResponse(object sender, SessionEventArgs e)
        {
            //read response headers
            var responseHeaders = e.WebSession.Response.ResponseHeaders;

            if ((e.WebSession.Request.Method == "GET" || e.WebSession.Request.Method == "POST") && e.WebSession.Response.ResponseStatusCode == "200")
            {
                if (e.WebSession.Response.ContentType != null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text") || e.WebSession.Request.Url.StartsWith("http://music.163.com/eapi/song/enhance/player/url"))
                {
                    if (e.WebSession.Request.Url.StartsWith("http://music.163.com/eapi/song/enhance/player/url"))
                    {
                        Console.WriteLine("从代理服务器获取歌曲地址");
                        var proxies = ps.GetTopProxies(1);
                        var tasks   = new List <Task <byte[]> >();
                        var st      = new Stopwatch();
                        st.Start();
                        foreach (var proxy in proxies)
                        {
                            tasks.Add(Task <byte[]> .Factory.StartNew(() =>
                            {
                                try
                                {
                                    using (var wc = new ImpatientWebClient())
                                    {
                                        wc.Proxy = new WebProxy(proxy.host, proxy.port);
                                        foreach (var aheader in head)
                                        {
                                            var str = aheader.Key.ToLower();
                                            if (str == "host" || str == "content-length" || str == "accept" || str == "user-agent" || str == "connection")
                                            {
                                                continue;
                                            }
                                            wc.Headers.Add(aheader.Key, aheader.Value.Value);
                                        }
                                        var ret = wc.UploadData(e.WebSession.Request.Url, body);
                                        return(ret);
                                    }
                                }
                                catch (Exception) { }
                                return(new byte[0]);
                            }));
                        }
                        var idx = Task.WaitAny(tasks.ToArray());
                        st.Stop();
                        await e.SetResponseBody(tasks[idx].Result);

                        Console.WriteLine("修改完成,用时 " + st.ElapsedMilliseconds + " ms");
                    }
                    else if (e.WebSession.Request.Url.StartsWith("http://music.163.com/eapi/"))
                    {
                        var body = await e.GetResponseBodyAsString();

                        if (Regex.Match(body, "\"st\":-\\d+").Success)
                        {
                            Console.WriteLine("替换歌曲列表信息");
                            body = Regex.Replace(body, "\"st\":-\\d+", "\"st\":0");
                            body = body.Replace("\"pl\":0", "\"pl\":320000");
                            body = body.Replace("\"dl\":0", "\"dl\":320000");
                            await e.SetResponseBodyString(body);
                        }
                    }
                }
            }
        }