ui.go 4.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
package ui

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"regexp"

	"github.com/bettercap/bettercap/session"

	"github.com/google/go-github/github"

	"github.com/evilsocket/islazy/fs"
	"github.com/evilsocket/islazy/tui"
	"github.com/evilsocket/islazy/zip"
)

var versionParser = regexp.MustCompile(`name:"ui",version:"([^"]+)"`)

type UIModule struct {
	session.SessionModule
	client   *github.Client
	tmpFile  string
	basePath string
	uiPath   string
}

func NewUIModule(s *session.Session) *UIModule {
	mod := &UIModule{
		SessionModule: session.NewSessionModule("ui", s),
		client:        github.NewClient(nil),
	}

	mod.AddParam(session.NewStringParameter("ui.basepath",
		"/usr/local/share/bettercap/",
		"",
		"UI base installation path."))

	mod.AddParam(session.NewStringParameter("ui.tmpfile",
		filepath.Join(os.TempDir(), "ui.zip"),
		"",
		"Temporary file to use while downloading UI updates."))

	mod.AddHandler(session.NewModuleHandler("ui.version", "",
		"Print the currently installed UI version.",
		func(args []string) error {
			return mod.showVersion()
		}))

	mod.AddHandler(session.NewModuleHandler("ui.update", "",
		"Download the latest available version of the UI and install it.",
		func(args []string) error {
			return mod.Start()
		}))

	return mod
}

func (mod *UIModule) Name() string {
	return "ui"
}

func (mod *UIModule) Description() string {
	return "A module to manage bettercap's UI updates and installed version."
}

func (mod *UIModule) Author() string {
	return "Simone Margaritelli <evilsocket@gmail.com>"
}

func (mod *UIModule) Configure() (err error) {
	if err, mod.basePath = mod.StringParam("ui.basepath"); err != nil {
		return err
	} else {
		mod.uiPath = filepath.Join(mod.basePath, "ui")
	}

	if err, mod.tmpFile = mod.StringParam("ui.tmpfile"); err != nil {
		return err
	}

	return nil
}

func (mod *UIModule) Stop() error {
	return nil
}

func (mod *UIModule) download(version, url string) error {
	if !fs.Exists(mod.basePath) {
		mod.Warning("creating ui install path %s ...", mod.basePath)
		if err := os.MkdirAll(mod.basePath, os.ModePerm); err != nil {
			return err
		}
	}

	out, err := os.Create(mod.tmpFile)
	if err != nil {
		return err
	}
	defer out.Close()
	defer os.Remove(mod.tmpFile)

	mod.Info("downloading ui %s from %s ...", tui.Bold(version), url)

	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if _, err := io.Copy(out, resp.Body); err != nil {
		return err
	}

	if fs.Exists(mod.uiPath) {
		mod.Warning("removing previously installed UI from %s ...", mod.uiPath)
		if err := os.RemoveAll(mod.uiPath); err != nil {
			return err
		}
	}

	mod.Info("installing to %s ...", mod.uiPath)

	if _, err = zip.Unzip(mod.tmpFile, mod.basePath); err != nil {
		return err
	}

	mod.Info("installation complete, you can now run the %s (or https-ui) caplet to start the UI.", tui.Bold("http-ui"))

	return nil
}

func (mod *UIModule) showVersion() error {
	if err := mod.Configure(); err != nil {
		return err
	}

	if !fs.Exists(mod.uiPath) {
		return fmt.Errorf("path %s does not exist, ui not installed", mod.uiPath)
	}

	search := filepath.Join(mod.uiPath, "/main.*.js")
	matches, err := filepath.Glob(search)
	if err != nil {
		return err
	} else if len(matches) == 0 {
		return fmt.Errorf("can't find any main.*.js files in %s", mod.uiPath)
	}

	for _, filename := range matches {
		if raw, err := ioutil.ReadFile(filename); err != nil {
			return err
		} else if m := versionParser.FindStringSubmatch(string(raw)); m != nil {
			version := m[1]
			mod.Info("v%s", version)
			return nil
		}
	}

	return fmt.Errorf("can't parse version from %s", search)
}

func (mod *UIModule) Start() error {
	if err := mod.Configure(); err != nil {
		return err
	} else if err := mod.SetRunning(true, nil); err != nil {
		return err
	}
	defer mod.SetRunning(false, nil)

	mod.Info("checking latest stable release ...")

	if releases, _, err := mod.client.Repositories.ListReleases(context.Background(), "bettercap", "ui", nil); err == nil {
		latest := releases[0]
		for _, a := range latest.Assets {
			if *a.Name == "ui.zip" {
				return mod.download(*latest.TagName, *a.BrowserDownloadURL)
			}
		}
	} else {
		mod.Error("error while fetching latest release info from GitHub: %s", err)
	}

	return nil
}