-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
59 lines (49 loc) · 1.13 KB
/
Copy pathclient.go
File metadata and controls
59 lines (49 loc) · 1.13 KB
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
package main
import (
"context"
"flag_sender/internal/models"
"flag_sender/pkg/plugins"
"fmt"
"net"
"strings"
)
const bufSize = 64
type Client struct {
url string
token string
}
type FlagInfo struct {
Flag string `json:"flag"`
Msg string `json:"msg"`
}
var NewClient plugins.NewClientFunc = func(url, token string) plugins.IClient {
return &Client{
url: url,
token: token,
}
}
func (c *Client) SendFlags(ctx context.Context, flags []string) (map[string]*plugins.FlagResult, error) {
conn, err := net.Dial("tcp", c.url)
if err != nil {
return nil, fmt.Errorf("error connecting: %w", err)
}
defer conn.Close()
flagMap := make(map[string]*plugins.FlagResult)
for _, flag := range flags {
conn.Write([]byte(flag + "\n"))
buf := make([]byte, bufSize)
n, _ := conn.Read(buf)
buf = buf[:n]
status := models.FlagStatusReject
if strings.Contains(string(buf), "Expired") {
status = models.FlagStatusOld
} else if strings.Contains(string(buf), "[OK]") {
status = models.FlagStatusSuccess
}
flagMap[flag] = &plugins.FlagResult{
Msg: string(buf),
Status: status,
}
}
return flagMap, nil
}