From 1367cea3a3e43e1a06ea8cfc65cf30e60ba7f97f Mon Sep 17 00:00:00 2001 From: idk Date: Mon, 7 Feb 2022 15:34:45 -0500 Subject: [PATCH] Fix all the linter errors I care about --- Makefile | 5 +++ get/get.go | 65 ++++++++++++++++++++++-------------- get/getffox.go | 6 ++-- serve/json.go | 7 ++-- serve/pages.go | 1 + serve/serve.go | 7 ++++ serve/template.go | 49 +++++++++++++-------------- supervise/supervise.go | 75 ++++++++++++++++++++++++++---------------- 8 files changed, 132 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index 5b557d8..8c43c39 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,11 @@ PLUGIN=$(HOME)/.i2p/plugins/$(BINARY)-$(GOOS)-$(GOARCH) PREFIX?=/usr/local +lint: + golint supervise/*.go + golint get/*.go + golint serve/*.go + binary: go build $(ARG) -tags="netgo" -o $(BINARY)-$(GOOS)-$(GOARCH) . diff --git a/get/get.go b/get/get.go index d459bee..f9da0e8 100644 --- a/get/get.go +++ b/get/get.go @@ -27,8 +27,10 @@ import ( "golang.org/x/crypto/openpgp" ) +// WORKING_DIR is the working directory for the application. var WORKING_DIR = "" +// DefaultDir returns the default directory for the application. func DefaultDir() string { if WORKING_DIR == "" { WORKING_DIR, _ = os.Getwd() @@ -43,22 +45,27 @@ func DefaultDir() string { return wd } +// UNPACK_PATH returns the path to the unpacked files. func UNPACK_PATH() string { var UNPACK_PATH = filepath.Join(DefaultDir(), "unpack") return UNPACK_PATH } +// DOWNLOAD_PATH returns the path to the downloads. func DOWNLOAD_PATH() string { var DOWNLOAD_PATH = filepath.Join(DefaultDir(), "tor-browser") return DOWNLOAD_PATH } +// TOR_UPDATES_URL is the URL of the Tor Browser update list. const TOR_UPDATES_URL string = "https://aus1.torproject.org/torbrowser/update_3/release/downloads.json" var ( + // DefaultIETFLang is the default language for the TBDownloader. DefaultIETFLang, _ = jibber_jabber.DetectIETF() ) +// TBDownloader is a struct which manages browser updates type TBDownloader struct { UnpackPath string DownloadPath string @@ -68,9 +75,13 @@ type TBDownloader struct { Profile *embed.FS } +// OS is the operating system of the TBDownloader. var OS = "linux" + +// ARCH is the architecture of the TBDownloader. var ARCH = "64" +// NewTBDownloader returns a new TBDownloader with the given language, using the TBDownloader's OS/ARCH pair func NewTBDownloader(lang string, os, arch string, content *embed.FS) *TBDownloader { OS = os ARCH = arch @@ -85,6 +96,7 @@ func NewTBDownloader(lang string, os, arch string, content *embed.FS) *TBDownloa } } +// ServeHTTP serves the DOWNLOAD_PATH as a mirror func (t *TBDownloader) ServeHTTP(w http.ResponseWriter, r *http.Request) { r.URL.Path = strings.Replace(r.URL.Path, "..", "", -1) ext := filepath.Ext(r.URL.Path) @@ -100,6 +112,7 @@ func (t *TBDownloader) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// Serve runs ServeHTTP on an I2P listener func (t *TBDownloader) Serve() { samlistener, err := sam.I2PListener("tor-mirror", "127.0.0.1:7656", "tor-mirror") if err != nil { @@ -109,6 +122,7 @@ func (t *TBDownloader) Serve() { http.Serve(samlistener, t) } +// GetRuntimePair returns the runtime.GOOS and runtime.GOARCH pair. func (t *TBDownloader) GetRuntimePair() string { if t.OS != "" && t.ARCH != "" { return fmt.Sprintf("%s%s", t.OS, t.ARCH) @@ -137,13 +151,13 @@ func (t *TBDownloader) GetRuntimePair() string { return t.OS } -// GetUpdaterForLangFromJsonBytes returns the updater for the given language, using the TBDownloader's OS/ARCH pair +// GetUpdater returns the updater for the given language, using the TBDownloader's OS/ARCH pair // and only the defaults. It returns the URL of the updater and the detatched signature, or an error if one is not found. func (t *TBDownloader) GetUpdater() (string, string, error) { return t.GetUpdaterForLang(t.Lang) } -// GetUpdaterForLangFromJsonBytes returns the updater for the given language, using the TBDownloader's OS/ARCH pair +// GetUpdaterForLang returns the updater for the given language, using the TBDownloader's OS/ARCH pair // it expects ietf to be a language. It returns the URL of the updater and the detatched signature, or an error if one is not found. func (t *TBDownloader) GetUpdaterForLang(ietf string) (string, string, error) { jsonText, err := http.Get(TOR_UPDATES_URL) @@ -151,22 +165,22 @@ func (t *TBDownloader) GetUpdaterForLang(ietf string) (string, string, error) { return "", "", fmt.Errorf("t.GetUpdaterForLang: %s", err) } defer jsonText.Body.Close() - return t.GetUpdaterForLangFromJson(jsonText.Body, ietf) + return t.GetUpdaterForLangFromJSON(jsonText.Body, ietf) } -// GetUpdaterForLangFromJson returns the updater for the given language, using the TBDownloader's OS/ARCH pair +// GetUpdaterForLangFromJSON returns the updater for the given language, using the TBDownloader's OS/ARCH pair // it expects body to be a valid json reader and ietf to be a language. It returns the URL of the updater and // the detatched signature, or an error if one is not found. -func (t *TBDownloader) GetUpdaterForLangFromJson(body io.ReadCloser, ietf string) (string, string, error) { +func (t *TBDownloader) GetUpdaterForLangFromJSON(body io.ReadCloser, ietf string) (string, string, error) { jsonBytes, err := io.ReadAll(body) if err != nil { - return "", "", fmt.Errorf("t.GetUpdaterForLangFromJson: %s", err) + return "", "", fmt.Errorf("t.GetUpdaterForLangFromJSON: %s", err) } t.MakeTBDirectory() if err = ioutil.WriteFile(filepath.Join(t.DownloadPath, "downloads.json"), jsonBytes, 0644); err != nil { - return "", "", fmt.Errorf("t.GetUpdaterForLangFromJson: %s", err) + return "", "", fmt.Errorf("t.GetUpdaterForLangFromJSON: %s", err) } - return t.GetUpdaterForLangFromJsonBytes(jsonBytes, ietf) + return t.GetUpdaterForLangFromJSONBytes(jsonBytes, ietf) } // Log logs things if Verbose is true. @@ -194,38 +208,37 @@ func (t *TBDownloader) MakeTBDirectory() { } } -// GetUpdaterForLangFromJsonBytes returns the updater for the given language, using the TBDownloader's OS/ARCH pair +// GetUpdaterForLangFromJSONBytes returns the updater for the given language, using the TBDownloader's OS/ARCH pair // it expects jsonBytes to be a valid json string and ietf to be a language. It returns the URL of the updater and // the detatched signature, or an error if one is not found. -func (t *TBDownloader) GetUpdaterForLangFromJsonBytes(jsonBytes []byte, ietf string) (string, string, error) { +func (t *TBDownloader) GetUpdaterForLangFromJSONBytes(jsonBytes []byte, ietf string) (string, string, error) { t.MakeTBDirectory() var dat map[string]interface{} - t.Log("GetUpdaterForLangFromJsonBytes()", "Parsing JSON") + t.Log("GetUpdaterForLangFromJSONBytes()", "Parsing JSON") if err := json.Unmarshal(jsonBytes, &dat); err != nil { return "", "", fmt.Errorf("func (t *TBDownloader)Name: %s", err) } - t.Log("GetUpdaterForLangFromJsonBytes()", "Parsing JSON complete") + t.Log("GetUpdaterForLangFromJSONBytes()", "Parsing JSON complete") if platform, ok := dat["downloads"]; ok { rtp := t.GetRuntimePair() if updater, ok := platform.(map[string]interface{})[rtp]; ok { if langUpdater, ok := updater.(map[string]interface{})[ietf]; ok { - t.Log("GetUpdaterForLangFromJsonBytes()", "Found updater for language") + t.Log("GetUpdaterForLangFromJSONBytes()", "Found updater for language") return langUpdater.(map[string]interface{})["binary"].(string), langUpdater.(map[string]interface{})["sig"].(string), nil } // If we didn't find the language, try splitting at the hyphen lang := strings.Split(ietf, "-")[0] if langUpdater, ok := updater.(map[string]interface{})[lang]; ok { - t.Log("GetUpdaterForLangFromJsonBytes()", "Found updater for backup language") + t.Log("GetUpdaterForLangFromJSONBytes()", "Found updater for backup language") return langUpdater.(map[string]interface{})["binary"].(string), langUpdater.(map[string]interface{})["sig"].(string), nil } // If we didn't find the language after splitting at the hyphen, try the default - t.Log("GetUpdaterForLangFromJsonBytes()", "Last attempt, trying default language") - return t.GetUpdaterForLangFromJsonBytes(jsonBytes, t.Lang) - } else { - return "", "", fmt.Errorf("t.GetUpdaterForLangFromJsonBytes: no updater for platform %s", rtp) + t.Log("GetUpdaterForLangFromJSONBytes()", "Last attempt, trying default language") + return t.GetUpdaterForLangFromJSONBytes(jsonBytes, t.Lang) } + return "", "", fmt.Errorf("t.GetUpdaterForLangFromJSONBytes: no updater for platform %s", rtp) } - return "", "", fmt.Errorf("t.GetUpdaterForLangFromJsonBytes: %s", ietf) + return "", "", fmt.Errorf("t.GetUpdaterForLangFromJSONBytes: %s", ietf) } // SingleFileDownload downloads a single file from the given URL to the given path. @@ -268,11 +281,11 @@ func (t *TBDownloader) BotherToDownload(url, name string) bool { return true } defer ioutil.WriteFile(filepath.Join(t.DownloadPath, name+".last-url"), []byte(url), 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 { return true } - if string(lastUrl) == url { + if string(lastURL) == url { return false } return true @@ -319,7 +332,7 @@ func (t *TBDownloader) DownloadUpdaterForLang(ietf string) (string, string, erro return binpath, sigpath, nil } -// BrowserDirectory returns the path to the directory where the browser is installed. +// BrowserDir returns the path to the directory where the browser is installed. func (t *TBDownloader) BrowserDir() string { return filepath.Join(t.UnpackPath, "tor-browser_"+t.Lang) } @@ -438,10 +451,12 @@ func (t *TBDownloader) BoolCheckSignature(binpath, sigpath string) bool { return err == nil } +// TestHTTPDefaultProxy returns true if the I2P proxy is up or blocks until it is. func TestHTTPDefaultProxy() bool { return TestHTTPProxy("127.0.0.1", "4444") } +// Seconds increments the seconds and displays the number of seconds every 10 seconds func Seconds(now int) int { time.Sleep(time.Second) if now == 10 { @@ -450,6 +465,7 @@ func Seconds(now int) int { return now + 1 } +// TestHTTPBackupProxy returns true if the I2P backup proxy is up or blocks until it is. func TestHTTPBackupProxy() bool { now := 0 limit := 0 @@ -472,6 +488,7 @@ func TestHTTPBackupProxy() bool { return false } +// TestHTTPProxy returns true if the proxy at host:port is up or blocks until it is. func TestHTTPProxy(host, port string) bool { now := 0 limit := 0 @@ -494,11 +511,11 @@ func TestHTTPProxy(host, port string) bool { } func hTTPProxy(host, port string) bool { - proxyUrl, err := url.Parse("http://" + host + ":" + port) + proxyURL, err := url.Parse("http://" + host + ":" + port) if err != nil { log.Panic(err) } - myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}} + myClient := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}} resp, err := myClient.Get("http://proxy.i2p/") if err == nil { defer resp.Body.Close() diff --git a/get/getffox.go b/get/getffox.go index 50fb4e6..9bcba0f 100644 --- a/get/getffox.go +++ b/get/getffox.go @@ -60,7 +60,7 @@ func (t *TBDownloader) SendFirefoxVersionHEADRequest() (string, error) { return resp.Header.Get("Location"), nil } -// ExtactFirefoxVersion extracts the Firefox version from the updater URL +// ExtractFirefoxVersion extracts the Firefox version from the updater URL func (t *TBDownloader) ExtractFirefoxVersion() (string, error) { url, err := t.SendFirefoxVersionHEADRequest() if err != nil { @@ -93,7 +93,7 @@ func (t *TBDownloader) NamePerPlatformFirefox(ietf string) string { return fmt.Sprintf("firefox%s-%s-%s.%s", windowsonly, t.GetRuntimePair(), ietf, extension) } -// FirefoxBrowserDirectory returns the path to the directory where the Firefox browser is installed. +// FirefoxBrowserDir returns the path to the directory where the Firefox browser is installed. func (t *TBDownloader) FirefoxBrowserDir() string { return filepath.Join(t.UnpackPath, "firefox_"+t.Lang) } @@ -205,7 +205,7 @@ func (t *TBDownloader) DownloadFirefoxUpdaterForLang(ietf string) (string, strin return binpath, sigpath, nil } -// CheckSignature checks the signature of the updater. +// CheckFirefoxSignature checks the signature of the updater. // it returns an error if one is encountered. If not, it // runs the updater and returns an error if one is encountered. func (t *TBDownloader) CheckFirefoxSignature(binpath, sigpath string) (string, error) { diff --git a/serve/json.go b/serve/json.go index 9d4f06f..9d76267 100644 --- a/serve/json.go +++ b/serve/json.go @@ -16,7 +16,7 @@ func (m *Client) generateMirrorJSON() (map[string]interface{}, error) { if err != nil { return nil, fmt.Errorf("GenerateMirrorJSON: %s", err) } - binpath, _, err := m.TBD.GetUpdaterForLangFromJsonBytes(preBytes, "en-US") + binpath, _, err := m.TBD.GetUpdaterForLangFromJSONBytes(preBytes, "en-US") if err != nil { return nil, fmt.Errorf("GenerateMirrorJSON: %s", err) } @@ -31,6 +31,7 @@ func (m *Client) generateMirrorJSON() (map[string]interface{}, error) { return JSON, nil } +// Hostname Returns the hostname of the client, if it has one. func (m *Client) Hostname() string { if !strings.HasSuffix(m.hostname, "/") { return m.hostname + "/" @@ -38,6 +39,7 @@ func (m *Client) Hostname() string { return m.hostname } +// GenerateMirrorJSON generates the JSON file for the mirror. func (m *Client) GenerateMirrorJSON() (string, error) { JSON, err := m.generateMirrorJSON() if err != nil { @@ -48,7 +50,7 @@ func (m *Client) GenerateMirrorJSON() (string, error) { if err != nil { return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", err) } - binpath, _, err := m.TBD.GetUpdaterForLangFromJsonBytes(preBytes, "en-US") + binpath, _, err := m.TBD.GetUpdaterForLangFromJSONBytes(preBytes, "en-US") if err != nil { return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", err) } @@ -77,6 +79,7 @@ func (m *Client) GenerateMirrorJSON() (string, error) { return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", "No downloads found") } +// GenerateReplaceString generates the string to replace in the JSON file. func GenerateReplaceString(urlparts []string) string { replaceString := "" for _, val := range urlparts { diff --git a/serve/pages.go b/serve/pages.go index 48c52d3..685b2e7 100644 --- a/serve/pages.go +++ b/serve/pages.go @@ -7,6 +7,7 @@ import ( "strings" ) +// Page generates the HTML for the panel. func (m *Client) Page() (string, error) { htmlbytes := htmlhead diff --git a/serve/serve.go b/serve/serve.go index 5367038..cce5684 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -15,6 +15,7 @@ import ( TBSupervise "i2pgit.org/idk/i2p.plugins.tor-manager/supervise" ) +// Client manages and supervises a Tor Browser instance. type Client struct { hostname string TBD *tbget.TBDownloader @@ -24,6 +25,7 @@ type Client struct { Port int } +// NewClient creates a new Client. func NewClient(verbose bool, lang string, os string, arch string, content *embed.FS) (*Client, error) { m := &Client{ TBD: tbget.NewTBDownloader(lang, os, arch, content), @@ -44,6 +46,7 @@ func NewClient(verbose bool, lang string, os string, arch string, content *embed return m, nil } +// GetHost returns the hostname of the client. func (m *Client) GetHost() string { if m.Host == "" { m.Host = "127.0.0.1" @@ -51,6 +54,7 @@ func (m *Client) GetHost() string { return m.Host } +// GetPort returns the port of the client. func (m *Client) GetPort() string { if m.Port == 0 { m.Port = 7695 @@ -58,10 +62,12 @@ func (m *Client) GetPort() string { return strconv.Itoa(m.Port) } +// GetAddress returns the address of the client. func (m *Client) GetAddress() string { return m.GetHost() + ":" + m.GetPort() } +// ServeHTTP handles HTTP requests. func (m *Client) ServeHTTP(rw http.ResponseWriter, rq *http.Request) { path := strings.Replace(rq.URL.Path, "..", "", -1) rq.URL.Path = path @@ -117,6 +123,7 @@ func (m *Client) ServeHTTP(rw http.ResponseWriter, rq *http.Request) { } +// Serve serve the control panel locally func (m *Client) Serve() error { //http.Handle("/", m) mirrorjson, err := m.GenerateMirrorJSON() diff --git a/serve/template.go b/serve/template.go index 2996e3b..4d489b5 100644 --- a/serve/template.go +++ b/serve/template.go @@ -61,6 +61,7 @@ var tstopped string = ` var torstopped []byte = []byte(tstopped) +// PageHTML returns the HTML for the page heading func (m *Client) PageHTML() []byte { dir := filepath.Dir(m.TBD.DownloadPath) mdpath := filepath.Join(dir, m.TBD.Lang, "index.md") @@ -73,6 +74,7 @@ func (m *Client) PageHTML() []byte { return htmlbytes } +// TorOnStatusHTML returns the HTML for "Tor Status" section the page func (m *Client) TorOnStatusHTML(ours bool) []byte { dir := filepath.Dir(m.TBD.DownloadPath) if ours { @@ -81,23 +83,21 @@ func (m *Client) TorOnStatusHTML(ours bool) []byte { if err != nil { htmlbytes := blackfriday.MarkdownCommon(torstop) return htmlbytes - } else { - htmlbytes := blackfriday.MarkdownCommon(torbytes) - return htmlbytes - } - } else { - mdpath := filepath.Join(dir, m.TBD.Lang, "torstarted.md") - toron, err := ioutil.ReadFile(mdpath) - if err != nil { - htmlbytes := blackfriday.MarkdownCommon(torrunning) - return htmlbytes - } else { - htmlbytes := blackfriday.MarkdownCommon(toron) - return htmlbytes } + htmlbytes := blackfriday.MarkdownCommon(torbytes) + return htmlbytes } + mdpath := filepath.Join(dir, m.TBD.Lang, "torstarted.md") + toron, err := ioutil.ReadFile(mdpath) + if err != nil { + htmlbytes := blackfriday.MarkdownCommon(torrunning) + return htmlbytes + } + htmlbytes := blackfriday.MarkdownCommon(toron) + return htmlbytes } +// TorOffStatusHTML returns the HTML for "Tor Status" section the page func (m *Client) TorOffStatusHTML(ours bool) []byte { dir := filepath.Dir(m.TBD.DownloadPath) if ours { @@ -106,19 +106,16 @@ func (m *Client) TorOffStatusHTML(ours bool) []byte { if err != nil { htmlbytes := blackfriday.MarkdownCommon(torstart) return htmlbytes - } else { - htmlbytes := blackfriday.MarkdownCommon(torbytes) - return htmlbytes - } - } else { - mdpath := filepath.Join(dir, m.TBD.Lang, "torstopped.md") - toroff, err := ioutil.ReadFile(mdpath) - if err != nil { - htmlbytes := blackfriday.MarkdownCommon(torstopped) - return htmlbytes - } else { - htmlbytes := blackfriday.MarkdownCommon(toroff) - return htmlbytes } + htmlbytes := blackfriday.MarkdownCommon(torbytes) + return htmlbytes } + mdpath := filepath.Join(dir, m.TBD.Lang, "torstopped.md") + toroff, err := ioutil.ReadFile(mdpath) + if err != nil { + htmlbytes := blackfriday.MarkdownCommon(torstopped) + return htmlbytes + } + htmlbytes := blackfriday.MarkdownCommon(toroff) + return htmlbytes } diff --git a/supervise/supervise.go b/supervise/supervise.go index 86cf49a..5b62b6d 100644 --- a/supervise/supervise.go +++ b/supervise/supervise.go @@ -18,17 +18,23 @@ import ( tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get" ) +// UNPACK_URL is the URL to place to unpack the Browser Bundle var UNPACK_URL = tbget.UNPACK_PATH + +// DEFAULT_TB_LANG is the default language to use for the Tor Browser Bundle var DEFAULT_TB_LANG = tbget.DefaultIETFLang +// OS returns the current OS func OS() string { return tbget.OS } +// ARCH returns the current architecture func ARCH() string { return tbget.ARCH } +// Supervisor is the main struct for the Tor Browser Bundle Supervisor type Supervisor struct { UnpackPath string Lang string @@ -39,6 +45,7 @@ type Supervisor struct { PassThroughArgs []string } +// PTAS is the validator for the pass-through arguments func (s *Supervisor) PTAS() []string { // loop over the arguments and make sure that we remove any --profile, -P args // and blank them out. @@ -57,10 +64,12 @@ func (s *Supervisor) PTAS() []string { return args } +// TBPath returns the path to the Tor Browser Bundle launcher func (s *Supervisor) TBPath() string { return filepath.Join(s.UnpackPath, "Browser", "start-tor-browser") } +// FirefoxPath returns the path to the Firefox executable inside Tor Browser func (s *Supervisor) FirefoxPath() string { switch OS() { case "linux": @@ -72,18 +81,22 @@ func (s *Supervisor) FirefoxPath() string { } } +// TBDirectory returns the path to the Tor Browser Bundle directory func (s *Supervisor) TBDirectory() string { return filepath.Join(s.UnpackPath, "Browser") } +// TorPath returns the path to the Tor executable func (s *Supervisor) TorPath() string { return filepath.Join(s.UnpackPath, "Browser", "TorBrowser", "Tor", "tor") } +// TorDataPath returns the path to the Tor Browser Bundle Data directory func (s *Supervisor) TorDataPath() string { return filepath.Join(s.UnpackPath, "Browser", "TorBrowser", "Data") } +// I2PProfilePath returns the path to the I2P profile func (s *Supervisor) I2PProfilePath() string { fp := filepath.Join(filepath.Dir(s.UnpackPath), ".i2p.firefox") if !tbget.FileExists(fp) { @@ -97,22 +110,23 @@ func (s *Supervisor) I2PProfilePath() string { return fp } +// I2PDataPath returns the path to the I2P data directory func (s *Supervisor) I2PDataPath() string { fp := s.I2PProfilePath() up := filepath.Join(filepath.Dir(s.UnpackPath), "i2p.firefox") if tbget.FileExists(up) { return up - } else { - log.Printf("i2p workdir not found at %s, copying", up) - if s.Profile != nil { - if err := cp.Copy(fp, up); err != nil { - log.Fatal(err) - } - } - return up } + log.Printf("i2p workdir not found at %s, copying", up) + if s.Profile != nil { + if err := cp.Copy(fp, up); err != nil { + log.Fatal(err) + } + } + return up } +// UnpackI2PData unpacks the I2P data into the s.UnpackPath func (s *Supervisor) UnpackI2PData() error { return fs.WalkDir(s.Profile, ".", func(embedpath string, d fs.DirEntry, err error) error { fp := filepath.Join(filepath.Dir(s.UnpackPath), ".i2p.firefox") @@ -137,22 +151,23 @@ func (s *Supervisor) UnpackI2PData() error { }) } +// I2PAppDataPath returns the path to the I2P application data directory func (s *Supervisor) I2PAppDataPath() string { fp := s.I2PProfilePath() up := filepath.Join(filepath.Dir(s.UnpackPath), "i2p.firefox.config") if tbget.FileExists(up) { return up - } else { - log.Printf("i2p workdir not found at %s, copying", up) - if s.Profile != nil { - if err := cp.Copy(fp, up); err != nil { - log.Fatal(err) - } - } - return up } + log.Printf("i2p workdir not found at %s, copying", up) + if s.Profile != nil { + if err := cp.Copy(fp, up); err != nil { + log.Fatal(err) + } + } + return up } +// UnpackI2PAppData unpacks the I2P application data into the s.UnpackPath func (s *Supervisor) UnpackI2PAppData() error { return fs.WalkDir(s.Profile, ".", func(embedpath string, d fs.DirEntry, err error) error { fp := filepath.Join(filepath.Dir(s.UnpackPath), ".i2p.firefox.config") @@ -187,6 +202,7 @@ func (s *Supervisor) tbbail() error { return nil } +// RunTBWithLang runs the Tor Browser with the given language func (s *Supervisor) RunTBWithLang() error { tbget.ARCH = ARCH() if s.Lang == "" { @@ -211,10 +227,9 @@ func (s *Supervisor) RunTBWithLang() error { s.tbcmd.Stdout = os.Stdout s.tbcmd.Stderr = os.Stderr return s.tbcmd.Run() - } else { - log.Println("tor browser not found at", s.TBPath()) - return fmt.Errorf("tor browser not found at %s", s.TBPath()) } + log.Println("tor browser not found at", s.TBPath()) + return fmt.Errorf("tor browser not found at %s", s.TBPath()) case "darwin": s.tbcmd = exec.Command("/usr/bin/env", "open", "-a", "\"Tor Browser.app\"") s.tbcmd.Dir = s.TBDirectory() @@ -242,6 +257,7 @@ func (s *Supervisor) ibbail() error { return nil } +// RunI2PBWithLang runs the I2P Browser with the given language func (s *Supervisor) RunI2PBWithLang() error { tbget.ARCH = ARCH() if s.Lang == "" { @@ -266,10 +282,9 @@ func (s *Supervisor) RunI2PBWithLang() error { s.ibcmd.Stdout = os.Stdout s.ibcmd.Stderr = os.Stderr return s.ibcmd.Run() - } else { - log.Println("tor browser not found at", s.FirefoxPath()) - return fmt.Errorf("tor browser not found at %s", s.FirefoxPath()) } + log.Println("tor browser not found at", s.FirefoxPath()) + return fmt.Errorf("tor browser not found at %s", s.FirefoxPath()) case "darwin": s.ibcmd = exec.Command("/usr/bin/env", "open", "-a", "\"Tor Browser.app\"") s.ibcmd.Dir = s.TBDirectory() @@ -289,6 +304,7 @@ func (s *Supervisor) RunI2PBWithLang() error { return nil } +// RunI2PBAppWithLang runs the I2P Browser with the given language func (s *Supervisor) RunI2PBAppWithLang() error { tbget.ARCH = ARCH() if s.Lang == "" { @@ -313,10 +329,9 @@ func (s *Supervisor) RunI2PBAppWithLang() error { s.ibcmd.Stdout = os.Stdout s.ibcmd.Stderr = os.Stderr return s.ibcmd.Run() - } else { - log.Println("tor browser not found at", s.FirefoxPath()) - return fmt.Errorf("tor browser not found at %s", s.FirefoxPath()) } + log.Println("tor browser not found at", s.FirefoxPath()) + return fmt.Errorf("tor browser not found at %s", s.FirefoxPath()) case "darwin": s.ibcmd = exec.Command("/usr/bin/env", "open", "-a", "\"Tor Browser.app\"") s.ibcmd.Dir = s.TBDirectory() @@ -354,6 +369,7 @@ func (s *Supervisor) torbail() error { return nil } +// RunTorWithLang runs the Tor Exe with the given language func (s *Supervisor) RunTorWithLang() error { tbget.ARCH = ARCH() if s.Lang == "" { @@ -375,10 +391,9 @@ func (s *Supervisor) RunTorWithLang() error { s.torcmd.Stdout = os.Stdout s.torcmd.Stderr = os.Stderr return s.torcmd.Run() - } else { - log.Println("tor not found at", s.TorPath()) - return fmt.Errorf("tor not found at %s", s.TorPath()) } + log.Println("tor not found at", s.TorPath()) + return fmt.Errorf("tor not found at %s", s.TorPath()) case "darwin": s.torcmd = exec.Command("/usr/bin/env", "open", "-a", "\"Tor Browser.app\"") s.torcmd.Dir = s.TBDirectory() @@ -394,10 +409,13 @@ func (s *Supervisor) RunTorWithLang() error { return nil } +// StopTor stops tor func (s *Supervisor) StopTor() error { return s.torcmd.Process.Kill() } +// TorIsAlive returns true,true if tor is alive and belongs to us, true,false +// if it's alive and doesn't belong to us, false,false if no Tor can be found func (s *Supervisor) TorIsAlive() (bool, bool) { _, err := net.Listen("TCP", "127.0.0.1:9050") if err != nil { @@ -422,6 +440,7 @@ func (s *Supervisor) TorIsAlive() (bool, bool) { return false, true } +// NewSupervisor creates a new supervisor func NewSupervisor(tbPath, lang string) *Supervisor { return &Supervisor{ UnpackPath: tbPath,