-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tunnel.go
178 lines (141 loc) · 3.3 KB
/
tunnel.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/afero"
)
var (
registry = make(map[string]*Tunnel)
)
// Tunnel holds the corresponding config, certificate, and service for a tunnel
type Tunnel struct {
Config *TunnelConfig
Certificate *Certificate
Service *Service
}
// TunnelConfig holds the necessary configuration for a tunnel
type TunnelConfig struct {
IP string
Hostname string
Port string
}
// NewTunnel returns a Tunnel with its corresponding config and certificate
func NewTunnel(config *TunnelConfig, certificate *Certificate) *Tunnel {
service := NewService(config.Hostname)
tunnel := &Tunnel{
Config: config,
Certificate: certificate,
Service: service,
}
return tunnel
}
// GetTunnelForHost returns the tunnel for a given hostname.
// An error is returned if a tunnel is not found.
func GetTunnelForHost(hostname string) (*Tunnel, error) {
tunnel, ok := registry[hostname]
if !ok {
return nil, fmt.Errorf("No tunnel exists for %s", hostname)
}
return tunnel, nil
}
// Start starts a tunnel
func (t *Tunnel) Start() error {
err := t.prepareService()
if err != nil {
return err
}
err = t.startService()
if err != nil {
return err
}
registry[t.Config.Hostname] = t
return nil
}
// Stop stops a tunnel
func (t *Tunnel) Stop() error {
log.Infof("Stopping tunnel %s", t.Config.Hostname)
err := t.Service.Stop()
if err != nil {
return err
}
return nil
}
// prepareService creates the service and necessary files for the tunnel service
func (t *Tunnel) prepareService() error {
err := t.Service.Create()
if err != nil {
return err
}
err = t.writeConfigFile()
if err != nil {
return err
}
err = t.writeRunFile()
if err != nil {
return err
}
return nil
}
// startService starts the tunnel service
func (t *Tunnel) startService() error {
supervised, err := t.Service.IsSupervised()
if err != nil {
return err
}
if !supervised {
log.Infof("Registering tunnel %s", t.Config.Hostname)
err := t.Service.Supervise()
if err != nil {
return err
}
return nil
}
running, err := t.Service.IsRunning()
if err != nil {
return err
}
if running {
log.Infof("Restarting tunnel %s", t.Config.Hostname)
err := t.Service.Restart()
if err != nil {
return err
}
} else {
log.Infof("Starting tunnel %s", t.Config.Hostname)
err := t.Service.Start()
if err != nil {
return err
}
}
return nil
}
// writeConfigFile creates the config file for a tunnel
func (t *Tunnel) writeConfigFile() error {
configLines := []string{
"hostname: %s",
"url: %s:%s",
"logfile: %s",
"origincert: %s",
"no-autoupdate: true",
}
contents := fmt.Sprintf(strings.Join(configLines[:], "\n"), t.Config.Hostname, t.Config.IP, t.Config.Port, t.Service.LogFilePath(), t.Certificate.FullPath())
err := afero.WriteFile(fs, t.Service.ConfigFilePath(), []byte(contents), 0644)
if err != nil {
return err
}
return nil
}
// writeRunFile creates the run file for a tunnel
func (t *Tunnel) writeRunFile() error {
runLines := []string{
"#!/bin/sh",
"exec cloudflared --config %s --name %s",
}
contents := fmt.Sprintf(strings.Join(runLines[:], "\n"), t.Service.ConfigFilePath(), t.Config.Hostname)
err := afero.WriteFile(fs, t.Service.RunFilePath(), []byte(contents), os.ModePerm)
if err != nil {
return err
}
return nil
}