generated from bearycool11/AI_memory_Loops
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_logs.c
231 lines (190 loc) · 7.64 KB
/
check_logs.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <time.h>
#include <bitcoin/bitcoin.h> // Include Bitcoin library for signing transactions
#include "io_socket.h"
#include "memory_silo.h"
#include "pml_logic_loop.h"
#include "knowledge.h"
#define LOG_FILE "custodian.log" // Log file to check
#define BITCOIN_WALLET "bc1qetkudft7hlsl3k7nhrg6zrkufpu6q3rdnx5ag5" // Example Bitcoin address
#define BITCOIN_RPC_URL "http://127.0.0.1:8332" // Your Bitcoin RPC URL
#define BUFFER_SIZE 1024
// Logging utility for logging activities
void log_message(const char* level, const char* message) {
FILE* log_file = fopen(LOG_FILE, "a");
if (log_file) {
time_t now = time(NULL);
fprintf(log_file, "[%s] [%s] %s\n", level, ctime(&now), message);
fclose(log_file);
}
}
// CURL Write callback for capturing API responses
size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t realsize = size * nmemb;
strncat((char*)userp, (char*)contents, realsize);
return realsize;
}
// Function to retrieve private key from an environment variable
const char* get_private_key() {
const char* private_key = getenv("PRIVATE_KEY");
if (private_key == NULL) {
log_message("ERROR", "Private key not found in environment variable.");
return NULL;
}
return private_key;
}
// Function to sign Bitcoin transaction
void sign_bitcoin_transaction(char* tx_hex, const char* private_key) {
if (private_key == NULL) {
log_message("ERROR", "Private key is NULL, cannot sign transaction.");
return;
}
// Use libbitcoin or other Bitcoin library to sign the transaction
libbitcoin::wallet::payment_address sender_address;
libbitcoin::wallet::secret_key secret_key;
if (!secret_key.from_base58(private_key)) {
log_message("ERROR", "Invalid private key.");
return;
}
// Create a dummy transaction for the purpose of demonstration
libbitcoin::transaction tx;
// Add inputs, outputs, etc., to the transaction
// Sign the transaction
libbitcoin::wallet::sign(tx, secret_key);
// Get the raw transaction hex
snprintf(tx_hex, BUFFER_SIZE, "signed_transaction_hex"); // Replace with actual signed transaction
log_message("INFO", "Transaction signed successfully.");
}
// Function to send the signed Bitcoin transaction to the network
int send_bitcoin_transaction(const char* raw_tx) {
CURL* curl;
CURLcode res;
curl = curl_easy_init();
if (!curl) {
log_message("ERROR", "Failed to initialize cURL.");
return -1;
}
// JSON-RPC parameters for sending the raw transaction
char post_data[BUFFER_SIZE];
snprintf(post_data, sizeof(post_data), "{\"jsonrpc\": \"1.0\", \"id\": \"1\", \"method\": \"sendrawtransaction\", \"params\": [\"%s\"]}", raw_tx);
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, BITCOIN_RPC_URL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
char response_buffer[BUFFER_SIZE];
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
log_message("ERROR", curl_easy_strerror(res));
curl_easy_cleanup(curl);
return -1;
}
log_message("INFO", "Transaction sent to Bitcoin network.");
curl_easy_cleanup(curl);
return 0;
}
// Function to create and sign a Bitcoin transaction
void create_and_sign_transaction(const char* recipient_address, double amount) {
char tx_hex[BUFFER_SIZE];
// Use the private key to sign the transaction
const char* private_key = get_private_key();
sign_bitcoin_transaction(tx_hex, private_key);
// Send the signed transaction to the Bitcoin network
if (send_bitcoin_transaction(tx_hex) != 0) {
log_message("ERROR", "Failed to send the transaction.");
return;
}
log_message("INFO", "Bitcoin transaction successfully sent.");
}
// Function to process Bitcoin payments
void process_bitcoin_payment(const char* recipient, double amount) {
log_message("INFO", "Initiating Bitcoin transaction.");
// Step 1: Create and sign the Bitcoin transaction
create_and_sign_transaction(recipient, amount);
// Step 2: Process the transaction
log_message("INFO", "Processing Bitcoin payment.");
}
// Function to read and output log file contents
void read_log_file(const char* log_file) {
FILE *file = fopen(log_file, "r");
if (file == NULL) {
log_message("ERROR", "Could not open log file.");
return;
}
char line[BUFFER_SIZE];
while (fgets(line, sizeof(line), file)) {
// Print the log content to the console
printf("%s", line);
}
fclose(file);
log_message("INFO", "Log file read successfully.");
}
// Function to interact with the API and check for any errors in logs
int interact_with_api_for_logs(const char* url, const char* headers[], const char* payload, char* response) {
CURL* curl = curl_easy_init();
if (!curl) {
log_message("ERROR", "Failed to initialize cURL.");
return -1;
}
struct curl_slist* header_list = NULL;
for (int i = 0; headers && headers[i]; i++) {
header_list = curl_slist_append(header_list, headers[i]);
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
log_message("ERROR", curl_easy_strerror(res));
curl_easy_cleanup(curl);
curl_slist_free_all(header_list);
return -1;
}
curl_easy_cleanup(curl);
curl_slist_free_all(header_list);
return 0;
}
// Function to check if logs indicate any issues in the system
void check_system_logs_for_issues() {
char response[BUFFER_SIZE] = {0};
const char* url = "http://127.0.0.1:8080/api/log_check";
const char* headers[] = { "Content-Type: application/json", NULL };
const char* payload = "{\"action\": \"check_logs\"}";
// Interact with the custom API to check for system log issues
if (interact_with_api_for_logs(url, headers, payload, response) == 0) {
log_message("INFO", "API interaction successful. Response: ");
printf("%s\n", response); // Print the response to the console
} else {
log_message("ERROR", "API interaction failed.");
}
}
// Function to sync with memory silo
void sync_with_memory_silo(io_socket_t* io_socket, memory_silo_t* silo) {
if (!io_socket || !silo) {
log_message("ERROR", "Invalid parameters for memory silo synchronization.");
return;
}
if (io_socket_send(io_socket->socket, silo->memory, silo->size) < 0) {
log_message("ERROR", "Failed to sync memory silo data.");
} else {
log_message("INFO", "Memory silo data synced successfully.");
}
char response[BUFFER_SIZE];
if (io_socket_receive(io_socket->socket, response, sizeof(response)) > 0) {
log_message("INFO", "Response received from memory silo synchronization.");
}
}
// Main function to check logs and perform system checks
void check_logs_and_perform_checks() {
log_message("INFO", "Starting log check process.");
// Step 1: Read log file and print to console
printf("Reading log file: %s\n", LOG_FILE);
read