-
Notifications
You must be signed in to change notification settings - Fork 24
Connection validation with shared key #14
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,13 @@ namespace Mirror.LiteNetLib4Mirror | |
public static class LiteNetLib4MirrorUtils | ||
{ | ||
internal static ushort LastForwardedPort; | ||
internal static readonly string ApplicationName; | ||
public static string SharedKey { get; set; } | ||
public static bool UpnpFailed { get; private set; } | ||
public static IPAddress ExternalIp { get; private set; } | ||
|
||
static LiteNetLib4MirrorUtils() | ||
{ | ||
ApplicationName = Application.productName; | ||
SharedKey = Application.productName + Application.companyName; | ||
} | ||
|
||
public static string ToBase64(string text) | ||
|
@@ -52,11 +52,11 @@ public static NetDataWriter ReusePut(NetDataWriter writer, string text, ref stri | |
|
||
public static NetDataWriter ReusePutDiscovery(NetDataWriter writer, string text, ref string lastText) | ||
{ | ||
if (ApplicationName + text != lastText) | ||
if (SharedKey + text != lastText) | ||
{ | ||
lastText = ApplicationName + text; | ||
lastText = SharedKey + text; | ||
writer.Reset(); | ||
writer.Put(ApplicationName); | ||
writer.Put(SharedKey); | ||
writer.Put(ToBase64(text)); | ||
} | ||
|
||
|
@@ -155,7 +155,7 @@ private static async Task ForwardPortInternalAsync(ushort port, int milisecondsD | |
} | ||
|
||
ExternalIp = await device.GetExternalIPAsync(); | ||
await device.CreatePortMapAsync(new Mapping(networkProtocolType, IPAddress.None, port, port, 0, ApplicationName)).ConfigureAwait(false); | ||
await device.CreatePortMapAsync(new Mapping(networkProtocolType, IPAddress.None, port, port, 0, Application.productName)).ConfigureAwait(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work, as this is a task and that property can only be fetched from the main thread. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does execute on the main thread in my test project for me, but I will change it to be thread-safe, as it probably can't be guaranteed that it will always run on the main thread. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, i forgot that unity always runs async code on the main thread, still, having it fetched outside is safer. |
||
LastForwardedPort = port; | ||
Debug.Log($"Port {port.ToString()} forwarded successfully!"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather have that shared key as a static protected variable in LiteNetLib4MirrorDiscovery that'd be set by a protected virtual method that could be overriden.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, sure thing. I will implement it as you describe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, because
LiteNetLib4MirrorTransport
currently usesLiteNetLib4MirrorUtils.ApplicationName
to generate a connection key,LiteNetLib4MirrorDiscovery
might not be the best place for this, as for the use case this PR addresses, both network discovery and connection need to be independent of usingApplication.productName
, and you probably wouldn't want to haveLiteNetLib4MirrorTransport
referenceLiteNetLib4MirrorDiscovery.SharedKey
in itsGetConnectKey
method. That is why I initially went with adding this toLiteNetLib4MirrorUtils
.Perhaps this shared key could be a static protected property in the transport instead, with a protected virtual method to set it? Or even just a serialized variable in the transport, in the same way that
authKey
currently is?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetConnectData which uses GetConnectKey is already overridable there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
GetConnectKey
itself needs to be overridable, asServerStart
calls it directly. OrLiteNetLib4MirrorTransport
needs a similar protectedConnectKey
property and overridable setter method (which would match the proposed change toLiteNetLib4MirrorDiscovery
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it does call that, then please make that overridable.