Skip to content

Commit 65ddea6

Browse files
committed
add: most code, needs polish and packaging framework
1 parent a703eaf commit 65ddea6

File tree

6 files changed

+220
-1
lines changed

6 files changed

+220
-1
lines changed

.github/workflows/build.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Set up Go
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: 1.18
18+
- name: Build
19+
run: go build -v ./...
20+
- name: Test
21+
run: go test -v ./...

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
.idea/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# arc-monitor
1+
# ArcDPS Monitor
22
A very simple daemon to notifiy a Discord Webhook whenever ArcDPS gets updated (within a set interval)

cmd/arcmon/main.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package arcmon
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"io"
8+
"net"
9+
"net/http"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
"time"
14+
15+
"github.com/sirupsen/logrus"
16+
"gopkg.in/yaml.v2"
17+
)
18+
19+
const (
20+
ArcDpsURL = "https://www.deltaconnected.com/arcdps/x64/"
21+
ArcDPSCheckSumURL = ArcDpsURL + "d3d9.dll.md5sum"
22+
ArcDPSDLLURL = ArcDpsURL + "d3d9.dll"
23+
DefaultTickDuration = 10 * time.Minute
24+
)
25+
26+
type ArcDPSVersion struct {
27+
Timestamp time.Time `yaml:"timestamp"`
28+
CheckSum string `yaml:"check_sum"`
29+
}
30+
31+
func main() {
32+
f, err := os.Open(filepath.Dir(os.TempDir() + "/arcdps.yaml"))
33+
if err != nil {
34+
if err != os.ErrNotExist {
35+
logrus.Fatalf("err opening temp file: %v\n", err)
36+
}
37+
f, err = os.CreateTemp("", "/arcdps.yml")
38+
if err != nil {
39+
logrus.Fatalf("temp file not exists and unable to create new one: %v\n", err)
40+
}
41+
}
42+
43+
arcdps := &ArcDPSVersion{}
44+
if err := yaml.NewDecoder(f).Decode(&arcdps); err != nil {
45+
logrus.Fatalf("unable to decode arcdps.yml: %v", err)
46+
}
47+
48+
s := NewServer()
49+
s.Tick()
50+
51+
}
52+
53+
type Server struct {
54+
http *http.Client
55+
webhookURL string
56+
}
57+
58+
func NewServer() *Server {
59+
return &Server{
60+
http: &http.Client{
61+
Transport: &http.Transport{
62+
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext,
63+
},
64+
Timeout: 5 * time.Second,
65+
},
66+
webhookURL: os.Getenv("DISCORD_WEBHOOK"),
67+
}
68+
}
69+
70+
func (s *Server) Tick() {
71+
72+
}
73+
74+
func (s *Server) GetChecksum(ctx context.Context) (string, error) {
75+
req, err := http.NewRequestWithContext(ctx, "GET", ArcDPSCheckSumURL, nil)
76+
if err != nil {
77+
return "", err
78+
}
79+
80+
resp, err := s.http.Do(req)
81+
if err != nil {
82+
return "", err
83+
}
84+
defer resp.Body.Close()
85+
86+
body, err := io.ReadAll(resp.Body)
87+
if err != nil {
88+
return "", err
89+
}
90+
91+
if resp.StatusCode > 299 {
92+
return "", fmt.Errorf("bad response from delta: (%s)", string(body))
93+
}
94+
95+
checkSumSplit := strings.Split(string(body), " ")
96+
if len(checkSumSplit) < 2 {
97+
return "", fmt.Errorf("incorrect size of checksum split")
98+
}
99+
return checkSumSplit[0], nil
100+
101+
}
102+
103+
func (s *Server) GetVersion(ctx context.Context) (time.Time, error) {
104+
req, err := http.NewRequestWithContext(ctx, "HEAD", ArcDPSDLLURL, nil)
105+
if err != nil {
106+
return time.Time{}, err
107+
}
108+
109+
resp, err := s.http.Do(req)
110+
if err != nil {
111+
return time.Time{}, err
112+
}
113+
defer resp.Body.Close()
114+
115+
if resp.StatusCode > 299 {
116+
body, err := io.ReadAll(resp.Body)
117+
if err != nil {
118+
return time.Time{}, err
119+
}
120+
return time.Time{}, fmt.Errorf("bad response from delta: (%s)", string(body))
121+
}
122+
lastModified, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
123+
if err != nil {
124+
return time.Time{}, fmt.Errorf("unable to parse time: (%v)", err)
125+
}
126+
127+
return lastModified, nil
128+
}
129+
130+
func (s *Server) SendWebHook(ctx context.Context, checksum, time string) error {
131+
payload := bytes.NewBufferString(fmt.Sprintf(PayloadJSON, checksum, time))
132+
req, err := http.NewRequestWithContext(ctx, "POST", s.webhookURL, payload)
133+
if err != nil {
134+
return err
135+
}
136+
137+
resp, err := s.http.Do(req)
138+
if err != nil {
139+
return err
140+
}
141+
142+
if resp.StatusCode > 299 {
143+
return fmt.Errorf("bad response from Discord: %d", resp.StatusCode)
144+
}
145+
return nil
146+
}
147+
148+
var (
149+
PayloadJSON = `
150+
{
151+
"content": null,
152+
"embeds": [
153+
{
154+
"title": "ArcDPS has updated!",
155+
"color": 12124160,
156+
"fields": [
157+
{
158+
"name": "CheckSum",
159+
"value": "%s"
160+
},
161+
{
162+
"name": "Timestamp Version",
163+
"value": "%s",
164+
"inline": true
165+
},
166+
{
167+
"name": "Direct Download Link",
168+
"value": "https://www.deltaconnected.com/arcdps/x64/d3d9.dll",
169+
"inline": true
170+
}
171+
],
172+
"footer": {
173+
"text": "ArcDPS Monitor"
174+
}
175+
}
176+
]
177+
}`
178+
)

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/mythwright/arc-monitor
2+
3+
go 1.17
4+
5+
require (
6+
github.com/sirupsen/logrus v1.8.1 // indirect
7+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
8+
gopkg.in/yaml.v2 v2.4.0 // indirect
9+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
4+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
5+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
6+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
7+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
10+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 commit comments

Comments
 (0)