From c1af7cbed3ea9446cd4557deba9764a93ace2cb7 Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Fri, 22 Nov 2024 11:42:00 -0500 Subject: [PATCH] Add view config page --- c/libs/go_p2fa.h | 105 ----------------------------------------------- c/src/p2fa.c | 76 +++++++++++++++++++--------------- go/ctils.go | 5 --- go/totp.go | 25 ++++++++++- 4 files changed, 67 insertions(+), 144 deletions(-) delete mode 100644 c/libs/go_p2fa.h diff --git a/c/libs/go_p2fa.h b/c/libs/go_p2fa.h deleted file mode 100644 index 9ea0391..0000000 --- a/c/libs/go_p2fa.h +++ /dev/null @@ -1,105 +0,0 @@ -/* Code generated by cmd/cgo; DO NOT EDIT. */ - -/* package P2FA */ - - -#line 1 "cgo-builtin-export-prolog" - -#include - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef struct { const char *p; ptrdiff_t n; } _GoString_; -#endif - -#endif - -/* Start of preamble from import "C" comments. */ - - -#line 3 "ctils.go" - -#include - -#line 1 "cgo-generated-wrapper" - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef size_t GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -#ifdef _MSC_VER -#include -typedef _Fcomplex GoComplex64; -typedef _Dcomplex GoComplex128; -#else -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; -#endif - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef _GoString_ GoString; -#endif -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - - -/* Return type for prepareCStringArray */ -struct prepareCStringArray_return { - char** r0; - GoInt r1; -}; -extern __declspec(dllexport) struct prepareCStringArray_return prepareCStringArray(GoSlice goStrings); -extern __declspec(dllexport) void freeCStringArray(char** cStrings, GoInt length); -extern __declspec(dllexport) void loadConfigs(); - -/* Return type for getConfigNames */ -struct getConfigNames_return { - char** r0; - int r1; -}; -extern __declspec(dllexport) struct getConfigNames_return getConfigNames(); -extern __declspec(dllexport) char* getCode(GoString configName); -extern __declspec(dllexport) GoUint64 getTimeRemaining(GoInt n); -extern __declspec(dllexport) GoUint64 getTimeRemainingMS(GoInt n); - -#ifdef __cplusplus -} -#endif diff --git a/c/src/p2fa.c b/c/src/p2fa.c index 28422db..ad7ee8b 100644 --- a/c/src/p2fa.c +++ b/c/src/p2fa.c @@ -40,7 +40,6 @@ int computeWidth(char* text, sFONT *font) { //TODO Put display to sleep after void code(UWORD *blackImage, char* confName) { int ch; - codeLoop: while(1) { ch = getch(); if (ch != ERR) { @@ -105,7 +104,42 @@ void code(UWORD *blackImage, char* confName) { } } -void home(UWORD *blackImage) { +void config(UWORD *blackImage, char* configName) { + GoString goConfName = { configName, strlen(configName) }; + struct getConfig_return getConfigReturn = getConfig(goConfName); + if (getConfigReturn.r1 == -1) { + DebugLine(10, 0, "Config not found\n"); + return; + } + + Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); + EPD_2in13_V4_Init(); + Paint_SelectImage(blackImage); + Paint_Clear(WHITE); + + Paint_DrawString(5, 5, getConfigReturn.r0, &Font12, WHITE, BLACK); + EPD_2in13_V4_Display(blackImage); + + int ch = getch(); + if (ch == 'q') return; +} + +void drawTotpHome(UWORD *blackImage, char** configNames) { + Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); + Paint_SelectImage(blackImage); + Paint_Clear(WHITE); + for (int j = 0; j < length; j++) { + if (configNames[j] == NULL) { + DebugLine(13, 0, "Null Config: %d\n", j); + } + Paint_DrawString(5, 5 + j * 20, configNames[j], &Font12, WHITE, BLACK); + } + EPD_2in13_V4_Display_Base(blackImage); + Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); + Paint_SelectImage(blackImage); +} + +void totpHome(UWORD *blackImage) { struct getConfigNames_return configNamesReturn = getConfigNames(); int length = configNamesReturn.r1; char** configNames = configNamesReturn.r0; @@ -117,21 +151,7 @@ void home(UWORD *blackImage) { return; } - Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); - EPD_2in13_V4_Init(); - Paint_SelectImage(blackImage); - Paint_Clear(WHITE); - - for (int i = 0; i < length; i++) { - if (configNames[i] == NULL) { - DebugLine(13, 0, "Null Config: %d\n", i); - } - Paint_DrawString(5, 5 + (i * 20), configNames[i], &Font12, WHITE, BLACK); - } - EPD_2in13_V4_Display_Base(blackImage); - - Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); - Paint_SelectImage(blackImage); + drawTotpHome(blackImage, configNames); int i = 0; Paint_DrawString(115, 5 + (i * 20), "<", &Font12, WHITE, BLACK); @@ -154,20 +174,11 @@ void home(UWORD *blackImage) { case '\n': timeout(0); code(blackImage, configNames[i]); - - Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); - Paint_SelectImage(blackImage); - Paint_Clear(WHITE); - for (int j = 0; j < length; j++) { - if (configNames[j] == NULL) { - DebugLine(13, 0, "Null Config: %d\n", j); - } - Paint_DrawString(5, 5 + j * 20, configNames[j], &Font12, WHITE, BLACK); - } - EPD_2in13_V4_Display_Base(blackImage); - Paint_NewImage(blackImage, EPD_2in13_V4_WIDTH, EPD_2in13_V4_HEIGHT, 0, WHITE); - Paint_SelectImage(blackImage); - + drawTotpHome(blackImage, configNames); + break; + case 'c': + config(blackImage, configNames[i]); + drawTotpHome(blackImage, configNames); break; case 'q': return; @@ -198,13 +209,14 @@ int drawLoop() { return -1; } - home(blackImage); + totpHome(blackImage); return 0; } int main() { signal(SIGINT, Handler); + signal(SIGTERM, Handler) initscr(); cbreak(); diff --git a/go/ctils.go b/go/ctils.go index f783354..92ad350 100644 --- a/go/ctils.go +++ b/go/ctils.go @@ -2,7 +2,6 @@ package main /* #include -#include */ import "C" import "unsafe" @@ -23,7 +22,3 @@ func freeCStringArray(cStrings **C.char, length int) { C.free(unsafe.Pointer(s)) } } - -func debug(message string) { - C.Debug(message) -} diff --git a/go/totp.go b/go/totp.go index cc3c447..5258109 100644 --- a/go/totp.go +++ b/go/totp.go @@ -19,6 +19,7 @@ import ( ) var token string +var homeDir string var configs map[string]otp.Config @@ -26,8 +27,6 @@ func getSha1() func() hash.Hash { return sha1.New } //export loadConfigs func loadConfigs() { - var homeDir string - osVar := runtime.GOOS if osVar == "windows" { homeDir = os.Getenv("USERPROFILE") @@ -157,6 +156,28 @@ func getConfigNames() (**C.char, C.int) { return (**C.char)(cArray), C.int(configsLen) } +//export getConfig +func getConfig(configName string) (*C.char, C.int) { + files, err := os.ReadDir(homeDir + "/.totp") + if err != nil { + log.Fatal(err) + } + + for _, fileEntry := range files { + if strings.HasPrefix(fileEntry.Name(), configName) { + fileBytes, err := os.ReadFile(homeDir + "/.totp/" + fileEntry.Name()) + if err != nil { + log.Fatal(err) + } + fileStr := string(fileBytes) + cStr := C.CString(fileStr) + runtime.KeepAlive(fileStr) + return (*C.char)(cStr), (C.int)(len(fileStr)) + } + } + return nil, -1 +} + //export getCode func getCode(configName string) *C.char { code := configs[configName].TOTP()