@@ -753,46 +753,107 @@ send_msg_frp_server(struct bufferevent *bev,
753
753
free (req_msg );
754
754
}
755
755
756
- void
757
- send_enc_msg_frp_server (struct bufferevent * bev ,
758
- const enum msg_type type ,
759
- const char * msg ,
760
- const size_t msg_len ,
761
- struct tmux_stream * stream )
756
+ static int prepare_encrypted_message (const enum msg_type type ,
757
+ const char * msg ,
758
+ const size_t msg_len ,
759
+ uint8_t * * enc_msg_out ,
760
+ size_t * enc_len_out )
762
761
{
763
- struct bufferevent * bout = NULL ;
764
- if (bev ) {
765
- bout = bev ;
766
- } else {
767
- bout = main_ctl -> connect_bev ;
762
+ // Validate inputs
763
+ if (!msg || !enc_msg_out || !enc_len_out ) {
764
+ debug (LOG_ERR , "Invalid input parameters" );
765
+ return -1 ;
766
+ }
767
+
768
+ // Prepare message header and content
769
+ size_t total_len = msg_len + sizeof (struct msg_hdr );
770
+ struct msg_hdr * req_msg = calloc (total_len , 1 );
771
+ if (!req_msg ) {
772
+ debug (LOG_ERR , "Failed to allocate memory for message" );
773
+ return -1 ;
768
774
}
769
- assert (bout );
770
775
771
- struct msg_hdr * req_msg = calloc (msg_len + sizeof (struct msg_hdr ), 1 );
772
- assert (req_msg );
773
776
req_msg -> type = type ;
774
777
req_msg -> length = msg_hton ((uint64_t )msg_len );
775
778
memcpy (req_msg -> data , msg , msg_len );
776
779
780
+ // Encrypt message
781
+ uint8_t * enc_msg = NULL ;
782
+ size_t enc_len = encrypt_data ((uint8_t * )req_msg , total_len ,
783
+ get_main_encoder (), & enc_msg );
784
+ free (req_msg );
785
+
786
+ if (enc_len <= 0 || !enc_msg ) {
787
+ debug (LOG_ERR , "Encryption failed" );
788
+ return -1 ;
789
+ }
790
+
791
+ * enc_msg_out = enc_msg ;
792
+ * enc_len_out = enc_len ;
793
+ return 0 ;
794
+ }
795
+
796
+ static int initialize_encoder (struct bufferevent * bout , struct tmux_stream * stream )
797
+ {
798
+ struct frp_coder * coder = init_main_encoder ();
799
+ if (!coder ) {
800
+ debug (LOG_ERR , "Failed to initialize encoder" );
801
+ return -1 ;
802
+ }
803
+
777
804
struct common_conf * c_conf = get_common_config ();
778
- if (get_main_encoder () == NULL ) {
779
- struct frp_coder * coder = init_main_encoder ();
780
- if (c_conf -> tcp_mux )
781
- tmux_stream_write (bout , coder -> iv , 16 , stream );
782
- else
783
- bufferevent_write (bout , coder -> iv , 16 );
805
+ if (c_conf -> tcp_mux ) {
806
+ if (tmux_stream_write (bout , coder -> iv , 16 , stream ) < 0 ) {
807
+ debug (LOG_ERR , "Failed to write IV through TCP mux" );
808
+ return -1 ;
809
+ }
810
+ } else {
811
+ if (bufferevent_write (bout , coder -> iv , 16 ) < 0 ) {
812
+ debug (LOG_ERR , "Failed to write IV directly" );
813
+ return -1 ;
814
+ }
815
+ }
816
+ return 0 ;
817
+ }
818
+
819
+ void send_enc_msg_frp_server (struct bufferevent * bev ,
820
+ const enum msg_type type ,
821
+ const char * msg ,
822
+ const size_t msg_len ,
823
+ struct tmux_stream * stream )
824
+ {
825
+ // Get output bufferevent
826
+ struct bufferevent * bout = bev ? bev : main_ctl -> connect_bev ;
827
+ if (!bout ) {
828
+ debug (LOG_ERR , "No valid bufferevent" );
829
+ return ;
784
830
}
785
831
832
+ // Initialize encoder if needed
833
+ if (!get_main_encoder () && initialize_encoder (bout , stream ) != 0 ) {
834
+ return ;
835
+ }
836
+
837
+ // Prepare and encrypt message
786
838
uint8_t * enc_msg = NULL ;
787
- size_t olen = encrypt_data ((uint8_t * )req_msg , msg_len + sizeof (struct msg_hdr ), get_main_encoder (), & enc_msg );
788
- assert (olen > 0 );
789
- if (c_conf -> tcp_mux )
790
- tmux_stream_write (bout , enc_msg , olen , stream );
791
- else
792
- bufferevent_write (bout , enc_msg , olen );
839
+ size_t enc_len = 0 ;
840
+ if (prepare_encrypted_message (type , msg , msg_len , & enc_msg , & enc_len ) != 0 ) {
841
+ return ;
842
+ }
793
843
794
- free (enc_msg );
795
- free (req_msg );
844
+ // Send encrypted message
845
+ struct common_conf * c_conf = get_common_config ();
846
+ if (c_conf -> tcp_mux ) {
847
+ if (tmux_stream_write (bout , enc_msg , enc_len , stream ) < 0 ) {
848
+ debug (LOG_ERR , "Failed to write encrypted message through TCP mux" );
849
+ }
850
+ } else {
851
+ if (bufferevent_write (bout , enc_msg , enc_len ) < 0 ) {
852
+ debug (LOG_ERR , "Failed to write encrypted message directly" );
853
+ }
854
+ }
855
+
856
+ free (enc_msg );
796
857
}
797
858
798
859
struct control *
0 commit comments