Skip to content

Commit a997df6

Browse files
author
Peng Wang
committed
Make this great again!
1 parent 383813a commit a997df6

File tree

7 files changed

+75
-75
lines changed

7 files changed

+75
-75
lines changed
Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Linq;
56
using System.Net;
6-
using System.Text.RegularExpressions;
77
using System.Threading.Tasks;
88
using Titanium.Web.Proxy;
99
using Titanium.Web.Proxy.EventArguments;
@@ -13,45 +13,40 @@ namespace NeteaseReverseLadder
1313
{
1414
class NeteaseProxy
1515
{
16-
private static string[] proxiedAddresses = { "music.163.com/eapi/v1/playlist/manipulate/tracks", "music.163.com/eapi/song/enhance", "music.163.com/eapi/song/like" };
16+
private static string[] proxiedAddresses;
1717
private static string[] skipRequestHeaders = { "host", "content-length", "accept", "user-agent", "connection", "accept-encoding" };
1818

1919
private ProxyServer proxyServer;
2020
public ProxySelector proxySelector;
2121

2222
public NeteaseProxy(ProxySelector proxySelector)
2323
{
24-
proxyServer = new ProxyServer
25-
{
26-
TrustRootCertificate = true
27-
};
24+
proxyServer = new ProxyServer();
2825
this.proxySelector = proxySelector;
26+
proxiedAddresses = File.ReadLines("ProxyPaths.txt").Where(l => l.Length > 0).ToArray();
2927
}
3028

3129
public void Start()
3230
{
3331
proxyServer.BeforeRequest += OnRequest;
34-
proxyServer.BeforeResponse += OnResponse;
3532
proxyServer.AddEndPoint(new ExplicitProxyEndPoint(IPAddress.Any, 15213, true));
3633
proxyServer.Start();
3734

3835
foreach (var endPoint in proxyServer.ProxyEndPoints)
39-
Console.WriteLine("IP {0} 和端口 {1} 上开启代理服务器", endPoint.IpAddress, endPoint.Port);
36+
Console.WriteLine("Started proxy on IP {0} and port {1}", endPoint.IpAddress, endPoint.Port);
4037
}
4138

4239
public void Stop()
4340
{
4441
proxyServer.BeforeRequest -= OnRequest;
45-
proxyServer.BeforeResponse -= OnResponse;
46-
4742
proxyServer.Stop();
4843
}
4944

5045
public async Task OnRequest(object sender, SessionEventArgs e)
5146
{
52-
if (proxiedAddresses.Any(str => e.WebSession.Request.Url.Contains(str)))
47+
if (proxiedAddresses.Any(str => e.HttpClient.Request.Url.Contains(str)))
5348
{
54-
Console.WriteLine("从代理服务器获取:" + e.WebSession.Request.Url);
49+
Console.WriteLine("Retrieving via proxy: " + e.HttpClient.Request.Url);
5550
var proxy = proxySelector.GetTop();
5651
var st = new Stopwatch();
5752
st.Start();
@@ -60,46 +55,25 @@ public async Task OnRequest(object sender, SessionEventArgs e)
6055
using (var wc = new ImpatientWebClient())
6156
{
6257
wc.Proxy = new WebProxy(proxy.host, proxy.port);
63-
foreach (var aheader in e.WebSession.Request.RequestHeaders)
58+
foreach (var aheader in e.HttpClient.Request.Headers)
6459
{
6560
var str = aheader.Name.ToLower();
6661
if (skipRequestHeaders.Contains(str)) continue;
6762
wc.Headers.Add(aheader.Name, aheader.Value);
6863
}
69-
var body = wc.UploadData(e.WebSession.Request.Url, await e.GetRequestBody());
64+
var body = wc.UploadData(e.HttpClient.Request.Url, await e.GetRequestBody());
7065
var headers = new Dictionary<string, HttpHeader>();
7166
foreach (var key in wc.ResponseHeaders.AllKeys)
7267
{
7368
headers.Add(key, new HttpHeader(key, wc.ResponseHeaders[key]));
7469
}
75-
await e.Ok(body, headers);
70+
e.Ok(body, headers);
7671
}
7772
st.Stop();
78-
Console.WriteLine("修改完成,用时 " + st.ElapsedMilliseconds + " ms");
73+
Console.WriteLine("Finished in " + st.ElapsedMilliseconds + " ms");
7974
}
8075
catch (Exception ex) { Console.WriteLine(ex); }
8176
}
8277
}
83-
84-
public async Task OnResponse(object sender, SessionEventArgs e)
85-
{
86-
var contentType = e.WebSession.Response.ContentType;
87-
if ((contentType.Contains("text") || contentType.Contains("json")) && e.WebSession.Request.Url.Contains("music.163.com/eapi/"))
88-
{
89-
var body = await e.GetResponseBodyAsString();
90-
if (Regex.Match(body, "\"st\":-\\d+").Success)
91-
{
92-
Console.WriteLine("替换歌曲列表信息");
93-
body = Regex.Replace(body, "\"st\":-\\d+", "\"st\":0");
94-
body = body.Replace("\"pl\":0", "\"pl\":320000");
95-
body = body.Replace("\"dl\":0", "\"dl\":320000");
96-
body = body.Replace("\"fl\":0", "\"fl\":320000");
97-
body = body.Replace("\"sp\":0", "\"sp\":7");
98-
body = body.Replace("\"cp\":0", "\"cp\":1");
99-
body = body.Replace("\"subp\":0", "\"subp\":1");
100-
await e.SetResponseBodyString(body);
101-
}
102-
}
103-
}
10478
}
10579
}

