Skip to content

Commit

Permalink
Improve and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jul 8, 2024
1 parent 01b205e commit 4caf1a1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
using System.Runtime.InteropServices;
using static v2rayN.Handler.ProxySetting.InternetConnectionOption;
using static v2rayN.Common.ProxySetting.InternetConnectionOption;

namespace v2rayN.Handler
namespace v2rayN.Common
{
internal class ProxySetting
{
private const string _regPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";

private static bool SetProxyFallback(string? strProxy, string? exceptions, int type)
{
if (type == 1)
{
Utils.RegWriteValue(_regPath, "ProxyEnable", 0);
Utils.RegWriteValue(_regPath, "ProxyServer", string.Empty);
Utils.RegWriteValue(_regPath, "ProxyOverride", string.Empty);
Utils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty);
}
if (type == 2)
{
Utils.RegWriteValue(_regPath, "ProxyEnable", 1);
Utils.RegWriteValue(_regPath, "ProxyServer", strProxy ?? string.Empty);
Utils.RegWriteValue(_regPath, "ProxyOverride", exceptions ?? string.Empty);
Utils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty);
}
else if (type == 4)
{
Utils.RegWriteValue(_regPath, "ProxyEnable", 0);
Utils.RegWriteValue(_regPath, "ProxyServer", string.Empty);
Utils.RegWriteValue(_regPath, "ProxyOverride", string.Empty);
Utils.RegWriteValue(_regPath, "AutoConfigURL", strProxy ?? string.Empty);
}
return true;
}

/// <summary>
// set to use no proxy
/// </summary>
Expand Down Expand Up @@ -43,6 +71,7 @@ public static bool SetProxy(string? strProxy, string? exceptions, int type)
}
catch (Exception ex)
{
SetProxyFallback(strProxy, exceptions, type);
Logging.SaveLog(ex.Message, ex);
return false;
}
Expand Down Expand Up @@ -102,37 +131,37 @@ private static bool SetConnectionProxy(string? connectionName, string? strProxy,
}
else
{
list.szConnection = IntPtr.Zero;
list.szConnection = nint.Zero;
}
list.dwOptionCount = options.Length;
list.dwOptionError = 0;

int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
// make a pointer out of all that ...
IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length); // !! remember to deallocate memory 4
nint optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length); // !! remember to deallocate memory 4
// copy the array over into that spot in memory ...
for (int i = 0; i < options.Length; ++i)
{
if (Environment.Is64BitOperatingSystem)
{
IntPtr opt = new(optionsPtr.ToInt64() + (i * optSize));
nint opt = new(optionsPtr.ToInt64() + i * optSize);
Marshal.StructureToPtr(options[i], opt, false);
}
else
{
IntPtr opt = new(optionsPtr.ToInt32() + (i * optSize));
nint opt = new(optionsPtr.ToInt32() + i * optSize);
Marshal.StructureToPtr(options[i], opt, false);
}
}

list.options = optionsPtr;

// and then make a pointer out of the whole list
IntPtr ipcoListPtr = Marshal.AllocCoTaskMem(list.dwSize); // !! remember to deallocate memory 5
nint ipcoListPtr = Marshal.AllocCoTaskMem(list.dwSize); // !! remember to deallocate memory 5
Marshal.StructureToPtr(list, ipcoListPtr, false);

