From 55fcad8cd7443f6454698ad6f1edf49fee76adf9 Mon Sep 17 00:00:00 2001 From: celeron533 Date: Sat, 14 Dec 2019 23:35:51 +0800 Subject: [PATCH] Fix issue when plugin program file is in system environment path --- .../Controller/Service/Sip003Plugin.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs b/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs index 48512c03..de6f883e 100644 --- a/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs +++ b/shadowsocks-csharp/Controller/Service/Sip003Plugin.cs @@ -94,18 +94,24 @@ namespace Shadowsocks.Controller.Service return false; } - if (!File.Exists(_pluginProcess.StartInfo.FileName)) - { - throw new FileNotFoundException(I18N.GetString("Cannot find the plugin program file"), _pluginProcess.StartInfo.FileName); - } - var localPort = GetNextFreeTcpPort(); LocalEndPoint = new IPEndPoint(IPAddress.Loopback, localPort); _pluginProcess.StartInfo.Environment["SS_LOCAL_HOST"] = LocalEndPoint.Address.ToString(); _pluginProcess.StartInfo.Environment["SS_LOCAL_PORT"] = LocalEndPoint.Port.ToString(); _pluginProcess.StartInfo.Arguments = ExpandEnvironmentVariables(_pluginProcess.StartInfo.Arguments, _pluginProcess.StartInfo.EnvironmentVariables); - _pluginProcess.Start(); + try + { + _pluginProcess.Start(); + } + catch (System.ComponentModel.Win32Exception ex) + { + // do not use File.Exists(...), it can not handle the scenarios when the plugin file is in system environment path. + if ((uint)ex.ErrorCode == 0x80004005) // file not found + { + throw new FileNotFoundException(I18N.GetString("Cannot find the plugin program file"), _pluginProcess.StartInfo.FileName, ex); + } + } _pluginJob.AddProcess(_pluginProcess.Handle); _started = true; }