-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (84 loc) · 2.45 KB
/
main.go
File metadata and controls
106 lines (84 loc) · 2.45 KB
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
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.
// simple does nothing except block while running the service.
package main
import (
"time"
"os"
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/kardianos/service"
"github.com/stop-error/scamjam-service/rpc"
"github.com/stop-error/scamjam-service/logging"
)
var logger zerolog.Logger
type program struct{
exit chan struct{}
}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
logger.Info().Msg("Starting grpc servers...")
go rpc.InitDnsAlertsRpc(&logger)
logger.Info().Msg("Starting main loop...")
go p.run()
return nil
}
func (p *program) run() {
ticker := time.NewTicker(20 * time.Second)
for {
logger.Info().Msg("Going to sleep for 20 second")
select {
case tm := <-ticker.C:
logger.Info().Msg("Recieved tick on ticker channel at " + tm.String())
// case <-p.exit:
// logger.Info().Msg("scamjam-dns-watchdog has recieved exit signal!")
// ticker.Stop()
}
}
// Do work here
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
// close(p.exit)
certsPath := os.Getenv("ProgramData") + "\\ScamJam\\grpc\\dns\\tls"
certsArray :=[]string{"scamjam-ca.pem"}
for i := 0; i < len(certsArray); i++ {
fullCertsPath := certsPath + certsArray[i]
if _, err := os.Stat(fullCertsPath); err == nil {
err := os.Remove(fullCertsPath)
if err != nil {
log.Error().Msg("Error cleaning up " + fullCertsPath + err.Error())
}
}
}
return nil
}
func main() {
useLogFile, logPath := logging.InitLogger()
if useLogFile == true {
logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664)
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening log file! logging will be console only.")
logger = zerolog.New(os.Stderr).With().Caller().Logger()
} else {
defer logFile.Close()
logger = zerolog.New(zerolog.MultiLevelWriter(os.Stdout, logFile)).With().Caller().Logger()
}
}
svcConfig := &service.Config{
Name: "scamjam-service",
DisplayName: "ScamJam Service",
Description: "Backround service for ScamJam protection",
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Error().Msg(err.Error())
}
err = s.Run()
if err != nil {
log.Error().Msg(err.Error())
}
}