Debian Codegraph @

modules/ui/ui.go

raw symbols ◀ ▶ SCIP
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/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 "/usr/share/bettercap/",
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 "Disable in Debian. Install the package bettercap-ui",
64 func(args []string) error {
65 return mod.Start()
66 }))
•67 */
68
69 return mod
70 }
71
72 func (mod *UIModule) Name() string {
73 return "ui"
74 }
75
76 func (mod *UIModule) Description() string {
•77 return "A module to manage bettercap's UI. Disabled in Debian: use apt install bettercap-ui to install and/or update ui."
78 }
79
80 func (mod *UIModule) Author() string {
81 return "Simone Margaritelli <[email protected]>"
82 }
83
84 func (mod *UIModule) Configure() (err error) {
85 if err, mod.basePath = mod.StringParam("ui.basepath"); err != nil {
86 return err
87 } else {
88 mod.uiPath = filepath.Join(mod.basePath, "ui")
89 }
90
91 if err, mod.tmpFile = mod.StringParam("ui.tmpfile"); err != nil {
92 return err
93 }
94
95 return nil
96 }
97
98 func (mod *UIModule) Stop() error {
99 return nil
100 }
101
102 func (mod *UIModule) download(version, url string) error {
103 if !fs.Exists(mod.basePath) {
104 mod.Warning("creating ui install path %s ...", mod.basePath)
105 if err := os.MkdirAll(mod.basePath, os.ModePerm); err != nil {
106 return err
107 }
108 }
109
110 out, err := os.Create(mod.tmpFile)
111 if err != nil {
112 return err
113 }
114 defer out.Close()
115 defer os.Remove(mod.tmpFile)
116
117 mod.Info("downloading ui %s from %s ...", tui.Bold(version), url)
118
119 resp, err := http.Get(url)
120 if err != nil {
121 return err
122 }
123 defer resp.Body.Close()
124
125 if _, err := io.Copy(out, resp.Body); err != nil {
126 return err
127 }
128
129 if fs.Exists(mod.uiPath) {
130 mod.Warning("removing previously installed UI from %s ...", mod.uiPath)
131 if err := os.RemoveAll(mod.uiPath); err != nil {
132 return err
133 }
134 }
135
136 mod.Info("installing to %s ...", mod.uiPath)
137
138 if _, err = zip.Unzip(mod.tmpFile, mod.basePath); err != nil {
139 return err
140 }
141
142 mod.Info("installation complete, you can now run the %s (or https-ui) caplet to start the UI.", tui.Bold("http-ui"))
143
144 return nil
145 }
146
147 func (mod *UIModule) showVersion() error {
148 if err := mod.Configure(); err != nil {
149 return err
150 }
151
152 if !fs.Exists(mod.uiPath) {
153 return fmt.Errorf("path %s does not exist, ui not installed", mod.uiPath)
154 }
155
156 search := filepath.Join(mod.uiPath, "/main.*.js")
157 matches, err := filepath.Glob(search)
158 if err != nil {
159 return err
160 } else if len(matches) == 0 {
161 return fmt.Errorf("can't find any main.*.js files in %s", mod.uiPath)
162 }
163
164 for _, filename := range matches {
165 if raw, err := ioutil.ReadFile(filename); err != nil {
166 return err
167 } else if m := versionParser.FindStringSubmatch(string(raw)); m != nil {
168 version := m[1]
169 mod.Info("v%s", version)
170 return nil
171 }
172 }
173
174 return fmt.Errorf("can't parse version from %s", search)
175 }
176
177 func (mod *UIModule) Start() error {
178 if err := mod.Configure(); err != nil {
179 return err
180 } else if err := mod.SetRunning(true, nil); err != nil {
181 return err
182 }
183 defer mod.SetRunning(false, nil)
184
185 mod.Info("checking latest stable release ...")
186
187 if releases, _, err := mod.client.Repositories.ListReleases(context.Background(), "bettercap", "ui", nil); err == nil {
188 latest := releases[0]
189 for _, a := range latest.Assets {
190 if *a.Name == "ui.zip" {
191 return mod.download(*latest.TagName, *a.BrowserDownloadURL)
192 }
193 }
194 } else {
195 mod.Error("error while fetching latest release info from GitHub: %s", err)
196 }
197
198 return nil
199 }