Skip to content

Commit

Permalink
adding water version control (#1427)
Browse files Browse the repository at this point in the history
* feat: adding wasm downloader

* fix: update tests package name

* fix: generating wasm downloader mock

* feat: adding magnet downloader

* feat: adding case for using magnet downloader

* fix: replacing magnetic references by magnet and also removing magnet skip

* fix: downgrade torrent version to 1.53.3 to avoid errors happening at confluence - anacrolix/confluence needs a new tag and we need to update that at lantern/replica and then here at lantern/flashlight

* fix: updating the way we're comparing hashes

* chore: go mod tidy

* feat: using DirectThenFronted proxy for fetching WASM and also update proxy vars

* feat: fix test and print error message

* feat: give life to the dial monster

* feat: release the beast

* fix: rename mocks.go to mocks_test.go

* fix: pass empty rootCA at this moment

* fix: removing hashsum parameter

* fix: replacing http client parameter by package var so I can mock it for tests

* fix: leaving mkdirtemp first parameter empty and set lantern-water-module dir name

* fix: update torrent to latest version

* fix: generating more mocks for torrent so we can unit test it without integration

* chore: removing test that was fetching random torrent

* chore: keeping torrent at v1.53.3

* fix: removing WithHTTPDownloader

* fix: adding garmr-ulfr suggestions from http-proxy PR

* feat: adding minimal version control for WASM files

* feat: using filename/transport as index for mapped wasm files

* feat: integrate version control with water wasm and replace direct downloader

* chore: sending writer directly instead of creating a temp buffer

* chore: sending config dir as parameter to water impl

* chore: small refactor for organizing download step

* feat: add support for WASM history

* feat: update history after loading WASM correctly

* chore: go mod tidy

* chore: running go generate

* fix: renaming versionControl to waterVersionControl

* fix: removing VersionControl unused interface

* fix: moving version control commit to GetWASM

* fix: moving WASMDownloader initialization from version control to water impl and receive downloader for fetching wasm

* fix: renaming structs to refer to water

* fix: renaming configDir to dir

* fix: updating mock test references

* fix: removing water_impl responsibility for setting the water version control dir

* fix: creating const at package level for outdated WASMs

* fix: removing csv dependency and making it simpler

* chore: go mod tidy

* fix: replace os.IsNotExist

* fix: grouping files to be deleted and delete them outside filepath.Walk

* fix: creating water dir if it doesn't exist

* fix: replacing os.Mkdir by os.MkdirAll
  • Loading branch information
WendelHime authored Oct 28, 2024
1 parent bab5a0d commit 3924cac
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 171 deletions.
111 changes: 57 additions & 54 deletions chained/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions chained/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
Expand All @@ -18,7 +20,7 @@ import (

"github.com/getlantern/common/config"
"github.com/getlantern/ema"
"github.com/getlantern/errors"
lerrors "github.com/getlantern/errors"
"github.com/getlantern/mtime"
"github.com/getlantern/netx"

Expand Down Expand Up @@ -151,7 +153,7 @@ func extractParams(s *config.ProxyConfig) (addr, transport, network string, err
forceProxy(s)
}
if s.Addr == "" {
err = errors.New("Empty addr")
err = lerrors.New("Empty addr")
return
}
addr = s.Addr
Expand Down Expand Up @@ -212,9 +214,13 @@ func createImpl(configDir, name, addr, transport string, s *config.ProxyConfig,
case "algeneva":
impl, err = newAlgenevaImpl(addr, s, reportDialCore)
case "water":
impl, err = newWaterImpl(addr, s, reportDialCore)
waterDir := filepath.Join(configDir, "water")
if err := os.MkdirAll(waterDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create water directory: %w", err)
}
impl, err = newWaterImpl(waterDir, addr, s, reportDialCore)
default:
err = errors.New("Unknown transport: %v", transport).With("addr", addr).With("plugabble-transport", transport)
err = lerrors.New("Unknown transport: %v", transport).With("addr", addr).With("plugabble-transport", transport)
}
if err != nil {
return nil, err
Expand Down
17 changes: 9 additions & 8 deletions chained/torrent_reader_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 9 additions & 17 deletions chained/water_downloader.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package chained

import (
"bytes"
"context"
"errors"
"io"
"net/http"
"strings"
)

//go:generate mockgen -package=chained -destination=mocks_test.go . WASMDownloader,TorrentClient,TorrentInfo
//go:generate mockgen -package=chained -destination=mocks_test.go . waterWASMDownloader,torrentClient,torrentInfo
//go:generate mockgen -package=chained -destination=torrent_reader_mock_test.go github.com/anacrolix/torrent Reader

type WASMDownloader interface {
type waterWASMDownloader interface {
DownloadWASM(context.Context, io.Writer) error
Close() error
}

type downloader struct {
urls []string
httpClient *http.Client
httpDownloader WASMDownloader
magnetDownloader WASMDownloader
httpDownloader waterWASMDownloader
magnetDownloader waterWASMDownloader
}

// NewWASMDownloader creates a new WASMDownloader instance.
func NewWASMDownloader(urls []string, httpClient *http.Client) (WASMDownloader, error) {
// newWaterWASMDownloader creates a new WASMDownloader instance.
func newWaterWASMDownloader(urls []string, httpClient *http.Client) (waterWASMDownloader, error) {
if len(urls) == 0 {
return nil, log.Error("WASM downloader requires URLs to download but received empty list")
}
Expand All @@ -47,14 +46,7 @@ func (d *downloader) Close() error {
func (d *downloader) DownloadWASM(ctx context.Context, w io.Writer) error {
joinedErrs := errors.New("failed to download WASM from all URLs")
for _, url := range d.urls {
tempBuffer := &bytes.Buffer{}
err := d.downloadWASM(ctx, tempBuffer, url)
if err != nil {
joinedErrs = errors.Join(joinedErrs, err)
continue
}

_, err = tempBuffer.WriteTo(w)
err := d.downloadWASM(ctx, w, url)
if err != nil {
joinedErrs = errors.Join(joinedErrs, err)
continue
Expand All @@ -71,13 +63,13 @@ func (d *downloader) downloadWASM(ctx context.Context, w io.Writer, url string)
switch {
case strings.HasPrefix(url, "http://"), strings.HasPrefix(url, "https://"):
if d.httpDownloader == nil {
d.httpDownloader = NewHTTPSDownloader(d.httpClient, url)
d.httpDownloader = newWaterHTTPSDownloader(d.httpClient, url)
}
return d.httpDownloader.DownloadWASM(ctx, w)
case strings.HasPrefix(url, "magnet:?"):
if d.magnetDownloader == nil {
var err error
downloader, err := NewMagnetDownloader(ctx, url)
downloader, err := newWaterMagnetDownloader(ctx, url)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 3924cac

Please sign in to comment.