Debian Codegraph @

modules/ui/ui.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package ui
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "io/ioutil"
8 "net/http"
9 "os"
10 "path/filepath"
11 "regexp"
12 "runtime"
13
14 "github.com/bettercap/bettercap/session"
15
16 "github.com/google/go-github/github"
17
18 "github.com/evilsocket/islazy/fs"
19 "github.com/evilsocket/islazy/tui"
20 "github.com/evilsocket/islazy/zip"
21 )
22
23 var versionParser = regexp.MustCompile(`name:"ui",version:"([^"]+)"`)
24
25 type UIModule struct {
26 session.SessionModule
27 client *github.Client
28 tmpFile string
29 basePath string
30 uiPath string
31 }
32
33 func getDefaultInstallBase() string {
34 if runtime.GOOS == "windows" {
35 return filepath.Join(os.Getenv("ALLUSERSPROFILE"), "bettercap")
36 }
37 return "/usr/local/share/bettercap/"
38 }
39
40 func NewUIModule(s *session.Session) *UIModule {
41 mod := &UIModule{
42 SessionModule: session.NewSessionModule("ui", s),
43 client: github.NewClient(nil),
44 }
45
46 mod.AddParam(session.NewStringParameter("ui.basepath",
47 getDefaultInstallBase(),
48 "",
49 "UI base installation path."))
50
51 mod.AddParam(session.NewStringParameter("ui.tmpfile",
52 filepath.Join(os.TempDir(), "ui.zip"),
53 "",
54 "Temporary file to use while downloading UI updates."))
55
56 mod.AddHandler(session.NewModuleHandler("ui.version", "",
57 "Print the currently installed UI version.",
58 func(args []string) error {
59 return mod.showVersion()
60 }))
61
62 mod.AddHandler(session.NewModuleHandler("ui.update", "",
63 "Download the latest available version of the UI and install it.",
64 func(args []string) error {
65 return mod.Start()
66 }))
67
68 return mod
69 }
70
71 func (mod *UIModule) Name() string {
72 return "ui"
73 }
74
75 func (mod *UIModule) Description() string {
76 return "A module to manage bettercap's UI updates and installed version."
77 }
78
79 func (mod *UIModule) Author() string {
80 return "Simone Margaritelli <[email protected]>"
81 }
82
83 func (mod *UIModule) Configure() (err error) {
84 if err, mod.basePath = mod.StringParam("ui.basepath"); err != nil {
85 return err
86 } else {
87 mod.uiPath = filepath.Join(mod.basePath, "ui")
88 }
89
90 if err, mod.tmpFile = mod.StringParam("ui.tmpfile"); err != nil {
91 return err
92 }
93
94 return nil
95 }
96
97 func (mod *UIModule) Stop() error {
98 return nil
99 }
100
101 func (mod *UIModule) download(version, url string) error {
102 if !fs.Exists(mod.basePath) {
103 mod.Warning("creating ui install path %s ...", mod.basePath)
104 if err := os.MkdirAll(mod.basePath, os.ModePerm); err != nil {
105 return err
106 }
107 }
108
109 out, err := os.Create(mod.tmpFile)
110 if err != nil {
111 return err
112 }
113 defer out.Close()
114 defer os.Remove(mod.tmpFile)
115
116 mod.Info("downloading ui %s from %s ...", tui.Bold(version), url)
117
118 resp, err := http.Get(url)
119 if err != nil {
120 return err
121 }
122 defer resp.Body.Close()
123
124 if _, err := io.Copy(out, resp.Body); err != nil {
125 return err
126 }
127
128 if fs.Exists(mod.uiPath) {
129 mod.Warning("removing previously installed UI from %s ...", mod.uiPath)
130 if err := os.RemoveAll(mod.uiPath); err != nil {
131 return err
132 }
133 }
134
135 mod.Info("installing to %s ...", mod.uiPath)
136
137 if _, err = zip.Unzip(mod.tmpFile, mod.basePath); err != nil {
138 return err
139 }
140
141 mod.Info("installation complete, you can now run the %s (or https-ui) caplet to start the UI.", tui.Bold("http-ui"))
142
143 return nil
144 }
145
146 func (mod *UIModule) showVersion() error {
147 if err := mod.Configure(); err != nil {
148 return err
149 }
150
151 if !fs.Exists(mod.uiPath) {
152 return fmt.Errorf("path %s does not exist, ui not installed", mod.uiPath)
153 }
154
155 search := filepath.Join(mod.uiPath, "/main.*.js")
156 matches, err := filepath.Glob(search)
157 if err != nil {
158 return err
159 } else if len(matches) == 0 {
160 return fmt.Errorf("can't find any main.*.js files in %s", mod.uiPath)
161 }
162
163 for _, filename := range matches {
164 if raw, err := ioutil.ReadFile(filename); err != nil {
165 return err
166 } else if m := versionParser.FindStringSubmatch(string(raw)); m != nil {
167 version := m[1]
168 mod.Info("v%s", version)
169 return nil
170 }
171 }
172
173 return fmt.Errorf("can't parse version from %s", search)
174 }
175
176 func (mod *UIModule) Start() error {
177 if err := mod.Configure(); err != nil {
178 return err
179 } else if err := mod.SetRunning(true, nil); err != nil {
180 return err
181 }
182 defer mod.SetRunning(false, nil)
183
184 mod.Info("checking latest stable release ...")
185
186 if releases, _, err := mod.client.Repositories.ListReleases(context.Background(), "bettercap", "ui", nil); err == nil {
187 latest := releases[0]
188 for _, a := range latest.Assets {
189 if *a.Name == "ui.zip" {
190 return mod.download(*latest.TagName, *a.BrowserDownloadURL)
191 }
192 }
193 } else {
194 mod.Error("error while fetching latest release info from GitHub: %s", err)
195 }
196
197 return nil
198 }