-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
99 lines (87 loc) · 2.27 KB
/
client.go
File metadata and controls
99 lines (87 loc) · 2.27 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
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
package main
import (
"context"
"crypto/tls"
"flag"
"net"
"time"
"github.com/gochik/chik"
"github.com/gochik/chik/config"
"github.com/gochik/chik/handlers/actor"
"github.com/gochik/chik/handlers/datetime"
"github.com/gochik/chik/handlers/heartbeat"
"github.com/gochik/chik/handlers/heating"
"github.com/gochik/chik/handlers/io"
"github.com/gochik/chik/handlers/snapcast"
"github.com/gochik/chik/handlers/status"
"github.com/gochik/chik/handlers/systemd"
"github.com/gochik/chik/handlers/telegram"
"github.com/gochik/chik/handlers/version"
"github.com/rs/zerolog/log"
)
// Current software version
var Version = "dev"
var localSearchPath string
var conf *tls.Config
func init() {
flag.StringVar(&localSearchPath, "config", "/etc/chik", "Config file path")
}
func main() {
flag.Parse()
config.SetConfigFileName("client.conf")
config.AddSearchPath(localSearchPath)
config.AddSearchPath("/etc/chik")
err := config.ParseConfig()
if err != nil {
log.Warn().Msgf("Failed parsing config file: %v", err)
}
var server string
config.GetStruct("connection.server", &server)
if server == "" {
log.Fatal().Msg("Cannot get server from config")
}
var token string
config.GetStruct("connection.token", &token)
if token == "" {
log.Fatal().Msg("Cannot get token from config")
}
log.Info().Msgf("Server: %v", server)
controller := chik.NewController()
ctx := context.Background()
// Creating handlers
go controller.Start(ctx, []chik.Handler{
status.New(),
io.New(),
heartbeat.New(),
version.New(Version),
datetime.New(),
actor.New(),
heating.New(),
telegram.New(),
systemd.New(),
snapcast.New(),
})
// Listening network
for {
conn, err := connect(ctx, token, server)
if err == nil {
log.Debug().Msg("New connection")
ctx, cancel := chik.StartRemote(controller, conn, chik.MaxIdleTime)
<-ctx.Done()
cancel()
} else {
log.Err(err).Msg("Client connection failed, retrying in 10s")
}
time.Sleep(10 * time.Second)
}
}
func connect(ctx context.Context, token string, server string) (conn *tls.Conn, err error) {
if conf == nil {
conf, err = config.TLSConfig(ctx, token)
if err != nil {
log.Err(err).Msgf("Cannot get TLS config")
return
}
}
return tls.DialWithDialer(&net.Dialer{Timeout: 1 * time.Minute}, "tcp", server, conf)
}