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])) (: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

@@ -10,43 +10,56 @@
(:gen-class)) (: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 (defn- print-confinuous
([secret] (print-confinuous secret "sha1" 6 30)) ([secret] (print-confinuous secret "sha1" 6 30 true))
([secret algorithm digits period] ([secret algorithm digits period bar]
(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)))
a (atom (int (- period (/ delay 1000)))) task-bar (proxy [TimerTask] []
up-task (proxy [TimerTask] [] (run [] (print-timer)))
(run [] (pd/animate! a :opts {:total period task-init (proxy [TimerTask] []
:style (:coloured-ascii-boxes pd/styles) (run [] (print-timer (- period delay-sec) period)))]
:label "Next TOTP"
:redraw-rate 1
;:line 1
}
;(println "inc" @a)
(if (< @a period)
(swap! a inc)
(reset! a 0)
))))]
(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)
(. (new Timer) (scheduleAtFixedRate up-task 0 1000)) ;; Now, start the tasks
(. (new Timer) (scheduleAtFixedRate task delay step-millis)) (when bar
) (. (new Timer) (schedule task-init 0))
(read-line))) ;; Waits for a key press (. (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 (defn cmd-generate
[& {:keys [secret continuous algorithm digits period]}] [& {:keys [secret continuous algorithm digits period bar]}]
;;(pp/pprint opts) ;;(pp/pprint opts)
(if continuous (if continuous
(print-confinuous secret algorithm digits period) (print-confinuous secret algorithm digits period bar)
(println (get-otp secret algorithm digits period)))) (println (get-otp secret algorithm digits period))))
@@ -64,29 +77,49 @@
([period apps] ([period apps]
(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 (abs (- step-millis (rem now step-millis) 100)))
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 _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)))
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)))))) (dorun (map #(print-app %) apps))))))
@@ -169,7 +202,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
@@ -198,7 +231,11 @@
{: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}]
: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"
@@ -214,7 +251,11 @@
{: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}]
: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"