From 0c2cb7c11d609980cfde1dbb03c61603a5f94a22 Mon Sep 17 00:00:00 2001 From: idk Date: Tue, 15 Mar 2022 18:54:42 -0400 Subject: [PATCH] Adds most of the Torrent Download option. Defaults to on if the torrent is already downloaded. --- get/get.go | 30 +++++++++++++++++++++++++----- get/torrent.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 27 +++++++++++++++++++++------ 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/get/get.go b/get/get.go index 410c062..4156e58 100644 --- a/get/get.go +++ b/get/get.go @@ -286,6 +286,7 @@ func (t *TBDownloader) GetUpdaterForLangFromJSONBytes(jsonBytes []byte, ietf str } func (t *TBDownloader) MirrorIze(replaceStr string) string { + log.Println("MirrorIze()", "Replacing", replaceStr, t.Mirror) if t.OS == "linux" && runtime.GOARCH == "arm64" { replaceStr = strings.Replace(replaceStr, "linux64", "linux-arm64", -1) if strings.HasSuffix(replaceStr, ".tar.xz.asc") { @@ -296,6 +297,14 @@ func (t *TBDownloader) MirrorIze(replaceStr string) string { replaceStr = strings.Replace(replaceStr, lastElement, "sha256sums-unsigned-build.txt.asc", -1) } } + if strings.Contains(t.Mirror, "i2psnark") { + replaceStr = strings.Replace(replaceStr, "https://dist.torproject.org/torbrowser/", t.Mirror, 1) + dpath := filepath.Base(replaceStr) + replaceStr = strings.Replace(replaceStr, "http://", "", 1) + replaceStr = filepath.Dir(replaceStr) + replaceStr = filepath.Dir(replaceStr) + return "http://" + filepath.Join(replaceStr, dpath) + } if t.Mirror != "" { return strings.Replace(replaceStr, "https://dist.torproject.org/torbrowser/", t.Mirror, 1) } @@ -498,12 +507,12 @@ func (t *TBDownloader) FetchContentLength(dl, name string) (int64, error) { Method: "HEAD", URL: dlurl, } - t.Log("SingleFileDownload()", "Downloading file "+dl) + t.Log("FetchContentLength()", "Downloading file "+dl) //file, err := http.Get(dl) file, err := http.DefaultClient.Do(&req) //Do(&req, nil) if err != nil { - return 0, fmt.Errorf("SingleFileDownload: Request Error %s", err) + return 0, fmt.Errorf("FetchContentLength: Request Error %s", err) } file.Body.Close() log.Println("Content-Length:", file.ContentLength) @@ -528,8 +537,12 @@ func (t *TBDownloader) BotherToDownload(dl, name string) bool { return true } - lenString := strconv.Itoa(int(contentLength))[:4] - lenSize := strconv.Itoa(int(stat.Size()))[:4] + l := 4 + if len(strconv.Itoa(int(contentLength))) < 4 { + l = 1 + } + lenString := strconv.Itoa(int(contentLength))[:l] + lenSize := strconv.Itoa(int(stat.Size()))[:l] log.Println("comparing sizes:", lenString, lenSize) //if stat.Size() != contentLength { @@ -562,7 +575,7 @@ func (t *TBDownloader) NamePerPlatform(ietf, version string) string { extension = "exe" } //version, err := t.Get - return fmt.Sprintf("torbrowser%s-%s-%s_%s.%s", windowsonly, t.GetRuntimePair(), version, ietf, extension) + return fmt.Sprintf("tor-browser%s-%s-%s_%s.%s", windowsonly, t.GetRuntimePair(), version, ietf, extension) } func (t *TBDownloader) GetVersion() string { @@ -594,6 +607,13 @@ func (t *TBDownloader) DownloadUpdaterForLang(ietf string) (string, string, stri return "", "", "", fmt.Errorf("DownloadUpdaterForLang: %s", err) } version := t.GetVersion() + if strings.Contains(t.Mirror, "i2psnark") { + for !TorrentDownloaded() { + time.Sleep(time.Second * 10) + log.Println("DownloadUpdaterForLang:", "Waiting for torrent to download") + } + time.Sleep(time.Second * 10) + } sigpath, err := t.SingleFileDownload(sig, t.NamePerPlatform(ietf, version)+".asc", 0) if err != nil { diff --git a/get/torrent.go b/get/torrent.go index a686835..a8113df 100644 --- a/get/torrent.go +++ b/get/torrent.go @@ -162,3 +162,49 @@ func TorrentReady() bool { } return true } + +func TorrentPath() (string, string) { + extension := "tar.xz" + windowsonly := "" + switch runtime.GOOS { + case "darwin": + extension = "dmg" + case "windows": + windowsonly = "-installer" + extension = "exe" + } + //version, err := t.Get + return fmt.Sprintf("tor-browser%s", windowsonly), extension +} + +func TorrentDownloaded() bool { + cmpsize := 8661000 + if dir, err := FindSnarkDirectory(); err == nil { + err := filepath.Walk(dir, + func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + prefix, suffix := TorrentPath() + if strings.HasPrefix(path, prefix) && strings.HasSuffix(path, suffix) { + if info.Size() > int64(cmpsize) { + log.Println("TorrentDownloaded: Torrent Download found:", path) + return nil + } + } + return nil + }) + return err == nil + } + return false +} + +func Torrent() bool { + if !TorrentReady() { + return false + } + if !TorrentDownloaded() { + return false + } + return true +} diff --git a/main.go b/main.go index a280fc2..193289c 100644 --- a/main.go +++ b/main.go @@ -92,7 +92,8 @@ var ( destruct = flag.Bool("destruct", false, "Destructively delete the working directory when finished") password = flag.String("password", Password(), "Password to encrypt the working directory with. Implies -destruct, only the encrypted container will be saved.") chat = flag.Bool("chat", false, "Open a WebChat client") - /*ptop = flag.Bool("p2p", tbget.TorrentReady(), "Use bittorrent over I2P to download the initial copy of Tor Browser")*/ + notor = flag.Bool("notor", false, "Do not automatically start Tor") + ptop = flag.Bool("p2p", tbget.TorrentDownloaded(), "Use bittorrent over I2P to download the initial copy of Tor Browser") ) func Clearnet() bool { @@ -136,21 +137,32 @@ func Mirror() string { if mir := os.Getenv("TOR_MANAGER_MIRROR"); mir != "" { return mir } + log.Println("No mirror specified, using default") if runtime.GOOS == "linux" && runtime.GOARCH == "arm64" { + log.Println("Using arm64 mirror") return "https://sourceforge.net/projects/tor-browser-ports/files" } clear := os.Getenv("TOR_MANAGER_CLEARNET") - if clear == "true" || clear == "1" { + switch clear { + case "1", "true", "yes", "on": + log.Println("Using clearnet mirror") return "https://dist.torproject.org/torbrowser/" } - if tbget.TorrentReady() { - // return "http://127.0.0.1:7657/i2psnark/" + clearmirror := os.Getenv("TOR_MANAGER_CLEARNET_MIRROR") + switch clearmirror { + case "1", "true", "yes", "on": + log.Println("Using clearnet mirror") return "https://dist.torproject.org/torbrowser/" } + if tbget.Torrent() { + log.Println("Using torrent mirror") + return "http://localhost:7657/i2psnark/" + } if tbget.TestHTTPDefaultProxy() { + log.Println("Using I2P mirror") return "http://dist.torproject.i2p/torbrowser/" } - + log.Println("Using clearnet mirror") return "https://dist.torproject.org/torbrowser/" } @@ -174,6 +186,9 @@ func main() { usage() } flag.Parse() + if *ptop { + *mirror = "http://localhost:7657/i2psnark/" + } if *password != "" { log.Println("Looking for directory with password") DecryptTarXZifThere(*directory, *password) @@ -330,7 +345,7 @@ func main() { log.Fatal(err) } } - if !*clearnet { + if !*clearnet && !*notor { client.TBS.RunTorWithLang() }