1
1
using AdobeBlockListConverter . Interfaces ;
2
+ using System . Text . Json ;
2
3
3
4
namespace AdobeBlockListConverter . Configs
4
5
{
5
6
public class AppConfig : IAppConfig
6
7
{
8
+ private readonly Dictionary < string , ConfigTemplate > _templates ;
9
+ private readonly string _configFilePath ;
10
+ private ConfigTemplate _currentTemplate ;
7
11
public string GetBlockListUrl => "https://a.dove.isdumb.one/list.txt" ;
12
+ public Dictionary < string , ConfigTemplate > AvailableTemplates => _templates ;
13
+ public ConfigTemplate CurrentTemplate => _currentTemplate ;
8
14
9
- public string OutputFileTemplate => Path . Combine ( Environment . CurrentDirectory , $ "KCNServer-AdobeBlockList-{ Guid . NewGuid ( ) } .txt") ;
10
-
11
- public string OutputLineTemplate => " - DOMAIN-SUFFIX,{0},Fucking-Adobe" ;
12
-
13
- public string OutputFileHeader => @"# By KCN-Server.AdobeBlockListConverter
14
- parsers:
15
- - url: 你的订阅地址Url
16
- yaml:
17
- prepend-proxy-groups:
18
- - name: Fucking-Adobe
19
- type: select
20
- proxies:
21
- - REJECT
22
-
23
- prepend-rules:
24
- " ;
15
+ public AppConfig ( )
16
+ {
17
+ _configFilePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "config.json" ) ;
18
+
19
+ if ( ! File . Exists ( _configFilePath ) )
20
+ {
21
+ Console . WriteLine ( "配置文件不存在: {0},创建默认配置。" , _configFilePath ) ;
22
+ CreateDefaultTemplates ( ) ;
23
+ }
24
+
25
+ _templates = LoadConfigTemplates ( ) ;
26
+
27
+ if ( _templates ? . Count > 0 )
28
+ _currentTemplate = _templates . Values . First ( ) ;
29
+ }
25
30
26
- public string OutputFileCommand => @"
27
- commands:
28
- - proxy-groups.Fucking-Adobe.proxies.0=REJECT
31
+ private void CreateDefaultTemplates ( )
32
+ {
33
+ string templateJson = @"
34
+ {
35
+ ""cfw"": {
36
+ ""outputFileHeader"": ""# By KCN-Server.AdobeBlockListConverter \nparsers:\n - url: 你的订阅地址Url\n yaml:\n prepend-proxy-groups:\n - name: Fucking-Adobe\n type: select\n proxies:\n - REJECT\n \n prepend-rules:\n"",
37
+ ""outputLineTemplate"": "" - DOMAIN-SUFFIX,{0},Fucking-Adobe"",
38
+ ""outputFileCommand"": ""\n commands:\n - proxy-groups.Fucking-Adobe.proxies.0=REJECT\n"",
39
+ ""outputFileNameTemplate"": ""KCNServer-AdobeBlockList-CFW-{0}.txt""
40
+ },
41
+ ""verge"": {
42
+ ""outputFileHeader"": ""# By KCN-Server.AdobeBlockListConverter \n# Clash Verge Merge 格式\n\nprepend-proxy-groups:\n - name: Fucking-Adobe\n type: select\n proxies:\n - REJECT\n\nprepend-rules:\n"",
43
+ ""outputLineTemplate"": "" - DOMAIN-SUFFIX,{0},Fucking-Adobe"",
44
+ ""outputFileCommand"": """",
45
+ ""outputFileNameTemplate"": ""KCNServer-AdobeBlockList-Verge-{0}.txt""
46
+ }
47
+ }
29
48
" ;
49
+ File . WriteAllText ( _configFilePath , templateJson ) ;
50
+ }
51
+
52
+ private Dictionary < string , ConfigTemplate > LoadConfigTemplates ( )
53
+ {
54
+ try
55
+ {
56
+ string json = File . ReadAllText ( _configFilePath ) ;
57
+ var options = new JsonSerializerOptions
58
+ {
59
+ PropertyNameCaseInsensitive = true
60
+ } ;
61
+ var templates = JsonSerializer . Deserialize < Dictionary < string , ConfigTemplate > > ( json , options ) ;
30
62
63
+ Console . WriteLine ( "已成功加载 {0} 个配置模板" , templates . Count ) ;
64
+ return templates ;
65
+ }
66
+ catch ( Exception ex )
67
+ {
68
+ Console . WriteLine ( "加载配置模板时出错: {0}" , ex . Message ) ;
69
+ return new Dictionary < string , ConfigTemplate > ( ) ;
70
+ }
71
+ }
72
+
73
+ public void SetTemplate ( string templateType )
74
+ {
75
+ if ( string . IsNullOrWhiteSpace ( templateType ) )
76
+ throw new ArgumentNullException ( nameof ( templateType ) ) ;
77
+
78
+ if ( ! _templates . TryGetValue ( templateType . ToLower ( ) , out var template ) )
79
+ throw new ArgumentException ( $ "未找到模板类型: { templateType } ") ;
80
+
81
+ template . OutputFileNameTemplate =
82
+ Path . Combine ( Environment . CurrentDirectory ,
83
+ string . Format ( _currentTemplate . OutputFileNameTemplate , Guid . NewGuid ( ) ) ) ;
84
+ _currentTemplate = template ;
85
+
86
+ Console . WriteLine ( "已切换到模板: {0}" , templateType ) ;
87
+ }
88
+
89
+ public string ? GetMode ( string [ ] args )
90
+ {
91
+ try
92
+ {
93
+ if ( args . Length > 0 )
94
+ return args [ 0 ] ;
95
+ return null ;
96
+ }
97
+ catch ( Exception ex )
98
+ {
99
+ Console . WriteLine ( "出错了: {0}" , ex . Message ) ;
100
+ return null ;
101
+ }
102
+ }
31
103
}
32
- }
104
+ }
0 commit comments