forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (72 loc) · 1.56 KB
/
main.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
83
84
85
86
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"github.com/rancher/agent/cluster"
"github.com/rancher/agent/node"
"github.com/rancher/rancher/pkg/remotedialer"
"github.com/sirupsen/logrus"
)
const (
Token = "X-API-Tunnel-Token"
Params = "X-API-Tunnel-Params"
)
func main() {
if os.Getenv("CATTLE_DEBUG") == "true" || os.Getenv("RANCHER_DEBUG") == "true" {
logrus.SetLevel(logrus.DebugLevel)
}
if err := run(); err != nil {
log.Fatal(err)
}
}
func getParams() (map[string]interface{}, error) {
if os.Getenv("CATTLE_CLUSTER") == "true" {
return cluster.Params()
}
return node.Params(), nil
}
func getTokenAndURL() (string, string, error) {
if os.Getenv("CATTLE_CLUSTER") == "true" {
return cluster.TokenAndURL()
}
return node.TokenAndURL()
}
func run() error {
params, err := getParams()
if err != nil {
return err
}
bytes, err := json.Marshal(params)
if err != nil {
return err
}
token, server, err := getTokenAndURL()
if err != nil {
return err
}
headers := map[string][]string{
Token: {token},
Params: {base64.StdEncoding.EncodeToString(bytes)},
}
serverURL, err := url.Parse(server)
if err != nil {
return err
}
wsURL := fmt.Sprintf("wss://%s/v3/connect", serverURL.Host)
logrus.Infof("Connecting to %s with token %s", wsURL, token)
remotedialer.ClientConnect(wsURL, http.Header(headers), nil, func(proto, address string) bool {
switch proto {
case "tcp":
return true
case "unix":
return address == "/var/run/docker.sock"
}
return false
})
return nil
}