@@ -68,6 +68,7 @@ public async Task LoadCore(ProfileItem? node)
6868 {
6969 ShowMsg ( true , $ "{ node . GetSummary ( ) } ") ;
7070 await CoreStop ( ) ;
71+ await Task . Delay ( 100 ) ;
7172 await CoreStart ( node ) ;
7273
7374 //In tun mode, do a delay check and restart the core
@@ -169,21 +170,12 @@ private async Task CoreStart(ProfileItem node)
169170 ShowMsg ( false , $ "{ Environment . OSVersion } - { ( Environment . Is64BitOperatingSystem ? 64 : 32 ) } ") ;
170171 ShowMsg ( false , string . Format ( ResUI . StartService , DateTime . Now . ToString ( "yyyy/MM/dd HH:mm:ss" ) ) ) ;
171172
172- //ECoreType coreType;
173- //if (node.configType != EConfigType.Custom && _config.tunModeItem.enableTun)
174- //{
175- // coreType = ECoreType.sing_box;
176- //}
177- //else
178- //{
179- // coreType = LazyConfig.Instance.GetCoreType(node, node.configType);
180- //}
181173 var coreType = AppHandler . Instance . GetCoreType ( node , node . ConfigType ) ;
182174 _config . RunningCoreType = coreType ;
183175 var coreInfo = CoreInfoHandler . Instance . GetCoreInfo ( coreType ) ;
184176
185177 var displayLog = node . ConfigType != EConfigType . Custom || node . DisplayLog ;
186- var proc = await RunProcess ( coreInfo , Global . CoreConfigFileName , displayLog ) ;
178+ var proc = await RunProcess ( coreInfo , Global . CoreConfigFileName , displayLog , true ) ;
187179 if ( proc is null )
188180 {
189181 return ;
@@ -225,7 +217,7 @@ private async Task CoreStart(ProfileItem node)
225217 if ( result . Success )
226218 {
227219 var coreInfo2 = CoreInfoHandler . Instance . GetCoreInfo ( preCoreType ) ;
228- var proc2 = await RunProcess ( coreInfo2 , Global . CorePreConfigFileName , true ) ;
220+ var proc2 = await RunProcess ( coreInfo2 , Global . CorePreConfigFileName , true , true ) ;
229221 if ( proc2 is not null )
230222 {
231223 _processPre = proc2 ;
@@ -243,7 +235,7 @@ private async Task<int> CoreStartSpeedtest(string configPath, ECoreType coreType
243235 try
244236 {
245237 var coreInfo = CoreInfoHandler . Instance . GetCoreInfo ( coreType ) ;
246- var proc = await RunProcess ( coreInfo , Global . CoreSpeedtestConfigFileName , true ) ;
238+ var proc = await RunProcess ( coreInfo , Global . CoreSpeedtestConfigFileName , true , false ) ;
247239 if ( proc is null )
248240 {
249241 return - 1 ;
@@ -264,17 +256,28 @@ private void ShowMsg(bool notify, string msg)
264256 _updateFunc ? . Invoke ( notify , msg ) ;
265257 }
266258
259+ private bool IsNeedSudo ( ECoreType eCoreType )
260+ {
261+ return _config . TunModeItem . EnableTun
262+ && eCoreType == ECoreType . sing_box
263+ && Utils . IsLinux ( )
264+ && _config . TunModeItem . LinuxSudoPassword . IsNotEmpty ( )
265+ ;
266+ }
267+
267268 #endregion Private
268269
269270 #region Process
270271
271- private async Task < Process ? > RunProcess ( CoreInfo coreInfo , string configPath , bool displayLog )
272+ private async Task < Process ? > RunProcess ( CoreInfo coreInfo , string configPath , bool displayLog , bool mayNeedSudo )
272273 {
273274 var fileName = CoreFindExe ( coreInfo ) ;
274275 if ( Utils . IsNullOrEmpty ( fileName ) )
275276 {
276277 return null ;
277278 }
279+
280+ var isNeedSudo = mayNeedSudo && IsNeedSudo ( coreInfo . CoreType ) ;
278281 try
279282 {
280283 Process proc = new ( )
@@ -292,6 +295,15 @@ private void ShowMsg(bool notify, string msg)
292295 StandardErrorEncoding = displayLog ? Encoding . UTF8 : null ,
293296 }
294297 } ;
298+
299+ if ( isNeedSudo )
300+ {
301+ proc . StartInfo . FileName = $ "/bin/sudo";
302+ proc . StartInfo . Arguments = $ "-S { fileName } { string . Format ( coreInfo . Arguments , configPath ) } ";
303+ proc . StartInfo . StandardInputEncoding = Encoding . UTF8 ;
304+ proc . StartInfo . RedirectStandardInput = true ;
305+ }
306+
295307 var startUpErrorMessage = new StringBuilder ( ) ;
296308 var startUpSuccessful = false ;
297309 if ( displayLog )
@@ -313,6 +325,15 @@ private void ShowMsg(bool notify, string msg)
313325 } ;
314326 }
315327 proc . Start ( ) ;
328+
329+ if ( isNeedSudo )
330+ {
331+ await Task . Delay ( 10 ) ;
332+ await proc . StandardInput . WriteLineAsync ( _config . TunModeItem . LinuxSudoPassword ) ;
333+ await Task . Delay ( 10 ) ;
334+ await proc . StandardInput . WriteLineAsync ( _config . TunModeItem . LinuxSudoPassword ) ;
335+ }
336+
316337 if ( displayLog )
317338 {
318339 proc . BeginOutputReadLine ( ) ;
0 commit comments