예제 #1
0
 internal void SendFirstPeerTestToCharlie(PeerTest msg)
 {
     lock ( Sessions )
     {
         if (Sessions.Count > 0)
         {
             Sessions.Random().Value.SendFirstPeerTestToCharlie(msg);
         }
     }
 }
예제 #2
0
        void SendFirstPeerTestToCharlie(PeerTest msg)
        {
            SendMessage(
                SSUHeader.MessageTypes.PeerTest,
                Session.MACKey,
                Session.SharedKey,
                (start, writer) =>
            {
                Logging.LogTransport($"SSU {this}: PeerTest. We are Bob, relaying first message to Charlie. {msg}");
                msg.WriteTo(writer);

                return(true);
            });
        }
예제 #3
0
        private void HandleIncomingPeerTestPackage(BufRefLen reader)
        {
            // We are Alice or Charlie and are receiving probe packet with intro key.

            var msg = new PeerTest(reader);

            if (Session.Host.PeerTestInstance.OurTestNonce(msg.TestNonce))
            {
                // We are alice running a test
                Logging.LogTransport($"SSU {this}: PeerTest. We are Alice, and are getting a direct probe from Charlie. {msg}");
                Session.Host.PeerTestInstance.CharlieDirectResponseReceived(msg);
                return;
            }

            var nonceinfo = Session.Host.GetNonceInfo(msg.TestNonce.Peek32(0));

            if (nonceinfo == null)
            {
                Logging.LogTransport($"SSU {this}: PeerTest. Created state: HandleIncomingPeerTestPackage received an unknown nonce. Dropped.");
                return;
            }

            if (nonceinfo.Role == PeerTestRole.Charlie)
            {
                // We are Charlie and are getting a direct probe from Alice

                SendMessage(
                    SSUHeader.MessageTypes.PeerTest,
                    msg.IntroKey,
                    msg.IntroKey,
                    (start, writer) =>
                {
                    var toalice = new PeerTest(msg.TestNonce, msg.AliceIPAddr, msg.AlicePort, Session.MyRouterContext.IntroKey);
                    Logging.LogTransport($"SSU {this}: PeerTest. We are Charlie, and are getting a direct probe from Alice. {msg}");
                    toalice.WriteTo(writer);

                    return(true);
                });
            }
            else
            {
                Logging.LogTransport("SSU {this}: PeerTest. Unexpected PeerTest received: {msg}");
            }
        }
예제 #4
0
        private void RespondToPeerTestInitiationFromAlice(PeerTest msg, PeerTestNonceInfo nonceinfo)
        {
            // Nonce already in use for another test? Just ignore.
            if (nonceinfo != null)
            {
                Logging.LogTransport("SSU {this}: PeerTest. We are Bob getting a iniation from Alice, but will drop it due to nonce clash. {msg}");
                return;
            }

            Session.Host.SetNonceInfo(msg.TestNonce.Peek32(0), PeerTestRole.Bob);

            var pt = new PeerTest(msg.TestNonce,
                                  Session.RemoteEP.Address, Session.RemoteEP.Port,
                                  msg.IntroKey);

            Logging.LogTransport($"SSU {this}: PeerTest. We are Bob and sending first relay to Charlie: {pt}");
            Session.Host.SendFirstPeerTestToCharlie(pt);
            return;
        }
예제 #5
0
        private void HandleIncomingPeerTestPackage(BufRefLen reader)
        {
            var msg       = new PeerTest(reader);
            var nonceinfo = Session.Host.GetNonceInfo(msg.TestNonce.Peek32(0));

            if (msg.AliceIPAddr.Length == 0 && msg.AlicePort.Peek16(0) == 0)
            {
                // We are Bob, and we got the first message from Alice
                RespondToPeerTestInitiationFromAlice(msg, nonceinfo);
                return;
            }

            if (!msg.IPAddressOk)
            {
                Logging.LogTransport($"SSU {this}: HandleIncomingPeerTestPackage: IP Address not accepted. Ignorning. {msg}");
                return;
            }

            if (nonceinfo != null)
            {
                switch (nonceinfo.Role)
                {
                case PeerTestRole.Bob:
                {
                    // Got initial response from Charlie
                    // Try to contact Alice
                    SendMessage(
                        new IPEndPoint(new IPAddress(msg.AliceIPAddr.ToByteArray()), msg.AlicePort.PeekFlip16(0)),
                        SSUHeader.MessageTypes.PeerTest,
                        msg.IntroKey,
                        msg.IntroKey,
                        (start, writer) =>
                        {
                            Logging.LogTransport($"SSU {this}: PeerTest. We are Bob, relaying response from Charlie to Alice {msg}");
                            msg.WriteTo(writer);

                            return(true);
                        });
                }
                    return;

                case PeerTestRole.Charlie:
                {
                    // We got the final test from Alice
                    SendMessage(
                        SSUHeader.MessageTypes.PeerTest,
                        Session.MACKey,
                        Session.SharedKey,
                        (start, writer) =>
                        {
                            Logging.LogTransport($"SSU {this}: PeerTest. We are Charlie, responding to the final message from Alice {msg}");
                            msg.WriteTo(writer);

                            return(true);
                        });
                }
                    return;

                default:
                    Logging.Log($"SSU {this}: PeerTest. Unexpedted PeerTest received. {msg}");
                    break;
                }
            }
            else
            {
                // We are Charlie receiving a relay from Bob.
                Session.Host.SetNonceInfo(msg.TestNonce.Peek32(0), PeerTestRole.Charlie);

                // Reply to Bob
                SendMessage(
                    SSUHeader.MessageTypes.PeerTest,
                    Session.MACKey,
                    Session.SharedKey,
                    (start, writer) =>
                {
                    Logging.LogTransport("SSU {this}: PeerTest. We are Charlie, responding to Bob. {msg}");
                    msg.WriteTo(writer);

                    return(true);
                });

                // Try to contact Alice
                var toalice = new PeerTest(msg.TestNonce, msg.AliceIPAddr, msg.AlicePort, Session.MyRouterContext.IntroKey);
                SendMessage(
                    new IPEndPoint(new IPAddress(msg.AliceIPAddr.ToByteArray()), msg.AlicePort.PeekFlip16(0)),
                    SSUHeader.MessageTypes.PeerTest,
                    msg.IntroKey,
                    msg.IntroKey,
                    (start, writer) =>
                {
                    Logging.LogTransport($"SSU {this}: PeerTest. We are Charlie, sending first probe to Alice. {toalice}");
                    toalice.WriteTo(writer);

                    return(true);
                });
            }
        }
예제 #6
0
 internal void CharlieDirectResponseReceived(PeerTest msg)
 {
 }