-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
67 lines (55 loc) · 1.3 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package pkiapi
import (
"encoding/json"
"fmt"
"net"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/opencoff/go-pki"
)
func jsonError(c *gin.Context, status int, fmtm string, args ...interface{}) {
c.AbortWithStatusJSON(status, map[string]string{"error": fmt.Sprintf(fmtm, args...)})
}
type IPList []net.IP
func NewIPList() *IPList {
return &IPList{}
}
func ParseIPList(s string) *IPList {
l := &IPList{}
l.Set(s)
return l
}
func (ipl *IPList) Set(s string) error {
v := strings.Split(s, ",")
ips := make([]net.IP, 0, 4)
for _, x := range v {
ip := net.ParseIP(x)
if ip == nil {
return fmt.Errorf("can't parse IP Address '%s'", s)
}
ips = append(ips, ip)
}
*ipl = append(*ipl, ips...)
return nil
}
func (ipl *IPList) String() string {
var x []string
ips := []net.IP(*ipl)
for i := range ips {
x = append(x, ips[i].String())
}
return fmt.Sprintf("[%s]", strings.Join(x, ","))
}
type jsonableCert struct {
*pki.Cert
}
func (cert jsonableCert) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"common_name": cert.Subject.CommonName,
"fingerprint": fmt.Sprintf("%#x", cert.SerialNumber),
"expired": time.Now().After(cert.NotAfter),
"expires_at": cert.NotAfter.Unix(),
"expires_at_human": cert.NotAfter.String(),
})
}