2017-01-16 20:59:50 +01:00
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
* that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
* following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
* materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2017-01-16 20:59:50 +01:00
|
|
|
|
|
|
|
using System;
|
2022-05-06 14:00:22 +02:00
|
|
|
using System.Reflection;
|
2022-05-06 14:54:21 +02:00
|
|
|
using System.Text;
|
2022-05-10 14:47:03 +02:00
|
|
|
using ThinCLI.Properties;
|
2017-01-16 20:59:50 +01:00
|
|
|
|
|
|
|
|
2019-07-02 01:23:01 +02:00
|
|
|
namespace ThinCLI
|
2017-01-16 20:59:50 +01:00
|
|
|
{
|
2022-05-18 13:19:23 +02:00
|
|
|
internal static class MainClass
|
2019-07-02 01:23:01 +02:00
|
|
|
{
|
2022-05-06 14:00:22 +02:00
|
|
|
public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version;
|
|
|
|
|
2019-07-02 01:23:01 +02:00
|
|
|
public static void Main(string[] args)
|
|
|
|
{
|
2022-05-06 14:30:51 +02:00
|
|
|
var conf = new Config();
|
2017-01-16 20:59:50 +01:00
|
|
|
|
2019-07-02 01:23:01 +02:00
|
|
|
string body = "";
|
2017-01-16 20:59:50 +01:00
|
|
|
|
2019-07-02 01:23:01 +02:00
|
|
|
for (int i = 0; i < args.Length; i++)
|
|
|
|
{
|
|
|
|
string s = args[i];
|
2017-01-16 20:59:50 +01:00
|
|
|
try
|
|
|
|
{
|
2022-05-06 14:54:21 +02:00
|
|
|
if (s.Equals("-s"))
|
2019-07-02 01:23:01 +02:00
|
|
|
{
|
2022-05-10 20:27:58 +02:00
|
|
|
conf.Hostname = args[++i];
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
|
|
|
else if (s.Equals("-u"))
|
|
|
|
{
|
2022-05-10 20:27:58 +02:00
|
|
|
conf.Username = args[++i];
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
|
|
|
else if (s.Equals("-pw"))
|
|
|
|
{
|
2022-05-10 20:27:58 +02:00
|
|
|
conf.Password = args[++i];
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
|
|
|
else if (s.Equals("-p"))
|
|
|
|
{
|
2022-05-10 20:27:58 +02:00
|
|
|
conf.Port = int.Parse(args[++i]);
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
|
|
|
else if (s.Equals("-debug"))
|
|
|
|
{
|
2022-05-10 20:27:58 +02:00
|
|
|
conf.Debug = true;
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
2022-05-10 14:47:03 +02:00
|
|
|
else if (s.Equals("-no-warn-new-certificates"))
|
|
|
|
{
|
|
|
|
conf.NoWarnNewCertificates = true;
|
|
|
|
}
|
|
|
|
else if (s.Equals("-no-warn-certificates"))
|
|
|
|
{
|
|
|
|
conf.NoWarnCertificates = true;
|
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
else if (s.Equals("-version"))
|
|
|
|
{
|
2022-05-06 14:00:22 +02:00
|
|
|
Console.WriteLine(Version.ToString());
|
2022-05-10 21:47:27 +02:00
|
|
|
return;
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
2022-05-06 14:54:21 +02:00
|
|
|
else if (s.Equals("-help") || s.Equals("/?"))
|
|
|
|
{
|
2022-05-18 12:43:57 +02:00
|
|
|
Logger.PrintUsage();
|
2022-05-10 21:47:27 +02:00
|
|
|
return;
|
2022-05-06 14:54:21 +02:00
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
else
|
|
|
|
{
|
2020-04-03 14:04:27 +02:00
|
|
|
if (s.Contains("="))
|
2022-05-10 21:47:27 +02:00
|
|
|
conf.EnteredParamValues.Add(s.Split('=')[1]);
|
2022-05-06 14:30:51 +02:00
|
|
|
|
2019-07-02 01:23:01 +02:00
|
|
|
body += s + "\n";
|
|
|
|
}
|
2017-01-16 20:59:50 +01:00
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
catch
|
|
|
|
{
|
2022-05-06 11:52:38 +02:00
|
|
|
Logger.Error("Failed to parse command-line arguments");
|
2022-05-18 12:43:57 +02:00
|
|
|
Logger.PrintUsage();
|
2019-07-02 01:23:01 +02:00
|
|
|
Environment.Exit(1);
|
2017-01-16 20:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
|
2022-05-10 14:47:03 +02:00
|
|
|
Settings.UpgradeFromPreviousVersion(conf);
|
|
|
|
|
2022-05-10 20:27:58 +02:00
|
|
|
if (string.IsNullOrEmpty(conf.Hostname))
|
2017-01-16 20:59:50 +01:00
|
|
|
{
|
2022-05-06 11:52:38 +02:00
|
|
|
Logger.Error("No hostname was specified.");
|
2022-05-18 12:43:57 +02:00
|
|
|
Logger.PrintUsage();
|
2017-01-16 20:59:50 +01:00
|
|
|
Environment.Exit(1);
|
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
|
2022-05-10 20:27:58 +02:00
|
|
|
if (string.IsNullOrEmpty(conf.Username))
|
|
|
|
{
|
|
|
|
Logger.Error("No username was specified.");
|
2022-05-18 12:43:57 +02:00
|
|
|
Logger.PrintUsage();
|
2022-05-10 20:27:58 +02:00
|
|
|
Environment.Exit(1);
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:47:27 +02:00
|
|
|
body += $"username={conf.Username}\n";
|
2022-05-18 13:19:23 +02:00
|
|
|
body += $"password={conf.Password}"; //do not add a line break after the last string
|
2022-05-10 21:47:27 +02:00
|
|
|
|
|
|
|
var command = "POST /cli HTTP/1.0\r\n" +
|
|
|
|
$"content-length: {Encoding.UTF8.GetBytes(body).Length}\r\n\r\n" +
|
|
|
|
body;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-05-10 14:47:03 +02:00
|
|
|
Marshalling.PerformCommand(command, conf);
|
2022-05-10 21:47:27 +02:00
|
|
|
}
|
2022-05-18 13:19:23 +02:00
|
|
|
catch (ThinCliProtocolException tcpEx)
|
2022-05-10 21:47:27 +02:00
|
|
|
{
|
|
|
|
Logger.Error(tcpEx.Message);
|
2022-05-10 14:47:03 +02:00
|
|
|
Logger.Debug(tcpEx, conf);
|
2022-05-10 21:47:27 +02:00
|
|
|
Environment.Exit(tcpEx.ExitCode);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.Error(ex.Message);
|
|
|
|
Logger.Debug(ex, conf);
|
|
|
|
Environment.Exit(1);
|
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
}
|
2022-05-06 11:55:28 +02:00
|
|
|
}
|
2019-07-02 01:23:01 +02:00
|
|
|
|
2022-05-18 13:19:23 +02:00
|
|
|
public class ThinCliProtocolException : Exception
|
2022-05-10 21:47:27 +02:00
|
|
|
{
|
2022-05-18 13:19:23 +02:00
|
|
|
public ThinCliProtocolException(string msg = null, int exitCode = 1)
|
2022-05-10 14:47:03 +02:00
|
|
|
: base(msg)
|
2022-05-10 21:47:27 +02:00
|
|
|
{
|
|
|
|
ExitCode = exitCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int ExitCode { get; }
|
|
|
|
}
|
2017-01-16 20:59:50 +01:00
|
|
|
}
|