2015-04-25 18:21:47 +09:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-04-25 18:21:47 +09:00
|
|
|
|
2019-07-14 12:43:13 +02:00
|
|
|
package syncthing
|
2015-04-25 18:21:47 +09:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
|
2019-07-23 10:50:37 +02:00
|
|
|
"github.com/thejerf/suture"
|
|
|
|
|
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2019-07-23 10:50:37 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2015-04-25 18:21:47 +09:00
|
|
|
)
|
|
|
|
|
|
2015-12-23 15:31:12 +00:00
|
|
|
// The auditService subscribes to events and writes these in JSON format, one
|
2015-04-25 18:21:47 +09:00
|
|
|
// event per line, to the specified writer.
|
2015-12-23 15:31:12 +00:00
|
|
|
type auditService struct {
|
2019-07-23 10:50:37 +02:00
|
|
|
suture.Service
|
|
|
|
|
w io.Writer // audit destination
|
|
|
|
|
sub *events.Subscription
|
2015-04-25 18:21:47 +09:00
|
|
|
}
|
|
|
|
|
|
2015-12-23 15:31:12 +00:00
|
|
|
func newAuditService(w io.Writer) *auditService {
|
2019-07-23 10:50:37 +02:00
|
|
|
s := &auditService{
|
|
|
|
|
w: w,
|
|
|
|
|
sub: events.Default.Subscribe(events.AllEvents),
|
2015-04-25 18:21:47 +09:00
|
|
|
}
|
2019-07-23 10:50:37 +02:00
|
|
|
s.Service = util.AsService(s.serve)
|
|
|
|
|
return s
|
2015-04-25 18:21:47 +09:00
|
|
|
}
|
|
|
|
|
|
2019-07-23 10:50:37 +02:00
|
|
|
// serve runs the audit service.
|
|
|
|
|
func (s *auditService) serve(stop chan struct{}) {
|
2015-04-25 18:21:47 +09:00
|
|
|
enc := json.NewEncoder(s.w)
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
2019-07-23 10:50:37 +02:00
|
|
|
case ev := <-s.sub.C():
|
2019-02-02 12:16:27 +01:00
|
|
|
enc.Encode(ev)
|
2019-07-23 10:50:37 +02:00
|
|
|
case <-stop:
|
2015-04-25 18:21:47 +09:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop stops the audit service.
|
2015-12-23 15:31:12 +00:00
|
|
|
func (s *auditService) Stop() {
|
2019-07-23 10:50:37 +02:00
|
|
|
s.Service.Stop()
|
|
|
|
|
events.Default.Unsubscribe(s.sub)
|
2015-04-25 18:21:47 +09:00
|
|
|
}
|