@@ -55,21 +55,21 @@ public async Task LoadCore(ProfileItem? node)
55
55
{
56
56
if ( node == null )
57
57
{
58
- ShowMsg ( false , ResUI . CheckServerSettings ) ;
58
+ UpdateFunc ( false , ResUI . CheckServerSettings ) ;
59
59
return ;
60
60
}
61
61
62
62
var fileName = Utils . GetConfigPath ( Global . CoreConfigFileName ) ;
63
63
var result = await CoreConfigHandler . GenerateClientConfig ( node , fileName ) ;
64
64
if ( result . Success != true )
65
65
{
66
- ShowMsg ( true , result . Msg ) ;
66
+ UpdateFunc ( true , result . Msg ) ;
67
67
return ;
68
68
}
69
69
70
- ShowMsg ( true , $ "{ node . GetSummary ( ) } ") ;
71
- ShowMsg ( false , $ "{ Utils . GetRuntimeInfo ( ) } ") ;
72
- ShowMsg ( false , string . Format ( ResUI . StartService , DateTime . Now . ToString ( "yyyy/MM/dd HH:mm:ss" ) ) ) ;
70
+ UpdateFunc ( true , $ "{ node . GetSummary ( ) } ") ;
71
+ UpdateFunc ( false , $ "{ Utils . GetRuntimeInfo ( ) } ") ;
72
+ UpdateFunc ( false , string . Format ( ResUI . StartService , DateTime . Now . ToString ( "yyyy/MM/dd HH:mm:ss" ) ) ) ;
73
73
await CoreStop ( ) ;
74
74
await Task . Delay ( 100 ) ;
75
75
await CoreStart ( node ) ;
@@ -81,15 +81,23 @@ public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
81
81
var coreType = selecteds . Exists ( t => t . ConfigType is EConfigType . Hysteria2 or EConfigType . TUIC or EConfigType . WireGuard ) ? ECoreType . sing_box : ECoreType . Xray ;
82
82
var configPath = Utils . GetConfigPath ( Global . CoreSpeedtestConfigFileName ) ;
83
83
var result = await CoreConfigHandler . GenerateClientSpeedtestConfig ( _config , configPath , selecteds , coreType ) ;
84
- ShowMsg ( false , result . Msg ) ;
84
+ UpdateFunc ( false , result . Msg ) ;
85
85
if ( result . Success != true )
86
86
{
87
87
return - 1 ;
88
88
}
89
89
90
- ShowMsg ( false , string . Format ( ResUI . StartService , DateTime . Now . ToString ( "yyyy/MM/dd HH:mm:ss" ) ) ) ;
91
- ShowMsg ( false , configPath ) ;
92
- return await CoreStartSpeedtest ( configPath , coreType ) ;
90
+ UpdateFunc ( false , string . Format ( ResUI . StartService , DateTime . Now . ToString ( "yyyy/MM/dd HH:mm:ss" ) ) ) ;
91
+ UpdateFunc ( false , configPath ) ;
92
+
93
+ var coreInfo = CoreInfoHandler . Instance . GetCoreInfo ( coreType ) ;
94
+ var proc = await RunProcess ( coreInfo , Global . CoreSpeedtestConfigFileName , true , false ) ;
95
+ if ( proc is null )
96
+ {
97
+ return - 1 ;
98
+ }
99
+
100
+ return proc . Id ;
93
101
}
94
102
95
103
public async Task CoreStop ( )
@@ -98,13 +106,13 @@ public async Task CoreStop()
98
106
{
99
107
if ( _process != null )
100
108
{
101
- await KillProcess ( _process , true ) ;
109
+ await ProcUtils . ProcessKill ( _process , true ) ;
102
110
_process = null ;
103
111
}
104
112
105
113
if ( _processPre != null )
106
114
{
107
- await KillProcess ( _processPre , true ) ;
115
+ await ProcUtils . ProcessKill ( _processPre , true ) ;
108
116
_processPre = null ;
109
117
}
110
118
@@ -120,41 +128,8 @@ public async Task CoreStop()
120
128
}
121
129
}
122
130
123
- public async Task CoreStopPid ( int pid )
124
- {
125
- try
126
- {
127
- await KillProcess ( Process . GetProcessById ( pid ) , false ) ;
128
- }
129
- catch ( Exception ex )
130
- {
131
- Logging . SaveLog ( _tag , ex ) ;
132
- }
133
- }
134
-
135
131
#region Private
136
132
137
- private string CoreFindExe ( CoreInfo coreInfo )
138
- {
139
- var fileName = string . Empty ;
140
- foreach ( var name in coreInfo . CoreExes )
141
- {
142
- var vName = Utils . GetBinPath ( Utils . GetExeName ( name ) , coreInfo . CoreType . ToString ( ) ) ;
143
- if ( File . Exists ( vName ) )
144
- {
145
- fileName = vName ;
146
- break ;
147
- }
148
- }
149
- if ( Utils . IsNullOrEmpty ( fileName ) )
150
- {
151
- var msg = string . Format ( ResUI . NotFoundCore , Utils . GetBinPath ( "" , coreInfo . CoreType . ToString ( ) ) , string . Join ( ", " , coreInfo . CoreExes . ToArray ( ) ) , coreInfo . Url ) ;
152
- Logging . SaveLog ( msg ) ;
153
- ShowMsg ( false , msg ) ;
154
- }
155
- return fileName ;
156
- }
157
-
158
133
private async Task CoreStart ( ProfileItem node )
159
134
{
160
135
var coreType = _config . RunningCoreType = AppHandler . Instance . GetCoreType ( node , node . ConfigType ) ;
@@ -194,28 +169,7 @@ private async Task CoreStartPreService(ProfileItem node)
194
169
}
195
170
}
196
171
197
- private async Task < int > CoreStartSpeedtest ( string configPath , ECoreType coreType )
198
- {
199
- try
200
- {
201
- var coreInfo = CoreInfoHandler . Instance . GetCoreInfo ( coreType ) ;
202
- var proc = await RunProcess ( coreInfo , Global . CoreSpeedtestConfigFileName , true , false ) ;
203
- if ( proc is null )
204
- {
205
- return - 1 ;
206
- }
207
-
208
- return proc . Id ;
209
- }
210
- catch ( Exception ex )
211
- {
212
- Logging . SaveLog ( _tag , ex ) ;
213
- ShowMsg ( false , ex . Message ) ;
214
- return - 1 ;
215
- }
216
- }
217
-
218
- private void ShowMsg ( bool notify , string msg )
172
+ private void UpdateFunc ( bool notify , string msg )
219
173
{
220
174
_updateFunc ? . Invoke ( notify , msg ) ;
221
175
}
@@ -233,11 +187,12 @@ private bool IsNeedSudo(ECoreType eCoreType)
233
187
234
188
#region Process
235
189
236
- private async Task < Process ? > RunProcess ( CoreInfo coreInfo , string configPath , bool displayLog , bool mayNeedSudo )
190
+ private async Task < Process ? > RunProcess ( CoreInfo ? coreInfo , string configPath , bool displayLog , bool mayNeedSudo )
237
191
{
238
- var fileName = CoreFindExe ( coreInfo ) ;
192
+ var fileName = CoreInfoHandler . Instance . GetCoreExecFile ( coreInfo , out var msg ) ;
239
193
if ( Utils . IsNullOrEmpty ( fileName ) )
240
194
{
195
+ UpdateFunc ( false , msg ) ;
241
196
return null ;
242
197
}
243
198
@@ -272,12 +227,12 @@ private bool IsNeedSudo(ECoreType eCoreType)
272
227
proc . OutputDataReceived += ( sender , e ) =>
273
228
{
274
229
if ( Utils . IsNullOrEmpty ( e . Data ) ) return ;
275
- ShowMsg ( false , e . Data + Environment . NewLine ) ;
230
+ UpdateFunc ( false , e . Data + Environment . NewLine ) ;
276
231
} ;
277
232
proc . ErrorDataReceived += ( sender , e ) =>
278
233
{
279
234
if ( Utils . IsNullOrEmpty ( e . Data ) ) return ;
280
- ShowMsg ( false , e . Data + Environment . NewLine ) ;
235
+ UpdateFunc ( false , e . Data + Environment . NewLine ) ;
281
236
282
237
if ( ! startUpSuccessful )
283
238
{
@@ -319,40 +274,11 @@ private bool IsNeedSudo(ECoreType eCoreType)
319
274
catch ( Exception ex )
320
275
{
321
276
Logging . SaveLog ( _tag , ex ) ;
322
- ShowMsg ( true , ex . Message ) ;
277
+ UpdateFunc ( true , ex . Message ) ;
323
278
return null ;
324
279
}
325
280
}
326
281
327
- private async Task KillProcess ( Process ? proc , bool review )
328
- {
329
- if ( proc is null )
330
- {
331
- return ;
332
- }
333
-
334
- var fileName = proc ? . MainModule ? . FileName ;
335
- var processName = proc ? . ProcessName ;
336
-
337
- try { proc ? . Kill ( true ) ; } catch ( Exception ex ) { Logging . SaveLog ( _tag , ex ) ; }
338
- try { proc ? . Kill ( ) ; } catch ( Exception ex ) { Logging . SaveLog ( _tag , ex ) ; }
339
- try { proc ? . Close ( ) ; } catch ( Exception ex ) { Logging . SaveLog ( _tag , ex ) ; }
340
- try { proc ? . Dispose ( ) ; } catch ( Exception ex ) { Logging . SaveLog ( _tag , ex ) ; }
341
-
342
- await Task . Delay ( 500 ) ;
343
- if ( review )
344
- {
345
- var proc2 = Process . GetProcessesByName ( processName )
346
- . FirstOrDefault ( t => t . MainModule ? . FileName == fileName ) ;
347
- if ( proc2 != null )
348
- {
349
- Logging . SaveLog ( $ "{ _tag } , KillProcess not completing the job") ;
350
- await KillProcess ( proc2 , false ) ;
351
- proc2 = null ;
352
- }
353
- }
354
- }
355
-
356
282
#endregion Process
357
283
358
284
#region Linux
0 commit comments