Added webserver, parse args, A1066
This commit is contained in:
242
main.cpp
242
main.cpp
@ -1,37 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <cstdio>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include <netdb.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
|
||||
#include "inc/args.hpp"
|
||||
#include "inc/json.hpp"
|
||||
#include "inc/macros.hpp"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
//Client side
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
//we need 2 things: ip address and port number, in that order
|
||||
if(argc != 3)
|
||||
{
|
||||
cerr << "Usage: ip_address port" << endl; exit(0);
|
||||
} //grab the IP address and port number
|
||||
char *serverIp = argv[1]; int port = atoi(argv[2]);
|
||||
//create a message buffer
|
||||
char msg[1500];
|
||||
//setup a socket and connection tools
|
||||
struct hostent* host = gethostbyname(serverIp);
|
||||
int port = 9070;
|
||||
|
||||
|
||||
char* getFrom1066(char* ip, char* cmd) {
|
||||
INFO("------ AVALON 1066 ------")
|
||||
INFO("IP: ", ip)
|
||||
INFO("Command: ", cmd)
|
||||
|
||||
int port = 4028;
|
||||
int msglen = 1024*8;
|
||||
char* msg = new char[msglen];
|
||||
|
||||
struct hostent* host = gethostbyname(ip);
|
||||
sockaddr_in sendSockAddr;
|
||||
bzero((char*)&sendSockAddr, sizeof(sendSockAddr));
|
||||
sendSockAddr.sin_family = AF_INET;
|
||||
@ -40,45 +47,172 @@ int main(int argc, char *argv[])
|
||||
int clientSd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
//try to connect...
|
||||
int status = connect(clientSd, (sockaddr*) &sendSockAddr, sizeof(sendSockAddr));
|
||||
if(status < 0)
|
||||
{
|
||||
cout<<"Error connecting to socket!"<<endl;
|
||||
return -1;
|
||||
if(status < 0) {
|
||||
ERR("Error connecting to socket!")
|
||||
return (char*)"";
|
||||
}
|
||||
cout << "Connected to the server!" << endl;
|
||||
int bytesRead, bytesWritten = 0;
|
||||
struct timeval start1, end1;
|
||||
gettimeofday(&start1, NULL);
|
||||
while(1)
|
||||
{
|
||||
cout << ">";
|
||||
string data;
|
||||
getline(cin, data);
|
||||
memset(&msg, 0, sizeof(msg));//clear the buffer
|
||||
strcpy(msg, data.c_str());
|
||||
if(data == "exit")
|
||||
{
|
||||
send(clientSd, (char*)&msg, strlen(msg), 0);
|
||||
break;
|
||||
}
|
||||
bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);
|
||||
cout << "Awaiting server response..." << endl;
|
||||
memset(&msg, 0, sizeof(msg));//clear the buffer
|
||||
bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0);
|
||||
if(!strcmp(msg, "exit"))
|
||||
{
|
||||
cout << "Server has quit the session" << endl;
|
||||
break;
|
||||
}
|
||||
cout << "Server: " << msg << endl;
|
||||
}
|
||||
gettimeofday(&end1, NULL);
|
||||
INFO("Connected to the server!")
|
||||
|
||||
memset(msg, 0, strlen(msg));
|
||||
send(clientSd, cmd, sizeof(cmd), 0);
|
||||
INFO("Awaiting server response...");
|
||||
recv(clientSd, msg, msglen, MSG_WAITALL);
|
||||
INFO("Server: ", msg)
|
||||
close(clientSd);
|
||||
cout << "********Session********" << endl;
|
||||
cout << "Bytes written: " << bytesWritten <<
|
||||
" Bytes read: " << bytesRead << endl;
|
||||
cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec)
|
||||
<< " secs" << endl;
|
||||
cout << "Connection closed" << endl;
|
||||
INFO("-------------------------")
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
char* httpGetURL(char line[], const char symbol[]) {
|
||||
char *copy = (char*)malloc(strlen(line) + 1);
|
||||
strcpy(copy, line);
|
||||
|
||||
char *message;
|
||||
char *token = strtok(copy, symbol);
|
||||
int current = 0;
|
||||
|
||||
while(token != nullptr) {
|
||||
token = strtok(nullptr, " ");
|
||||
if(current == 0) {
|
||||
message = token;
|
||||
if (message == nullptr) {
|
||||
message = (char *) "";
|
||||
}
|
||||
return message;
|
||||
}
|
||||
current = current + 1;
|
||||
}
|
||||
free(copy);
|
||||
free(token);
|
||||
return message;
|
||||
}
|
||||
|
||||
void httpServer(int port) {
|
||||
char http_header[25] = "HTTP/1.1 200 Ok\r\n";
|
||||
int server_fd, new_socket, pid;
|
||||
struct sockaddr_in address;
|
||||
int addrlen = sizeof(address);
|
||||
|
||||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
||||
CRIT("In sockets")
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons(port);
|
||||
|
||||
memset(address.sin_zero, '\0', sizeof address.sin_zero);
|
||||
|
||||
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
|
||||
CRIT("In bind")
|
||||
close(server_fd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (listen(server_fd, 10) < 0) {
|
||||
CRIT("In listen")
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
INFO("HTTP server started on: http://127.0.0.1:", port)
|
||||
|
||||
while(1) {
|
||||
printf("\n+++++++ Waiting for new connection ++++++++\n\n");
|
||||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
|
||||
CRIT("In accept")
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
pid = fork();
|
||||
if(pid < 0) {
|
||||
CRIT("Error on fork")
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(pid == 0) {
|
||||
char buffer[30000] = {0};
|
||||
read(new_socket , buffer, 30000);
|
||||
char *url = httpGetURL(buffer, " ");
|
||||
INFO(url)
|
||||
|
||||
char *copy_head = new char[strlen(http_header) + 200];
|
||||
strcpy(copy_head, http_header);
|
||||
|
||||
if(strstr(url, "/api/"))
|
||||
strcat(copy_head, "Content-Type: application/json\r\n\r\n");
|
||||
else if(strstr(url, ".js"))
|
||||
strcat(copy_head, "Content-Type: text/javascript\r\n\r\n");
|
||||
else
|
||||
strcat(copy_head, "Content-Type: text/html\r\n\r\n");
|
||||
|
||||
write(new_socket, copy_head, strlen(copy_head));
|
||||
|
||||
|
||||
if(strstr(url, ".js")) {
|
||||
char file_path[512] = ".";
|
||||
strcat(file_path, url);
|
||||
|
||||
int file = open(file_path, O_RDONLY);
|
||||
if(file < 0){
|
||||
WARN("Cannot Open file path : ", file_path, " with error ", file)
|
||||
send(new_socket, R"({"error":"FILE_NOT_EXISTS"})", 27, MSG_DONTWAIT);
|
||||
}
|
||||
|
||||
struct stat stat_buf;
|
||||
fstat(file, &stat_buf);
|
||||
sendfile(new_socket, file, nullptr, stat_buf.st_blksize);
|
||||
}
|
||||
else if(strcmp(url, "/") == 0) {
|
||||
char* msg = (char*)R"(<meta charset="UTF-8"><script defer src="/index.js"></script>)";
|
||||
send(new_socket, msg, strlen(msg), MSG_DONTWAIT);
|
||||
}
|
||||
else {
|
||||
char* msg = getFrom1066((char*)"128.128.128.56", (char*)"estats");
|
||||
send(new_socket, msg, strlen(msg), MSG_DONTWAIT);
|
||||
}
|
||||
|
||||
close(new_socket);
|
||||
free(copy_head);
|
||||
}
|
||||
else{
|
||||
printf(">>>>>>>>>>Parent create child with pid: %d <<<<<<<<<", pid);
|
||||
close(new_socket);
|
||||
}
|
||||
}
|
||||
close(server_fd);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
INFO("Bit.ASICmon started!")
|
||||
|
||||
InputParser input(argc, argv);
|
||||
if(input.cmdOptionExists("-h")) {
|
||||
INFO("HELP")
|
||||
|
||||
exit(0);
|
||||
}
|
||||
if(input.cmdOptionExists("-p")) {
|
||||
const char* aport = input.getCmdOption("-p").c_str();
|
||||
if (strcmp(aport, "") == 0){
|
||||
port = atoi(aport);
|
||||
INFO("Set port: ", port)
|
||||
}
|
||||
else {
|
||||
WARN("Port invalid using default: ", port)
|
||||
}
|
||||
}
|
||||
|
||||
getFrom1066((char*)"128.128.128.56", (char*)"version");
|
||||
char* msg = getFrom1066((char*)"128.128.128.56", (char*)"estats");
|
||||
|
||||
regex re(R"(BOOTBY\[([\d\s\w.]+)\])");
|
||||
cmatch m;
|
||||
|
||||
regex_search(msg, m, re);
|
||||
INFO(m[1])
|
||||
|
||||
httpServer(port);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user