-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (84 loc) · 2.33 KB
/
main.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
package main
import (
"golang.org/x/sys/windows/svc"
//"golang.org/x/sys/windows/svc/debug"
"gopkg.in/natefinch/lumberjack.v2"
"log"
"os"
"path/filepath"
"time"
)
type ServContext struct {
dnsSvcStop SvrStopFunc
}
// svc.Handler 인터페이스 구현
func (srv *ServContext) Execute(args []string, req <-chan svc.ChangeRequest, stat chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) {
stat <- svc.Status{State: svc.StartPending}
// 실제 서비스 내용
stopChan := make(chan bool, 1)
srv.runBody()
stat <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown}
LOOP:
for {
// 서비스 변경 요청에 대해 핸들링
switch r := <-req; r.Cmd {
case svc.Stop, svc.Shutdown:
stopChan <- true
break LOOP
case svc.Interrogate:
stat <- r.CurrentStatus
time.Sleep(100 * time.Millisecond)
stat <- r.CurrentStatus
//case svc.Pause:
//case svc.Continue:
}
}
stat <- svc.Status{State: svc.StopPending}
// Stop DNS server
log.Println("Shutting down...")
de := srv.dnsSvcStop()
if de != nil {
WriteErrorLogMsg("DNS service shutdown error: ", de)
} else {
log.Println("DNS service stopped.")
}
log.Println("SecDNS was stopped.")
return
}
func (srv *ServContext) runBody() {
// DNS 서버를 go routine으로 시작하고
// 서버 종료를 위한 함수를 얻어 저장한다.
stopFunc, err := RunDNS(53, func(err error) {
WriteErrorLogMsg("DNS service error: ", err)
})
if err != nil {
WriteErrorLogMsgF("Can't start DNS service. ", err)
} else {
srv.dnsSvcStop = stopFunc
log.Println("SecDNS service started.")
}
}
func logPath(filename string) string {
ex, err := os.Executable()
if err != nil {
ex = "." // current working directory
}
exPath := filepath.Dir(ex)
return filepath.Join(exPath, filename)
}
func main() {
log.SetOutput(&lumberjack.Logger{
Filename: logPath("sec-dns.log"),
MaxSize: 10, // megabytes
MaxBackups: 1,
MaxAge: 28, //days
Compress: false, // disabled by default
})
log.Println("Initializing...")
err := svc.Run("SecDNS", &ServContext{})
//err = debug.Run("DummyService", &dummyService{}) //콘솔출력 디버깅시
if err != nil {
WriteErrorLogMsgF("Fatal service error: ", err)
panic(err)
}
}