// and finally, call the API method!
bool isSuccess = NativeMethods.InternetSetOption(IntPtr.Zero,
bool isSuccess = NativeMethods.InternetSetOption(nint.Zero,
InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION,
ipcoListPtr, list.dwSize);
int returnvalue = 0; // ERROR_SUCCESS
Expand All @@ -143,12 +172,12 @@ private static bool SetConnectionProxy(string? connectionName, string? strProxy,
else
{
// Notify the system that the registry settings have been changed and cause them to be refreshed
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOption.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
NativeMethods.InternetSetOption(nint.Zero, InternetOption.INTERNET_OPTION_SETTINGS_CHANGED, nint.Zero, 0);
NativeMethods.InternetSetOption(nint.Zero, InternetOption.INTERNET_OPTION_REFRESH, nint.Zero, 0);
}

// FREE the data ASAP
if (list.szConnection != IntPtr.Zero) Marshal.FreeHGlobal(list.szConnection); // release mem 3
if (list.szConnection != nint.Zero) Marshal.FreeHGlobal(list.szConnection); // release mem 3
if (optionCount > 1)
{
Marshal.FreeHGlobal(options[1].m_Value.m_StringPtr); // release mem 1
Expand Down Expand Up @@ -212,12 +241,12 @@ private static IEnumerable<string> EnumerateRasEntries()
public struct InternetPerConnOptionList
{
public int dwSize; // size of the INTERNET_PER_CONN_OPTION_LIST struct
public IntPtr szConnection; // connection name to set/query options
public nint szConnection; // connection name to set/query options
public int dwOptionCount; // number of options to set/query
public int dwOptionError; // on error, which option failed

//[MarshalAs(UnmanagedType.)]
public IntPtr options;
public nint options;
};

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
Expand All @@ -244,7 +273,7 @@ public struct InternetConnectionOptionValue
public int m_Int;

[FieldOffset(0)]
public IntPtr m_StringPtr;
public nint m_StringPtr;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
Expand Down Expand Up @@ -316,7 +345,7 @@ internal static class NativeMethods
{
[DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
public static extern bool InternetSetOption(nint hInternet, InternetOption dwOption, nint lpBuffer, int dwBufferLength);

[DllImport("Rasapi32.dll", CharSet = CharSet.Auto)]
public static extern uint RasEnumEntries(
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Converters/DelayColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public object Convert(object value, Type targetType, object parameter, System.Gl

if (delay <= 0)
return new SolidColorBrush(Colors.Red);
if (delay <= 200)
if (delay <= 500)
return new SolidColorBrush(Colors.Green);
else
return new SolidColorBrush(Colors.IndianRed);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using v2rayN.Models;

namespace v2rayN.Handler
namespace v2rayN.Handler.Statistics
{
internal class StatisticsHandler
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using v2rayN.Enums;
using v2rayN.Models;

namespace v2rayN.Handler
namespace v2rayN.Handler.Statistics
{
internal class StatisticsSingbox
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using v2rayN.Enums;
using v2rayN.Models;

namespace v2rayN.Handler
namespace v2rayN.Handler.Statistics
{
internal class StatisticsV2ray
{
Expand Down
49 changes: 5 additions & 44 deletions v2rayN/v2rayN/Handler/SysProxyHandle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PacLib;
using v2rayN.Common;
using v2rayN.Enums;
using v2rayN.Models;

Expand Down Expand Up @@ -42,17 +43,11 @@ public static bool UpdateSysProxy(Config config, bool forceDisable)
.Replace("{http_port}", port.ToString())
.Replace("{socks_port}", portSocks.ToString());
}
if (!ProxySetting.SetProxy(strProxy, strExceptions, 2))
{
SetProxy(strProxy, strExceptions, 2);
}
ProxySetting.SetProxy(strProxy, strExceptions, 2);
}
else if (type == ESysProxyType.ForcedClear)
{
if (!ProxySetting.UnsetProxy())
{
UnsetProxy();
}
ProxySetting.UnsetProxy();
}
else if (type == ESysProxyType.Unchanged)
{
Expand All @@ -61,10 +56,7 @@ public static bool UpdateSysProxy(Config config, bool forceDisable)
{
PacHandler.Start(Utils.GetConfigPath(), port, portPac);
var strProxy = $"{Global.HttpProtocol}{Global.Loopback}:{portPac}/pac?t={DateTime.Now.Ticks}";
if (!ProxySetting.SetProxy(strProxy, "", 4))
{
SetProxy(strProxy, "", 4);
}
ProxySetting.SetProxy(strProxy, "", 4);
}

if (type != ESysProxyType.Pac)
Expand All @@ -81,38 +73,7 @@ public static bool UpdateSysProxy(Config config, bool forceDisable)

public static void ResetIEProxy4WindowsShutDown()
{
SetProxy(null, null, 1);
}

private static void UnsetProxy()
{
SetProxy(null, null, 1);
}

private static bool SetProxy(string? strProxy, string? exceptions, int type)
{
if (type == 1)
{
Utils.RegWriteValue(_regPath, "ProxyEnable", 0);
Utils.RegWriteValue(_regPath, "ProxyServer", string.Empty);
Utils.RegWriteValue(_regPath, "ProxyOverride", string.Empty);
Utils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty);
}
if (type == 2)
{
Utils.RegWriteValue(_regPath, "ProxyEnable", 1);
Utils.RegWriteValue(_regPath, "ProxyServer", strProxy ?? string.Empty);
Utils.RegWriteValue(_regPath, "ProxyOverride", exceptions ?? string.Empty);
Utils.RegWriteValue(_regPath, "AutoConfigURL", string.Empty);
}
else if (type == 4)
{
Utils.RegWriteValue(_regPath, "ProxyEnable", 0);
Utils.RegWriteValue(_regPath, "ProxyServer", string.Empty);
Utils.RegWriteValue(_regPath, "ProxyOverride", string.Empty);
Utils.RegWriteValue(_regPath, "AutoConfigURL", strProxy ?? string.Empty);
}
return true;
ProxySetting.UnsetProxy();
}
}
}
1 change: 1 addition & 0 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Handler.Fmt;
using v2rayN.Handler.Statistics;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.Views;
Expand Down

0 comments on commit 4caf1a1

Please sign in to comment.