This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
summary.go
110 lines (104 loc) · 2.87 KB
/
summary.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* ZGrab Copyright 2015 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package main
import (
"encoding/json"
"time"
)
type Summary struct {
Port uint16
Success uint
Failure uint
Total uint
StartTime time.Time
EndTime time.Time
Duration time.Duration
Senders uint
Timeout time.Duration
TLSVersion string
MailType string
CAFile string
SNISupport bool
Flags []string
}
type encodedSummary struct {
Port uint16 `json:"port"`
Success uint `json:"success_count"`
Failure uint `json:"failure_count"`
Total uint `json:"total"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Duration time.Duration `json:"duration"`
Senders uint `json:"senders"`
Timeout uint `json:"timeout"`
TLSVersion *string `json:"tls_version"`
MailType *string `json:"mail_type"`
CAFile *string `json:"ca_file_name"`
SNISupport bool `json:"sni_support"`
Flags []string `json:"flags"`
}
func (s *Summary) MarshalJSON() ([]byte, error) {
e := new(encodedSummary)
e.Port = s.Port
e.Success = s.Success
e.Failure = s.Failure
e.Total = s.Total
e.StartTime = s.StartTime.Format(time.RFC3339)
e.EndTime = s.EndTime.Format(time.RFC3339)
e.Duration = s.EndTime.Sub(s.StartTime) / time.Second
e.Senders = s.Senders
e.Timeout = uint(s.Timeout / time.Second)
e.SNISupport = s.SNISupport
e.Flags = s.Flags
if s.TLSVersion != "" {
e.TLSVersion = &s.TLSVersion
}
if s.MailType != "" {
e.MailType = &s.MailType
}
if s.CAFile != "" {
e.CAFile = &s.CAFile
}
return json.Marshal(e)
}
func (s *Summary) UnmarshalJSON(b []byte) error {
e := new(encodedSummary)
if err := json.Unmarshal(b, e); err != nil {
return err
}
s.Port = e.Port
s.Success = e.Success
s.Failure = e.Failure
s.Total = e.Total
var err error
if s.StartTime, err = time.Parse(time.RFC3339, e.StartTime); err != nil {
return err
}
if s.EndTime, err = time.Parse(time.RFC3339, e.EndTime); err != nil {
return err
}
s.Duration = s.EndTime.Sub(s.StartTime)
s.Senders = e.Senders
s.Timeout = time.Duration(e.Timeout) * time.Second
if e.TLSVersion != nil {
s.TLSVersion = *e.TLSVersion
}
if e.MailType != nil {
s.MailType = *e.MailType
}
if e.CAFile != nil {
s.CAFile = *e.CAFile
}
return nil
}