|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <map> |
| 4 | +#include <algorithm> |
| 5 | +#include <cstdlib> |
| 6 | +#include <ctime> |
| 7 | +#include <cstring> |
| 8 | +#include <cerrno> |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +#include <sys/types.h> |
| 12 | +#include <sys/socket.h> |
| 13 | +#include <netinet/in.h> |
| 14 | +#include <arpa/inet.h> |
| 15 | +#include <sys/time.h> |
| 16 | +#include <wait.h> |
| 17 | +#include <unistd.h> |
| 18 | + |
| 19 | +#include "custom_utils.h" |
| 20 | + |
| 21 | +bool process_challenge(const string &R, const string &PH, string &answer); |
| 22 | +int socket_to_server(const char *IP, int port); |
| 23 | + |
| 24 | +int main(int argc, char *argv[]) |
| 25 | +{ |
| 26 | + int server_socket = socket_to_server("127.0.0.1", 17777); |
| 27 | + |
| 28 | + if (server_socket != -1) |
| 29 | + { |
| 30 | + struct timeval tv; |
| 31 | + |
| 32 | + tv.tv_sec = 10; |
| 33 | + tv.tv_usec = 0; |
| 34 | + |
| 35 | + setsockopt(server_socket, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); |
| 36 | + |
| 37 | + // read challenge |
| 38 | + const string &challenge = read_packet(server_socket); |
| 39 | + int slash_pos = challenge.find('/'); |
| 40 | + |
| 41 | + if (slash_pos != challenge.npos) |
| 42 | + { |
| 43 | + const string &R = hex_to_string(challenge.substr(0, slash_pos)); |
| 44 | + const string &PH = challenge.substr(slash_pos + 1, challenge.length()); |
| 45 | + |
| 46 | + string answer; |
| 47 | + |
| 48 | + if (process_challenge(R, PH, answer)) |
| 49 | + { |
| 50 | + cout << "Answer: " << answer << endl; |
| 51 | + |
| 52 | + send(server_socket, (answer + "\n").c_str(), strlen((answer + "\n").c_str()), MSG_NOSIGNAL); |
| 53 | + |
| 54 | + const string &result = read_packet(server_socket); |
| 55 | + cout << "Result: " << result << endl; |
| 56 | + |
| 57 | + close(server_socket); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + close(server_socket); |
| 62 | + } |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + cerr << "Wrong challenge format!" << endl; |
| 67 | + close(server_socket); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +bool process_challenge(const string &R, const string &PH, string &answer) |
| 75 | +{ |
| 76 | + int min_processing_time = get_min_processing_time(); |
| 77 | + int max_processing_time = get_max_processing_time(PH.length() * 4); |
| 78 | + |
| 79 | + // start timer |
| 80 | + clock_t start = clock(); |
| 81 | + |
| 82 | + string hash; |
| 83 | + double processing_time; |
| 84 | + |
| 85 | + // do PoW |
| 86 | + do |
| 87 | + { |
| 88 | + answer = R + generate_random_string(128) + R; |
| 89 | + |
| 90 | + if (!get_sha256(answer, hash)) |
| 91 | + { |
| 92 | + cerr << "Cannot generate hash!" << endl; |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + // stop timer |
| 97 | + clock_t end = clock(); |
| 98 | + |
| 99 | + processing_time = (double)(end - start) / CLOCKS_PER_SEC; |
| 100 | + |
| 101 | + if (processing_time > max_processing_time) |
| 102 | + { |
| 103 | + cerr << "It takes too long!" << endl; |
| 104 | + return false; |
| 105 | + } |
| 106 | + } while (hash.find(PH) == hash.npos || hash.find(PH) != 0); |
| 107 | + |
| 108 | + cout << "Processing time: " << processing_time << "s" << endl; |
| 109 | + |
| 110 | + while (processing_time < min_processing_time) |
| 111 | + { |
| 112 | + usleep(500000); |
| 113 | + processing_time += 0.5; |
| 114 | + } |
| 115 | + |
| 116 | + answer = string_to_hex(answer); |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +int socket_to_server(const char *IP, int port) |
| 122 | +{ |
| 123 | + struct sockaddr_in address; |
| 124 | + |
| 125 | + address.sin_family = AF_INET; |
| 126 | + address.sin_addr.s_addr = inet_addr(IP); |
| 127 | + address.sin_port = htons(port); |
| 128 | + |
| 129 | + int sock = socket(AF_INET, SOCK_STREAM, 0); |
| 130 | + |
| 131 | + if (connect(sock, (struct sockaddr *)&address, sizeof(address)) == -1) |
| 132 | + { |
| 133 | + return -1; |
| 134 | + } |
| 135 | + |
| 136 | + return sock; |
| 137 | +} |
0 commit comments