11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4+ using System . IO ;
45using System . Linq ;
56using System . Net ;
6- using System . Text . RegularExpressions ;
77using System . Threading . Tasks ;
88using Titanium . Web . Proxy ;
99using 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}
0 commit comments