summaryrefslogtreecommitdiff
path: root/internal/server/server.go
blob: 97a25b485ecf0e16ffe290ca22861df4156feaee (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Package server implements functions that have to deal with server interaction
package server

import (
	"context"
	"errors"
	"os"
	"time"

	"github.com/eduvpn/eduvpn-common/internal/api"
	"github.com/eduvpn/eduvpn-common/internal/api/profiles"
	v2 "github.com/eduvpn/eduvpn-common/internal/config/v2"
	"github.com/eduvpn/eduvpn-common/types/protocol"
	srvtypes "github.com/eduvpn/eduvpn-common/types/server"
)

// Server is the struct for a single server
type Server struct {
	identifier string
	t          srvtypes.Type
	apiw       *api.API
	storage    *v2.V2
}

// ErrInvalidProfile is an error that is returned when an invalid profile has been chosen
var ErrInvalidProfile = errors.New("invalid profile")

// NewServer creates a new server
func (s *Servers) NewServer(identifier string, t srvtypes.Type, api *api.API) Server {
	return Server{
		identifier: identifier,
		t:          t,
		apiw:       api,
		storage:    s.config,
	}
}

// Profiles gets the cached profiles from the configuration/state file
func (s *Server) Profiles() (*srvtypes.Profiles, error) {
	cfgs, err := s.cfgServer()
	if err != nil {
		return nil, err
	}
	return &cfgs.Profiles, nil
}

// FreshProfiles gets the profiles for the server
// It only does a /info network request if the profiles have not been cached
// force indicates whether or not the profiles should be fetched fresh
func (s *Server) FreshProfiles(ctx context.Context) (*profiles.Info, error) {
	a, err := s.api()
	if err != nil {
		return nil, err
	}
	// Otherwise get fresh profiles and set the cache
	prfs, err := a.Info(ctx)
	if err != nil {
		return nil, err
	}
	// Update the profile list in the config
	err = s.SetProfileList(prfs.Public())
	if err != nil {
		return nil, err
	}
	return prfs, nil
}

func (s *Server) api() (*api.API, error) {
	if s.apiw == nil {
		return nil, errors.New("no API object found")
	}
	return s.apiw, nil
}

func (s *Server) findProfile(ctx context.Context, wgSupport bool) (*profiles.Profile, error) {
	// Get the profiles by ignoring the cache
	prfs, err := s.FreshProfiles(ctx)
	if err != nil {
		return nil, err
	}

	// No profiles available
	if prfs.Len() == 0 {
		return nil, errors.New("the server has no available profiles for your account")
	}

	// No WireGuard support, we have to filter the profiles that only have WireGuard
	if !wgSupport {
		prfs = prfs.FilterWireGuard()
	}

	var chosenP profiles.Profile

	n := prfs.Len()
	switch n {
	// If we now get no profiles then that means a profile with only WireGuard was removed
	case 0:
		return nil, errors.New("the server has only WireGuard profiles but the client does not support WireGuard")
	case 1:
		// Only one profile, make sure it is set
		chosenP = prfs.MustIndex(0)
	default:
		// Profile doesn't exist
		prID, err := s.ProfileID()
		if err != nil {
			return nil, err
		}
		v := prfs.Get(prID)
		if v == nil {
			return nil, ErrInvalidProfile
		}
		chosenP = *v
	}
	return &chosenP, nil
}

func (s *Server) connect(ctx context.Context, wgSupport bool, pTCP bool) (*srvtypes.Configuration, error) {
	a, err := s.api()
	if err != nil {
		return nil, err
	}

	// find a suitable profile to connect
	chosenP, err := s.findProfile(ctx, wgSupport)
	if err != nil {
		return nil, err
	}
	err = s.SetProfileID(chosenP.ID)
	if err != nil {
		return nil, err
	}

	protos := []protocol.Protocol{protocol.OpenVPN}
	if wgSupport {
		protos = append(protos, protocol.WireGuard)
	}
	// If the client supports WireGuard and the profile supports both protocols we remove openvpn from client support if EDUVPN_PREFER_WG is set to "1"
	// This also only happens if prefer TCP is set to false
	if wgSupport && os.Getenv("EDUVPN_PREFER_WG") == "1" {
		if chosenP.HasWireGuard() && chosenP.HasOpenVPN() {
			protos = []protocol.Protocol{protocol.WireGuard}
		}
	}
	// SAFETY: chosenP is guaranteed to be non-nil
	apicfg, err := a.Connect(ctx, *chosenP, protos, pTCP)
	if err != nil {
		return nil, err
	}
	err = s.SetExpireTime(apicfg.Expires)
	if err != nil {
		return nil, err
	}
	var proxy *srvtypes.Proxy
	if apicfg.Proxy != nil {
		proxy = &srvtypes.Proxy{
			SourcePort: apicfg.Proxy.SourcePort,
			Listen:     apicfg.Proxy.Listen,
			Peer:       apicfg.Proxy.Peer,
		}
	}
	return &srvtypes.Configuration{
		VPNConfig:        apicfg.Configuration,
		Protocol:         apicfg.Protocol,
		DefaultGateway:   chosenP.DefaultGateway,
		DNSSearchDomains: chosenP.DNSSearchDomains,
		ShouldFailover:   chosenP.ShouldFailover() && !pTCP,
		Proxy:            proxy,
	}, nil
}

// Disconnect sends an API /disconnect to the server
func (s *Server) Disconnect(ctx context.Context) error {
	a, err := s.api()
	if err != nil {
		return err
	}
	return a.Disconnect(ctx)
}

func (s *Server) cfgServer() (*v2.Server, error) {
	if s.storage == nil {
		return nil, errors.New("cannot get server, no configuration passed")
	}
	return s.storage.GetServer(s.identifier, s.t)
}

// SetProfileID sets the profile id `id` for the server
func (s *Server) SetProfileID(id string) error {
	cs, err := s.cfgServer()
	if err != nil {
		return err
	}
	cs.Profiles.Current = id
	return nil
}

// SetProfileList sets the profile list `prfs` for the server
func (s *Server) SetProfileList(prfs srvtypes.Profiles) error {
	cs, err := s.cfgServer()
	if err != nil {
		return err
	}
	cs.Profiles.Map = prfs.Map
	return nil
}

// SetExpireTime sets the time `et` when the VPN expires
func (s *Server) SetExpireTime(et time.Time) error {
	cs, err := s.cfgServer()
	if err != nil {
		return err
	}
	cs.ExpireTime = et
	return nil
}

// ProfileID gets the profile ID for the server
func (s *Server) ProfileID() (string, error) {
	cs, err := s.cfgServer()
	if err != nil {
		return "", err
	}
	return cs.Profiles.Current, nil
}

// SetLocation sets the secure internet location for the server
func (s *Server) SetLocation(loc string) error {
	if s.t != srvtypes.TypeSecureInternet {
		return errors.New("changing secure internet location is only possible when the server is a secure location")
	}
	cs, err := s.cfgServer()
	if err != nil {
		return err
	}
	cs.CountryCode = loc
	return nil
}

// SetCurrent sets the current server in the state file to this one
func (s *Server) SetCurrent() error {
	if s.storage == nil {
		return errors.New("no storage available")
	}
	s.storage.LastChosen = &v2.ServerKey{
		ID: s.identifier,
		T:  s.t,
	}
	return nil
}