示例#1
0
        public Boolean TryConnectAndInitialize(TlsSettings tlsSettings, Buf sendBuffer, ServerInfo serverInfo /*, SelectTunnelsThread tunnelsThread*/)
        {
            if (Connected)
            {
                throw new InvalidOperationException(String.Format(
                                                        "You called Connect() on accessor '{0}' but its already connected", accessorHostString));
            }

            try
            {
                accessorSocket = NewSocketConnection(ref accessorHost);

                //
                // Send initial connection information
                //
                Boolean setupTls = tlsSettings.requireTlsForTmpConnections;

                Byte[] connectionInfoPacket = new Byte[] { Tmp.CreateConnectionInfoFromTmpServerToAccessor(
                                                               tlsSettings.requireTlsForTmpConnections, false) };
                accessorSocket.Send(connectionInfoPacket);

                //
                // Only receive packet if tls was not required
                //
                if (!tlsSettings.requireTlsForTmpConnections)
                {
                    int bytesRead = accessorSocket.Receive(connectionInfoPacket, 0, 1, SocketFlags.None);
                    if (bytesRead <= 0)
                    {
                        throw new SocketException();
                    }

                    setupTls = Tmp.ReadTlsRequirementFromAccessorToTmpServer(connectionInfoPacket[0]);
                }


                DataHandler accessorSendHandler = new SocketSendDataHandler(accessorSocket).HandleData;
                this.accessorReceiveHandler = new DataFilterHandler(new FrameProtocolFilter(),
                                                                    new TmpServerHandler(this, tlsSettings /*, tunnelsThread*/));

                //
                // Initiate a tls connection if required
                //
                if (setupTls)
                {
                    throw new NotImplementedException("Tls not yet implemented");
                }

                //
                // Send server info
                //
                UInt32 commandLength = Tmp.SerializeCommand(ServerInfo.Serializer, Tmp.ToAccessorServerInfoID, serverInfo, sendBuffer, 0);
                accessorSendHandler(sendBuffer.array, 0, commandLength);

                return(true);
            }
            catch (Exception)
            {
                this.accessorSocket         = null;
                this.accessorReceiveHandler = null;
                return(false);
            }
        }
示例#2
0
            public void AcceptClientHandler(ref SelectControl control, Socket listenSocket, Buf safeBuffer)
            //(Socket listenSocket, Socket socket, Buf safeBuffer)
            {
                //return tmpConnectionManager.AcceptAndInitiateTunnel(this, socket, safeBuffer);

                Socket clientSocket = listenSocket.Accept();

                Console.WriteLine("{0} [{1}] Received tunnel connection for server '{2}' to connect to target '{3}:{4}'",
                                  DateTime.Now, clientSocket.SafeRemoteEndPointString(), serverName, targetHost, targetPort);

                //
                // Check if server is connected
                //
                TmpControlConnection tmpControlConnection = TryGetTmpControlConnection(serverName);

                if (tmpControlConnection == null)
                {
                    Console.WriteLine("{0} [{1}] Server '{2}' is not currently connected", DateTime.Now, clientSocket.SafeRemoteEndPointString(), serverName);
                    clientSocket.ShutdownAndDispose();
                    return;
                }

                //
                // Generate a tunnel key
                //
                Int32 randomKey = random.Next();

                //
                // TODO: This would generate an infinite loop if every single key was taken up in the dictionary,
                //       however, I'm not sure if I should worry about this or not? Maybe there's a better way to do
                //       this?
                //
                while (true)
                {
                    if (!incompleteTunnels.ContainsKey(randomKey))
                    {
                        break;
                    }
                    randomKey++;
                }

                DisconnectedTunnel disconnectedTunnel = new DisconnectedTunnel(clientSocket);

                incompleteTunnels.Add(randomKey, disconnectedTunnel);

                Byte[] tunnelKey = new Byte[4];
                tunnelKey[0] = (Byte)(randomKey >> 24);
                tunnelKey[1] = (Byte)(randomKey >> 16);
                tunnelKey[2] = (Byte)(randomKey >> 8);
                tunnelKey[3] = (Byte)(randomKey);

                //
                // Send Open Tunnel command to TmpServer
                //
                OpenAccessorTunnelRequest request = new OpenAccessorTunnelRequest(0,
                                                                                  targetHostBytes, targetPort, tunnelKey);
                UInt32 commandLength = Tmp.SerializeCommand <OpenAccessorTunnelRequest>(OpenAccessorTunnelRequest.Serializer,
                                                                                        Tmp.ToServerOpenAccessorTunnelRequestID, request, safeBuffer, 0);

                tmpControlConnection.dataSender.HandleData(safeBuffer.array, 0, commandLength);

                //
                // Create a diconnected tunnel handler
                //
                control.AddReceiveSocket(clientSocket, disconnectedTunnel.ConnectedSocketReceiveHandler);
            }