From 9f00a9265c05dc02ad1b1e4c8148e1950c394530 Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Mon, 21 May 2018 13:31:45 +0800 Subject: remove FQTermSSH{1,2}PacketSender, remove duplicated code --- src/protocol/fqterm_ssh_socket.cpp | 127 ++++++++++++--------------- src/protocol/internal/fqterm_ssh1_packet.cpp | 14 --- src/protocol/internal/fqterm_ssh1_packet.h | 7 -- src/protocol/internal/fqterm_ssh2_packet.cpp | 9 -- src/protocol/internal/fqterm_ssh2_packet.h | 6 -- src/protocol/internal/fqterm_ssh_packet.cpp | 8 +- src/protocol/internal/fqterm_ssh_packet.h | 17 +++- 7 files changed, 73 insertions(+), 115 deletions(-) diff --git a/src/protocol/fqterm_ssh_socket.cpp b/src/protocol/fqterm_ssh_socket.cpp index 0fb4a4d..c61fe25 100644 --- a/src/protocol/fqterm_ssh_socket.cpp +++ b/src/protocol/fqterm_ssh_socket.cpp @@ -79,79 +79,60 @@ FQTermSSHSocket::~FQTermSSHSocket() { delete ssh_channel_; } -void FQTermSSHSocket::init(int ssh_version) { - // Actually we could reuse these buffers, sender/receivers, and etc. - // but in that case reset methods should be added to all these classes. - // Guys lazy as me won't do that. - - buffer_clear(&input_buffer); - buffer_clear(&output_buffer); - buffer_clear(&socket_buffer); - delete packet_receiver_; - delete packet_sender_; - delete key_exchanger_; - delete authentication_; - delete ssh_channel_; - - is_channel_ok_ = false; - auth_ok_emitted_ = false; - - if (ssh_version == 1) { - packet_receiver_ = new FQTermSSH1PacketReceiver; - packet_sender_ = new FQTermSSH1PacketSender; - key_exchanger_ = new FQTermSSH1Kex(SSH_V1_C, server_name_.toLatin1().constData()); - authentication_ = new FQTermSSH1PasswdAuth(init_user_, init_passwd_); - ssh_channel_ = new FQTermSSH1Channel; - - FQ_VERIFY(connect(packet_receiver_, SIGNAL(packetAvaliable(int)), this, SLOT(handlePacket(int)))); - FQ_VERIFY(connect(packet_receiver_, SIGNAL(packetError(QString)), this, SLOT(handleError(QString)))); - - FQ_VERIFY(connect(packet_sender_, SIGNAL(dataToWrite()), this, SLOT(writeData()))); - - FQ_VERIFY(connect(key_exchanger_, SIGNAL(kexOK()), this, SLOT(kexOK()))); - FQ_VERIFY(connect(key_exchanger_, SIGNAL(kexError(QString)), this, SLOT(handleError(QString)))); - FQ_VERIFY(connect(key_exchanger_, SIGNAL(reKex()), packet_receiver_, SLOT(resetEncryption()))); - FQ_VERIFY(connect(key_exchanger_, SIGNAL(reKex()), packet_sender_, SLOT(resetEncryption()))); - - FQ_VERIFY(connect(authentication_, SIGNAL(requestUserPwd(QString *, QString *, bool *)), this, SIGNAL(requestUserPwd(QString *, QString *, bool *)))); - FQ_VERIFY(connect(authentication_, SIGNAL(authOK()), this, SLOT(authOK()))); - FQ_VERIFY(connect(authentication_, SIGNAL(authError(QString)), this, SLOT(handleError(QString)))); - - FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelOK()), this, SLOT(channelOK()))); - FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelReadyRead(const char *, int)), this, SLOT(channelReadyRead(const char *, int)))); - - key_exchanger_->initKex(packet_receiver_, packet_sender_); - } else { - packet_receiver_ = new FQTermSSH2PacketReceiver; - packet_sender_ = new FQTermSSH2PacketSender; - key_exchanger_ = new FQTermSSH2Kex(SSH_V2_C, server_name_.toLatin1().constData()); - authentication_ = new FQTermSSH2PasswdAuth(init_user_, init_passwd_); - ssh_channel_ = new FQTermSSH2Channel; - - FQ_VERIFY(connect(packet_receiver_, SIGNAL(packetAvaliable(int)), this, SLOT(handlePacket(int)))); - FQ_VERIFY(connect(packet_receiver_, SIGNAL(packetError(QString)), this, SLOT(handleError(QString)))); - - FQ_VERIFY(connect(packet_sender_, SIGNAL(dataToWrite()), this, SLOT(writeData()))); - - FQ_VERIFY(connect(key_exchanger_, SIGNAL(kexOK()), this, SLOT(kexOK()))); - FQ_VERIFY(connect(key_exchanger_, SIGNAL(kexError(QString)), this, SLOT(handleError(QString)))); - FQ_VERIFY(connect(key_exchanger_, SIGNAL(reKex()), packet_receiver_, SLOT(resetEncryption()))); - FQ_VERIFY(connect(key_exchanger_, SIGNAL(reKex()), packet_sender_, SLOT(resetEncryption()))); - - FQ_VERIFY(connect(authentication_, SIGNAL(requestUserPwd(QString *, QString *, bool *)), this, SIGNAL(requestUserPwd(QString *, QString *, bool *)))); - FQ_VERIFY(connect(authentication_, SIGNAL(authOK()), this, SLOT(authOK()))); - FQ_VERIFY(connect(authentication_, SIGNAL(authError(QString)), this, SLOT(handleError(QString)))); - - FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelOK()), this, SLOT(channelOK()))); - FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelReadyRead(const char *, int)), this, SLOT(channelReadyRead(const char *, int)))); - FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelError(QString)), this, SLOT(handleError(QString)))); - FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelClosed()), this, SIGNAL(connectionClosed()))); - - key_exchanger_->initKex(packet_receiver_, packet_sender_); - } - - conn_info.proto = PROTO_SSH; - conn_info.ssh_proto_info.proto_version = ssh_version; +void FQTermSSHSocket::init(int ssh_version) +{ + // Actually we could reuse these buffers, sender/receivers, and etc. + // but in that case reset methods should be added to all these classes. + // Guys lazy as me won't do that. + + buffer_clear(&input_buffer); + buffer_clear(&output_buffer); + buffer_clear(&socket_buffer); + delete packet_receiver_; + delete packet_sender_; + delete key_exchanger_; + delete authentication_; + delete ssh_channel_; + + is_channel_ok_ = false; + auth_ok_emitted_ = false; + + packet_sender_ = new FQTermSSHPacketSender(ssh_version); + if (ssh_version == 1) { + packet_receiver_ = new FQTermSSH1PacketReceiver; + key_exchanger_ = new FQTermSSH1Kex(SSH_V1_C, server_name_.toLatin1().constData()); + authentication_ = new FQTermSSH1PasswdAuth(init_user_, init_passwd_); + ssh_channel_ = new FQTermSSH1Channel; + } else { + packet_receiver_ = new FQTermSSH2PacketReceiver; + key_exchanger_ = new FQTermSSH2Kex(SSH_V2_C, server_name_.toLatin1().constData()); + authentication_ = new FQTermSSH2PasswdAuth(init_user_, init_passwd_); + ssh_channel_ = new FQTermSSH2Channel; + } + FQ_VERIFY(connect(packet_receiver_, SIGNAL(packetAvaliable(int)), this, SLOT(handlePacket(int)))); + FQ_VERIFY(connect(packet_receiver_, SIGNAL(packetError(QString)), this, SLOT(handleError(QString)))); + FQ_VERIFY(connect(packet_sender_, SIGNAL(dataToWrite()), this, SLOT(writeData()))); + FQ_VERIFY(connect(key_exchanger_, SIGNAL(kexOK()), this, SLOT(kexOK()))); + FQ_VERIFY(connect(key_exchanger_, SIGNAL(kexError(QString)), this, SLOT(handleError(QString)))); + FQ_VERIFY(connect(key_exchanger_, SIGNAL(reKex()), packet_receiver_, SLOT(resetEncryption()))); + FQ_VERIFY(connect(key_exchanger_, SIGNAL(reKex()), packet_sender_, SLOT(resetEncryption()))); + FQ_VERIFY(connect(authentication_, SIGNAL(requestUserPwd(QString *, QString *, bool *)), + this, SIGNAL(requestUserPwd(QString *, QString *, bool *)))); + FQ_VERIFY(connect(authentication_, SIGNAL(authOK()), this, SLOT(authOK()))); + FQ_VERIFY(connect(authentication_, SIGNAL(authError(QString)), this, SLOT(handleError(QString)))); + FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelOK()), this, SLOT(channelOK()))); + FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelReadyRead(const char *, int)), + this, SLOT(channelReadyRead(const char *, int)))); + if (ssh_version == 2) { + FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelError(QString)), + this, SLOT(handleError(QString)))); + FQ_VERIFY(connect(ssh_channel_, SIGNAL(channelClosed()), + this, SIGNAL(connectionClosed()))); + } + key_exchanger_->initKex(packet_receiver_, packet_sender_); + + conn_info.proto = PROTO_SSH; + conn_info.ssh_proto_info.proto_version = ssh_version; } void FQTermSSHSocket::kexOK() diff --git a/src/protocol/internal/fqterm_ssh1_packet.cpp b/src/protocol/internal/fqterm_ssh1_packet.cpp index 7605b51..7510194 100644 --- a/src/protocol/internal/fqterm_ssh1_packet.cpp +++ b/src/protocol/internal/fqterm_ssh1_packet.cpp @@ -26,20 +26,6 @@ #include "ssh_packet.h" namespace FQTerm { -//============================================================================== -//FQTermSSH1PacketSender -//============================================================================== - - FQTermSSH1PacketSender::FQTermSSH1PacketSender() - { - cipher = new_3des_ssh1(1); - } - -void FQTermSSH1PacketSender::makePacket() -{ - make_ssh1_packet(&orig_data, &data_to_send, cipher); -} - //============================================================================== //FQTermSSH1PacketReceiver //============================================================================== diff --git a/src/protocol/internal/fqterm_ssh1_packet.h b/src/protocol/internal/fqterm_ssh1_packet.h index 16af5c9..e8a0806 100644 --- a/src/protocol/internal/fqterm_ssh1_packet.h +++ b/src/protocol/internal/fqterm_ssh1_packet.h @@ -25,13 +25,6 @@ namespace FQTerm { -class FQTermSSH1PacketSender: public FQTermSSHPacketSender { -protected: - virtual void makePacket(); -public: - FQTermSSH1PacketSender(); -}; - class FQTermSSH1PacketReceiver: public FQTermSSHPacketReceiver { public: virtual void parseData(buffer *input); diff --git a/src/protocol/internal/fqterm_ssh2_packet.cpp b/src/protocol/internal/fqterm_ssh2_packet.cpp index 4a5cde4..0345883 100644 --- a/src/protocol/internal/fqterm_ssh2_packet.cpp +++ b/src/protocol/internal/fqterm_ssh2_packet.cpp @@ -26,15 +26,6 @@ #include "ssh_packet.h" namespace FQTerm { -//============================================================================== -//FQTermSSH2PacketSender -//============================================================================== -void FQTermSSH2PacketSender::makePacket() -{ - make_ssh2_packet(&orig_data, &data_to_send, cipher, - mac, is_mac_, &sequence_no_); -} - //============================================================================== //FQTermSSH2PacketReceiver //============================================================================== diff --git a/src/protocol/internal/fqterm_ssh2_packet.h b/src/protocol/internal/fqterm_ssh2_packet.h index ff65ec5..c670203 100644 --- a/src/protocol/internal/fqterm_ssh2_packet.h +++ b/src/protocol/internal/fqterm_ssh2_packet.h @@ -26,12 +26,6 @@ namespace FQTerm { -class FQTermSSH2PacketSender: public FQTermSSHPacketSender -{ -protected: - virtual void makePacket(); -}; - class FQTermSSH2PacketReceiver: public FQTermSSHPacketReceiver { private: diff --git a/src/protocol/internal/fqterm_ssh_packet.cpp b/src/protocol/internal/fqterm_ssh_packet.cpp index 1e53250..36bf7e8 100644 --- a/src/protocol/internal/fqterm_ssh_packet.cpp +++ b/src/protocol/internal/fqterm_ssh_packet.cpp @@ -31,13 +31,17 @@ namespace FQTerm { //FQTermSSHPacketSender //============================================================================== -FQTermSSHPacketSender::FQTermSSHPacketSender() +FQTermSSHPacketSender::FQTermSSHPacketSender(int ver) { buffer_init(&orig_data); buffer_init(&data_to_send); - cipher = &ssh_cipher_dummy; + if (ver == 2) + cipher = &ssh_cipher_dummy; + else + cipher = new_3des_ssh1(1); + this->ver = ver; is_mac_ = false; mac = NULL; diff --git a/src/protocol/internal/fqterm_ssh_packet.h b/src/protocol/internal/fqterm_ssh_packet.h index 071c8e0..3020d6b 100644 --- a/src/protocol/internal/fqterm_ssh_packet.h +++ b/src/protocol/internal/fqterm_ssh_packet.h @@ -29,6 +29,7 @@ #include "ssh_mac.h" #include "ssh_cipher.h" #include "buffer.h" +#include "ssh_packet.h" namespace FQTerm { @@ -42,7 +43,9 @@ class FQTermSSHPacketSender: public QObject { bool is_mac_; uint32_t sequence_no_; - FQTermSSHPacketSender(); + int ver; + + FQTermSSHPacketSender(int); virtual ~FQTermSSHPacketSender(); void startPacket(uint8_t pkt_type); @@ -61,14 +64,20 @@ class FQTermSSHPacketSender: public QObject { void startEncryption(const u_char *key, const u_char *IV = NULL); void startMac(const u_char *sessionkey); void resetMac(); + inline void makePacket() + { + if (ver == 2) { + make_ssh2_packet(&orig_data, &data_to_send, cipher, + mac, is_mac_, &sequence_no_); + } else { + make_ssh1_packet(&orig_data, &data_to_send, cipher); + } + } public slots: void resetEncryption(); signals: void dataToWrite(); - - protected: - virtual void makePacket() = 0; }; class FQTermSSHPacketReceiver: public QObject { -- cgit v1.2.3