Skip to content

Commit 060a538

Browse files
committed
Adds an option to skip authentication with localhost calls, watchdog timer on Anykas
1 parent 87bcf8a commit 060a538

File tree

5 files changed

+65
-24
lines changed

5 files changed

+65
-24
lines changed

divinus.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ system:
44
web_enable_auth: false
55
web_auth_user: admin
66
web_auth_pass: 12345
7+
web_auth_skiplocal: true
78
web_enable_static: false
89
isp_thread_stack_size: 16384
910
venc_stream_thread_stack_size: 16384

src/app_config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ int save_app_config(void) {
8686
fprintf(file, " web_enable_auth: %s\n", app_config.web_enable_auth ? "true" : "false");
8787
fprintf(file, " web_auth_user: %s\n", app_config.web_auth_user);
8888
fprintf(file, " web_auth_pass: %s\n", app_config.web_auth_pass);
89+
fprintf(file, " web_auth_skiplocal: %s\n", app_config.web_auth_skiplocal ? "true" : "false");
8990
fprintf(file, " web_enable_static: %s\n", app_config.web_enable_static ? "true" : "false");
9091
fprintf(file, " isp_thread_stack_size: %d\n", app_config.isp_thread_stack_size);
9192
fprintf(file, " venc_stream_thread_stack_size: %d\n", app_config.venc_stream_thread_stack_size);
@@ -217,6 +218,7 @@ enum ConfigError parse_app_config(void) {
217218
app_config.web_port = 8080;
218219
*app_config.web_whitelist[0] = '\0';
219220
app_config.web_enable_auth = false;
221+
app_config.web_auth_skiplocal = false;
220222
app_config.web_enable_static = false;
221223
app_config.isp_thread_stack_size = 16 * 1024;
222224
app_config.venc_stream_thread_stack_size = 16 * 1024;
@@ -311,6 +313,7 @@ enum ConfigError parse_app_config(void) {
311313
&ini, "system", "web_auth_user", app_config.web_auth_user);
312314
parse_param_value(
313315
&ini, "system", "web_auth_pass", app_config.web_auth_pass);
316+
parse_bool(&ini, "system", "web_auth_skiplocal", &app_config.web_auth_skiplocal);
314317
err = parse_bool(
315318
&ini, "system", "web_enable_static", &app_config.web_enable_static);
316319
if (err != CONFIG_OK)

src/app_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct AppConfig {
1919
bool web_enable_auth;
2020
char web_auth_user[32];
2121
char web_auth_pass[32];
22+
bool web_auth_skiplocal;
2223
bool web_enable_static;
2324
unsigned int isp_thread_stack_size;
2425
unsigned int venc_stream_thread_stack_size;

src/hal/plus/ak_hal.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ int ak_pipeline_create(char mirror, char flip)
8585
config.capt.height = _ak_vi_res.height;
8686
config.capt.x = 0;
8787
config.capt.y = 0;
88-
for (char i = 0; i < 2; i++) {
89-
config.dest[i].width = 640;
90-
config.dest[i].height = 480;
91-
config.dest[i].maxWidth = 640;
92-
config.dest[i].maxHeight = 480;
93-
}
88+
config.dest[0].width = 640;
89+
config.dest[0].height = 480;
90+
config.dest[0].maxWidth = 640;
91+
config.dest[0].maxHeight = 480;
92+
config.dest[1].width = _ak_vi_res.width;
93+
config.dest[1].height = _ak_vi_res.height;
94+
config.dest[1].maxWidth = _ak_vi_res.width;
95+
config.dest[1].maxHeight = _ak_vi_res.height;
9496

9597
if (ak_vi.fnSetDeviceConfig(_ak_vi_dev, &config))
9698
HAL_ERROR("ak_vi", "Setting the sensor resolution failed with %#x!\n%s\n",
@@ -195,7 +197,7 @@ void *ak_video_thread(void)
195197
int ret;
196198

197199
while (keepRunning) {
198-
for (int i = 0; i < AK_VENC_CHN_NUM; i++) {
200+
for (char i = 0; i < AK_VENC_CHN_NUM; i++) {
199201
if (!ak_state[i].enable) continue;
200202
if (!ak_state[i].mainLoop) continue;
201203

@@ -241,7 +243,10 @@ void ak_system_deinit(void)
241243

242244
int ak_system_init(char *snrConfig)
243245
{
244-
int ret = EXIT_SUCCESS;
246+
int ret = EXIT_SUCCESS, val = 0xAA000000;
247+
248+
// Disable the watchdog timer if it isn't already
249+
hal_registry(0x080080E8, &val, OP_WRITE);
245250

246251
if (ak_vi.fnLoadSensorConfig(snrConfig))
247252
HAL_DANGER("ak_vi", "Loading the sensor config failed"

src/server.c

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ int server_fd = -1;
5454
pthread_t server_thread_id;
5555
pthread_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+
5773
static 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

Comments
 (0)