Skip to content

Commit

Permalink
Merge pull request #375 from evanlabs/master
Browse files Browse the repository at this point in the history
添加用户网络代理支持,使用--proxyAddress指定代理地址。
  • Loading branch information
nilaoda committed Feb 22, 2021
2 parents 880af02 + 965c173 commit e65dfa5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
57 changes: 52 additions & 5 deletions N_m3u8DL-CLI/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class Global
public static string AUDIO_TYPE = "";
public static bool HadReadInfo = false;
private static bool noProxy = false;
private static string useProxyAddress = "";

public static bool ShouldStop { get => shouldStop; set => shouldStop = value; }
public static bool NoProxy { get => noProxy; set => noProxy = value; }
public static string UseProxyAddress { get => useProxyAddress; set => useProxyAddress = value; }


/*===============================================================================*/
Expand Down Expand Up @@ -109,7 +111,16 @@ public static string GetWebSource(String url, string headers = "", int TimeOut =
reProcess:
HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Method = "GET";
if (NoProxy) webRequest.Proxy = null;
if (NoProxy)
{
webRequest.Proxy = null;
}
else if (UseProxyAddress != "")
{
WebProxy proxy = new WebProxy(UseProxyAddress);
//proxy.Credentials = new NetworkCredential(username, password);
webRequest.Proxy = proxy;
}
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
webRequest.Accept = "*/*";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br");
Expand Down Expand Up @@ -370,7 +381,16 @@ public static string Get302(string url, string headers = "", int timeout = 5000)
string redirectUrl;
WebRequest myRequest = WebRequest.Create(url);
myRequest.Timeout = timeout;
if (NoProxy) myRequest.Proxy = null;
if (NoProxy)
{
myRequest.Proxy = null;
}
else if (UseProxyAddress != "")
{
WebProxy proxy = new WebProxy(UseProxyAddress);
//proxy.Credentials = new NetworkCredential(username, password);
myRequest.Proxy = proxy;
}
//添加headers
if (headers != "")
{
Expand Down Expand Up @@ -424,7 +444,16 @@ public static byte[] HttpDownloadFileToBytes(string url, string headers = "", in
req.Timeout = timeOut;
req.ReadWriteTimeout = timeOut; //重要
req.AllowAutoRedirect = false; //手动处理重定向,否则会丢失Referer
if (NoProxy) req.Proxy = null;
if (NoProxy)
{
req.Proxy = null;
}
else if (UseProxyAddress != "")
{
WebProxy proxy = new WebProxy(UseProxyAddress);
//proxy.Credentials = new NetworkCredential(username, password);
req.Proxy = proxy;
}
req.Headers.Add("Accept-Encoding", "gzip, deflate");
req.Accept = "*/*";
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
Expand Down Expand Up @@ -529,7 +558,16 @@ public static void HttpDownloadFile(string url, string path, int timeOut = 20000
request.AllowAutoRedirect = false; //手动处理重定向,否则会丢失Referer
request.KeepAlive = false;
request.Method = "GET";
if (NoProxy) request.Proxy = null;
if (NoProxy)
{
request.Proxy = null;
}
else if (UseProxyAddress != "")
{
WebProxy proxy = new WebProxy(UseProxyAddress);
//proxy.Credentials = new NetworkCredential(username, password);
request.Proxy = proxy;
}
if (url.Contains("data.video.iqiyi.com"))
request.UserAgent = "QYPlayer/Android/4.4.5;NetType/3G;QTP/1.1.4.3";
else if (url.Contains("pcvideo") && url.Contains(".titan.mgtv.com"))
Expand Down Expand Up @@ -1110,7 +1148,16 @@ public WebClientEx(int timeout, long from, long to)
protected override WebRequest GetWebRequest(Uri address)
{
var wr = (HttpWebRequest)base.GetWebRequest(address);
if (NoProxy) wr.Proxy = null;
if (NoProxy)
{
wr.Proxy = null;
}
else if (UseProxyAddress != "")
{
WebProxy proxy = new WebProxy(UseProxyAddress);
//proxy.Credentials = new NetworkCredential(username, password);
wr.Proxy = proxy;
}
if (setRange)
wr.AddRange(this.from, this.to);
if (setTimeout)
Expand Down
4 changes: 4 additions & 0 deletions N_m3u8DL-CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ static void Main(string[] args)
{
Global.NoProxy = true;
}
if (arguments.Has("--proxyAddress"))
{
Global.UseProxyAddress = arguments.Get("--proxyAddress").Next;
}
if (arguments.Has("--headers"))
{
reqHeaders = arguments.Get("--headers").Next;
Expand Down

0 comments on commit e65dfa5

Please sign in to comment.