13 Commits
v1.1 ... v1.2

6 changed files with 133 additions and 23 deletions

View File

@@ -145,11 +145,16 @@ clj-totp.sh import <alias> "<url>"
- [x] Show several OTPs at once - [x] Show several OTPs at once
### v1.2 ### v1.2
- [x] Show progress bar
- [x] Styles for progress bar
- [x] Native compilation script corrections
### v2
- [ ] REST API - [ ] REST API
- [ ] User management - [ ] User management
- [ ] Robust BD backend (H2, datomic, or similar) - [ ] Robust BD backend (H2, datomic, or similar)
### v1.3 ### v3
- [ ] Simple web connected to REST API - [ ] Simple web connected to REST API

View File

@@ -2,7 +2,7 @@
(:require [clojure.tools.build.api :as b])) (:require [clojure.tools.build.api :as b]))
(def lib 'es.rcorral/clj-totp) (def lib 'es.rcorral/clj-totp)
(def version (format "1.1.%s" (b/git-count-revs nil))) (def version (format "1.2.%s" (b/git-count-revs nil)))
(def target-dir "target") (def target-dir "target")
(def class-dir (str target-dir "/classes")) (def class-dir (str target-dir "/classes"))
(def uber-file (format "target/%s-%s-standalone.jar" (name lib) version)) (def uber-file (format "target/%s-%s-standalone.jar" (name lib) version))

View File

