@@ -556,7 +556,7 @@ static void _webui_connection_remove(_webui_window_t* win, struct mg_connection*
556556static void _webui_remove_firefox_profile_ini (const char * path , const char * profile_name );
557557static bool _webui_is_firefox_ini_profile_exist (const char * path , const char * profile_name );
558558static void _webui_send_client (_webui_window_t * win , struct mg_connection * client ,
559- uint16_t id , uint8_t cmd , const char * data , size_t len );
559+ uint16_t id , uint8_t cmd , const char * data , size_t len , bool token_bypass );
560560static void _webui_send_all (_webui_window_t * win , uint16_t id , uint8_t cmd , const char * data , size_t len );
561561static uint16_t _webui_get_id (const char * data );
562562static uint32_t _webui_get_token (const char * data );
@@ -694,7 +694,7 @@ void webui_run_client(webui_event_t* e, const char* script) {
694694 // [Script]
695695
696696 // Send the packet to a single client
697- _webui_send_client (win , _webui .clients [e -> connection_id ], 0 , WEBUI_CMD_JS_QUICK , script , js_len );
697+ _webui_send_client (win , _webui .clients [e -> connection_id ], 0 , WEBUI_CMD_JS_QUICK , script , js_len , false );
698698}
699699
700700void webui_run (size_t window , const char * script ) {
@@ -790,7 +790,7 @@ bool webui_script_client(webui_event_t* e, const char* script, size_t timeout,
790790 // [Script]
791791
792792 // Send the packet to a single client
793- _webui_send_client (win , _webui .clients [e -> connection_id ], run_id , WEBUI_CMD_JS , script , js_len );
793+ _webui_send_client (win , _webui .clients [e -> connection_id ], run_id , WEBUI_CMD_JS , script , js_len , false );
794794
795795 bool js_status = false;
796796
@@ -1081,7 +1081,7 @@ void webui_close_client(webui_event_t* e) {
10811081 // [CMD]
10821082
10831083 // Send the packet to a single client
1084- _webui_send_client (win , _webui .clients [e -> connection_id ], 0 , WEBUI_CMD_CLOSE , NULL , 0 );
1084+ _webui_send_client (win , _webui .clients [e -> connection_id ], 0 , WEBUI_CMD_CLOSE , NULL , 0 , false );
10851085
10861086 // Forced close
10871087 mg_close_connection (_webui .clients [e -> connection_id ]);
@@ -1275,7 +1275,7 @@ void webui_navigate_client(webui_event_t* e, const char* url) {
12751275
12761276 // Send the packet to a single client
12771277 _webui_send_client (win , _webui .clients [e -> connection_id ],
1278- 0 , WEBUI_CMD_NAVIGATION , url , _webui_strlen (url )
1278+ 0 , WEBUI_CMD_NAVIGATION , url , _webui_strlen (url ), false
12791279 );
12801280 }
12811281}
@@ -2739,7 +2739,7 @@ void webui_send_raw_client(webui_event_t* e, const char* function, const void* r
27392739
27402740 // Send the packet to single a client
27412741 _webui_send_client (win , _webui .clients [e -> connection_id ],
2742- 0 , WEBUI_CMD_SEND_RAW , (const char * )buf , data_len
2742+ 0 , WEBUI_CMD_SEND_RAW , (const char * )buf , data_len , false
27432743 );
27442744
27452745 _webui_free_mem ((void * )buf );
@@ -4674,33 +4674,13 @@ static const char* _webui_generate_js_bridge(_webui_window_t* win, struct mg_con
46744674 token = win -> token ;
46754675 }
46764676
4677- // Calculate the cb size
4678- size_t cb_mem_size = 64 ; // To hold 'const _webui_bind_list = ["elem1", "elem2",];'
4679- for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
4680- if (!_webui_is_empty (win -> html_elements [i ])) {
4681- cb_mem_size += _webui_strlen (win -> html_elements [i ]) + 3 ;
4682- }
4683- }
4684-
4685- // Generate the cb array
4686- char * event_cb_js_array = (char * )_webui_malloc (cb_mem_size );
4687- WEBUI_STR_CAT_DYN (event_cb_js_array , cb_mem_size , "[" );
4688- for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
4689- if (!_webui_is_empty (win -> html_elements [i ])) {
4690- WEBUI_STR_CAT_DYN (event_cb_js_array , cb_mem_size , "\"" );
4691- WEBUI_STR_CAT_DYN (event_cb_js_array , cb_mem_size , win -> html_elements [i ]);
4692- WEBUI_STR_CAT_DYN (event_cb_js_array , cb_mem_size , "\"," );
4693- }
4694- }
4695- WEBUI_STR_CAT_DYN (event_cb_js_array , cb_mem_size , "]" );
4696-
46974677 // Generate the full WebUI Bridge
46984678 #ifdef WEBUI_LOG
46994679 const char * log = "true" ;
47004680 #else
47014681 const char * log = "false" ;
47024682 #endif
4703- size_t len = 256 + cb_mem_size + _webui_strlen ((const char * )webui_javascript_bridge );
4683+ size_t len = 256 + _webui_strlen ((const char * )webui_javascript_bridge );
47044684 char * js = (char * )_webui_malloc (len );
47054685 #ifdef WEBUI_TLS
47064686 const char * TLS = "true" ;
@@ -4710,8 +4690,8 @@ static const char* _webui_generate_js_bridge(_webui_window_t* win, struct mg_con
47104690 int c = WEBUI_SN_PRINTF_DYN (
47114691 js , len ,
47124692 "%s\n document.addEventListener(\"DOMContentLoaded\",function(){ globalThis.webui = "
4713- "new WebuiBridge({ secure: %s, token: %" PRIu32 ", port: %zu, winNum: %zu, bindList: %s, log: %s, " ,
4714- webui_javascript_bridge , TLS , token , win -> server_port , win -> num , event_cb_js_array , log
4693+ "new WebuiBridge({ secure: %s, token: %" PRIu32 ", port: %zu, log: %s, " ,
4694+ webui_javascript_bridge , TLS , token , win -> server_port , log
47154695 );
47164696 // Window Size
47174697 if (win -> size_set )
@@ -4723,7 +4703,6 @@ static const char* _webui_generate_js_bridge(_webui_window_t* win, struct mg_con
47234703 WEBUI_STR_CAT_DYN (js , len , "});});" );
47244704
47254705 // Free
4726- _webui_free_mem ((void * )event_cb_js_array );
47274706 _webui_mutex_unlock (& _webui .mutex_bridge );
47284707 return js ;
47294708}
@@ -5068,30 +5047,37 @@ static void _webui_send_all(_webui_window_t* win, uint16_t id, uint8_t cmd, cons
50685047 // Loop trough all connected clients in this window
50695048 for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
50705049 if ((_webui .clients [i ] != NULL ) && (_webui .clients_win_num [i ] == win -> num ) && (_webui .clients_token_check [i ])) {
5071- _webui_send_client (win , _webui .clients [i ], 0 , cmd , data , len );
5050+ _webui_send_client (win , _webui .clients [i ], 0 , cmd , data , len , false );
50725051 }
50735052 }
50745053 } else {
50755054 // Single client
50765055 if ((win -> single_client != NULL ) && (win -> single_client_token_check )) {
5077- _webui_send_client (win , win -> single_client , 0 , cmd , data , len );
5056+ _webui_send_client (win , win -> single_client , 0 , cmd , data , len , false );
50785057 }
50795058 }
50805059}
50815060
50825061static void _webui_send_client (
50835062 _webui_window_t * win , struct mg_connection * client ,
5084- uint16_t id , uint8_t cmd , const char * data , size_t len ) {
5063+ uint16_t id , uint8_t cmd , const char * data , size_t len , bool token_bypass ) {
50855064
50865065 #ifdef WEBUI_LOG
50875066 printf ("[Core]\t\t_webui_send_client()\n" );
50885067 #endif
50895068
5069+ // Get connection id
50905070 size_t connection_id = 0 ;
50915071 if (!_webui_connection_get_id (win , client , & connection_id ))
50925072 return ;
5093- if ((_webui .clients [connection_id ] == NULL ) || (! _webui . clients_token_check [ connection_id ]) )
5073+ if ((_webui .clients [connection_id ] == NULL ))
50945074 return ;
5075+
5076+ // Check Token
5077+ if (!token_bypass ) {
5078+ if (!_webui .clients_token_check [connection_id ])
5079+ return ;
5080+ }
50955081
50965082 #ifdef WEBUI_LOG
50975083 printf ("[Core]\t\t_webui_send_client() -> ID = 0x%04X \n" , id );
@@ -7156,7 +7142,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
71567142 // Update single client
71577143 _webui_send_client (
71587144 win , client , 0 , WEBUI_CMD_NAVIGATION ,
7159- (const char * )window_url , _webui_strlen (window_url )
7145+ (const char * )window_url , _webui_strlen (window_url ), false
71607146 );
71617147 }
71627148 else {
@@ -9301,7 +9287,7 @@ static void _webui_ws_process(
93019287 // Send the packet
93029288 _webui_send_client (
93039289 win , client , packet_id , WEBUI_CMD_CALL_FUNC ,
9304- event_inf -> response , _webui_strlen (event_inf -> response )
9290+ event_inf -> response , _webui_strlen (event_inf -> response ), false
93059291 );
93069292
93079293 // Free event
@@ -9326,7 +9312,7 @@ static void _webui_ws_process(
93269312
93279313 // Send the packet
93289314 _webui_send_client (
9329- win , client , packet_id , WEBUI_CMD_CALL_FUNC , NULL , 0
9315+ win , client , packet_id , WEBUI_CMD_CALL_FUNC , NULL , 0 , false
93309316 );
93319317 }
93329318
@@ -9346,42 +9332,64 @@ static void _webui_ws_process(
93469332 );
93479333 #endif
93489334
9349- unsigned char status = 0x00 ;
9350-
93519335 size_t connection_id = 0 ;
93529336 if (_webui_connection_get_id (win , client , & connection_id )) {
9337+
93539338 if (win -> single_client == client ) {
93549339 win -> single_client_token_check = true;
93559340 }
93569341 _webui .clients_token_check [connection_id ] = true;
9357- status = 0x01 ;
9358- #ifdef WEBUI_LOG
9359- printf (
9360- "[Core]\t\t_webui_ws_process(%zu) -> Check token accepted.\n" ,
9361- recvNum
9362- );
9363- #endif
9364- }
9365- else {
9342+
93669343 #ifdef WEBUI_LOG
93679344 printf (
9368- "[Core]\t\t_webui_ws_process(%zu) -> Check token not accepted.\n" ,
9345+ "[Core]\t\t_webui_ws_process(%zu) -> Token accepted. Sending bind list \n" ,
93699346 recvNum
93709347 );
93719348 #endif
9372- }
93739349
9374- // Packet Protocol Format:
9375- // [...]
9376- // [CMD]
9377- // [Check Token Status]
9350+ // Calculate the bind list size
9351+ // [0x01][element1,element2,element3,]
9352+ size_t csv_size = 1 ;
9353+ for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
9354+ if (!_webui_is_empty (win -> html_elements [i ])) {
9355+ csv_size += _webui_strlen (win -> html_elements [i ]) + 1 ;
9356+ }
9357+ }
93789358
9379- // Send the packet
9380- _webui_send_client (
9381- win , client , packet_id , WEBUI_CMD_CHECK_TK , (const char * )& status , 1
9382- );
9359+ // Allocate
9360+ char * csv = (char * )_webui_malloc (csv_size );
9361+ csv [0 ] = 0x01 ;
9362+
9363+ // Generate the bind list array (CSV)
9364+ for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
9365+ if (!_webui_is_empty (win -> html_elements [i ])) {
9366+ // [element1,element2,element3,]
9367+ WEBUI_STR_CAT_DYN (& csv [1 ], csv_size , win -> html_elements [i ]);
9368+ WEBUI_STR_CAT_DYN (& csv [1 ], csv_size , "," );
9369+ }
9370+ }
9371+
9372+ // Add all events bind element (empty)
9373+ if (win -> has_all_events ) {
9374+ // [element1,...,,]
9375+ WEBUI_STR_CAT_DYN (& csv [1 ], csv_size , "," );
9376+ }
9377+
9378+ // Packet Protocol Format:
9379+ // [...]
9380+ // [CMD]
9381+ // [Token Status]
9382+ // [0x01 element1,element2,element3,]
9383+
9384+ // Send the packet
9385+ _webui_send_client (
9386+ win , client , packet_id , WEBUI_CMD_CHECK_TK ,
9387+ (const char * )csv , _webui_strlen (csv ), true
9388+ );
9389+
9390+ // Free
9391+ _webui_free_mem ((void * )csv );
93839392
9384- if (status == 0x01 ) {
93859393 // New Event
93869394 if (win -> has_all_events ) {
93879395
@@ -9406,6 +9414,28 @@ static void _webui_ws_process(
94069414 // _webui_free_event_inf(win, event_num);
94079415 }
94089416 }
9417+ else {
9418+ #ifdef WEBUI_LOG
9419+ printf (
9420+ "[Core]\t\t_webui_ws_process(%zu) -> Token not accepted.\n" ,
9421+ recvNum
9422+ );
9423+ #endif
9424+
9425+ unsigned char status = 0x00 ;
9426+
9427+ // Packet Protocol Format:
9428+ // [...]
9429+ // [CMD]
9430+ // [Token Status]
9431+ // [0x00]
9432+
9433+ // Send the packet
9434+ _webui_send_client (
9435+ win , client , packet_id , WEBUI_CMD_CHECK_TK ,
9436+ (const char * )& status , 1 , true
9437+ );
9438+ }
94099439 }
94109440 #ifdef WEBUI_LOG
94119441 else {
@@ -9449,7 +9479,7 @@ static void _webui_ws_process(
94499479
94509480 // Send the packet
94519481 _webui_send_client (
9452- win , client , packet_id , 0x00 , NULL , 0
9482+ win , client , packet_id , 0x00 , NULL , 0 , true
94539483 );
94549484
94559485 // Forced close
@@ -11287,7 +11317,7 @@ static WEBUI_THREAD_MONITOR {
1128711317 // Loop trough all connected clients in this window
1128811318 for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
1128911319 if ((_webui .clients [i ] != NULL ) && (_webui .clients_win_num [i ] == win -> num ) && (_webui .clients_token_check [i ])) {
11290- _webui_send_client (win , _webui .clients [i ], 0 , WEBUI_CMD_JS_QUICK , js , js_len );
11320+ _webui_send_client (win , _webui .clients [i ], 0 , WEBUI_CMD_JS_QUICK , js , js_len , false );
1129111321 }
1129211322 }
1129311323 } else {
@@ -11338,7 +11368,7 @@ static WEBUI_THREAD_MONITOR {
1133811368 // Loop trough all connected clients in this window
1133911369 for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
1134011370 if ((_webui .clients [i ] != NULL ) && (_webui .clients_win_num [i ] == win -> num ) && (_webui .clients_token_check [i ])) {
11341- _webui_send_client (win , _webui .clients [i ], 0 , WEBUI_CMD_JS_QUICK , js , js_len );
11371+ _webui_send_client (win , _webui .clients [i ], 0 , WEBUI_CMD_JS_QUICK , js , js_len , false );
1134211372 }
1134311373 }
1134411374 }
@@ -11386,7 +11416,7 @@ static WEBUI_THREAD_MONITOR {
1138611416 // Loop trough all connected clients in this window
1138711417 for (size_t i = 0 ; i < WEBUI_MAX_IDS ; i ++ ) {
1138811418 if ((_webui .clients [i ] != NULL ) && (_webui .clients_win_num [i ] == win -> num ) && (_webui .clients_token_check [i ])) {
11389- _webui_send_client (win , _webui .clients [i ], 0 , WEBUI_CMD_JS_QUICK , js , js_len );
11419+ _webui_send_client (win , _webui .clients [i ], 0 , WEBUI_CMD_JS_QUICK , js , js_len , false );
1139011420 }
1139111421 }
1139211422 }
0 commit comments