1+ using System . Net . Http . Json ;
2+ using System . Text . Json ;
3+ using System . Text . Json . Serialization ;
4+ using Lagrange . Core . Common ;
5+
6+ namespace Lagrange . Core . Runner ;
7+
8+ public class UrlSignProvider : BotSignProvider
9+ {
10+ private static readonly HashSet < string > PcWhiteListCommand =
11+ [
12+ "trpc.o3.ecdh_access.EcdhAccess.SsoEstablishShareKey" ,
13+ "trpc.o3.ecdh_access.EcdhAccess.SsoSecureAccess" ,
14+ "trpc.o3.report.Report.SsoReport" ,
15+ "MessageSvc.PbSendMsg" ,
16+ "wtlogin.trans_emp" ,
17+ "wtlogin.login" ,
18+ "wtlogin.exchange_emp" ,
19+ "trpc.login.ecdh.EcdhService.SsoKeyExchange" ,
20+ "trpc.login.ecdh.EcdhService.SsoNTLoginPasswordLogin" ,
21+ "trpc.login.ecdh.EcdhService.SsoNTLoginEasyLogin" ,
22+ "trpc.login.ecdh.EcdhService.SsoNTLoginPasswordLoginNewDevice" ,
23+ "trpc.login.ecdh.EcdhService.SsoNTLoginEasyLoginUnusualDevice" ,
24+ "trpc.login.ecdh.EcdhService.SsoNTLoginPasswordLoginUnusualDevice" ,
25+ "trpc.login.ecdh.EcdhService.SsoNTLoginRefreshTicket" ,
26+ "trpc.login.ecdh.EcdhService.SsoNTLoginRefreshA2" ,
27+ "OidbSvcTrpcTcp.0x11ec_1" ,
28+ "OidbSvcTrpcTcp.0x758_1" , // create group
29+ "OidbSvcTrpcTcp.0x7c1_1" ,
30+ "OidbSvcTrpcTcp.0x7c2_5" , // request friend
31+ "OidbSvcTrpcTcp.0x10db_1" ,
32+ "OidbSvcTrpcTcp.0x8a1_7" , // request group
33+ "OidbSvcTrpcTcp.0x89a_0" ,
34+ "OidbSvcTrpcTcp.0x89a_15" ,
35+ "OidbSvcTrpcTcp.0x88d_0" , // fetch group detail
36+ "OidbSvcTrpcTcp.0x88d_14" ,
37+ "OidbSvcTrpcTcp.0x112a_1" ,
38+ "OidbSvcTrpcTcp.0x587_74" ,
39+ "OidbSvcTrpcTcp.0x1100_1" ,
40+ "OidbSvcTrpcTcp.0x1102_1" ,
41+ "OidbSvcTrpcTcp.0x1103_1" ,
42+ "OidbSvcTrpcTcp.0x1107_1" ,
43+ "OidbSvcTrpcTcp.0x1105_1" ,
44+ "OidbSvcTrpcTcp.0xf88_1" ,
45+ "OidbSvcTrpcTcp.0xf89_1" ,
46+ "OidbSvcTrpcTcp.0xf57_1" ,
47+ "OidbSvcTrpcTcp.0xf57_106" ,
48+ "OidbSvcTrpcTcp.0xf57_9" ,
49+ "OidbSvcTrpcTcp.0xf55_1" ,
50+ "OidbSvcTrpcTcp.0xf67_1" ,
51+ "OidbSvcTrpcTcp.0xf67_5" ,
52+ "OidbSvcTrpcTcp.0x6d9_4"
53+ ] ;
54+
55+ private readonly HttpClient _client = new ( ) ;
56+
57+ private readonly string _base = "https://sign.lagrangecore.org/api/sign" ;
58+ private readonly string _version = "30366" ;
59+
60+ public override bool IsWhiteListCommand ( string cmd ) => PcWhiteListCommand . Contains ( cmd ) ;
61+
62+ public override async Task < SsoSecureInfo ? > GetSecSign ( long uin , string cmd , int seq , ReadOnlyMemory < byte > body )
63+ {
64+ var response = await GetSign < PcSecSignRequest , PcSecSignResponse > (
65+ $ "{ _base } /{ _version } ",
66+ new PcSecSignRequest ( cmd , seq , Convert . ToHexString ( body . Span ) )
67+ ) ;
68+
69+ return new SsoSecureInfo
70+ {
71+ SecSign = Convert . FromHexString ( response . Value . Sign ) ,
72+ SecToken = Convert . FromHexString ( response . Value . Token ) ,
73+ SecExtra = Convert . FromHexString ( response . Value . Extra )
74+ } ;
75+ }
76+
77+ private async Task < TResponse > GetSign < TRequest , TResponse > ( string url , TRequest requestJson ) where TRequest : class where TResponse : class
78+ {
79+ using var request = new HttpRequestMessage ( ) ;
80+ request . Method = HttpMethod . Post ;
81+ request . RequestUri = new Uri ( url ) ;
82+ request . Content = JsonContent . Create ( requestJson ) ;
83+ using var response = await _client . SendAsync ( request ) ;
84+ if ( ! response . IsSuccessStatusCode ) throw new Exception ( $ "Unexpected http status code({ response . StatusCode } )") ;
85+
86+ var result = JsonSerializer . Deserialize < TResponse > ( await response . Content . ReadAsStreamAsync ( ) ) ;
87+ if ( result == null ) throw new NullReferenceException ( "Result is null" ) ;
88+
89+ return result ;
90+ }
91+ }
92+
93+ public class PcSecSignRequest ( string cmd , int seq , string src )
94+ {
95+ [ JsonPropertyName ( "cmd" ) ]
96+ public string Cmd { get ; } = cmd ;
97+
98+ [ JsonPropertyName ( "seq" ) ]
99+ public int Seq { get ; } = seq ;
100+
101+ [ JsonPropertyName ( "src" ) ]
102+ public string Src { get ; } = src ;
103+ }
104+
105+ public class PcSecSignResponse ( PcSecSignResponseValue value )
106+ {
107+ [ JsonRequired ]
108+ [ JsonPropertyName ( "value" ) ]
109+ public PcSecSignResponseValue Value { get ; init ; } = value ;
110+ }
111+
112+ public class PcSecSignResponseValue ( string sign , string token , string extra )
113+ {
114+ [ JsonRequired ]
115+ [ JsonPropertyName ( "sign" ) ]
116+ public string Sign { get ; init ; } = sign ;
117+
118+ [ JsonRequired ]
119+ [ JsonPropertyName ( "token" ) ]
120+ public string Token { get ; init ; } = token ;
121+
122+ [ JsonRequired ]
123+ [ JsonPropertyName ( "extra" ) ]
124+ public string Extra { get ; init ; } = extra ;
125+ }
0 commit comments