commit 008b5f74235a7fd04852e6f640429269f1ec4845
parent 8aac44bb98af5442e29c8cb9a5a4acbe40d96bb2
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 29 Apr 2021 16:20:38 +0200
basic service structure and docker setup
Diffstat:
10 files changed, 229 insertions(+), 52 deletions(-)
diff --git a/service/Dockerfile b/service/Dockerfile
@@ -1,6 +1,6 @@
FROM ubuntu:18.04
-RUN apt update && apt install -y --no-install-recommends socat
+RUN apt update && apt install -y --no-install-recommends socat build-essential
RUN addgroup --system service
RUN adduser --system --ingroup service --uid 1000 service
@@ -13,6 +13,9 @@ RUN chmod +x /entrypoint.sh
COPY src/ /service/
WORKDIR /service/
+
+RUN make clean && make
+
EXPOSE 9000
ENTRYPOINT ["/entrypoint.sh"]
diff --git a/service/docker-compose.yml b/service/docker-compose.yml
@@ -1,8 +1,8 @@
-version: '1'
+version: '3'
services:
printdoc:
build: .
volumes:
- ./data/:/data:rw
ports:
- - "2323:8000"
+ - "9000:9000"
diff --git a/service/entrypoint.sh b/service/entrypoint.sh
@@ -1,9 +1,10 @@
#!/bin/sh
-set -e
-set -x
-# Chown the mounted data volume
+set -e -x
+
chown -R service:service "/data/"
-# Launch our service as user 'service'
-exec su -s /bin/sh -c 'PYTHONUNBUFFERED=1 python3 n0t3b00k.py' service
-\ No newline at end of file
+export RESULTDIR="/data"
+
+servicecmd='socat TCP-LISTEN:9000,fork,reuseaddr EXEC:"/service/printdoc",raw,pty,echo=0,stderr'
+exec su -s /bin/sh -c "$servicecmd" service
diff --git a/service/src/Makefile b/service/src/Makefile
@@ -1,6 +1,13 @@
+.PHONY: all clean
+
+all: printdoc
+
+clean:
+ rm -f printdoc *.o
+
%.o: %.c %.h
$(CC) -c -o $@ $< -I .
printdoc: printdoc.c stlfile.o
- $(CC) -o $@ $< $(CFLAGS) $(LDLIBS)
+ $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS)
diff --git a/service/src/cat b/service/src/cat
@@ -1,5 +0,0 @@
-
- /\_/\ [ENO] _
- = u u =_______| \\ < GET THE FLAG! ;)
- _ w __( \__))
- c_____>__(_____)__,
diff --git a/service/src/msgs/cat_flag b/service/src/msgs/cat_flag
@@ -0,0 +1,5 @@
+
+ /\_/\ [ENO] _
+ = u u =_______| \\
+ _ w __( \__))
+ c_____>__(_____)__,
diff --git a/service/src/msgs/welcome b/service/src/msgs/welcome
@@ -0,0 +1,2 @@
+Welcome to PrintDoc!
+Upload stl files and see all the interesting details!
diff --git a/service/src/printdoc.c b/service/src/printdoc.c
@@ -2,41 +2,62 @@
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
+#include <dirent.h>
#include "stlfile.h"
#define ARRSIZE(x) (sizeof(x)/sizeof((x)[0]))
+#define MIN(x,y) ((x) > (y) ? (y) : (x))
struct command {
const char *name;
- void (*func)();
+ void (*func)(char *);
};
-void request_info();
-void parse_file();
-void cat_flag();
-void list_commands();
+void* checkp(void *p);
+const char* ask(const char *fmtstr, ...);
+void* die(const char *fmtstr, ...);
+char* aprintf(const char *fmtstr, ...);
+void dump(const char *filepath);
+
+void search_cmd(char *arg);
+void submit_cmd(char *arg);
+void list_cmd(char *arg);
+void cat_cmd(char *arg);
struct command commands[] = {
- { "request", request_info },
- { "info", parse_file },
- { "cat", cat_flag },
- { "ls", list_commands },
+ { "submit", submit_cmd },
+ { "cat", cat_cmd },
+ { "help", list_cmd },
+ { "search", search_cmd }
};
+struct parseinfo *lastrun = NULL;
+const char *resultdir;
+
+void*
+checkp(void *p)
+{
+ if (!p) die("pointer assertion failed, OOM?\n");
+ return p;
+}
+
const char*
ask(const char *fmtstr, ...)
{
static char linebuf[256];
va_list ap;
+ int fail;
va_start(ap, fmtstr);
vprintf(fmtstr, ap);
va_end(ap);
- fgets(linebuf, sizeof(linebuf), stdin);
+ fail = !fgets(linebuf, sizeof(linebuf), stdin);
+
+ if (!fail) linebuf[strlen(linebuf) - 1] = '\0';
- return linebuf;
+ return fail ? "" : linebuf;
}
void*
@@ -51,49 +72,163 @@ die(const char *fmtstr, ...)
exit(EXIT_FAILURE);
}
-void
-request_info()
+char*
+aprintf(const char *fmtstr, ...)
{
- const char *bufp;
- char *end, *contents;
- int len;
+ va_list ap, cpy;
+ size_t nb;
+ char *str;
- bufp = ask("How large is your file?\n");
- len = strtoul(bufp, &end, 10);
- if (!len || *end || len < 7000)
- die("Invalid file length!\n");
+ va_copy(cpy, ap);
- printf("Ok! Im listening..\n");
- contents = malloc(len);
- read(STDIN_FILENO, contents, len);
+ va_start(cpy, fmtstr);
+ nb = vsnprintf(NULL, 0, fmtstr, ap);
+ va_end(cpy);
- printf("I got: %s\n", contents);
+ if (nb <= 0) die("encountered invalid fmtstr!\n");
- free(contents);
+ str = checkp(malloc(nb));
+ va_start(ap, fmtstr);
+ nb = vsnprintf(str, nb, fmtstr, ap);
+ va_end(ap);
+
+ return str;
}
-void
-parse_file()
+char*
+strhash(const char *filename)
{
- printf("hi\n");
+ static const char *hexalph = "0123456789ABCDEF";
+ char *buf;
+ int i, k;
+
+ buf = checkp(malloc(33));
+ for (i = 0; i < MIN(32, strlen(filename)); i++) {
+ unsigned char v = 0;
+ for (k = i; k < strlen(filename); k += 32)
+ v ^= filename[k];
+ buf[i*2+0] = hexalph[(v >> 0) & 0x0f];
+ buf[i*2+1] = hexalph[(v >> 4) & 0x0f];
+ }
+
+ if (i == 0) {
+ memset(buf, '0', 32);
+ } else if (i < 32) {
+ for (k = 0; k < 32; k++)
+ buf[k] = buf[k % i];
+ }
+
+ buf[32] = '\0';
+
+ return buf;
}
void
-cat_flag()
+dump(const char *filename)
{
+ char buf[256];
FILE *f;
- char catbuf[256];
int nb;
- f = fopen("cat", "r");
- nb = fread(catbuf, 1, sizeof(catbuf), f);
+ if (!(f = fopen(filename, "r"))) return;
+
+ while ((nb = fread(buf, 1, sizeof(buf), f)))
+ printf("%.*s\n", nb, buf);
+
fclose(f);
+}
+
+void
+search_cmd(char *arg)
+{
+ char *end, *resultfile = NULL;
+ char *hash;
+ struct dirent *de;
+ int i, which;
+ DIR *d = NULL;
+
+ if (!lastrun)
+ hash = strhash(ask("What is the model name?\n"));
+ else
+ hash = checkp(strdup(lastrun->namehash));
+
+ d = opendir("scans");
+ if (!d) return;
+
+ i = 0;
+ while ((de = readdir(d))) {
+ if (de->d_type != DT_DIR && !strncmp(hash, de->d_name, strlen(hash))) {
+ printf("%i : %s\n", i, de->d_name);
+ i++;
+ }
+ }
- printf("%.*s\n", nb, catbuf);
+ if (i == 0) {
+ fprintf(stderr, "Sorry, couldnt find any matching scan result!\n");
+ goto cleanup;
+ } else {
+ which = strtoul(ask("Which of these results?\n"), &end, 10);
+ if (which >= i || which < 0 || *end) {
+ fprintf(stderr, "Invalid index!\n");
+ goto cleanup;
+ }
+ }
+
+ while ((de = readdir(d))) {
+ if (de->d_type != DT_DIR && !strncmp(hash, de->d_name, strlen(hash))) {
+ if (i == which) {
+ resultfile = aprintf("scans/%s/info", strdup(de->d_name));
+ break;
+ }
+ i++;
+ }
+ }
+
+ if (resultfile)
+ dump(resultfile);
+
+cleanup:
+ free(resultfile);
+ closedir(d);
+}
+
+void
+submit_cmd(char *arg)
+{
+ const char *bufp;
+ char *end, *contents;
+ size_t len;
+
+ bufp = ask("How large is your file?\n");
+ len = strtoul(bufp, &end, 10);
+ if (len <= 0 || len >= 7000 || *end) {
+ fprintf(stderr, "Invalid file length!\n");
+ return;
+ }
+
+ printf("Ok! Im listening..\n");
+ contents = checkp(malloc(len));
+ fread(contents, 1, len, stdin);
+
+ if (lastrun) free_parseinfo(lastrun);
+ lastrun = parse_file(contents, len);
+
+ dump(lastrun->infopath);
+
+ free(contents);
}
void
-list_commands()
+cat_cmd(char *arg)
+{
+ if (arg && !strncmp(arg, "flag", 4))
+ dump("msgs/cat_flag");
+ else
+ printf("meow\n");
+}
+
+void
+list_cmd(char *arg)
{
int i;
@@ -106,9 +241,16 @@ list_commands()
int
main()
{
- char linebuf[256], *cp;
+ char linebuf[256], *cp, *arg;
int exit, i;
+ setvbuf(stdin, NULL, _IONBF, 0);
+
+ if (!(resultdir = getenv("RESULTDIR")))
+ resultdir = "/data";
+
+ dump("msgs/welcome");
+
exit = 0;
while (!exit) {
memset(linebuf, ' ', sizeof(linebuf));
@@ -121,13 +263,18 @@ main()
linebuf[strlen(linebuf) - 1] = '\0';
cp = strchr(linebuf, ' ');
+ arg = cp ? cp + 1 : NULL;
if (cp) *cp = 0;
+
for (i = 0; i < ARRSIZE(commands); i++) {
if (!strcmp(commands[i].name, linebuf)) {
- commands[i].func();
+ commands[i].func(arg);
break;
}
}
+
+ if (i == ARRSIZE(commands) && strlen(linebuf) != 0)
+ fprintf(stderr, "No such command!\n");
}
printf("see you later!\n");
diff --git a/service/src/stlfile.c b/service/src/stlfile.c
@@ -1,4 +1,16 @@
#include "stlfile.h"
+struct parseinfo*
+parse_file(char *buf, size_t len)
+{
+ return NULL;
+}
+
+
+void
+free_parseinfo(struct parseinfo *info)
+{
+ free(info);
+}
diff --git a/service/src/stlfile.h b/service/src/stlfile.h
@@ -4,7 +4,13 @@
#include <stdlib.h>
#include <string.h>
+struct parseinfo {
+ char fmtbuf[256];
+ int attrs;
+ char *namehash, *infopath, *stlpath;
+};
-
+struct parseinfo* parse_file(char *buf, size_t len);
+void free_parseinfo(struct parseinfo *info);
#endif // STLFILE_H