summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2017-02-06 19:24:36 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2017-02-06 19:24:36 -0500
commite2b6ec92921a2d43f77015f0e75c362a03430e76 (patch)
tree9eaabf1e7d1f17c575d6156da8f93aadd807ed5f
parent2692e95db86b454a92e760446bb4e1424f79e461 (diff)
Move some go stuff into a shared 'util'.
-rw-r--r--colordate.js9
-rw-r--r--crtsh-pem2html.go75
-rw-r--r--tls-pem2html.go75
-rw-r--r--util/date.go51
4 files changed, 90 insertions, 120 deletions
diff --git a/colordate.js b/colordate.js
index a228eb5..22be25b 100644
--- a/colordate.js
+++ b/colordate.js
@@ -3,7 +3,8 @@
var now = Date.now();
var oneday = 1000*60*60*24;
- var mapTo = function(iRange, oRange, iPoint) {
+ // maps from a point on iRange to a point on oRange
+ var mapRange = function(iRange, oRange, iPoint) {
var pct = (iPoint - iRange[0])/(iRange[1]-iRange[0]);
if (pct < 0) {
pct = 0;
@@ -21,17 +22,17 @@
var date2color = function(t) {
var max = 0xF3;
- var red = mapTo([now-oneday, now-(oneday/2)],
+ var red = mapRange([now-oneday, now-(oneday/2)],
[max, 0],
t);
- var green = mapTo([now-(oneday/2), now],
+ var green = mapRange([now-(oneday/2), now],
[0, max],
t);
return rgb(max-green, max-red, max-green-red);
};
var main = function() {
- document.querySelectorAll('time.colordate').forEach(function(time) {
+ document.querySelectorAll('time.daily').forEach(function(time) {
time.style.backgroundColor = date2color(Date.parse(time.dateTime));
});
};
diff --git a/crtsh-pem2html.go b/crtsh-pem2html.go
index 518c919..33a22d7 100644
--- a/crtsh-pem2html.go
+++ b/crtsh-pem2html.go
@@ -9,6 +9,8 @@ import (
"os"
"sort"
"time"
+
+ "./util"
)
func handleErr(err error, str string, a ...interface{}) {
@@ -30,9 +32,9 @@ var tmpl = template.Must(template.New("pem2html").
Funcs(template.FuncMap{
"red": red,
"green": green,
- "date": fDate,
- "datetime": fDateTime,
- "colorDatetime": cDateTime,
+ "date": util.Date2HTML,
+ "datetime": util.DateTime2HTML,
+ "colorDatetime": util.DateTime2ColorHTML,
}).Parse(`<table class=sortable>
<caption>
<p>CT log (Updated {{.now | colorDatetime}})</p>
@@ -58,18 +60,6 @@ var tmpl = template.Must(template.New("pem2html").
</table>
`))
-func fDate(t time.Time) template.HTML {
- return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\" title=\"2006-01-02 15:04:05\">2006-01-02 <span class=time>15:04:05</span></time>"))
-}
-
-func fDateTime(t time.Time) template.HTML {
- return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
-}
-
-func cDateTime(t time.Time) template.HTML {
- return template.HTML(t.Local().Format("<time class=colordate datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
-}
-
func getNow() time.Time {
stat, err := os.Stdin.Stat()
if err == nil {
@@ -81,57 +71,26 @@ func getNow() time.Time {
var now = getNow()
-type DateRange struct {
- a, b time.Time
-}
-
-func (dr DateRange) ToPct(point time.Time) float64 {
- dur_ab := dr.b.Sub(dr.a)
- dur_ap := point.Sub(dr.a)
- return float64(dur_ap) / float64(dur_ab)
-}
-
-type ByteRange struct {
- a, b byte
-}
-
-func (br ByteRange) FromPct(pct float64) byte {
- ab := int16(br.b) - int16(br.a)
- ap := int16(pct * float64(ab))
- return byte(int16(br.a) + ap)
-}
-
-func CapPct(pct float64) float64 {
- if pct < 0 {
- pct = 0
- } else if pct > 1 {
- pct = 1
- }
- return pct
-}
-
-func mapRange(dr DateRange, br ByteRange, t time.Time) byte {
- return br.FromPct(CapPct(dr.ToPct(t)))
-}
-
func green(t time.Time) string {
max := byte(0xF3)
- // 30 days ago: 0 green
- // now: max green
- greenness := mapRange(
- DateRange{now.AddDate(0, 0, -30), now},
- ByteRange{0, max},
+ // When did we get the cert?
+ // - 30 days ago => 0 green
+ // - just now => max green
+ greenness := util.MapRange(
+ util.TimeRange{now.AddDate(0, 0, -30), now},
+ util.ByteRange{0, max},
t)
return fmt.Sprintf("#%02X%02X%02X", max-greenness, max, max-greenness)
}
func red(t time.Time) string {
max := byte(0xF3)
- // now: max red
- // 30 days from now: 0 red
- redness := mapRange(
- DateRange{now, now.AddDate(0, 0, 30)},
- ByteRange{max, 0},
+ // When with the cert expire?
+ // - now => max red
+ // - 30 days from now => 0 red
+ redness := util.MapRange(
+ util.TimeRange{now, now.AddDate(0, 0, 30)},
+ util.ByteRange{max, 0},
t)
return fmt.Sprintf("#%02X%02X%02X", max, max-redness, max-redness)
}
diff --git a/tls-pem2html.go b/tls-pem2html.go
index 3b55f73..d12504b 100644
--- a/tls-pem2html.go
+++ b/tls-pem2html.go
@@ -9,6 +9,8 @@ import (
"os"
"sort"
"time"
+
+ "./util"
)
func handleErr(err error, str string, a ...interface{}) {
@@ -30,9 +32,9 @@ var tmpl = template.Must(template.New("pem2html").
Funcs(template.FuncMap{
"red": red,
"green": green,
- "date": fDate,
- "datetime": fDateTime,
- "colorDatetime": cDateTime,
+ "date": util.Date2HTML,
+ "datetime": util.DateTime2HTML,
+ "colorDatetime": util.DateTime2ColorHTML,
}).Parse(`<table class=sortable>
<caption>
<p>Live Certs (Updated {{.now | colorDatetime}})</p>
@@ -54,18 +56,6 @@ var tmpl = template.Must(template.New("pem2html").
</table>
`))
-func fDate(t time.Time) template.HTML {
- return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\" title=\"2006-01-02 15:04:05\">2006-01-02 <span class=time>15:04:05</span></time>"))
-}
-
-func fDateTime(t time.Time) template.HTML {
- return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
-}
-
-func cDateTime(t time.Time) template.HTML {
- return template.HTML(t.Local().Format("<time class=colordate datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
-}
-
func getNow() time.Time {
stat, err := os.Stdin.Stat()
if err == nil {
@@ -77,57 +67,26 @@ func getNow() time.Time {
var now = getNow()
-type DateRange struct {
- a, b time.Time
-}
-
-func (dr DateRange) ToPct(point time.Time) float64 {
- dur_ab := dr.b.Sub(dr.a)
- dur_ap := point.Sub(dr.a)
- return float64(dur_ap) / float64(dur_ab)
-}
-
-type ByteRange struct {
- a, b byte
-}
-
-func (br ByteRange) FromPct(pct float64) byte {
- ab := int16(br.b) - int16(br.a)
- ap := int16(pct * float64(ab))
- return byte(int16(br.a) + ap)
-}
-
-func CapPct(pct float64) float64 {
- if pct < 0 {
- pct = 0
- } else if pct > 1 {
- pct = 1
- }
- return pct
-}
-
-func mapRange(dr DateRange, br ByteRange, t time.Time) byte {
- return br.FromPct(CapPct(dr.ToPct(t)))
-}
-
func green(t time.Time) string {
max := byte(0xF3)
- // 30 days ago: 0 green
- // now: max green
- greenness := mapRange(
- DateRange{now.AddDate(0, 0, -30), now},
- ByteRange{0, max},
+ // When did we get the cert?
+ // - 30 days ago => 0 green
+ // - just now => max green
+ greenness := util.MapRange(
+ util.TimeRange{now.AddDate(0, 0, -30), now},
+ util.ByteRange{0, max},
t)
return fmt.Sprintf("#%02X%02X%02X", max-greenness, max, max-greenness)
}
func red(t time.Time) string {
max := byte(0xF3)
- // now: max red
- // 30 days from now: 0 red
- redness := mapRange(
- DateRange{now, now.AddDate(0, 0, 30)},
- ByteRange{max, 0},
+ // When with the cert expire?
+ // - now => max red
+ // - 30 days from now => 0 red
+ redness := util.MapRange(
+ util.TimeRange{now, now.AddDate(0, 0, 30)},
+ util.ByteRange{max, 0},
t)
return fmt.Sprintf("#%02X%02X%02X", max, max-redness, max-redness)
}
diff --git a/util/date.go b/util/date.go
new file mode 100644
index 0000000..3b5c457
--- /dev/null
+++ b/util/date.go
@@ -0,0 +1,51 @@
+package util
+
+import (
+ "html/template"
+ "time"
+)
+
+func Date2HTML(t time.Time) template.HTML {
+ return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\" title=\"2006-01-02 15:04:05\">2006-01-02 <span class=time>15:04:05</span></time>"))
+}
+
+func DateTime2HTML(t time.Time) template.HTML {
+ return template.HTML(t.Local().Format("<time datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
+}
+
+func DateTime2ColorHTML(t time.Time) template.HTML {
+ return template.HTML(t.Local().Format("<time class=daily datetime=\"2006-01-02 15:04:05\">2006-01-02 15:04:05</time>"))
+}
+
+type TimeRange struct {
+ A, B time.Time
+}
+
+func (tr TimeRange) ToPct(point time.Time) float64 {
+ dur_ab := tr.B.Sub(tr.A)
+ dur_ap := point.Sub(tr.A)
+ return float64(dur_ap) / float64(dur_ab)
+}
+
+type ByteRange struct {
+ A, B byte
+}
+
+func (br ByteRange) FromPct(pct float64) byte {
+ ab := int16(br.B) - int16(br.A)
+ ap := int16(pct * float64(ab))
+ return byte(int16(br.A) + ap)
+}
+
+func PctCap(pct float64) float64 {
+ if pct < 0 {
+ pct = 0
+ } else if pct > 1 {
+ pct = 1
+ }
+ return pct
+}
+
+func MapRange(tr TimeRange, br ByteRange, t time.Time) byte {
+ return br.FromPct(PctCap(tr.ToPct(t)))
+}