-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceService.cs
150 lines (130 loc) · 5.6 KB
/
SourceService.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
using System;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using RestSharp;
using System.IO;
using System.Collections.Generic;
namespace DataLocalizer
{
public class SourceService
{
private RestClient _client;
private string url;
private string Data;
Uri baseUri;
private SourceModel DataJson;
private JsonSerializerOptions options;
public DownloadManager downloadManager;
public SourceService()
{
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
//ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
var handler = new RestClientOptions
{
// 模拟浏览器的 User-Agent
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
// 自动处理 3xx 重定向
//FollowRedirects = true,
// 启用 GZip/Deflate/Brotli 压缩
// AutomaticDecompression = DecompressionMethods.GZip,
// 设置超时时间
MaxTimeout = 30000, // 10 秒超时
// 配置 Cookie 容器(模拟浏览器的 Cookie 管理)
// CookieContainer = new System.Net.CookieContainer(),
// 最大连接数,类似浏览器对服务器的最大连接数
//MaxRedirects = 10,
// 启用 HTTP/2,如果服务器支持
//ThrowOnAnyError = false
};
_client = new RestClient();
options = new JsonSerializerOptions
{
TypeInfoResolver = MyJsonContext.Default,
WriteIndented = true, // 美化输出
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, // 忽略为 null 的字段
AllowTrailingCommas = true, // 允许 JSON 末尾有逗号
};
downloadManager = new DownloadManager(5);
}
public async Task<string> GetSource(string _url)
{
url = _url;
baseUri = new Uri(url);
try
{
var request = new RestRequest(url, Method.Get);
request.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
var req = await _client.ExecuteAsync(request);
if (req.IsSuccessful)
{
string? json = req.Content;
DataJson = JsonSerializer.Deserialize<SourceModel>(json, options);
string formattedJson = JsonSerializer.Serialize(DataJson, options);
Data = Regex.Unescape(formattedJson);
return Data;
}
else
{
return "ERROR:" + req.ErrorMessage;
}
}
catch (Exception e)
{
return "ERROR:" + e.Message;
}
}
List<string> strings = new List<string>();
public async Task<int> SaveLocal(string path)
{
strings.Clear();
DataJson.homeLogo = DownLoad(DataJson.homeLogo, path, "./");
DataJson.Spider = DownLoad(DataJson.Spider, path, "./jar/");
foreach (var item in DataJson.Sites)
{
item.api = DownLoad(item.api, path, "./libs/");
item.ext = DownLoad(item.ext, path, "./js/");
}
var json = JsonSerializer.Serialize(DataJson, options);
json = Uri.UnescapeDataString(json);
json = Regex.Unescape(json);
if (File.Exists(Path.Combine(path, "index.json")))
File.Delete(Path.Combine(path, "index.json"));
await File.WriteAllTextAsync(Path.Combine(path, "index.json"), json);
return strings.Count + 1;
}
private string DownLoad(string? url, string path, string path1)
{
if (string.IsNullOrEmpty(url) || url.Contains("csp") || (!url.StartsWith("http") && !url.StartsWith(".")))
return url;
var sp = AssembleUrl(url);
if (!strings.Contains(sp.Item2))
{
strings.Add(sp.Item2);
downloadManager.AddTask(sp.Item1, $"{path}{path1.TrimStart('.')}{sp.Item2}");
}
return $"{path1}{sp.Item2}";
}
private (string, string) AssembleUrl(string _url)
{
Uri uri;
// 使用 baseUri 拼接相对路径
if (_url?.StartsWith("./") == true)
uri = new Uri(baseUri, _url);
else
uri = new Uri(_url);
string fullUri = uri.Segments[uri.Segments.Length - 1];
int questionMarkIndex = fullUri.IndexOf('?');
// 如果找到问号,截取问号之前的部分
if (questionMarkIndex != -1)
{
fullUri = fullUri.Substring(0, questionMarkIndex);
}
fullUri = Uri.UnescapeDataString(fullUri);
fullUri = Regex.Unescape(fullUri);
return (uri.AbsoluteUri, fullUri);
}
}
}