-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
82 lines (71 loc) · 1.62 KB
/
client.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
package agent
import (
"github.com/26597925/EastCloud/internal/agent/core"
"github.com/26597925/EastCloud/internal/agent/msg"
"github.com/26597925/EastCloud/internal/agent/router/tcpclient"
"github.com/26597925/EastCloud/pkg/network/tcp"
"github.com/26597925/EastCloud/pkg/process"
"time"
)
type Client struct {
client *tcp.Client
manager *core.Manager
process *core.Process
quit chan bool
}
func NewClient() *Client {
cli := &Client{
client: tcp.NewClient(),
manager: core.NewManager("E://GoWork//src//cnas//"),
process: core.NewProcess("E:\\GoWork\\src\\douyu\\bsw\\cmd\\agent\\work"),
quit: make(chan bool, 1),
}
return cli
}
func (c *Client) Start() {
info := &core.Info{
ProcInfo: process.ProcInfo{
Pid: 0,
//Cmdline: "./ttorrent test.torrent",
Cmdline: "C:\\Windows\\System32\\notepad.exe\nD:\\1.txt",
Env: "",
},
Name: "notepad.exe",
Status: 0,
SavePath: "",
}
c.process.AddProcess(info)
c.client.AddRouter(msg.Heartbeat, &tcpclient.HeartbeatRouter{})
c.client.AddRouter(msg.Command, &tcpclient.CommandRouter{
Process: c.process,
})
c.client.AddRouter(msg.File, tcpclient.NewFileRouter(c.manager))
go c.client.Connect()
th := time.NewTicker(5 * time.Second)
go func(t *time.Ticker) {
for {
select {
case <-t.C:
err := c.client.SendBuffMsg(msg.Heartbeat, nil)
if err != nil {
c.client.Close()
}
case <-c.quit:
c.client.Close()
return
}
}
}(th)
tm := time.NewTicker(500 * time.Millisecond)
go func(t *time.Ticker) {
for {
select {
case <-t.C:
c.process.Monitor()
}
}
}(tm)
}
func (c *Client) Stop() {
close(c.quit)
}