summaryrefslogtreecommitdiff
path: root/crtsh-pem2html.go
diff options
context:
space:
mode:
Diffstat (limited to 'crtsh-pem2html.go')
-rw-r--r--crtsh-pem2html.go75
1 files changed, 17 insertions, 58 deletions
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)
}