-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateChecker.go
110 lines (87 loc) · 2.38 KB
/
updateChecker.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
package main
import (
"bytes"
"crypto/sha1"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
_ "github.com/joho/godotenv/autoload"
"github.com/microcosm-cc/bluemonday"
)
const hashesDir = "/hashes/"
func main() {
fetchURL, parseURLErr := url.Parse(os.Getenv("URL"))
checkErr(parseURLErr)
_, fileName := path.Split(fetchURL.Path)
if len(fileName) < 1 {
fileName = strings.ReplaceAll(fetchURL.Path, "/", "-")
}
ex, exErr := os.Executable()
checkErr(exErr)
workingDir := filepath.Dir(ex)
hashesPath := workingDir + hashesDir
if len(os.Args) > 1 {
optionalFolderArg := os.Args[1]
hashesPath = optionalFolderArg
if !strings.HasSuffix(optionalFolderArg, "/") {
hashesPath = hashesPath + "/"
}
}
fileContent, readFileErr := ioutil.ReadFile(hashesPath + fileName)
if os.IsNotExist(readFileErr) {
fmt.Println("File does not exist yet")
if _, readHashesDirErr := os.Stat(hashesPath); os.IsNotExist(readHashesDirErr) {
fmt.Println("Creating hashes directory")
os.Mkdir(hashesPath, os.ModePerm)
}
}
resp, httpErr := http.Get(fetchURL.String())
checkErr(httpErr)
defer resp.Body.Close()
body, readBodyErr := ioutil.ReadAll(resp.Body)
checkErr(readBodyErr)
p := bluemonday.UGCPolicy()
sanitizedBody := p.Sanitize(string(body))
bodyHash := sha1.New()
bodyHash.Write([]byte(sanitizedBody))
bodyHashBytes := bodyHash.Sum(nil)
fmt.Printf("Previous: %x\n", fileContent)
fmt.Printf("Incoming: %x\n", bodyHashBytes)
if bytes.Equal(fileContent, bodyHashBytes) {
fmt.Println("Hashes match!")
return
}
fmt.Printf("Writing file to %s\n", hashesPath+fileName)
writeErr := ioutil.WriteFile(hashesPath+fileName, bodyHashBytes, 0644)
checkErr(writeErr)
chatID, parseErr := strconv.ParseInt(os.Getenv("TG_CHAT_ID"), 10, 64)
checkErr(parseErr)
if readFileErr == nil {
fmt.Println("Sending notification")
sendNotification(chatID, fetchURL.String())
}
}
func sendNotification(chatID int64, updatedURL string) {
bot, botErr := tgbotapi.NewBotAPI(os.Getenv("TG_API_TOKEN"))
if botErr != nil {
panic(botErr)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
messageString := fmt.Sprintf(`An update is available!
See: %s`, updatedURL)
msg := tgbotapi.NewMessage(chatID, messageString)
bot.Send(msg)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}