Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyts committed Jun 6, 2023
1 parent 22646f1 commit bda75d7
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 0 deletions.
25 changes: 25 additions & 0 deletions XDPing.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XDPing", "XDPing\XDPing.csproj", "{02DDDBD4-90C7-453C-AFAC-3F3408328F7C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{02DDDBD4-90C7-453C-AFAC-3F3408328F7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02DDDBD4-90C7-453C-AFAC-3F3408328F7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02DDDBD4-90C7-453C-AFAC-3F3408328F7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02DDDBD4-90C7-453C-AFAC-3F3408328F7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2C769790-38FF-4C55-8887-5AEC0A22E3DD}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions XDPing/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
148 changes: 148 additions & 0 deletions XDPing/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Text;
// Required for sockets
using System.Net.Sockets;

namespace XDPing
{
class Program
{
static int Main(string[] args)
{
string deliverycontroller = string.Empty;
int port = 80;

for (int i = 0; i < args.Length; i++)
{
var arg = args[i].ToLower();
if (arg == "-deliverycontroller" || arg == "--deliverycontroller")
{
if (args.Length >= i + 2)
{
deliverycontroller = args[i + 1];
}
}

if (arg == "-port" || arg == "--port")
{
if (args.Length >= i + 2)
{
int.TryParse(args[i + 1], out port);
}
}
}

if (string.IsNullOrEmpty(deliverycontroller))
{
Console.WriteLine("Valid command line arguments must be supplied:");
Console.WriteLine("-deliverycontroller or --deliverycontroller is a required flag. This must be a Delivery Controller or Cloud Connector.");
Console.WriteLine("-port or --port is an optional flag. It will default to 80 if not supplied. This is the port the IRegistrar service listens on.");
return -1;
}

XDPing(deliverycontroller, port);

return 0;
}

/// <summary>
/// Performs an XDPing to make sure the Delivery Controller or Cloud Connector is in a healthy state.
/// It test whether the Broker service is reachable, listening and processing requests on its configured port.
/// We do this by issuing a blank HTTP POST requests to the Broker's IRegistrar service.
/// Including "Expect: 100-continue" in the body will ensure we receive a respose of "HTTP/1.1 100 Continue",
/// which is what we use to verify that it's in a healthy state.
/// </summary>
/// <param name="deliverycontroller"></param>
/// <param name="port"></param>
/// <returns></returns>
static private bool XDPing(string deliverycontroller, int port)
{
// This code has essentially been taken from the Citrix Health Assistant Tool and improved for reliability and troubleshooting purposes.
// I was able to reverse engineer the process by decompiling the VDAAssistant.Backend.dll, which is a component of the Citrix Health
// Assistant Tool.
string service = "http://" + deliverycontroller + ":" + port +"/Citrix/CdsController/IRegistrar";
string s = string.Format("POST {0} HTTP/1.1\r\nContent-Type: application/soap+xml; charset=utf-8\r\nHost: {1}:{2}\r\nContent-Length: 1\r\nExpect: 100-continue\r\nConnection: Close\r\n\r\n", (object)service, (object)deliverycontroller, (object)port);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Attempting an XDPing against " + deliverycontroller + " on TCP port number " + port.ToString());
bool listening = false;
try
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(deliverycontroller, port);
if (socket.Connected)
{
stringBuilder.AppendLine("- Socket connected");
byte[] bytes = Encoding.ASCII.GetBytes(s);
// Send the string as bytes.
socket.Send(bytes, bytes.Length, SocketFlags.None);
stringBuilder.AppendLine("- Sent the data");
byte[] numArray = new byte[21];
socket.ReceiveTimeout = 5000;
socket.Receive(numArray);
stringBuilder.AppendLine("- Received the following 21 byte array: " + BitConverter.ToString(numArray));
// ASCII conversion - string from bytes
string strASCII = Encoding.ASCII.GetString(numArray, 0, numArray.Length);
// UTF conversion - String from bytes
string strUTF8 = Encoding.UTF8.GetString(numArray, 0, numArray.Length);
stringBuilder.AppendLine("- Converting the byte array to an ASCII string we get the output between the quotes: \"" + strASCII + "\"");
stringBuilder.AppendLine("- Converting the byte array to a UTF8 string we get the output between the quotes: \"" + strUTF8 + "\"");
// Send an additional single byte of 32 (space) as 1 byte with no flags.
socket.Send(new byte[1] { (byte)32 }, 1, SocketFlags.None);
stringBuilder.AppendLine("- Sending the following string as a byte to help clear the connection: \"" + BitConverter.ToString(new byte[1] { 32 }) + "\"");
if (strASCII.Trim().IndexOf("HTTP/1.1 100 Continue", StringComparison.CurrentCultureIgnoreCase) == 0)
{
listening = true;
stringBuilder.AppendLine("- The service is listening and healthy");
}
else
{
stringBuilder.AppendLine("- The service is not listening");
}
}
else
{
stringBuilder.AppendLine("- Socket failed to connect");
}
}
catch (SocketException se)
{
stringBuilder.AppendLine("- Failed to connect to service");
stringBuilder.AppendLine("- ERROR: " + se.Message);
}
catch (Exception e)
{
stringBuilder.AppendLine("- Failed with an unexpected error");
stringBuilder.AppendLine("- ERROR: " + e.Message);
}
if (socket.Connected)
{
try
{
socket.Close();
stringBuilder.AppendLine("- Socket closed");
}
catch (SocketException se)
{
stringBuilder.AppendLine("- Failed to close the socket");
stringBuilder.AppendLine("- ERROR: " + se.Message);
}
catch (Exception e)
{
stringBuilder.AppendLine("- Failed with an unexpected error");
stringBuilder.AppendLine("- ERROR: " + e.Message);
}
}
socket.Dispose();
}
catch (Exception e)
{
stringBuilder.AppendLine("- Failed to create a socket");
stringBuilder.AppendLine("- ERROR: " + e.Message);
}
Console.WriteLine(stringBuilder.ToString().Substring(0, stringBuilder.ToString().Length - 1));
return listening;
}
}
}
36 changes: 36 additions & 0 deletions XDPing/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("XDPing")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("XDPing")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("02dddbd4-90c7-453c-afac-3f3408328f7c")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
53 changes: 53 additions & 0 deletions XDPing/XDPing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{02DDDBD4-90C7-453C-AFAC-3F3408328F7C}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>XDPing</RootNamespace>
<AssemblyName>XDPing</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit bda75d7

Please sign in to comment.