@@ -3,10 +3,12 @@
io.github.clojure/tools.build {:mvn/version "0.10.10"} io.github.clojure/tools.build {:mvn/version "0.10.10"}
mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; Native image (GraalVM)
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"} ;; Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/ com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"} ;; Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
;; Protobuf for java ;; Protobuf for java
com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}} com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}
;; Progress bar
com.github.pmonks/spinner {:mvn/version "2.0.284"}
}
:aliases {;; Execute the app :aliases {;; Execute the app
:run {:main-opts ["-m" "totp.app"]} :run {:main-opts ["-m" "totp.app"]}

33
native.cmd Normal file
View File

@@ -0,0 +1,33 @@
@echo off
setlocal
REM YOUR LOCAL GRAAL VM INSTALLATION
set JAVA_HOME=D:\programas\graalvm-jdk-21.0.7+8.1
REM generated file
set BIN_FILE=totp
set DEST_DIR=C:\Users\rubencj\util
set PATH=%JAVA_HOME%\bin;%CLOJURE_HOME%;%PATH%
set NATIVE=%JAVA_HOME%\bin\native-image.cmd
echo Using GraalVM native compiler: %NATIVE%
echo Creating uberjar
clojure -T:build uber
set UBERJAR=
for /f "delims=" %%a in ('dir /b /s target\clj-totp-*-standalone.jar') do @set UBERJAR=%%a
echo Created uberjar: %UBERJAR%
echo "Creating native image"
cmd /c %NATIVE% -jar %UBERJAR% -o target\%BIN_FILE% -H:+ReportExceptionStackTraces --features=clj_easy.graal_build_time.InitClojureClasses --report-unsupported-elements-at-runtime --verbose --no-fallback -H:ReflectionConfigurationFiles=reflect_config.json -H:-CheckToolchain
echo Executable created: target\%BIN_FILE%.exe
copy target\%BIN_FILE%.exe %DEST_DIR%
echo Native image copied to %DEST_DIR%\%BIN_FILE%.exe

View File

@@ -8,7 +8,14 @@ clojure -T:build uber
UBERJAR=$(realpath --relative-to=target target/clj-totp-*-standalone.jar) UBERJAR=$(realpath --relative-to=target target/clj-totp-*-standalone.jar)
echo "Creating native image" echo "Creating native image"
$NATIVE -jar target/$UBERJAR -o target/$BIN_FILE -H:+ReportExceptionStackTraces --features=clj_easy.graal_build_time.InitClojureClasses --report-unsupported-elements-at-runtime --verbose --no-fallback -H:ReflectionConfigurationFiles=./reflect_config.json $NATIVE -jar target/$UBERJAR -o target/$BIN_FILE\
-H:+ReportExceptionStackTraces\
-H:ReflectionConfigurationFiles=./reflect_config.json\
--verbose --no-fallback\
--features=clj_easy.graal_build_time.InitClojureClasses\
--report-unsupported-elements-at-runtime\
--initialize-at-build-time=org.fusesource.jansi.Ansi\
#--trace-class-initialization=org.fusesource.jansi.Ansi
echo "Executable created on target/$BIN_FILE" echo "Executable created on target/$BIN_FILE"
cp target/$BIN_FILE ~/bin cp target/$BIN_FILE ~/bin

View File

@@ -4,33 +4,63 @@
[totp.data :refer :all] [totp.data :refer :all]
[cli-matic.core :refer [run-cmd]] [cli-matic.core :refer [run-cmd]]
[clojure.pprint :as pp] [clojure.pprint :as pp]
[clojure.string :as str]) [clojure.string :as str]
[progress.determinate :as pd])
(:import [java.util TimerTask Timer]) (:import [java.util TimerTask Timer])
(:gen-class)) (:gen-class))
(def DEFAULT_BAR_STYLE :coloured-ascii-boxes)
(defn print-timer
([] (print-timer 1 30 DEFAULT_BAR_STYLE))
([bar-style] (print-timer 1 30 bar-style))
([start period bar-style]
(let [a (atom start)]
(pd/animate! a :opts {:total period
;:line 1
:label "Next TOTP: "
;:redraw-rate 60 ;; updates per second
:style (get pd/styles bar-style)}
;(println)
(run! (fn [_] (Thread/sleep 1000) (swap! a inc)) (range start (inc period)))
;(println)
))))
(defn- print-confinuous (defn- print-confinuous
([secret] (print-confinuous secret "sha1" 6 30)) ([secret] (print-confinuous secret "sha1" 6 30 true DEFAULT_BAR_STYLE))
([secret algorithm digits period] ([secret algorithm digits period bar bar-style]
(let [step-millis (* 1000 period) (let [step-millis (* 1000 period)
now (System/currentTimeMillis) now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis))) delay (int (- step-millis (rem now step-millis)))
fn-show (fn [s] (println (format "[%d] %s" (System/currentTimeMillis) (get-otp s algorithm digits period)))) delay-sec (int (/ delay 1000))
fn-show (fn [s] (println (format "%n[%d] %s%n"
(System/currentTimeMillis)
(get-otp s algorithm digits period))))
task (proxy [TimerTask] [] task (proxy [TimerTask] []
(run [] (fn-show secret)))] (run [] (println) (fn-show secret)))
task-bar (proxy [TimerTask] []
(run [] (print-timer bar-style)))
task-init (proxy [TimerTask] []
(run [] (print-timer (- period delay-sec) period bar-style)))]
(println "\n <Generating continuosly, press enter to stop>\n") (println "\n <Generating continuosly, press enter to stop>\n")
;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay)) ;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay))
(println "Refresing in" (int (/ delay 1000)) "seconds") (println "Refresing in" delay-sec "seconds")
(fn-show secret) (fn-show secret)
;; Now, start the tasks
(when bar
(. (new Timer) (schedule task-init 0))
(. (new Timer) (scheduleAtFixedRate task-bar delay step-millis)))
(. (new Timer) (scheduleAtFixedRate task delay step-millis))) (. (new Timer) (scheduleAtFixedRate task delay step-millis)))
(read-line))) ;; Waits for a key press ;; Waits for a key press
(read-line)))
(defn cmd-generate (defn cmd-generate
[& {:keys [secret continuous algorithm digits period]}] [& {:keys [secret continuous algorithm digits period bar bar-style]}]
;;(pp/pprint opts) ;;(pp/pprint opts)
(if continuous (if continuous
(print-confinuous secret algorithm digits period) (print-confinuous secret algorithm digits period bar bar-style)
(println (get-otp secret algorithm digits period)))) (println (get-otp secret algorithm digits period))))
@@ -49,28 +79,45 @@
(let [step-millis (* 1000 period) (let [step-millis (* 1000 period)
now (System/currentTimeMillis) now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis))) delay (int (- step-millis (rem now step-millis)))
delay-sec (int (/ delay 1000))
fn-show (fn [s] fn-show (fn [s]
(println "\n")
(dorun (map print-app s)) (dorun (map print-app s))
(println "")) ;; Separate each (println) ;; Separate each
)
task (proxy [TimerTask] [] task (proxy [TimerTask] []
(run [] (fn-show apps)))] (run [] (fn-show apps)))]
(println "\n <Generating continuosly, press enter to stop>\n") (println "\n <Generating continuosly, press enter to stop>\n")
;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay)) ;; (println "Now:" now ", Delay:" delay ", Next execution: " (+ now delay))
(println "Refresing in" (int (/ delay 1000)) "seconds") (println "Refresing in" delay-sec "seconds")
(fn-show apps) (fn-show apps)
;; Now, start the tasks
(. (new Timer) (scheduleAtFixedRate task delay step-millis))) (. (new Timer) (scheduleAtFixedRate task delay step-millis)))
(read-line))) ;; Waits for a key press )) ;; Waits for a key press
(defn cmd-get-multi (defn cmd-get-multi
[& {:keys [continuous _arguments]}] [& {:keys [continuous bar bar-style _arguments]}]
;(pp/pprint opts) ;(pp/pprint opts)
(with-config (with-config
(let [apps (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]} (let [apps (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]}
(map #(get-app cfg %) _arguments))] (map #(get-app cfg %) _arguments))]
;(println "found apps: " apps) ;(println "found apps: " apps)
(if continuous (if continuous
(print-app-continuous 30 apps) (let [period 30
step-millis (* 1000 period)
now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis)))
delay-sec (int (/ delay 1000))
task-bar (proxy [TimerTask] []
(run [] (print-timer bar-style)))
task-init (proxy [TimerTask] []
(run [] (print-timer (- period delay-sec) period bar-style)))]
(print-app-continuous period apps)
(when bar
(. (new Timer) (schedule task-init 0))
(. (new Timer) (scheduleAtFixedRate task-bar delay step-millis)))
(read-line))
(dorun (map #(print-app %) apps)))))) (dorun (map #(print-app %) apps))))))
@@ -153,7 +200,7 @@
(def cli-options (def cli-options
{:app {:command "totp" {:app {:command "totp"
:version "1.1" :version "1.2"
:description ["Generate a TOTP"]} :description ["Generate a TOTP"]}
:commands [;; Generate a TOTP with given params :commands [;; Generate a TOTP with given params
@@ -182,7 +229,15 @@
{:option "period" :short "p" {:option "period" :short "p"
:as "Validity time in seconds" :as "Validity time in seconds"
:type :int :type :int
:default 30}] :default 30}
{:option "bar" :short "b"
:as "Show progress bar"
:type :with-flag
:default true}
{:option "bar-style" :short "s"
:as "Progress bar style"
:type #{:ascii-basic :ascii-boxes :coloured-ascii-boxes :emoji-circles :emoji-boxes}
:default :coloured-ascii-boxes}]
:runs cmd-generate} :runs cmd-generate}
;; Generate a TOTP for a configured app ;; Generate a TOTP for a configured app
{:command "get" :short "g" {:command "get" :short "g"
@@ -198,7 +253,15 @@
{:option "continuous" :short "c" {:option "continuous" :short "c"
:as "Contiuous mode" :as "Contiuous mode"
:type :with-flag :type :with-flag
:default false}] :default false}
{:option "bar" :short "b"
:as "Show progress bar"
:type :with-flag
:default true}
{:option "bar-style" :short "s"
:as "Progress bar style"
:type #{:ascii-basic :ascii-boxes :coloured-ascii-boxes :emoji-circles :emoji-boxes}
:default :coloured-ascii-boxes}]
:runs cmd-get-multi} :runs cmd-get-multi}
;; Check and init your config file ;; Check and init your config file
{:command "config" :short "c" {:command "config" :short "c"