This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
216 lines (190 loc) · 7.44 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
class Program
{
private static string strBearer = "";
private static string strLicUpdateIP = "";
private static string strBase_url = "";
public class Config
{
public string? Bearer { get; set; }
public string? LicUpdateIP { get; set; }
public string? Base_url { get; set; }
}
static async Task Main()
{
var json = File.ReadAllText("LuncherConfig.json");
var config = JsonConvert.DeserializeObject<Config>(json);
strBearer = config.Bearer;
strBase_url = config.Base_url;
strLicUpdateIP = config.LicUpdateIP;
await RunPandora(true);
using (HttpClient client = new HttpClient())
{
Timer timer = new Timer(CheckIP, null, 0, 60000);
Timer Token_timer = new Timer(CheckandUpdateToken, null, 0, 24 * 60 * 60 * 1000);
await Task.Run(() => Console.ReadLine());
}
}
static async Task RunPandora(bool isrun = false)
{
string newIP = await GetPublicIP();
bool isupdateip = false;
if (strLicUpdateIP != newIP)
{
isupdateip = true;
Console.WriteLine($"IP changed to {newIP}. Downloading file...");
await DownloadFile();
strLicUpdateIP = newIP;
var json = File.ReadAllText("LuncherConfig.json");
JObject jsonObject = JObject.Parse(json);
jsonObject["LicUpdateIP"] = strLicUpdateIP;
File.WriteAllText("LuncherConfig.json", jsonObject.ToString());
}
if (isupdateip || isrun)
RestartProcess("PandoraNext.exe");
}
static async void CheckIP(object? state)
{
await RunPandora();
}
static async void CheckandUpdateToken(object? state)
{
await Task.Delay(20 * 1000);
var strtokensjson = File.ReadAllText("tokens.json");
JObject jsonObject = JObject.Parse(strtokensjson);
bool isupdate = false;
foreach (var account in jsonObject)
{
string accountName = account.Key;
string username = "";
try
{
username = account.Value["accuser"].ToString();
}
catch { }
if (username == "")
continue;
string accpsw = account.Value["accpsw"].ToString();
long timestamp = (long)account.Value["updatetimestamp"];
if (IsTimestampOlderThan7Days(timestamp))
{
try
{
string newtoken = await GetAccessTokenAsync(strBase_url, username, accpsw);
string sharetoken = await RegisterTokenAsync(username, newtoken);
account.Value["token"] = newtoken;
account.Value["sharetoken"] = sharetoken;
account.Value["updatetimestamp"] = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
isupdate = true;
//Console.WriteLine($"Update Account: {accountName}, Token: {newtoken}");
}
catch
{
Console.WriteLine($"Update Account False: {accountName}");
}
}
}
if (isupdate)
{
File.WriteAllText($"tokens_{DateTime.Now.ToString("yyyyMMdd_HHmmss_fff")}.json", strtokensjson);
File.WriteAllText("tokens.json", jsonObject.ToString());
Console.WriteLine($"Update Account token info successful, restart Pandora ....");
RestartProcess("PandoraNext.exe");
}
}
static async Task<string> GetAccessTokenAsync(string base_url, string username, string password)
{
string login_url = $"{base_url}/api/auth/login";
var data = new System.Collections.Generic.Dictionary<string, string>
{
{ "username", username },
{ "password", password }
};
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.PostAsync(login_url, new FormUrlEncodedContent(data));
if (response.IsSuccessStatusCode)
{
string jsonResult = await response.Content.ReadAsStringAsync();
return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonResult).access_token;
}
else
{
throw new Exception(username + "Failed to log in and get access token!");
}
}
}
static async Task<string> RegisterTokenAsync(string username, string accesstoken)
{
using (HttpClient client = new HttpClient())
{
string url = "https://ai.fakeopen.com/token/register";
var content = new StringContent(
$"unique_name={username}" +
$"&access_token={accesstoken}" +
"&site_limit=" +
"&expires_in=0" +
"&show_conversations=true" +
"&show_userinfo=true",
Encoding.UTF8,
"application/x-www-form-urlencoded");
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result).token_key;
}
else
{
throw new Exception($"Error: {response.StatusCode}");
}
}
}
static bool IsTimestampOlderThan7Days(long timestamp)
{
// 获取当前时间的 Unix 时间戳
long currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
// 判断是否超过4天
return (currentTimestamp - timestamp) > (4 * 24 * 60 * 60);
}
static async Task<string> GetPublicIP()
{
string url = "https://myip.ipip.net";
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
string data = await response.Content.ReadAsStringAsync();
Regex regex = new Regex(@"(\d+\.\d+\.\d+\.\d+)");
Match match = regex.Match(data);
return match.Success ? match.Value : string.Empty;
}
}
static async Task DownloadFile()
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", strBearer);
var response = await client.GetAsync("https://dash.pandoranext.com/data/license.jwt");
var content = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("license.jwt", content);
}
}
static void RestartProcess(string processName)
{
foreach (var process in System.Diagnostics.Process.GetProcessesByName("PandoraNext"))
{
process.Kill();
process.WaitForExit();
}
System.Diagnostics.Process.Start(processName);
}
}