-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
175 lines (147 loc) · 4.66 KB
/
main.go
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
// GLORP - Delete this code
package main
import (
"net/url"
"strings"
"flag"
"fmt"
"log"
"os"
"strconv"
"github.com/denandz/glorp/modifier"
"github.com/denandz/glorp/proxy"
"github.com/denandz/glorp/views"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// Window handles windows
type Window func() (title string, content tview.Primitive)
func main() {
// process command line flags
addr := flag.String("addr", "", "The bind address, default 0.0.0.0")
downstreamProxy := flag.String("proxy", "", "downstream proxy to use in URI format. example: socks5://127.0.0.1:9050. empty means no downstream proxy")
cert := flag.String("cert", "", "Path to a CA Certificate")
key := flag.String("key", "", "Path to the CA cert's private key")
port := flag.Uint("port", 0, "Listen port for the proxy, default 8080")
help := flag.Bool("help", false, "Show help")
flag.Parse()
if (*downstreamProxy !="") {
u, _:= url.Parse(*downstreamProxy)
if (u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "socks5") {
log.Fatal("Bad Proxy Scheme: Proxy must use URI format. example: socks5://127.0.0.1:9050.")
}
port := strings.Split(u.Host, ":")
if (len(port) < 2) {
log.Fatal("Missing Port Number: Proxy URI must include port number. example: socks5://127.0.0.1:9050.")
}
}
if *help ||
(*cert == "" && *key != "") ||
(*key == "" && *cert != "") {
flag.Usage()
os.Exit(1)
}
app := tview.NewApplication()
// create the replayview stuff
replayview := new(views.ReplayView)
replayview.Init(app)
// start the Martian proxy
config := &proxy.Config{
Addr: *addr,
Cert: *cert,
Proxy: *downstreamProxy,
Key: *key,
Port: *port,
}
proxychan := make(chan modifier.Notification, 1024)
sitemapchan := make(chan modifier.Notification, 1024)
logger := modifier.NewLogger(app, proxychan, sitemapchan)
proxy.StartProxy(logger, config)
// create the main proxy window
proxyview := new(views.ProxyView)
proxyview.Init(app, replayview, logger, proxychan)
// View for the logs, create this now so we dont miss logs
logText := tview.NewTextView()
logText.SetChangedFunc(func() {
if logText.HasFocus() {
app.Draw()
}
})
log.SetOutput(logText)
log.Println("Logger started")
Log := func() (title string, content tview.Primitive) { return "Log", logText }
// sitemap view
sitemapview := new(views.SiteMapView)
sitemapview.Init(app, proxyview.Logger, sitemapchan)
// Save/load view
saveview := new(views.SaveRestoreView)
saveview.Init(app, replayview, proxyview, sitemapview)
// Pages
pages := []Window{
proxyview.GetView,
sitemapview.GetView,
replayview.GetView,
Log,
saveview.GetView,
}
// Main layout
mainWindow := tview.NewPages()
footer := tview.NewTextView().SetDynamicColors(true).SetRegions(true).SetWrap(false)
footer.SetHighlightedFunc(func(added, removed, remaining []string) {
mainWindow.SwitchToPage(added[0]) // switching to page does not SetFocus on the page, go figure...
_, p := mainWindow.GetFrontPage()
app.SetFocus(p)
})
// Create the pages for all slides.
prevPage := func() {
slide, _ := strconv.Atoi(footer.GetHighlights()[0])
slide = (slide - 1 + len(pages)) % len(pages)
footer.Highlight(strconv.Itoa(slide)).
ScrollToHighlight()
}
nextPage := func() {
slide, _ := strconv.Atoi(footer.GetHighlights()[0])
slide = (slide + 1) % len(pages)
footer.Highlight(strconv.Itoa(slide)).
ScrollToHighlight()
}
for index, slide := range pages {
title, primitive := slide()
mainWindow.AddPage(strconv.Itoa(index), primitive, true, index == 0)
fmt.Fprintf(footer, `%d ["%d"][mediumpurple]%s[white][""] `, index+1, index, title)
}
footer.Highlight("0")
// create the main layout
layout := tview.NewFlex().SetDirection(tview.FlexRow)
layout.AddItem(mainWindow, 0, 1, false)
layout.AddItem(footer, 1, 1, false)
// add a quit modal
quitModal := tview.NewModal().
SetText("Quit Glorp?").
AddButtons([]string{"Quit", "Cancel"}).SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Quit" {
app.Stop()
} else {
mainWindow.HidePage("quitModal")
}
})
mainWindow.AddPage("quitModal", quitModal, false, false)
// Shortcuts to navigate the slides.
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlC:
mainWindow.ShowPage("quitModal")
app.SetFocus(quitModal)
return nil
case tcell.KeyCtrlN:
nextPage()
case tcell.KeyCtrlP:
prevPage()
}
return event
})
// Start the application.
if err := app.SetRoot(layout, true).EnableMouse(true).SetFocus(proxyview.Table).Run(); err != nil {
panic(err)
}
}