NeteaseReverseLadder/NeteaseReverseLadder.csproj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@
3737
<Prefer32Bit>false</Prefer32Bit>
3838
</PropertyGroup>
3939
<ItemGroup>
40-
<Reference Include="BouncyCastle.Crypto, Version=1.8.1.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
41-
<HintPath>..\packages\Portable.BouncyCastle.1.8.1.3\lib\net40\BouncyCastle.Crypto.dll</HintPath>
40+
<Reference Include="BouncyCastle.Crypto, Version=1.8.4.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Portable.BouncyCastle.1.8.4\lib\net40\BouncyCastle.Crypto.dll</HintPath>
4242
</Reference>
43-
<Reference Include="StreamExtended, Version=1.0.1.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
44-
<HintPath>..\packages\StreamExtended.1.0.18\lib\net45\StreamExtended.dll</HintPath>
43+
<Reference Include="BrotliSharpLib, Version=0.3.2.0, Culture=neutral, PublicKeyToken=3f4e2a1cd615fcb7, processorArchitecture=MSIL">
44+
<HintPath>..\packages\BrotliSharpLib.0.3.2\lib\net45\BrotliSharpLib.dll</HintPath>
45+
</Reference>
46+
<Reference Include="StreamExtended, Version=1.0.190.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
47+
<HintPath>..\packages\StreamExtended.1.0.190\lib\net45\StreamExtended.dll</HintPath>
4548
</Reference>
4649
<Reference Include="System" />
4750
<Reference Include="System.Core" />
@@ -53,8 +56,8 @@
5356
<Reference Include="Microsoft.CSharp" />
5457
<Reference Include="System.Data" />
5558
<Reference Include="System.Xml" />
56-
<Reference Include="Titanium.Web.Proxy, Version=3.0.130.0, Culture=neutral, PublicKeyToken=8e41e1f1c790d7cf, processorArchitecture=MSIL">
57-
<HintPath>..\packages\Titanium.Web.Proxy.3.0.130\lib\net45\Titanium.Web.Proxy.dll</HintPath>
59+
<Reference Include="Titanium.Web.Proxy, Version=3.0.883.0, Culture=neutral, PublicKeyToken=8e41e1f1c790d7cf, processorArchitecture=MSIL">
60+
<HintPath>..\packages\Titanium.Web.Proxy.3.0.883\lib\net45\Titanium.Web.Proxy.dll</HintPath>
5861
</Reference>
5962
</ItemGroup>
6063
<ItemGroup>
@@ -67,6 +70,11 @@
6770
<None Include="app.config" />
6871
<None Include="packages.config" />
6972
</ItemGroup>
73+
<ItemGroup>
74+
<Content Include="ProxyPaths.txt">
75+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
76+
</Content>
77+
</ItemGroup>
7078
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7179
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7280
Other similar extension points exist, see Microsoft.Common.targets.

