Browse Source

use CommandLineParser to parse command line

tags/4.2.0.0
Student Main 4 years ago
parent
commit
f209f8d159
No known key found for this signature in database GPG Key ID: AA78519C208C8742
4 changed files with 42 additions and 41 deletions
  1. +10
    -0
      shadowsocks-csharp/CommandLineOption.cs
  2. +27
    -41
      shadowsocks-csharp/Program.cs
  3. +1
    -0
      shadowsocks-csharp/packages.config
  4. +4
    -0
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 10
- 0
shadowsocks-csharp/CommandLineOption.cs View File

@@ -0,0 +1,10 @@
using CommandLine;

namespace Shadowsocks
{
public class CommandLineOption
{
[Option("open-url",Required = false,HelpText = "Add an ss:// URL")]
public string OpenUrl { get; set; }
}
}

+ 27
- 41
shadowsocks-csharp/Program.cs View File

@@ -1,20 +1,19 @@
using Microsoft.Win32;
using NLog;
using Shadowsocks.Controller;
using Shadowsocks.Controller.Hotkeys;
using Shadowsocks.Util;
using Shadowsocks.View;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommandLine;
using Microsoft.Win32;
using NLog;
using Shadowsocks.Controller;
using Shadowsocks.Controller.Hotkeys;
using Shadowsocks.Util;
using Shadowsocks.View;
namespace Shadowsocks
{
@@ -23,6 +22,7 @@ namespace Shadowsocks
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public static ShadowsocksController MainController { get; private set; }
public static MenuViewController MenuController { get; private set; }
public static CommandLineOption Options { get; private set; }
public static string[] Args { get; private set; }
// https://github.com/dotnet/runtime/issues/13051#issuecomment-510267727
@@ -35,6 +35,12 @@ namespace Shadowsocks
[STAThread]
private static void Main(string[] args)
{
// store args for further use
Args = args;
Parser.Default.ParseArguments<CommandLineOption>(args)
.WithParsed(opt => Options = opt)
.WithNotParsed(e => e.Output());
Directory.SetCurrentDirectory(WorkingDirectory);
// todo: initialize the NLog configuartion
Model.NLogConfig.TouchAndApplyNLogConfig();
@@ -43,8 +49,6 @@ namespace Shadowsocks
ServicePointManager.SecurityProtocol |=
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
// store args for further use
Args = args;
// Check OS since we are using dual-mode socket
if (!Utils.IsWinVistaOrHigher())
{
@@ -65,8 +69,6 @@ namespace Shadowsocks
}
string pipename = $"Shadowsocks\\{ExecutablePath.GetHashCode()}";
string addedUrl = null;
using (NamedPipeClientStream pipe = new NamedPipeClientStream(pipename))
{
bool pipeExist = false;
@@ -80,35 +82,19 @@ namespace Shadowsocks
pipeExist = false;
}
// TODO: switch to better argv parser when it's getting complicate
List<string> alist = Args.ToList();
// check --open-url param
int urlidx = alist.IndexOf("--open-url") + 1;
if (urlidx > 0)
// --open-url exist, and no other instance, add it later
if (pipeExist && !string.IsNullOrWhiteSpace(Options.OpenUrl))
{
if (Args.Length <= urlidx)
{
return;
}
// --open-url exist, and no other instance, add it later
if (!pipeExist)
{
addedUrl = Args[urlidx];
}
// has other instance, send url via pipe then exit
else
{
byte[] b = Encoding.UTF8.GetBytes(Args[urlidx]);
byte[] opAddUrl = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1));
byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length));
pipe.Write(opAddUrl, 0, 4); // opcode addurl
pipe.Write(blen, 0, 4);
pipe.Write(b, 0, b.Length);
pipe.Close();
return;
}
byte[] b = Encoding.UTF8.GetBytes(Options.OpenUrl);
byte[] opAddUrl = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(1));
byte[] blen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(b.Length));
pipe.Write(opAddUrl, 0, 4); // opcode addurl
pipe.Write(blen, 0, 4);
pipe.Write(b, 0, b.Length);
pipe.Close();
return;
}
// has another instance, and no need to communicate with it return
else if (pipeExist)
{
@@ -153,9 +139,9 @@ namespace Shadowsocks
NamedPipeServer namedPipeServer = new NamedPipeServer();
Task.Run(() => namedPipeServer.Run(pipename));
namedPipeServer.AddUrlRequested += (_1, e) => MainController.AskAddServerBySSURL(e.Url);
if (!addedUrl.IsNullOrEmpty())
if (!string.IsNullOrWhiteSpace(Options.OpenUrl))
{
MainController.AskAddServerBySSURL(addedUrl);
MainController.AskAddServerBySSURL(Options.OpenUrl);
}
Application.Run();
}


+ 1
- 0
shadowsocks-csharp/packages.config View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Caseless.Fody" version="1.8.3" targetFramework="net472" />
<package id="CommandLineParser" version="2.8.0" targetFramework="net472" />
<package id="Costura.Fody" version="3.3.3" targetFramework="net472" />
<package id="Fody" version="4.2.1" targetFramework="net472" developmentDependency="true" />
<package id="GlobalHotKey" version="1.1.0" targetFramework="net472" />


+ 4
- 0
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -71,6 +71,9 @@
<Reference Include="Caseless, Version=1.8.3.0, Culture=neutral, PublicKeyToken=409b3227471b0f0d, processorArchitecture=MSIL">
<HintPath>..\packages\Caseless.Fody.1.8.3\lib\net452\Caseless.dll</HintPath>
</Reference>
<Reference Include="CommandLine, Version=2.8.0.0, Culture=neutral, PublicKeyToken=5a870481e358d379, processorArchitecture=MSIL">
<HintPath>..\packages\CommandLineParser.2.8.0\lib\net461\CommandLine.dll</HintPath>
</Reference>
<Reference Include="Costura, Version=3.3.3.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.3.3.3\lib\net40\Costura.dll</HintPath>
</Reference>
@@ -162,6 +165,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CommandLineOption.cs" />
<Compile Include="Controller\HotkeyReg.cs" />
<Compile Include="Controller\LoggerExtension.cs" />
<Compile Include="Controller\Service\GeositeUpdater.cs" />


Loading…
Cancel
Save