Esempio n. 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);
            }
        }
Esempio n. 2
0
        public void HandleData(Byte[] data, UInt32 offset, UInt32 length)
        {
            byte id = data[offset];

            switch (id)
            {
            case Tmp.ToServerOpenTunnelRequestID:
                Console.WriteLine("{0} Got OpenTunnelRequest", DateTime.Now);

                break;

            case Tmp.ToServerOpenAccessorTunnelRequestID:
                Console.WriteLine("{0} Got OpenAccessorTunnelRequest", DateTime.Now);

                try
                {
                    OpenAccessorTunnelRequest request;
                    OpenAccessorTunnelRequest.Serializer.Deserialize(data, offset + 1, offset + length, out request);

                    throw new NotImplementedException();
#if COMMENT
                    //
                    // Connect to accessor
                    //
                    Socket socketToAccessor = accessorConnection.MakeNewSocketAndConnect();

                    //
                    // Handle encryption options
                    //
                    IDataHandler sendHandler = new SocketSendDataHandler(socketToAccessor);

                    if ((request.Options & TunnelOptions.RequireTls) == TunnelOptions.RequireTls)
                    {
                        //
                        // Send Connection Info
                        //
                        Byte[] connectionInfo = new Byte[] { Tmp.CreateConnectionInfoFromTmpServerToAccessor(true, true) };
                        socketToAccessor.Send(connectionInfo);

                        throw new NotImplementedException("Ssl not implemented for tunnels yet");
                    }
                    else
                    {
                        //
                        // Send connection info and tunnel key in the same packet
                        //
                        Byte[] connectionInfoAndTunnelKey = new Byte[2 + request.TunnelKey.Length];
                        connectionInfoAndTunnelKey[0] = Tmp.CreateConnectionInfoFromTmpServerToAccessor(false, true);
                        connectionInfoAndTunnelKey[1] = (Byte)request.TunnelKey.Length;
                        Array.Copy(request.TunnelKey, 0, connectionInfoAndTunnelKey, 2, request.TunnelKey.Length);
                        socketToAccessor.Send(connectionInfoAndTunnelKey);
                    }

                    //
                    // Connect to target
                    //
                    String     targetIPOrHost = Encoding.ASCII.GetString(request.TargetHost);
                    IPEndPoint targetEndPoint = new IPEndPoint(EndPoints.ParseIPOrResolveHost(targetIPOrHost, DnsPriority.IPv4ThenIPv6), request.TargetPort);

                    Socket socketToTarget = new Socket(targetEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    socketToTarget.Connect(targetEndPoint);

                    tunnelsThread.Add(socketToAccessor, socketToTarget);
#endif
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                break;

            default:
                Console.WriteLine("{0} Got Frame with unknown id {1}", DateTime.Now, id);
                break;
            }
        }