public static void Negotiate(NatPortType portType, byte version, uint cookie) { //find Sessions according to the partern Dictionary <string, NNSession> result = SessionPool .Where(s => s.Key.Contains(portType.ToString())) .ToDictionary(s => s.Key, s => s.Value); //there are at least a pair of session if (result.Count < 2) { LogWriter.ToLog(LogEventLevel.Debug, "No match found we continue waitting."); return; } //find the negotiator pairs var negotiatorPairs = result.Where(s => s.Value.UserInfo.Cookie == cookie); //find negitiators and negotiatees by a same cookie var negotiators = negotiatorPairs.Where(s => s.Value.UserInfo.ClientIndex == 0); var negotiatees = negotiatorPairs.Where(s => s.Value.UserInfo.ClientIndex == 1); //negotiator are not ready, we keep waitting if (negotiators.Count() < 1 || negotiatees.Count() < 1) { return; } //we find both negotiators var negotiator = negotiators.First(); var negotiatee = negotiatees.First(); LogWriter.ToLog(LogEventLevel.Debug, $"Find negotiator {negotiator.Value.RemoteEndPoint}"); LogWriter.ToLog(LogEventLevel.Debug, $"Find negotiatee {negotiatee.Value.RemoteEndPoint}"); byte[] dataToNegotiator = new ConnectResponse(version, cookie, negotiatee.Value.RemoteEndPoint).BuildResponse(); byte[] dataToNegotiatee = new ConnectResponse(version, cookie, negotiator.Value.RemoteEndPoint).BuildResponse(); negotiator.Value.UserInfo.UpdateRetryNATNegotiationTime(); negotiatee.Value.UserInfo.UpdateRetryNATNegotiationTime(); negotiator.Value.SendAsync(dataToNegotiator); negotiatee.Value.SendAsync(dataToNegotiatee); //finally we remove two sessions from our session pool SessionPool.TryRemove(negotiatee.Key, out _); SessionPool.TryRemove(negotiator.Key, out _); }
public static void FindNatNegotiatorsAndSendConnectPacket(NatPortType portType, byte version, uint cookie) { List <NatNegSession> result = Sessions. Where(s => s.Key.Contains(portType.ToString())). Select(s => s.Value).ToList(); if (result.Count < 2) { LogWriter.ToLog(LogEventLevel.Debug, "No match found we contine wait."); return; } var negotiatorPairs = result.Where(s => s.UserInfo.Cookie == cookie); var negotiators = negotiatorPairs.Where(s => s.UserInfo.ClientIndex == 0); var negotiatees = negotiatorPairs.Where(s => s.UserInfo.ClientIndex == 1); if (negotiators.Count() < 1 || negotiatees.Count() < 1) { return; } var negotiator = negotiators.First(); var negotiatee = negotiatees.First(); LogWriter.ToLog(LogEventLevel.Debug, $"Find negotiator {negotiator.RemoteEndPoint}"); LogWriter.ToLog(LogEventLevel.Debug, $"Find negotiatee {negotiatee.RemoteEndPoint}"); ConnectPacket packetToNegotiator = new ConnectPacket(); byte[] dataToNegotiator = packetToNegotiator .SetVersionAndCookie(version, cookie) .SetRemoteEndPoint(negotiatee.RemoteEndPoint) .BuildResponse(); ConnectPacket packetToNegotiatee = new ConnectPacket(); byte[] dataToNegotiatee = packetToNegotiatee .SetVersionAndCookie(version, cookie) .SetRemoteEndPoint(negotiator.RemoteEndPoint) .BuildResponse(); negotiator.UserInfo.UpdateRetryNatNegotiationTime(); negotiatee.UserInfo.UpdateRetryNatNegotiationTime(); negotiator.SendAsync(dataToNegotiator); negotiatee.SendAsync(dataToNegotiatee); }
public InitRequest SetPortType(NatPortType type) { PortType = type; return(this); }
public InitPacket SetPortType(NatPortType type) { PortType = type; return(this); }