diff --git a/Makefile b/Makefile index a93ea26..92549d2 100644 --- a/Makefile +++ b/Makefile @@ -402,8 +402,8 @@ all-torrents: TORRENT=true LANG=vi make torrents torrents: - TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os win -lang "$(LANG)" #-torrent $(TORRENT) - TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os osx -lang "$(LANG)" #-torrent $(TORRENT) - TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os linux -lang "$(LANG)" #-torrent $(TORRENT) - TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os win -arch 32 -lang "$(LANG)" #-torrent $(TORRENT) - TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os linux -arch 32 -lang "$(LANG)" #-torrent $(TORRENT) \ No newline at end of file + TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os win -lang "$(LANG)" #-p2p=false #-torrent $(TORRENT) + TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os osx -lang "$(LANG)" #-p2p=false #-torrent $(TORRENT) + TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os linux -lang "$(LANG)" #-p2p=false #-torrent $(TORRENT) + TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os win -arch 32 -lang "$(LANG)" #-p2p=false #-torrent $(TORRENT) + TOR_MANAGER_CLEARNET_MIRROR=true TOR_MANAGER_REQUIRE_PASSWORD=false ./i2p.plugins.tor-manager -nounpack -notor -os linux -arch 32 -lang "$(LANG)" #-p2p=false #-torrent $(TORRENT) \ No newline at end of file diff --git a/get/get.go b/get/get.go index b82340e..4bff258 100644 --- a/get/get.go +++ b/get/get.go @@ -17,7 +17,6 @@ import ( "path" "path/filepath" "runtime" - "strconv" "strings" "time" @@ -354,6 +353,10 @@ func (wc WriteCounter) PrintProgress() { } func (t *TBDownloader) StartConf() *tor.StartConf { + return StartConf(t.TorPath()) +} + +func StartConf(tp string) *tor.StartConf { paths := []string{ "/bin/tor", "/usr/bin/tor", @@ -374,7 +377,6 @@ func (t *TBDownloader) StartConf() *tor.StartConf { } } } - tp := t.TorPath() if FileExists(tp) { return &tor.StartConf{ ExePath: tp, @@ -386,8 +388,12 @@ func (t *TBDownloader) StartConf() *tor.StartConf { // SetupProxy sets up the proxy for the given URL func (t *TBDownloader) SetupProxy() error { + return SetupProxy(t.Mirror, t.TorPath()) +} + +func SetupProxy(mirror, tp string) error { var d proxy.Dialer - if t.MirrorIsI2P() { + if MirrorIsI2P(mirror) { log.Println("Using I2P mirror, setting up proxy") var err error proxyURL, err := url.Parse("http://127.0.0.1:4444") @@ -403,10 +409,10 @@ func (t *TBDownloader) SetupProxy() error { } http.DefaultClient.Transport = tr } else { - if !strings.Contains(t.Mirror, "127.0.0.1") { + if !strings.Contains(mirror, "127.0.0.1") { if tmp, torerr := net.Listen("tcp", "127.0.0.1:9050"); torerr != nil { log.Println("System Tor is running, downloading over that because obviously.") - t, err := tor.Start(context.Background(), t.StartConf()) + t, err := tor.Start(context.Background(), StartConf(tp)) if err != nil { if t == nil { return err @@ -552,32 +558,32 @@ func (t *TBDownloader) BotherToDownload(dl, name string) bool { if !FileExists(path) { return true } - stat, err := os.Stat(path) - if err != nil { - return true - } + //stat, err := os.Stat(path) + //if err != nil { + // return true + //} // 86 MB - if !strings.Contains(name, ".asc") { - contentLength, err := t.FetchContentLength(dl, name) - if err != nil { - return true - } + //if !strings.Contains(name, ".asc") { + //contentLength, err := t.FetchContentLength(dl, name) + //if err != nil { + // return true + //} - 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) + //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 { - if lenString != lenSize { - return true - } else { - return false - } - } + //if stat.Size() != contentLength { + //if lenString != lenSize { + // return true + //} else { + // return false + //} + //} defer ioutil.WriteFile(filepath.Join(t.DownloadPath, name+".last-url"), []byte(dl), 0644) lastURL, err := ioutil.ReadFile(filepath.Join(t.DownloadPath, name+".last-url")) if err != nil { @@ -887,8 +893,12 @@ func hTTPProxy(host, port string) bool { } func (t *TBDownloader) MirrorIsI2P() bool { + return MirrorIsI2P(t.Mirror) +} + +func MirrorIsI2P(mirror string) bool { // check if hostname is an I2P hostname - url, err := url.Parse(t.Mirror) + url, err := url.Parse(mirror) if err != nil { return false } diff --git a/get/torrent.go b/get/torrent.go index eb17dd6..12985b7 100644 --- a/get/torrent.go +++ b/get/torrent.go @@ -1,9 +1,11 @@ package tbget import ( + "encoding/json" "fmt" "io/ioutil" "log" + "net/http" "net/url" "os" "path/filepath" @@ -201,7 +203,52 @@ func TorrentPath() (string, string) { return fmt.Sprintf("tor-browser%s", windowsonly), extension } +func GetTorBrowserVersionFromUpdateURL() (string, error) { + // download the json file from TOR_UPDATES_URL + // parse the json file to get the latest version + // return the latest version + err := SetupProxy(TOR_UPDATES_URL, "") + if err != nil { + return "", err + } + resp, err := http.Get(TOR_UPDATES_URL) + if err != nil { + return "", err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + var updates map[string]interface{} + err = json.Unmarshal(body, &updates) + if err != nil { + return "", err + } + latest := updates["downloads"].(map[string]interface{})["linux64"].(map[string]interface{})["en-US"].(map[string]interface{}) + + for key, value := range latest { + if key == "binary" { + log.Printf("%s: %s\n", key, value) + url, err := url.Parse(value.(string)) + if err != nil { + return "", err + } + spl := strings.Split(url.Path, "/") + return spl[len(spl)-1], nil + } + } + + return "11.0.9", nil +} + func TorrentDownloaded() bool { + version, err := GetTorBrowserVersionFromUpdateURL() + if err != nil { + panic(err) + } + log.Println("Tor Browser Version", version) + cmpsize := 8661000 found := false if dir, err := FindSnarkDirectory(); err == nil { @@ -212,10 +259,10 @@ func TorrentDownloaded() bool { } prefix, suffix := TorrentPath() path = filepath.Base(path) - if strings.HasPrefix(path, prefix) && strings.HasSuffix(path, suffix) { + if strings.HasPrefix(path, prefix) && strings.Contains(path, version) && strings.HasSuffix(path, suffix) { if !strings.HasSuffix(path, ".torrent") { if info.Size() > int64(cmpsize) { - //fmt.Println("TorrentDownloaded: Torrent Download found:", path) + fmt.Println("TorrentDownloaded: Torrent Download found:", path, info.Size(), int64(cmpsize)) found = true return nil } else { diff --git a/go.mod b/go.mod index 3b1f576..d96d6fa 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,8 @@ require ( golang.org/x/net v0.0.0-20211206223403-eba003a116a9 ) +require github.com/eyedeekay/go-htmleditor v0.0.0-20220322223215-1641dd079151 // indirect + require ( github.com/eyedeekay/go-I2P-jpackage v0.0.0-20220219044935-be7909c9f4c5 github.com/ybbus/jsonrpc/v2 v2.1.6 // indirect diff --git a/go.sum b/go.sum index d9c68a1..4599c9d 100644 --- a/go.sum +++ b/go.sum @@ -202,6 +202,8 @@ github.com/eyedeekay/checki2cp v0.0.21 h1:DVer7H6RffCWS8Bo3+J6EyppUc1y8lvApKdQnA github.com/eyedeekay/checki2cp v0.0.21/go.mod h1:75sGwBgnacHmxxx8RQ7BIeS0gu5Pw916gFb2c80OUTc= github.com/eyedeekay/eephttpd v0.0.0-20190903000420-52f5a8485a4e/go.mod h1:wFPQsNBnY95LkuujFEZARo7slafRwoF0D97FFHBoZro= github.com/eyedeekay/go-fpw v0.0.0-20200512022837-c8b4dcdc74d4/go.mod h1:RyCx7KuH+5ryvIpUF7SpxiChLtjeuPbVFCIzf8shIFc= +github.com/eyedeekay/go-htmleditor v0.0.0-20220322223215-1641dd079151 h1:WKf4WMHkRx6CD3QQcLvJr2I/HR65lZiCx61/ffGIzhM= +github.com/eyedeekay/go-htmleditor v0.0.0-20220322223215-1641dd079151/go.mod h1:P1hV8SQHOTDs3qn5nMAbYHPhQy6GZ62NEv/jvGiXeSE= github.com/eyedeekay/go-i2cp v0.0.0-20190716135428-6d41bed718b0 h1:rnn9OlD/3+tATEZNuiMR1C84O5CX8bZL2qqgttprKrw= github.com/eyedeekay/go-i2cp v0.0.0-20190716135428-6d41bed718b0/go.mod h1:+P0fIhkqIYjo7exMJRTlSteRMbRyHbiBiKw+YlPWk+c= github.com/eyedeekay/go-i2pcontrol v0.0.0-20200110011336-510cca77e350/go.mod h1:bhIQsVpbNNXMtcoZ9UF4hLQleOjaCgKGXiRRhNc8TOA= diff --git a/main.go b/main.go index 7fa9e21..eb79feb 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,9 @@ import ( "github.com/ncruces/zenity" tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get" tbserve "i2pgit.org/idk/i2p.plugins.tor-manager/serve" + tbsupervise "i2pgit.org/idk/i2p.plugins.tor-manager/supervise" + + tinymce "github.com/eyedeekay/go-htmleditor" ) /* @@ -190,6 +193,7 @@ func main() { } flag.Parse() if *ptop { + log.Println("Using p2p") *mirror = "http://localhost:7657/i2psnark/" } if *password != "" { @@ -362,11 +366,7 @@ func main() { } if *solidarity { client.Onion.UnpackSite() - go func() { - if err := client.Onion.ListenAndServe(); err != nil { - log.Println("Onion error:", err) - } - }() + go ServeOnion() } go runSysTray(false) if err := client.Serve(); err != nil { @@ -375,6 +375,24 @@ func main() { } } +func ServeOnion() error { + if err := client.Onion.ListenAndServe(); err != nil { + log.Println("Onion error:", err) + } + return nil +} + +func ServeEditor() error { + docroot, err := tbsupervise.FindEepsiteDocroot() + if err != nil { + return err + } + if err := tinymce.Serve("127.0.0.1", docroot, "index.html", 7685); err != nil { + log.Println("Couldn't serve editor", err) + } + return nil +} + func pathToMe() (string, error) { ex, err := os.Executable() if err != nil { diff --git a/supervise/supervise.go b/supervise/supervise.go index 95a9f4b..f4bbd96 100644 --- a/supervise/supervise.go +++ b/supervise/supervise.go @@ -11,6 +11,7 @@ import ( "os/exec" "path" "path/filepath" + "runtime" "strings" "github.com/mitchellh/go-ps" @@ -625,3 +626,131 @@ func NewSupervisor(tbPath, lang string) *Supervisor { Lang: lang, } } + +func FindEepsiteDocroot() (string, error) { + // eepsite docroot could be at: + // or: $I2P_CONFIG/eepsite/docroot/ + // or: $I2P/eepsite/docroot/ + // or: $HOME/.i2p/eepsite/docroot/ + // or: /var/lib/i2p/i2p-config/eepsite/docroot/ + // or: %LOCALAPPDATA\i2p\eepsite\docroot\ + // or: %APPDATA\i2p\eepsite\docroot\ + // or: %USERPROFILE\i2p\eepsite\docroot\ + SNARK_CONFIG := os.Getenv("SNARK_CONFIG") + if SNARK_CONFIG != "" { + checkfori2pcustom := filepath.Join(SNARK_CONFIG) + if tbget.FileExists(checkfori2pcustom) { + return checkfori2pcustom, nil + } + } + + I2P_CONFIG := os.Getenv("I2P_CONFIG") + if I2P_CONFIG != "" { + checkfori2pcustom := filepath.Join(I2P_CONFIG, "eepsite", "docroot") + if tbget.FileExists(checkfori2pcustom) { + return checkfori2pcustom, nil + } + } + + I2P := os.Getenv("I2P") + if I2P != "" { + checkfori2p := filepath.Join(I2P, "eepsite", "docroot") + if tbget.FileExists(checkfori2p) { + return checkfori2p, nil + } + } + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + // Start by getting the home directory + switch runtime.GOOS { + case "windows": + checkfori2plocal := filepath.Join(home, "AppData", "Local", "i2p", "eepsite", "docroot") + if tbget.FileExists(checkfori2plocal) { + return checkfori2plocal, nil + } + checkfori2proaming := filepath.Join(home, "AppData", "Roaming", "i2p", "eepsite", "docroot") + if tbget.FileExists(checkfori2proaming) { + return checkfori2proaming, nil + } + case "linux": + checkfori2phome := filepath.Join(home, ".i2p", "eepsite", "docroot") + if tbget.FileExists(checkfori2phome) { + return checkfori2phome, nil + } + checkfori2pservice := filepath.Join("/var/lib/i2p/i2p-config", "eepsite", "docroot") + if tbget.FileExists(checkfori2pservice) { + return checkfori2pservice, nil + } + case "darwin": + return "", fmt.Errorf("FindSnarkDirectory: Automatic torrent generation is not supported on MacOS, for now copy the files manually") + } + return "", fmt.Errorf("FindSnarkDirectory: Unable to find snark directory") + +} + +// RunTBBWithOfflineProfile runs the I2P Browser with the given language +func (s *Supervisor) RunI2PSiteEditorWithOfflineClearnetProfile(profiledata string) error { + defaultpage := "http://127.0.0.1:7685" + clearnet := true + offline := true + if clearnet { + log.Print("Generating Clearnet Profile") + if err := s.GenerateClearnetProfile(profiledata); err != nil { + log.Println("Error generating Clearnet Profile", err) + return err + } + } + if offline { + if err := s.CopyAWOXPI(profiledata); err != nil { + log.Println("Error copying AWO XPI", err) + return err + } + } + tbget.ARCH = ARCH() + if s.Lang == "" { + s.Lang = DEFAULT_TB_LANG + } + if s.UnpackPath == "" { + s.UnpackPath = UNPACK_URL() + } + + log.Println("running i2p in tor browser with lang", s.Lang, s.UnpackPath, OS()) + switch OS() { + case "linux": + if tbget.FileExists(s.UnpackPath) { + args := []string{"--profile", profiledata, defaultpage} + args = append(args, s.PTAS()...) + log.Println("running Tor browser with lang and Custom Profile", s.Lang, s.UnpackPath, s.FirefoxPath(), args) + bcmd := exec.Command(s.FirefoxPath(), args...) + bcmd.Stdout = os.Stdout + bcmd.Stderr = os.Stderr + return bcmd.Run() + } + log.Println("tor browser not found at", s.FirefoxPath()) + return fmt.Errorf("tor browser not found at %s", s.FirefoxPath()) + case "osx": + firefoxPath := filepath.Join(s.UnpackPath, "Tor Browser.app", "Contents", "MacOS", "firefox") + args := []string{"--profile", profiledata, defaultpage} + args = append(args, s.PTAS()...) + log.Println("running Tor browser with lang and Custom Profile", s.Lang, s.UnpackPath, firefoxPath, args) + bcmd := exec.Command(firefoxPath, args...) + bcmd.Dir = profiledata + bcmd.Stdout = os.Stdout + bcmd.Stderr = os.Stderr + + return bcmd.Run() + case "win": + args := []string{"--profile", profiledata, defaultpage} + args = append(args, s.PTAS()...) + log.Println("running Tor browser with lang and Custom Profile", s.Lang, s.UnpackPath, s.TBPath(), args) + bcmd := exec.Command(s.TBPath(), args...) + bcmd.Dir = profiledata + bcmd.Stdout = os.Stdout + bcmd.Stderr = os.Stderr + return bcmd.Run() + default: + } + return nil +}