NeteaseReverseLadder/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ static void Main(string[] args)
1010
var ps = new ProxySelector();
1111
if (!UpdateProxySelector(ps))
1212
{
13-
Console.WriteLine("获取代理列表失败");
13+
Console.WriteLine("Failed retriving proxy list");
1414
Console.ReadLine();
1515
return;
1616
}
1717
var proxy = new NeteaseProxy(ps);
1818
proxy.Start();
19-
Console.WriteLine("请设置网易云音乐代理为127.0.0.1,端口15213");
20-
Console.WriteLine("如果播放失败,按回车切换到下一个代理服务器");
19+
Console.WriteLine("Please change proxy of Netease Music to address 127.0.0.1,port 15213");
20+
Console.WriteLine("Press enter to use the next proxy");
2121
while (true)
2222
{
2323
var aproxy = ps.GetTop();
2424
if (aproxy == null)
2525
{
26-
Console.WriteLine("没有可用代理,重新搜索");
26+
Console.WriteLine("No available proxy, retrying");
2727
proxy.Stop();
2828
goto start;
2929
}
30-
Console.WriteLine("现在使用的是:" + aproxy);
30+
Console.WriteLine("Using: " + aproxy);
3131
Console.ReadLine();
3232
ps.Remove(aproxy);
3333
}
3434
}
3535
static bool UpdateProxySelector(ProxySelector ps)
3636
{
37-
Console.WriteLine("获取代理列表");
37+
Console.WriteLine("Retriving proxy list");
3838
ps.UpdateList();
39-
Console.WriteLine("共" + ps.Proxies.Count + "条结果,测试速度");
39+
Console.WriteLine(ps.Proxies.Count + " results, measuring speed");
4040
ps.UpdateLatency();
4141
return ps.Proxies.Count >= 1 && ps.Proxies[0].latency != int.MaxValue;
4242
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
https://music.163.com/eapi
2+
https://interface.music.163.com/eapi

NeteaseReverseLadder/ProxySelector.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class Proxy
2020
public bool valid;
2121
public override string ToString()
2222
{
23-
if (valid) return String.Format("{0}:{1} [{2}ms]", host, port, latency);
24-
else return String.Format("{0}:{1} [x]", host, port, latency);
23+
if (valid) return string.Format("{0}:{1} [{2}ms]", host, port, latency);
24+
else return string.Format("{0}:{1} [x]", host, port, latency);
2525
}
2626
}
2727
public List<Proxy> Proxies;
@@ -30,27 +30,24 @@ public void UpdateList()
3030
var newProxies = new List<Proxy>();
3131
var ret = "";
3232
using (var wc = new ImpatientWebClient())
33-
ret = wc.DownloadString("http://cn-proxy.com/");
34-
var tables = Regex.Matches(ret, "<table class=\"sortable\">.+?<tbody>(.*?)<\\/tbody>", RegexOptions.Singleline);
35-
var first = true;
36-
foreach (Match mat in tables)
37-
{
38-
if (first)
39-
{
40-
first = false;
41-
continue;
42-
}
43-
var trs = Regex.Matches(mat.Groups[1].Value, "<tr>(.*?)<\\/tr>", RegexOptions.Singleline);
33+
ret = wc.DownloadString("https://cn-proxy.com/");
34+
var trs = Regex.Matches(ret, "<tr>(.*?)<\\/tr>", RegexOptions.Singleline);
4435
foreach (Match tr in trs)
4536
{
4637
var tds = Regex.Matches(tr.Groups[1].Value, "<td>(.*?)<\\/td>");
47-
try
38+
if (tds.Count > 2)
39+
{
40+
var host = tds[0].Groups[1].Value;
41+
if (Regex.IsMatch(host, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
4842
{
49-
newProxies.Add(new Proxy() { host = tds[0].Groups[1].Value, port = int.Parse(tds[1].Groups[1].Value), latency = int.MaxValue });
43+
if (int.TryParse(tds[1].Groups[1].Value, out var port))
44+
{
45+
newProxies.Add(new Proxy() { host=host, port=port, latency = int.MaxValue });
46+
}
5047
}
51-
catch (Exception) { }
5248
}
53-
}
49+
50+
}
5451
lock (this)
5552
Proxies = newProxies;
5653
}
@@ -70,7 +67,7 @@ public void UpdateLatency()
7067
wc.Proxy = new WebProxy(proxy.host, proxy.port);
7168
var sw = new Stopwatch();
7269
sw.Start();
73-
var ret = wc.DownloadString("http://music.163.com/about");
70+
var ret = wc.DownloadString("https://music.163.com/about");
7471
sw.Stop();
7572
if (ret.Contains("music.126"))
7673
{
@@ -82,12 +79,17 @@ public void UpdateLatency()
8279
proxy.latency = latency;
8380
proxy.valid = latency != int.MaxValue;
8481
}
82+
Console.WriteLine("{0}: OK", proxy);
83+
}
84+
catch (Exception e) {
85+
Console.WriteLine("{0}: {1}", proxy, e.Message);
8586
}
86-
catch (Exception) { }
87-
Console.WriteLine(proxy);
8887
});
8988
}
9089
Parallel.Invoke(new ParallelOptions { MaxDegreeOfParallelism = parallelism }, actions.ToArray());
90+
newProxies = newProxies.Where(p => p.valid).OrderBy(p => p.latency).ToList();
91+
Console.WriteLine("Available proxies: ");
92+
newProxies.ForEach(p => Console.WriteLine(p));
9193
lock(this)
9294
Proxies = newProxies.Where(p => p.valid).OrderBy(p => p.latency).ToList();
9395
}

