forked from mnakada/atomcam_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl.c
149 lines (133 loc) · 4.63 KB
/
curl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
typedef void CURL;
typedef int CURLcode;
typedef long long curl_off_t;
typedef enum {
HTTPREQ_NONE,
HTTPREQ_GET,
HTTPREQ_POST,
HTTPREQ_POST_FORM,
HTTPREQ_POST_MIME,
HTTPREQ_PUT,
HTTPREQ_HEAD,
HTTPREQ_OPTIONS,
HTTPREQ_LAST
} Curl_HttpReq;
static const int CURL_OK = 0;
const char *methods[] = {
"NONE", "GET", "POST", "POST_FORM", "POST_MIME", "PUT", "HEAD", "OPTIONS", "LAST", ""
};
static const char *AlarmPath = "/device/v1/alarm/add";
static const char *DummyRes = "{\"ts\":1641390551000,\"code\":\"1\",\"msg\":\"\",\"data\":{\"alarm_file_list\":[{\"file_type\":1,\"file_suffix\":\"jpg\",\"file_url\":\"https://localhost/hoge.jpg\",\"encryption_algorithm\":0,\"encryption_password\":\"\"},{\"file_type\":2,\"file_suffix\":\"mp4\",\"file_url\":\"https://localhost/fuga.mp4\",\"encryption_algorithm\":0,\"encryption_password\":\"\"}]}}";
static const char *DummyHost = "https://localhost/";
typedef int (*curl_seek_callback)(void *instream, int offset, int origin);
typedef int (*curl_write_callback)(char *buffer, int size, int nitems, void *outstream);
struct SessionHandle {
unsigned char padding0[1392];
unsigned char padding1[16];
void *out; // offset 1392 + 16
unsigned char padding2[40];
void *postfields; // offset 1392 + 60
unsigned char padding3[8];
curl_off_t postfieldsize; // offset offset 1392 + 72
unsigned char padding4[8];
curl_write_callback fwrite_func; // offset 1392 + 88
unsigned char padding5[568];
Curl_HttpReq httpreq; // offset 1392 + 660
unsigned char padding6[664];
char *url; // offset 2720 + 0
unsigned char padding7[16];
unsigned char padding8[988];
int httpcode; // offset 3728 + 0
};
static CURLcode (*original_curl_easy_perform)(CURL *curl);
static int curl_minimum_alarm_cycle = 0;
static int debug = 0;
static void __attribute ((constructor)) curl_hook_init(void) {
original_curl_easy_perform = dlsym(dlopen("/thirdlib/libcurl.so", RTLD_LAZY), "curl_easy_perform");
char *p = getenv("MINIMIZE_ALARM_CYCLE");
if(p && !strcmp(p, "on")) {
curl_minimum_alarm_cycle = 300;
}
p = getenv("ATOMTECH_AWS_ACCESS");
if(p && !strcmp(p, "disable_video")) {
curl_minimum_alarm_cycle = -1;
}
}
char *CurlDebug(int fd, char *tokenPtr) {
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) return debug ? "on" : "off";
if(!strcmp(p, "on")) {
debug = 1;
printf("[curl] curl debug on\n", p);
return "ok";
}
if(!strcmp(p, "off")) {
debug = 0;
printf("[curl] curl debug off\n", p);
return "ok";
}
return "error";
}
static void Dump(const char *str, void *start, int size) {
fprintf(stderr, "[curl] Dump %s\n", str);
for(int i = 0; i < size; i+= 16) {
char buf1[256];
char buf2[256];
sprintf(buf1, "%08x : ", start + i);
for(int j = 0; j < 16; j++) {
if(i + j >= size) break;
unsigned char d = ((unsigned char *)start)[i + j];
sprintf(buf1 + strlen(buf1), "%02x ", d);
if((d < 0x20) || (d > 0x7f)) d = '.';
sprintf(buf2 + j, "%c", d);
}
fprintf(stderr, "%s %s\n", buf1, buf2);
}
}
CURLcode curl_easy_perform(struct SessionHandle *data) {
if(!curl_minimum_alarm_cycle) return original_curl_easy_perform(data);
unsigned int ra = 0;
asm volatile(
"ori %0, $31, 0\n"
: "=r"(ra)
);
int method = data->httpreq;
if(method > HTTPREQ_LAST) method = HTTPREQ_LAST;
printf("[curl] %s %s\n", methods[method], data->url);
if(debug) fprintf(stderr, "[curl] %s %s ra=0x%08x\n", methods[method], data->url, ra);
if(data->postfields) {
if(data->postfieldsize > 0) {
if(debug) Dump("[curl] post", data->postfields, data->postfieldsize);
} else {
if(debug) fprintf(stderr, "[curl] post : %s\n", data->postfields);
}
}
if(data->url && !strcmp(data->url + strlen(data->url) - strlen(AlarmPath), AlarmPath)) {
static time_t lastAccess = 0;
struct timeval now;
gettimeofday(&now, NULL);
if((curl_minimum_alarm_cycle < 0) || (now.tv_sec - lastAccess < curl_minimum_alarm_cycle)) {
printf("[curl] Dismiss short cycle alarms.\n");
memcpy(data->out, DummyRes, strlen(DummyRes));
data->httpcode = 200;
return CURL_OK;
}
CURLcode res = original_curl_easy_perform(data);
if(!res) lastAccess = now.tv_sec;
return res;
}
if(data->url && !strncmp(data->url, DummyHost, strlen(DummyHost))) {
printf("[curl] skip http-post.\n");
data->httpcode = 200;
return CURL_OK;
}
CURLcode res = original_curl_easy_perform(data);
if(data->out) printf("[curl] res : %s\n", data->out);
if(debug) fprintf(stderr, "[curl] ret: %d\n", res);
return res;
}