/
main.go
153 lines (122 loc) · 2.86 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"fmt"
"github.com/bluele/slack"
"github.com/goji/httpauth"
"github.com/zenazn/goji"
"net"
"github.com/BurntSushi/toml"
"github.com/ccding/go-stun/stun"
_ "github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"os"
"os/exec"
"path"
"time"
)
type tomlConfig struct {
Slack slackConfig
Server serverConfig
Web webConfig
}
type slackConfig struct {
Channel string
Token string
}
type serverConfig struct {
TargetDomain string
NameServer string
}
type webConfig struct {
User string
Pass string
Bind string
}
type serverStatus struct {
SameCount int64
CheckCount int64
}
var status serverStatus
var config tomlConfig
func main() {
// move binary directory
dir := path.Dir(os.Args[0])
os.Chdir(dir)
// load config
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
panic(err)
}
flag.Set("bind", config.Web.Bind)
go work()
goji.Use(httpauth.SimpleBasicAuth(config.Web.User, config.Web.Pass))
goji.Get("/v1/stat", StatAPIContoller)
goji.Serve()
}
func work() {
targetDomain := config.Server.TargetDomain
nameServer := config.Server.NameServer
// counter
status.SameCount = 0
status.CheckCount = 0
// connection slack-api
api := slack.New(config.Slack.Token)
channel, err := api.FindChannelByName(config.Slack.Channel)
if err != nil {
panic(err)
}
for {
status.CheckCount = status.CheckCount + 1
// notify running?
if status.CheckCount%60 == 0 {
err = api.ChatPostMessage(channel.Id, "@takoyaki I'm running. ", nil)
if err != nil {
fmt.Println("slack api error", err)
}
}
time.Sleep(60 * time.Second)
// nonreq-resovle using dig(/hack/resolve.sh), get registry ipv4 address
ripaddr, err := resolve(targetDomain, nameServer)
if err != nil {
fmt.Println("resolve error:", err)
continue
}
if ripaddr == nil {
fmt.Println("resolve error:", "resolve ip == nil")
continue
}
fmt.Println("resovle ipaddr:", ripaddr.String())
// get global-ipv4-address using STUN
_, host, err := stun.NewClient().Discover()
if err != nil {
fmt.Println("stun client error", err)
continue
}
if host == nil {
fmt.Println("stun client error", "host == nil")
continue
}
gipaddr := net.ParseIP(host.IP())
fmt.Println("stun client ipaddr:", gipaddr.String())
// compare
if gipaddr.String() == ripaddr.String() {
fmt.Println("same")
status.SameCount = status.SameCount + 1
continue
}
// notify update
err = api.ChatPostMessage(channel.Id, "@takoyaki global-ipv4-address != resolve-ipv4-address. Now, I try to update. ", nil)
if err != nil {
fmt.Println("slack api error", err)
}
fmt.Println("not equal ipaddr")
// update
_, err = exec.Command("/bin/bash", "hack/update.sh", gipaddr.String()).Output()
if err != nil {
fmt.Println("update error", err)
continue
}
// wait
time.Sleep(5 * 10 * time.Second)
}
}