Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SRV record support #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,10 @@ $RECYCLE.BIN/

# Mac desktop service store files
.DS_Store

# NuGet
/packages

# Dist files
/cards.cdb
/WindBot.exe
10 changes: 10 additions & 0 deletions Game/ConnectionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Net;

namespace WindBot.Game
{
public class ConnectionInfo
{
public IPAddress host;
public int port;
}
}
74 changes: 62 additions & 12 deletions Game/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using YGOSharp.Network;
using YGOSharp.Network.Enums;
using YGOSharp.Network.Utils;
using DnsClient;
using DnsClient.Protocol;

namespace WindBot.Game
{
Expand Down Expand Up @@ -41,6 +43,64 @@ public GameClient(WindBotInfo Info)
_proVersion = (short)Info.Version;
}

private IPAddress LookupHost(string hostname)
{
IPAddress address;
try
{
address = IPAddress.Parse(hostname);
}
catch (System.Exception)
{
IPHostEntry _hostEntry = Dns.GetHostEntry(hostname);
address = _hostEntry.AddressList.FirstOrDefault(findIPv4 => findIPv4.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
}
if (Debug)
Logger.WriteLine($"Resolved {hostname} to {address.ToString()}");
return address;
}

private ConnectionInfo ParseHost()
{
var conn = new ConnectionInfo();
if (_serverPort == 0)
{
var srvAddress = $"_ygopro._tcp.{_serverHost}";
if (Debug)
Logger.WriteLine($"Resolving SRV record {srvAddress}");
var srvResult = new LookupClient().Query(srvAddress, QueryType.SRV);
if (!srvResult.HasError)
{
var record = srvResult.Answers.OfType<SrvRecord>()
.FirstOrDefault();
if (record != null)
{
if (Debug)
Logger.WriteLine($"Resolved {srvAddress} to {record.Target}:{record.Port}");
var additionalRecord = srvResult.Additionals
.FirstOrDefault(p => p.DomainName.Equals(record.Target));
if (additionalRecord is ARecord aRecord)
{
conn.host = aRecord.Address;
} else if (additionalRecord is CNameRecord cNameRecord)
{
conn.host = LookupHost(cNameRecord.CanonicalName);
}
else
{
conn.host = LookupHost(record.Target);
}
conn.port = record.Port;
return conn;
}
}

conn.port = 7911;
}
conn.host = LookupHost(_serverHost);
return conn;
}

public void Start()
{
Connection = new YGOClient();
Expand All @@ -49,18 +109,8 @@ public void Start()
Connection.Connected += OnConnected;
Connection.PacketReceived += OnPacketReceived;

IPAddress target_address;
try
{
target_address = IPAddress.Parse(_serverHost);
}
catch (System.Exception)
{
IPHostEntry _hostEntry = Dns.GetHostEntry(_serverHost);
target_address = _hostEntry.AddressList.FirstOrDefault(findIPv4 => findIPv4.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
}

Connection.Connect(target_address, _serverPort);
var conn = ParseHost();
Connection.Connect(conn.host, conn.port);
}

private void OnConnected()
Expand Down
22 changes: 20 additions & 2 deletions WindBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WindBot</RootNamespace>
<AssemblyName>WindBot</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.0 please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That DnsClient package supports only from 4.7.1.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then we won't support DnsClient

</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -59,6 +59,22 @@
<Reference Include="YGOSharp.OCGWrapper.Enums">
<HintPath>.\YGOSharp.OCGWrapper.Enums.dll</HintPath>
</Reference>
<Reference Include="System.Buffers">
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="System.Security.Principal.Windows">
<HintPath>packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
</Reference>
<Reference Include="System.Security.AccessControl">
<HintPath>packages\System.Security.AccessControl.5.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.Registry">
<HintPath>packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
<Reference Include="DnsClient">
<HintPath>packages\DnsClient.1.6.1\lib\net471\DnsClient.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
Expand Down Expand Up @@ -147,12 +163,14 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WindBotInfo.cs" />
<Compile Include="Game\ConnectionInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="sqlite3.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<None Include="Decks\*.ydk">
Expand All @@ -173,4 +191,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
2 changes: 1 addition & 1 deletion WindBotInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public WindBotInfo()
DeckFile = null;
Dialog = "default";
Host = "127.0.0.1";
Port = 7911;
Port = 0;
HostInfo = "";
Version = 0x1353;
Hand = 0;
Expand Down
8 changes: 8 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DnsClient" version="1.6.1" targetFramework="net48" />
<package id="Microsoft.Win32.Registry" version="5.0.0" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Security.AccessControl" version="5.0.0" targetFramework="net48" />
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
</packages>