generated from brevdev/seed
/
task.go
144 lines (126 loc) · 3.42 KB
/
task.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
package vpn
import (
"fmt"
"strings"
"time"
"github.com/brevdev/brev-cli/pkg/auth"
"github.com/brevdev/brev-cli/pkg/autostartconf"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/tasks"
)
type ServiceMeshStore interface {
autostartconf.AutoStartStore
VPNStore
GetNetworkAuthKey() (*store.GetAuthKeyResponse, error)
GetCurrentWorkspaceID() (string, error)
GetWorkspace(workspaceID string) (*entity.Workspace, error)
GetNetwork(networkID string) (*store.GetNetworkResponse, error)
GetActiveOrganizationOrNil() (*entity.Organization, error)
}
type VPNDaemon struct {
Store ServiceMeshStore
}
var _ tasks.Task = VPNDaemon{}
func (vpnd VPNDaemon) GetTaskSpec() tasks.TaskSpec {
return tasks.TaskSpec{RunCronImmediately: true, Cron: ""}
}
func (vpnd VPNDaemon) Run() error {
ts := NewTailscale(vpnd.Store)
workspaceID, err := vpnd.Store.GetCurrentWorkspaceID()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if workspaceID != "" {
ts.WithUserspaceNetworking(true).
WithSockProxyPort(1055)
}
err = ts.Start()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (vpnd VPNDaemon) Configure() error {
daemonConfigurer := autostartconf.NewVPNConfig(vpnd.Store)
err := daemonConfigurer.Install()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = ConfigureVPN(vpnd.Store)
if err != nil {
// if can't connect to tailscale try again, and if error is not auth return it
if strings.Contains(err.Error(), "failed to connect to local Tailscale service") {
fmt.Println("failed to connect to vpn trying again")
time.Sleep(2 * time.Second)
err = ConfigureVPN(vpnd.Store)
if err != nil {
return breverrors.WrapAndTrace(err)
}
} else if !auth.IsAuthError(err) {
return breverrors.WrapAndTrace(err)
}
}
return nil
}
func ConfigureVPN(store ServiceMeshStore) error {
ts := NewTailscale(store)
nodeIdentifier := "me"
workspaceID, err := store.GetCurrentWorkspaceID()
if err != nil {
return breverrors.WrapAndTrace(err)
}
networkdID := ""
if workspaceID != "" {
var workspace *entity.Workspace
workspace, err = store.GetWorkspace(workspaceID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
nodeIdentifier = workspace.GetNodeIdentifierForVPN()
networkdID = workspace.NetworkID
}
authKeyResp, err := store.GetNetworkAuthKey()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if networkdID == "" {
var org *entity.Organization
org, err = store.GetActiveOrganizationOrNil()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if org == nil {
return fmt.Errorf("no org exists")
}
networkdID = org.UserNetworkID
}
network, err := store.GetNetwork(networkdID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
tsc := ts.WithConfigurerOptions(nodeIdentifier, authKeyResp.CoordServerURL).WithForceReauth(true).WithSearchDomains(network.DNSSearchDomains)
tsca := tsc.WithAuthKey(authKeyResp.AuthKey)
if !autostartconf.ShouldSymlink() {
err = tsca.ApplyConfig()
if err != nil {
return breverrors.WrapAndTrace(err)
}
}
return nil
}
func ResetVPN(store ServiceMeshStore) error {
ts := NewTailscale(store)
err := ts.Reset()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func NewVPNDaemon(store ServiceMeshStore) VPNDaemon {
vpnd := VPNDaemon{
Store: store,
}
return vpnd
}