Add ability to get the Tor version without instantiating a whole object.

This commit is contained in:
idk
2022-03-22 21:24:08 -04:00
parent 89319b9e56
commit 9ce7c2dbe4
7 changed files with 249 additions and 41 deletions

View File

@ -402,8 +402,8 @@ all-torrents:
TORRENT=true LANG=vi make torrents TORRENT=true LANG=vi make torrents
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 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)" #-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)" #-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)" #-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)" #-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)

View File

@ -17,7 +17,6 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
@ -354,6 +353,10 @@ func (wc WriteCounter) PrintProgress() {
} }
func (t *TBDownloader) StartConf() *tor.StartConf { func (t *TBDownloader) StartConf() *tor.StartConf {
return StartConf(t.TorPath())
}
func StartConf(tp string) *tor.StartConf {
paths := []string{ paths := []string{
"/bin/tor", "/bin/tor",
"/usr/bin/tor", "/usr/bin/tor",
@ -374,7 +377,6 @@ func (t *TBDownloader) StartConf() *tor.StartConf {
} }
} }
} }
tp := t.TorPath()
if FileExists(tp) { if FileExists(tp) {
return &tor.StartConf{ return &tor.StartConf{
ExePath: tp, ExePath: tp,
@ -386,8 +388,12 @@ func (t *TBDownloader) StartConf() *tor.StartConf {
// SetupProxy sets up the proxy for the given URL // SetupProxy sets up the proxy for the given URL
func (t *TBDownloader) SetupProxy() error { func (t *TBDownloader) SetupProxy() error {
return SetupProxy(t.Mirror, t.TorPath())
}
func SetupProxy(mirror, tp string) error {
var d proxy.Dialer var d proxy.Dialer
if t.MirrorIsI2P() { if MirrorIsI2P(mirror) {
log.Println("Using I2P mirror, setting up proxy") log.Println("Using I2P mirror, setting up proxy")
var err error var err error
proxyURL, err := url.Parse("http://127.0.0.1:4444") proxyURL, err := url.Parse("http://127.0.0.1:4444")
@ -403,10 +409,10 @@ func (t *TBDownloader) SetupProxy() error {
} }
http.DefaultClient.Transport = tr http.DefaultClient.Transport = tr
} else { } 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 { if tmp, torerr := net.Listen("tcp", "127.0.0.1:9050"); torerr != nil {
log.Println("System Tor is running, downloading over that because obviously.") 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 err != nil {
if t == nil { if t == nil {
return err return err
@ -552,32 +558,32 @@ func (t *TBDownloader) BotherToDownload(dl, name string) bool {
if !FileExists(path) { if !FileExists(path) {
return true return true
} }
stat, err := os.Stat(path) //stat, err := os.Stat(path)
if err != nil { //if err != nil {
return true // return true
} //}
// 86 MB // 86 MB
if !strings.Contains(name, ".asc") { //if !strings.Contains(name, ".asc") {
contentLength, err := t.FetchContentLength(dl, name) //contentLength, err := t.FetchContentLength(dl, name)
if err != nil { //if err != nil {
return true // return true
} //}
l := 4 //l := 4
if len(strconv.Itoa(int(contentLength))) < 4 { //if len(strconv.Itoa(int(contentLength))) < 4 {
l = 1 // l = 1
} //}
lenString := strconv.Itoa(int(contentLength))[:l] //lenString := strconv.Itoa(int(contentLength))[:l]
lenSize := strconv.Itoa(int(stat.Size()))[:l] //lenSize := strconv.Itoa(int(stat.Size()))[:l]
log.Println("comparing sizes:", lenString, lenSize) //log.Println("comparing sizes:", lenString, lenSize)
//if stat.Size() != contentLength { //if stat.Size() != contentLength {
if lenString != lenSize { //if lenString != lenSize {
return true // return true
} else { //} else {
return false // return false
} //}
} //}
defer ioutil.WriteFile(filepath.Join(t.DownloadPath, name+".last-url"), []byte(dl), 0644) defer ioutil.WriteFile(filepath.Join(t.DownloadPath, name+".last-url"), []byte(dl), 0644)
lastURL, err := ioutil.ReadFile(filepath.Join(t.DownloadPath, name+".last-url")) lastURL, err := ioutil.ReadFile(filepath.Join(t.DownloadPath, name+".last-url"))
if err != nil { if err != nil {
@ -887,8 +893,12 @@ func hTTPProxy(host, port string) bool {
} }
func (t *TBDownloader) MirrorIsI2P() bool { func (t *TBDownloader) MirrorIsI2P() bool {
return MirrorIsI2P(t.Mirror)
}
func MirrorIsI2P(mirror string) bool {
// check if hostname is an I2P hostname // check if hostname is an I2P hostname
url, err := url.Parse(t.Mirror) url, err := url.Parse(mirror)
if err != nil { if err != nil {
return false return false
} }

View File

@ -1,9 +1,11 @@
package tbget package tbget
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -201,7 +203,52 @@ func TorrentPath() (string, string) {
return fmt.Sprintf("tor-browser%s", windowsonly), extension 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 { func TorrentDownloaded() bool {
version, err := GetTorBrowserVersionFromUpdateURL()
if err != nil {
panic(err)
}
log.Println("Tor Browser Version", version)
cmpsize := 8661000 cmpsize := 8661000
found := false found := false
if dir, err := FindSnarkDirectory(); err == nil { if dir, err := FindSnarkDirectory(); err == nil {
@ -212,10 +259,10 @@ func TorrentDownloaded() bool {
} }
prefix, suffix := TorrentPath() prefix, suffix := TorrentPath()
path = filepath.Base(path) 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 !strings.HasSuffix(path, ".torrent") {
if info.Size() > int64(cmpsize) { 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 found = true
return nil return nil
} else { } else {

2
go.mod
View File

@ -11,6 +11,8 @@ require (
golang.org/x/net v0.0.0-20211206223403-eba003a116a9 golang.org/x/net v0.0.0-20211206223403-eba003a116a9
) )
require github.com/eyedeekay/go-htmleditor v0.0.0-20220322223215-1641dd079151 // indirect
require ( require (
github.com/eyedeekay/go-I2P-jpackage v0.0.0-20220219044935-be7909c9f4c5 github.com/eyedeekay/go-I2P-jpackage v0.0.0-20220219044935-be7909c9f4c5
github.com/ybbus/jsonrpc/v2 v2.1.6 // indirect github.com/ybbus/jsonrpc/v2 v2.1.6 // indirect

2
go.sum
View File

@ -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/checki2cp v0.0.21/go.mod h1:75sGwBgnacHmxxx8RQ7BIeS0gu5Pw916gFb2c80OUTc=
github.com/eyedeekay/eephttpd v0.0.0-20190903000420-52f5a8485a4e/go.mod h1:wFPQsNBnY95LkuujFEZARo7slafRwoF0D97FFHBoZro= 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-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 h1:rnn9OlD/3+tATEZNuiMR1C84O5CX8bZL2qqgttprKrw=
github.com/eyedeekay/go-i2cp v0.0.0-20190716135428-6d41bed718b0/go.mod h1:+P0fIhkqIYjo7exMJRTlSteRMbRyHbiBiKw+YlPWk+c= 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= github.com/eyedeekay/go-i2pcontrol v0.0.0-20200110011336-510cca77e350/go.mod h1:bhIQsVpbNNXMtcoZ9UF4hLQleOjaCgKGXiRRhNc8TOA=

28
main.go
View File

@ -18,6 +18,9 @@ import (
"github.com/ncruces/zenity" "github.com/ncruces/zenity"
tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get" tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get"
tbserve "i2pgit.org/idk/i2p.plugins.tor-manager/serve" 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() flag.Parse()
if *ptop { if *ptop {
log.Println("Using p2p")
*mirror = "http://localhost:7657/i2psnark/" *mirror = "http://localhost:7657/i2psnark/"
} }
if *password != "" { if *password != "" {
@ -362,11 +366,7 @@ func main() {
} }
if *solidarity { if *solidarity {
client.Onion.UnpackSite() client.Onion.UnpackSite()
go func() { go ServeOnion()
if err := client.Onion.ListenAndServe(); err != nil {
log.Println("Onion error:", err)
}
}()
} }
go runSysTray(false) go runSysTray(false)
if err := client.Serve(); err != nil { 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) { func pathToMe() (string, error) {
ex, err := os.Executable() ex, err := os.Executable()
if err != nil { if err != nil {

View File

@ -11,6 +11,7 @@ import (
"os/exec" "os/exec"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"github.com/mitchellh/go-ps" "github.com/mitchellh/go-ps"
@ -625,3 +626,131 @@ func NewSupervisor(tbPath, lang string) *Supervisor {
Lang: lang, 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
}