From 82168f9955eb19373fa9b69572481a5906095c72 Mon Sep 17 00:00:00 2001 From: lostsh Date: Thu, 16 May 2024 11:27:05 +0200 Subject: [PATCH 1/2] start creating a go version --- Makefile | 33 ++++++++------------ src/go.mod | 3 ++ src/main.c | 73 -------------------------------------------- src/main.go | 39 ++++++++++++++++++++++++ src/requestor.c | 80 ------------------------------------------------- src/requestor.h | 19 ------------ 6 files changed, 55 insertions(+), 192 deletions(-) create mode 100644 src/go.mod delete mode 100644 src/main.c create mode 100644 src/main.go delete mode 100644 src/requestor.c delete mode 100644 src/requestor.h diff --git a/Makefile b/Makefile index c57fef9..ae216b5 100644 --- a/Makefile +++ b/Makefile @@ -1,34 +1,24 @@ CFLAGS= -g -std=c99 -Wall -pedantic -D_XOPEN_SOURCE=700 -fsanitize=address ## Working env -BIN_DIR=bin -SRC_DIR=src -DOC_DIR=doc +BIN_DIR=bin/ +SRC_DIR=src/ +DOC_DIR=doc/ # Build vars TARGET=$(BIN_DIR)/main -SRC=$(wildcard $(SRC_DIR)/*.c) -OBJ=$(subst $(SRC_DIR),$(BIN_DIR),$(SRC:.c=.o)) -# Juste in case ... -CC=gcc +SRC=$(wildcard $(SRC_DIR)/*.go) + +CC=go build all: $(TARGET) ## Compile main Target # obj file are in bin not in src so $^ => $(^:src%.o=bin%.o) -$(TARGET): $(OBJ) - $(CC) $(CFLAGS) $^ -o $@ - -## Compile objects -$(BIN_DIR)/%.o: $(SRC_DIR)/%.c - $(CC) $(CFLAGS) -c $^ -o $@ - +$(TARGET): $(SRC) + $(CC) -o $@ $^ -.PHONY: pack doc clean clean_doc clean_bin - -# Build gzip compressed archive -pack: clean - @cd ../ && tar -czf `basename $(CURDIR)`.tgz `basename $(CURDIR)`/ +.PHONY: doc clean clean_doc clean_bin doc: @doxygen @@ -40,4 +30,7 @@ clean_doc: # Clean *~ *.o clean_bin: - @rm -rf $(BIN_DIR)/* \ No newline at end of file + @rm -rf $(BIN_DIR)/* + + +go build -o bin/ src/main.go diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..ca8ecdc --- /dev/null +++ b/src/go.mod @@ -0,0 +1,3 @@ +module statuscode/main + +go 1.22.3 diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 925ce07..0000000 --- a/src/main.c +++ /dev/null @@ -1,73 +0,0 @@ -/** -* \file main.c -* \version 1.0 beta -* \author Vernhes Yohann (lostsh) -* \date 1 april 2024 -* \brief check the http code of inputed websites -* Main file using requestor lib to send requests -*/ - -#include "requestor.h" -#include - -void printUsage(const char *scriptname){ - printf("[ = ]\tRequestor\n"); - printf("[ * ]\tSend HTTP request on port 80 of spcified target.\n"); - printf("[ * ]\tRetrieve HTTP response code.\n\n"); - printf("[ + ]\tUsage: %s\n", scriptname); - printf("[ > ]\t-t [target_ip] \t Input target ip (required)\n"); - printf("[ > ]\t-v \t Enable verbose mode\n"); - printf("[ > ]\t-h \t Show this help\n\n"); -} - -int main(int argc, char **argv){ - //Handle arguments - if(argc < 2){ - fprintf(stderr, "[ ! ]\tException: no arguments given\n"); - printUsage(argv[0]); - return 1; - } - - // arguments vars - short int argv_target_index = -1; - unsigned char debug = 0; - - // iterate over argv - for(int i=0; i ]\tHTTP CODE: [%s]\n", return_code); - else printf("%s", return_code); - return 0; -} \ No newline at end of file diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..544547f --- /dev/null +++ b/src/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "net" + "os" + "bufio" + "log" +) + +func main(){ + log.SetPrefix("[-]\ttcprequest: ") + log.SetFlags(0) + + if len(os.Args) < 2 { + fmt.Printf("Usage: %v target_ip_address\n", os.Args[0]) + log.Fatal("Exception: missing target addr") + } + + //connect target + sock, err := net.Dial("tcp", os.Args[1]) + if nil != err { + fmt.Println("Exception: ", err) + log.Fatal("Exception: tcp bind failed.") + } + //send request + fmt.Fprintf(sock, "HEAD / HTTP/1.0\r\n\r\n") + status, err := bufio.NewReader(sock).ReadString('\n') + if nil != err { + fmt.Println("Exception: ", err) + log.Fatal("Exception: tcp r/w failed.") + } + + log.Print("Done.") + + //now parse request result + //var http_code string + fmt.Println(status) +} \ No newline at end of file diff --git a/src/requestor.c b/src/requestor.c deleted file mode 100644 index 44f6d76..0000000 --- a/src/requestor.c +++ /dev/null @@ -1,80 +0,0 @@ -/** -* \file requestor.c -* \version 1.0 beta -* \author Vernhes Yohann (lostsh) -* \date 1 april 2024 -* \brief requestor lib to send HTTP request -* requestor module file -*/ - -#include "requestor.h" -#include -#include -#include - -/** -* \note this func do not use send and recv -* it use read and write from the unistd lib -*/ -int getHttpCode(const char *target_ip, char *return_code){ - int sockfd = socket(AF_INET, SOCK_STREAM, 0); - - struct sockaddr_in target = {0}; - target.sin_family = AF_INET; - target.sin_port = htons(80); - target.sin_addr.s_addr = inet_addr(target_ip); - - //Connect socket to server - if(connect(sockfd, (struct sockaddr*)&target, sizeof(target)) < 0){ - write(STDERR_FILENO, "[ ! ]\tException: connect failed\n", 32); - return 1; - } - - //Send HTTP request - char *reqStr = "HEAD / HTTP/1.0\r\n\r\n"; - if(write(sockfd, reqStr, len(reqStr)) < 0){ - write(STDERR_FILENO, "[ ! ]\tException: send failed\n", 29); - return 1; - } - - //Handle http response - char resp_buff[16] = ""; - int resp_size = read(sockfd, resp_buff, sizeof(resp_buff)); - if(resp_size <= 0){ - write(STDERR_FILENO, "[ ! ]\tException: read or socket closed\n", 40); - return 1; - } - *(resp_buff+resp_size-1) = '\0'; - //write(STDOUT_FILENO, resp_buff, resp_size); - //write(1, "\n", 1); - - //parse output - int parse_success = parseCode(resp_buff, return_code); - if(parse_success < 0){ - write(STDERR_FILENO, "[ ! ]\tExeption: parsing code\n", 29); - return 1; - } - - close(sockfd); - return 0; -} - -int parseCode(const char *responseStr, char *out_code){ - unsigned short int c = 0; - while('\0' != *(responseStr+c) && len(out_code) < 3){ - if(' ' == *(responseStr+c)){ - *(out_code) = *(responseStr+c+1); - *(out_code+1) = *(responseStr+c+2); - *(out_code+2) = *(responseStr+c+3); - *(out_code+3) = '\0'; - } - c++; - } - return 0; -} - -wlen_t len(const char *word){ - wlen_t size = 0; - while('\0' != *(word+size)) size++; - return size; -} \ No newline at end of file diff --git a/src/requestor.h b/src/requestor.h deleted file mode 100644 index 1c4b7fe..0000000 --- a/src/requestor.h +++ /dev/null @@ -1,19 +0,0 @@ -/** -* \file requestor.h -* \version 1.0 beta -* \author Vernhes Yohann (lostsh) -* \date 1 april 2024 -* \brief requestor lib to send HTTP request -* requestor module header file -*/ - -#ifndef REQUESTOR_H -#define REQUESTOR_H - -int getHttpCode(const char *target_ip, char *return_code); -int parseCode(const char*, char*); - -typedef unsigned short int wlen_t; -wlen_t len(const char*); - -#endif /*REQUESTOR_H*/ \ No newline at end of file From b3f1beb0c8a23f2ae11eb74e8f93cb45aa760325 Mon Sep 17 00:00:00 2001 From: lostsh Date: Thu, 16 May 2024 11:31:23 +0200 Subject: [PATCH 2/2] fix makfile --- Makefile | 3 --- 1 file changed, 3 deletions(-) diff --git a/Makefile b/Makefile index ae216b5..0951fa4 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,3 @@ clean_doc: # Clean *~ *.o clean_bin: @rm -rf $(BIN_DIR)/* - - -go build -o bin/ src/main.go