NeteaseReverseLadder/app.config

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3-
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
3+
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup>
4+
<runtime>
5+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
6+
<dependentAssembly>
7+
<assemblyIdentity name="BouncyCastle.Crypto" publicKeyToken="0e99375e54769942" culture="neutral" />
8+
<bindingRedirect oldVersion="0.0.0.0-1.8.4.0" newVersion="1.8.4.0" />
9+
</dependentAssembly>
10+
<dependentAssembly>
11+
<assemblyIdentity name="BrotliSharpLib" publicKeyToken="3f4e2a1cd615fcb7" culture="neutral" />
12+
<bindingRedirect oldVersion="0.0.0.0-0.3.2.0" newVersion="0.3.2.0" />
13+
</dependentAssembly>
14+
</assemblyBinding>
15+
</runtime>
16+
</configuration>
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="net45" />
4-
<package id="StreamExtended" version="1.0.18" targetFramework="net45" />
5-
<package id="Titanium.Web.Proxy" version="3.0.130" targetFramework="net45" />
3+
<package id="BrotliSharpLib" version="0.3.2" targetFramework="net45" />
4+
<package id="Portable.BouncyCastle" version="1.8.4" targetFramework="net45" />
5+
<package id="StreamExtended" version="1.0.190" targetFramework="net45" />
6+
<package id="Titanium.Web.Proxy" version="3.0.883" targetFramework="net45" />
67
</packages>

0 commit comments

Comments
 (0)