Skip to content

Commit 2c48fad

Browse files
authored
feat: add writer output with custom formatter support (#1)
* feat: add writer output with custom formatter support Add `Output` and `Formatter` fields to `Options` for logging blacklist detections. When `Output` is set, detection events are written using either a user-provided `Formatter` function or a default format. The `Formatter` callback receives the full `sslbl.Record` and fingerprint, allowing users to customize output format for their logging needs. Signed-off-by: Dwi Siswanto <git@dw1.io> * docs: checks TODO Signed-off-by: Dwi Siswanto <git@dw1.io> --------- Signed-off-by: Dwi Siswanto <git@dw1.io>
1 parent 6ac4abc commit 2c48fad

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ These examples demonstrate various ways to set up Sebel and integrate it with HT
7272
## TODO
7373

7474
* [ ] Caching SSLBL data under user-specific cache directory.
75-
* [ ] Add `io.Writer` option.
75+
* [x] Add `io.Writer` option.
7676
* [ ] ~Add `CheckIP` method.~ Not planned, instead:
7777
* [ ] Add `CheckHost` method.
7878

options.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package sebel
22

3+
import (
4+
"io"
5+
6+
"github.com/teler-sh/sebel/pkg/sslbl"
7+
)
8+
39
// Options holds configuration settings for the [Sebel] package.
410
type Options struct {
511
// DisableSSLBlacklist, when set to true, disables SSL/TLS certificate
612
// blacklist checks.
713
DisableSSLBlacklist bool
814

15+
// Output specifies the writer for logging blacklist detections.
16+
// If nil, no output is written.
17+
Output io.Writer
18+
19+
// Formatter customizes the output format for blacklist detections.
20+
// If nil, a default format is used.
21+
Formatter func(record *sslbl.Record, fingerprint string) string
22+
923
// TODO(dwisiswant0): Add these fields
1024
// DisableHostBlacklist bool
11-
// Output io.Writer
1225
}

sebel.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,26 @@ func (s *Sebel) checkTLS() (*sslbl.Record, error) {
8888
record, ok = sslbl.Find(sha1sum, data)
8989
if ok {
9090
reason := record.Listing.Reason
91+
s.write(record, sha1sum)
9192

9293
return record, fmt.Errorf("%w: %s detected", ErrSSLBlacklist, reason)
9394
}
9495

9596
return record, nil
9697
}
98+
99+
// write writes the blacklist detection to the configured output writer.
100+
func (s *Sebel) write(record *sslbl.Record, fingerprint string) {
101+
if s.options.Output == nil {
102+
return
103+
}
104+
105+
var msg string
106+
if s.options.Formatter != nil {
107+
msg = s.options.Formatter(record, fingerprint)
108+
} else {
109+
msg = fmt.Sprintf("%s: fingerprint=%q reason=%q\n", ErrSSLBlacklist, fingerprint, record.Listing.Reason)
110+
}
111+
112+
_, _ = fmt.Fprint(s.options.Output, msg)
113+
}

0 commit comments

Comments
 (0)