This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
455c472
commit f37642d
Showing
7 changed files
with
230 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/eduvpn/eduvpn-common/internal/api" | ||
"github.com/eduvpn/eduvpn-common/internal/api/profiles" | ||
v2cfg "github.com/eduvpn/eduvpn-common/internal/config/v2" | ||
"github.com/eduvpn/eduvpn-common/internal/http" | ||
"github.com/eduvpn/eduvpn-common/types/protocol" | ||
) | ||
|
||
type Base struct { | ||
apiw *api.API | ||
CachedInfo *profiles.Info | ||
Profile *profiles.Profile | ||
} | ||
|
||
var InvalidProfileErr = errors.New("invalid profile") | ||
|
||
// Profiles 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 (b *Base) Profiles(ctx context.Context, force bool) (*profiles.Info, error) { | ||
// If we have a cached copy we only return that if force is false | ||
if s.CachedInfo != nil && !force { | ||
return s.CachedInfo, nil | ||
} | ||
// Otherwise get fresh profiles and set the cache | ||
prfs, err := s.apiw.Info(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.CachedInfo = prfs | ||
return prfs, nil | ||
} | ||
|
||
|
||
func (b *Base) Profile() (*profiles.Profile, error) { | ||
if s.Profile == nil { | ||
return nil, InvalidProfileErr | ||
} | ||
return s.Profile, nil | ||
} | ||
|
||
func (b *Base) API() (*api.API, error) { | ||
if b.apiw == nil { | ||
return nil, errors.New("no API object found") | ||
} | ||
return b.apiw, nil | ||
} | ||
|
||
func (b *Base) findProfile(ctx context.Context, prID string, wgSupport bool) (*profile.Profile, error) { | ||
// Get the profiles by ignoring the cache | ||
prfs, err := s.Profiles(ctx, false) | ||
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(protos) | ||
} | ||
|
||
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 | ||
v := prfs.Get(prID) | ||
if v == nil { | ||
return nil, InvalidProfileErr | ||
} | ||
chosenP = *v | ||
} | ||
return &chosenP, nil | ||
} | ||
|
||
func (b *Base) Connect(ctx context.Context, wgSupport bool, pTCP bool) (*api.ConnectData, error) { | ||
a, err := b.API() | ||
if err != nil { | ||
return nil, err | ||
} | ||
prof, err := s.Profile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// find a suitable profile to connect | ||
chosenP , err := s.findProfile(ctx, s.Profile.ID, wgSupport) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.Profile = chosenP | ||
|
||
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 | ||
// TODO: remove the prefer TCP check when we have implemented proxyguard | ||
if wgSupport && os.Getenv("EDUVPN_PREFER_WG", "0") == "1" { | ||
if !pTCP && chosenP.HasWireGuard() && chosenP.HasOpenVPN() { | ||
protos = []protocol.Protocol{Protocol.WireGuard} | ||
} | ||
} | ||
return a.Connect(ctx, chosenP, protos, pTCP) | ||
} | ||
|
||
func (b *Base) Disconnect(ctx context.Context) error { | ||
a, err := b.API() | ||
if err != nil { | ||
return err | ||
} | ||
return a.Disconnect(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.