@@ -54,6 +54,22 @@ int server_fd = -1;
5454pthread_t server_thread_id ;
5555pthread_mutex_t client_fds_mutex ;
5656
57+ static bool is_local_address (const char * client_ip ) {
58+ if (!client_ip ) return false;
59+
60+ if (!strcmp (client_ip , "127.0.0.1" ) ||
61+ !strncmp (client_ip , "127." , 4 ))
62+ return true;
63+
64+ if (!strcmp (client_ip , "::1" ))
65+ return true;
66+
67+ if (!strncmp (client_ip , "::ffff:127." , 11 ))
68+ return true;
69+
70+ return false;
71+ }
72+
5773static void close_socket_fd (int sockFd ) {
5874 shutdown (sockFd , SHUT_RDWR );
5975 close (sockFd );
@@ -467,8 +483,8 @@ void parse_request(http_request_t *req) {
467483 req -> total = 0 ;
468484 return ;
469485 }
470- grant_access :
471486
487+ grant_access :
472488 req -> total = 0 ;
473489 int received = recv (req -> clntFd , req -> input , REQSIZE , 0 );
474490 if (received < 0 )
@@ -605,23 +621,38 @@ void respond_request(http_request_t *req) {
605621 }
606622
607623 if (app_config .web_enable_auth ) {
608- char * auth = request_header ("Authorization" );
609- char cred [66 ], valid [256 ];
624+ bool should_skip_auth = false;
610625
611- strcpy (cred , app_config .web_auth_user );
612- strcpy (cred + strlen (app_config .web_auth_user ), ":" );
613- strcpy (cred + strlen (app_config .web_auth_user ) + 1 , app_config .web_auth_pass );
614- strcpy (valid , "Basic " );
615- base64_encode (valid + 6 , cred , strlen (cred ));
626+ if (app_config .web_auth_skiplocal ) {
627+ struct sockaddr_in client_sock ;
628+ socklen_t client_sock_len = sizeof (client_sock );
629+ memset (& client_sock , 0 , client_sock_len );
616630
617- if (!auth || !EQUALS (auth , valid )) {
618- respLen = sprintf (response ,
619- "HTTP/1.1 401 Unauthorized\r\n"
620- "Content-Type: text/plain\r\n"
621- "WWW-Authenticate: Basic realm=\"Access the camera services\"\r\n"
622- "Connection: close\r\n\r\n" );
623- send_and_close (req -> clntFd , response , respLen );
624- return ;
631+ if (getpeername (req -> clntFd , (struct sockaddr * )& client_sock , & client_sock_len ) == 0 ) {
632+ char * client_ip = inet_ntoa (client_sock .sin_addr );
633+ should_skip_auth = is_local_address (client_ip );
634+ }
635+ }
636+
637+ if (!should_skip_auth ) {
638+ char * auth = request_header ("Authorization" );
639+ char cred [66 ], valid [256 ];
640+
641+ strcpy (cred , app_config .web_auth_user );
642+ strcpy (cred + strlen (app_config .web_auth_user ), ":" );
643+ strcpy (cred + strlen (app_config .web_auth_user ) + 1 , app_config .web_auth_pass );
644+ strcpy (valid , "Basic " );
645+ base64_encode (valid + 6 , cred , strlen (cred ));
646+
647+ if (!auth || !EQUALS (auth , valid )) {
648+ respLen = sprintf (response ,
649+ "HTTP/1.1 401 Unauthorized\r\n"
650+ "Content-Type: text/plain\r\n"
651+ "WWW-Authenticate: Basic realm=\"Access the camera services\"\r\n"
652+ "Connection: close\r\n\r\n" );
653+ send_and_close (req -> clntFd , response , respLen );
654+ return ;
655+ }
625656 }
626657 }
627658
0 commit comments