Compare commits

...

3 Commits

2 changed files with 75 additions and 34 deletions

View File

@@ -2,7 +2,7 @@
(:require [clojure.tools.build.api :as b]))
(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 class-dir (str target-dir "/classes"))
(def uber-file (format "target/%s-%s-standalone.jar" (name lib) version))

View File

@@ -10,43 +10,56 @@
(:gen-class))
(defn print-timer
([] (print-timer 1 30))
([start] (print-timer start 30))
([start period]
(let [a (atom start)]
(pd/animate! a :opts {:total period
;:line 1
:label "Next TOTP: "
;:redraw-rate 60 ;; updates per second
:style (:coloured-ascii-boxes pd/styles)}
;(println)
(run! (fn [_] (Thread/sleep 1000) (swap! a inc)) (range start (inc period)))
;(println)
))))
(defn- print-confinuous
([secret] (print-confinuous secret "sha1" 6 30))
([secret algorithm digits period]
([secret] (print-confinuous secret "sha1" 6 30 true))
([secret algorithm digits period bar]
(let [step-millis (* 1000 period)
now (System/currentTimeMillis)
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] []
(run [] (fn-show secret)))
a (atom (int (- period (/ delay 1000))))
up-task (proxy [TimerTask] []
(run [] (pd/animate! a :opts {:total period
:style (:coloured-ascii-boxes pd/styles)
:label "Next TOTP"
:redraw-rate 1
;:line 1
}
;(println "inc" @a)
(if (< @a period)
(swap! a inc)
(reset! a 0)
))))]
(run [] (println) (fn-show secret)))
task-bar (proxy [TimerTask] []
(run [] (print-timer)))
task-init (proxy [TimerTask] []
(run [] (print-timer (- period delay-sec) period)))]
(println "\n <Generating continuosly, press enter to stop>\n")
;; (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)
(. (new Timer) (scheduleAtFixedRate up-task 0 1000))
(. (new Timer) (scheduleAtFixedRate task delay step-millis))
)
(read-line))) ;; Waits for a key press
;; 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)))
;; Waits for a key press
(read-line)))
(defn cmd-generate
[& {:keys [secret continuous algorithm digits period]}]
[& {:keys [secret continuous algorithm digits period bar]}]
;;(pp/pprint opts)
(if continuous
(print-confinuous secret algorithm digits period)
(print-confinuous secret algorithm digits period bar)
(println (get-otp secret algorithm digits period))))
@@ -64,29 +77,49 @@
([period apps]
(let [step-millis (* 1000 period)
now (System/currentTimeMillis)
delay (int (- step-millis (rem now step-millis)))
delay (int (abs (- step-millis (rem now step-millis) 100)))
delay-sec (int (/ delay 1000))
fn-show (fn [s]
(println "\n")
(dorun (map print-app s))
(println "")) ;; Separate each
(println) ;; Separate each
)
task (proxy [TimerTask] []
(run [] (fn-show apps)))]
(println "\n <Generating continuosly, press enter to stop>\n")
;; (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)
;; Now, start the tasks
(. (new Timer) (scheduleAtFixedRate task delay step-millis)))
(read-line))) ;; Waits for a key press
)) ;; Waits for a key press
(defn cmd-get-multi
[& {:keys [continuous _arguments]}]
[& {:keys [continuous bar _arguments]}]
;(pp/pprint opts)
(with-config
(let [apps (filter some? #_{:clj-kondo/ignore [:unresolved-symbol]}
(map #(get-app cfg %) _arguments))]
;(println "found apps: " apps)
(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)))
task-init (proxy [TimerTask] []
(run [] (print-timer (- period delay-sec) period)))]
(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))))))
@@ -169,7 +202,7 @@
(def cli-options
{:app {:command "totp"
:version "1.1"
:version "1.2"
:description ["Generate a TOTP"]}
:commands [;; Generate a TOTP with given params
@@ -198,7 +231,11 @@
{:option "period" :short "p"
:as "Validity time in seconds"
:type :int
:default 30}]
:default 30}
{:option "bar" :short "b"
:as "Show progress bar"
:type :with-flag
:default true}]
:runs cmd-generate}
;; Generate a TOTP for a configured app
{:command "get" :short "g"
@@ -214,7 +251,11 @@
{:option "continuous" :short "c"
:as "Contiuous mode"
:type :with-flag
:default false}]
:default false}
{:option "bar" :short "b"
:as "Show progress bar"
:type :with-flag
:default true}]
:runs cmd-get-multi}
;; Check and init your config file
{:command "config" :short "c"