@@ -5,6 +5,10 @@ import (
5
5
"encoding/json"
6
6
"io"
7
7
"net/http"
8
+ "os"
9
+ "strings"
10
+
11
+ "github.com/google/uuid"
8
12
)
9
13
10
14
type VersionChecker struct {
@@ -18,26 +22,36 @@ func NewVersionChecker(endpoint string) *VersionChecker {
18
22
return & v
19
23
}
20
24
21
- func (v * VersionChecker ) CheckIn () (* Response , error ) {
25
+ func (v * VersionChecker ) CheckIn () (* VerCheckResponse , error ) {
26
+
27
+ id , err := v .WriteClientUUID ()
28
+ if err != nil {
29
+ return nil , err
30
+ }
22
31
23
32
body := make (map [string ]string , 2 )
24
33
body ["entry_type" ] = "user"
25
- body ["version" ] = VersionInfo (true )
34
+ body ["version" ] = Version
35
+ body ["client_id" ] = id
36
+
26
37
req , err := json .Marshal (body )
27
38
if err != nil {
28
39
return nil , err
29
40
}
30
- _ , err = http .Post (v .URL , "application/json" , bytes .NewBuffer (req ))
41
+
42
+ wReq := bytes .NewReader (req )
43
+
31
44
if err != nil {
32
45
return nil , err
33
46
}
34
47
35
- var json_resp Response
36
- r , err := http .Get (v .URL )
48
+ var json_resp VerCheckResponse
49
+ r , err := http .Post (v .URL , "application/json" , wReq )
37
50
if err != nil {
38
51
return nil , err
39
52
}
40
53
data , err := io .ReadAll (r .Body )
54
+
41
55
if err != nil {
42
56
return nil , err
43
57
}
@@ -51,10 +65,52 @@ func (v *VersionChecker) CheckIn() (*Response, error) {
51
65
52
66
}
53
67
54
- type Response struct {
55
- Information InfoSubResponse `json:"info"`
68
+ func (v * VersionChecker ) GenerateClientUUID () (string , error ) {
69
+ clientUUID := uuid .New ()
70
+ cleanedUUID := strings .Replace (clientUUID .String (), "-" , "" , - 1 )
71
+ return cleanedUUID , nil
72
+ }
73
+
74
+ func (v * VersionChecker ) WriteClientUUID () (string , error ) {
75
+ filename := ".client_id.txt"
76
+ _ , err := os .Stat (filename )
77
+ if err != nil {
78
+ if os .IsNotExist (err ) {
79
+ fh , err := os .Create (filename )
80
+ if err != nil {
81
+ return "" , err
82
+ }
83
+ defer fh .Close ()
84
+ id , err := v .GenerateClientUUID ()
85
+ if err != nil {
86
+ return "" , err
87
+ }
88
+
89
+ _ , err = fh .WriteString (id )
90
+ if err != nil {
91
+ return "" , err
92
+ }
93
+
94
+ return "" , err
95
+ } else {
96
+ return "" , err
97
+ }
98
+ }
99
+
100
+ fh , err := os .Open (filename )
101
+ if err != nil {
102
+ return "" , err
103
+ }
104
+ defer fh .Close ()
105
+ buf , err := os .ReadFile (filename )
106
+
107
+ if err != nil {
108
+ return "" , err
109
+ }
110
+ return string (buf ), nil
111
+
56
112
}
57
113
58
- type InfoSubResponse struct {
59
- Version string `json:"version "`
114
+ type VerCheckResponse struct {
115
+ Version string `json:"latest_version "`
60
116
}
0 commit comments