diff options
| -rw-r--r-- | client/discovery.go | 2 | ||||
| -rw-r--r-- | cmd/eduvpn-cli/main.go | 7 | ||||
| -rw-r--r-- | internal/api/cache.go | 2 | ||||
| -rw-r--r-- | internal/config/atomicfile/atomicfile.go | 4 | ||||
| -rw-r--r-- | internal/config/atomicfile/atomicfile_test.go | 4 | ||||
| -rw-r--r-- | internal/config/v1/v1.go | 4 | ||||
| -rw-r--r-- | internal/discovery/discovery.go | 7 | ||||
| -rw-r--r-- | internal/discovery/manager.go | 8 | ||||
| -rw-r--r-- | internal/levenshtein/levenshtein.go | 1 | ||||
| -rw-r--r-- | internal/test/server.go | 5 | ||||
| -rw-r--r-- | internal/verify/verify_test.go | 3 | ||||
| -rw-r--r-- | internal/wireguard/wireguard.go | 4 | ||||
| -rw-r--r-- | proxy/proxy.go | 7 | ||||
| -rw-r--r-- | util/util.go | 2 |
14 files changed, 44 insertions, 16 deletions
diff --git a/client/discovery.go b/client/discovery.go index 596715f..b1cae32 100644 --- a/client/discovery.go +++ b/client/discovery.go @@ -112,6 +112,8 @@ func (c *Client) DiscoServers(ck *cookie.Cookie, search string) (*discotypes.Ser }, err } +// DiscoveryStartup gets the discovery when the client is just starting up +// cb is called when discovery has finished in the background func (c *Client) DiscoveryStartup(cb func()) error { // Not supported with Let's Connect! & govVPN if !c.hasDiscovery() { diff --git a/cmd/eduvpn-cli/main.go b/cmd/eduvpn-cli/main.go index d8942c5..2a990f2 100644 --- a/cmd/eduvpn-cli/main.go +++ b/cmd/eduvpn-cli/main.go @@ -99,8 +99,8 @@ func stateCallback(_ client.FSMStateID, newState client.FSMStateID, data interfa } if newState == client.StateAskLocation { - // defer not run due to os.Exit - os.RemoveAll(dir) + // removing is best effort + _ = os.RemoveAll(dir) fmt.Fprint(os.Stderr, "An invalid secure location is stored. This CLI doesn't support interactively choosing a location yet. Give a correct location with the -country-code flag") os.Exit(1) } @@ -144,7 +144,8 @@ func printConfig(url string, cc string, srvType srvtypes.Type, prof string, debu if err != nil { return err } - defer os.RemoveAll(dir) + // removing is best effort + defer os.RemoveAll(dir) //nolint:errcheck c, err = client.New( "org.eduvpn.app.linux", fmt.Sprintf("%s-cli", version.Version), diff --git a/internal/api/cache.go b/internal/api/cache.go index 9a478df..5c682f4 100644 --- a/internal/api/cache.go +++ b/internal/api/cache.go @@ -16,7 +16,7 @@ type EndpointCache struct { mu sync.Mutex } -// Get() returns a cached or fresh endpoint cache copy +// Get returns a cached or fresh endpoint cache copy func (ec *EndpointCache) Get(ctx context.Context, wk string, transport http.RoundTripper) (*endpoints.Endpoints, error) { ec.mu.Lock() defer ec.mu.Unlock() diff --git a/internal/config/atomicfile/atomicfile.go b/internal/config/atomicfile/atomicfile.go index 5c18e85..542f58a 100644 --- a/internal/config/atomicfile/atomicfile.go +++ b/internal/config/atomicfile/atomicfile.go @@ -29,8 +29,8 @@ func WriteFile(filename string, data []byte, perm os.FileMode) (err error) { tmpName := f.Name() defer func() { if err != nil { - f.Close() - os.Remove(tmpName) + f.Close() //nolint:errcheck + os.Remove(tmpName) //nolint:errcheck } }() if _, err := f.Write(data); err != nil { diff --git a/internal/config/atomicfile/atomicfile_test.go b/internal/config/atomicfile/atomicfile_test.go index 670d225..f14cb31 100644 --- a/internal/config/atomicfile/atomicfile_test.go +++ b/internal/config/atomicfile/atomicfile_test.go @@ -24,7 +24,7 @@ func TestDoesNotOverwriteIrregularFiles(t *testing.T) { // macOS private temp does not allow unix socket creation, but /tmp does. if runtime.GOOS == "darwin" { path = filepath.Join("/tmp", filename) - t.Cleanup(func() { os.Remove(path) }) + t.Cleanup(func() { os.Remove(path) }) //nolint:errcheck } else { path = filepath.Join(t.TempDir(), filename) } @@ -35,7 +35,7 @@ func TestDoesNotOverwriteIrregularFiles(t *testing.T) { if err != nil { t.Fatal(err) } - defer l.Close() + defer l.Close() //nolint:errcheck err = WriteFile(path, []byte("hello"), 0o644) if err == nil { diff --git a/internal/config/v1/v1.go b/internal/config/v1/v1.go index 973c3d3..5f5cd93 100644 --- a/internal/config/v1/v1.go +++ b/internal/config/v1/v1.go @@ -64,11 +64,15 @@ type SecureInternetHome struct { CurrentLocation string `json:"current_location"` } +// Type is the type of server, a server from discovery or one entered manually by typing it in the client type Type int8 const ( + // CustomServerType is the type of server that is manually added by typing a URL CustomServerType Type = iota + // InstituteAccessServerType is the type of server that is in the discovery server_list.json with type "institute_access" InstituteAccessServerType + // SecureInternetServerType is the type of server that is in the discovery server_list.json with type "secure_internet" SecureInternetServerType ) diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index b6fef6f..c7eae95 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -44,6 +44,7 @@ type Organization struct { KeywordList discotypes.MapOrString `json:"keyword_list,omitempty"` } +// Score returns the levenshstein score for a search query `search` on the organizations func (o *Organization) Score(search string) int { return levenshtein.DiscoveryScore(search, o.DisplayName, o.KeywordList) } @@ -75,7 +76,7 @@ type Server struct { SupportContact []string `json:"support_contact,omitempty"` } -// Matches returns if the search query `str` matches with this server +// Score returns the score of the search query `str` on this server func (s *Server) Score(search string) int { return levenshtein.DiscoveryScore(search, s.DisplayName, s.KeywordList) } @@ -404,12 +405,16 @@ func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error) return &discovery.ServerList, true, nil } +// UpdateServers updates the discovery servers to the new version +// It does this by checking versions func (discovery *Discovery) UpdateServers(other Discovery) { if other.ServerList.Version >= discovery.ServerList.Version { discovery.ServerList = other.ServerList } } +// Copy creates a deep-copy for the discovery struct +// It does this by marshalling and unmarshalling it as JSON func (discovery *Discovery) Copy() (Discovery, error) { var dest Discovery b, err := json.Marshal(discovery) diff --git a/internal/discovery/manager.go b/internal/discovery/manager.go index e71f13a..6a78486 100644 --- a/internal/discovery/manager.go +++ b/internal/discovery/manager.go @@ -7,6 +7,8 @@ import ( "codeberg.org/eduVPN/eduvpn-common/internal/log" ) +// Manager is the discovery struct that is cached +// with some bookkeeping to avoid race conditions type Manager struct { disco *Discovery @@ -15,6 +17,7 @@ type Manager struct { wait sync.WaitGroup } +// NewManager creates a new Discovery manager func NewManager(disco *Discovery) *Manager { return &Manager{disco: disco} } @@ -35,6 +38,8 @@ func (m *Manager) unlock(write bool) { m.mu.RUnlock() } +// Discovery gets the cached discovery +// `write` is true if discovery will be written to func (m *Manager) Discovery(write bool) (*Discovery, func()) { if write { m.wait.Wait() @@ -45,6 +50,7 @@ func (m *Manager) Discovery(write bool) (*Discovery, func()) { } } +// Cancel aborts the running discovery startup process func (m *Manager) Cancel() { if m.cancel != nil { m.cancel() @@ -52,6 +58,8 @@ func (m *Manager) Cancel() { m.wait.Wait() } +// Startup handles the discovery process in the background +// It's called Startup because it's called when the lib is initialised func (m *Manager) Startup(ctx context.Context, cb func()) { ctx, cancel := context.WithCancel(ctx) m.cancel = cancel diff --git a/internal/levenshtein/levenshtein.go b/internal/levenshtein/levenshtein.go index 68108bc..3be81c8 100644 --- a/internal/levenshtein/levenshtein.go +++ b/internal/levenshtein/levenshtein.go @@ -1,3 +1,4 @@ +// Package levenshtein implements the algorithm for calculating the "distance" between two strings package levenshtein import ( diff --git a/internal/test/server.go b/internal/test/server.go index 1abbd8c..f222abc 100644 --- a/internal/test/server.go +++ b/internal/test/server.go @@ -24,12 +24,14 @@ func NewServer(handler http.Handler, listener net.Listener) *Server { } s := httptest.NewUnstartedServer(handler) - s.Listener.Close() + // This is only for tests and if this fails, bad luck + s.Listener.Close() //nolint:errcheck s.Listener = listener s.StartTLS() return &Server{s} } +// HandlerPath handles a specific HTTP query type HandlerPath struct { Method string Path string @@ -38,6 +40,7 @@ type HandlerPath struct { ResponseCode int } +// HandlerFunc handles a HTTP request func (hp *HandlerPath) HandlerFunc() func(http.ResponseWriter, *http.Request) { if hp.ResponseHandler != nil { return hp.ResponseHandler diff --git a/internal/verify/verify_test.go b/internal/verify/verify_test.go index 2c499b1..aa373d1 100644 --- a/internal/verify/verify_test.go +++ b/internal/verify/verify_test.go @@ -15,7 +15,8 @@ func Test_verifyWithKeys(t *testing.T) { if err != nil { panic(err) } - defer file.Close() + // this is best effort + defer file.Close() //nolint:errcheck // Get last line (key string) from file scanner := bufio.NewScanner(file) diff --git a/internal/wireguard/wireguard.go b/internal/wireguard/wireguard.go index 8c2753b..b156430 100644 --- a/internal/wireguard/wireguard.go +++ b/internal/wireguard/wireguard.go @@ -19,7 +19,7 @@ func availableTCPPort() (int, error) { if err != nil { return -1, err } - defer ltcp.Close() + defer ltcp.Close() //nolint:errcheck return ltcp.Addr().(*net.TCPAddr).Port, nil } @@ -32,7 +32,7 @@ func availableUDPPort() (int, error) { if err != nil { return -1, err } - defer ludp.Close() + defer ludp.Close() //nolint:errcheck return ludp.LocalAddr().(*net.UDPAddr).Port, nil } diff --git a/proxy/proxy.go b/proxy/proxy.go index 7a35c8d..a43083f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,4 +1,4 @@ -// package proxy is a wrapper around proxyguard that integrates it with eduvpn-common settings +// Package proxy is a wrapper around proxyguard that integrates it with eduvpn-common settings // - leaves out some options not applicable to the common integration, e.g. fwmark // - integrates with eduvpn-common's logger // - integrates eduvpn-common's user agent @@ -27,6 +27,7 @@ func (l *Logger) Log(msg string) { log.Logger.Infof("[Proxyguard] %s", msg) } +// Proxy is the ProxyGuard client with a channel used for restarting type Proxy struct { proxyguard.Client resChan chan struct{} @@ -45,7 +46,7 @@ func NewProxyguard(ctx context.Context, lp int, tcpsp int, peer string, setupSoc }, resChan: make(chan struct{}), } - _, err := proxy.Client.Setup(ctx) + _, err := proxy.Setup(ctx) if err != nil { return nil, i18nerr.WrapInternal(err, "The ProxyGuard DNS could not be resolved") } @@ -53,6 +54,7 @@ func NewProxyguard(ctx context.Context, lp int, tcpsp int, peer string, setupSoc return &proxy, nil } +// Tunnel tunnels the ProxyGuard connection. `wglisten` is the WireGuard listen port func (p *Proxy) Tunnel(ctx context.Context, wglisten int) error { log.Logger.Infof("callying tunnel") errChan := make(chan error, 1) @@ -76,6 +78,7 @@ func (p *Proxy) Tunnel(ctx context.Context, wglisten int) error { } } +// Restart restarts the existing ProxyGuard process, for e.g. roaming func (p *Proxy) Restart() { p.resChan <- struct{}{} } diff --git a/util/util.go b/util/util.go index cd7ed9b..4609199 100644 --- a/util/util.go +++ b/util/util.go @@ -1,4 +1,4 @@ -// package util defines public utility functions to be used by applications +// Package util defines public utility functions to be used by applications // these are outside of the client package as they can be used even if a client hasn't been created yet package util |
