Fix comments
This commit is contained in:
parent
cad33f7d09
commit
3f79d04161
26
capture.go
26
capture.go
|
@ -9,20 +9,20 @@ import (
|
|||
|
||||
var captureID int
|
||||
|
||||
// CaptureService handles captures
|
||||
// CaptureService handles captures.
|
||||
type CaptureService struct {
|
||||
items []Capture
|
||||
mu sync.RWMutex
|
||||
maxItems int
|
||||
updated chan struct{} // signals any change in "items"
|
||||
updated chan struct{} // signals any change in "items".
|
||||
}
|
||||
|
||||
// Capture is our traffic data
|
||||
// Capture is our traffic data.
|
||||
type Capture struct {
|
||||
ID int
|
||||
Req Req
|
||||
Res Res
|
||||
// Elapsed time of the request, in milliseconds
|
||||
// Elapsed time of the request, in milliseconds.
|
||||
Elapsed time.Duration
|
||||
}
|
||||
|
||||
|
@ -43,14 +43,14 @@ type Res struct {
|
|||
Body []byte
|
||||
}
|
||||
|
||||
// CaptureInfo is the capture info shown in the dashboard
|
||||
// CaptureInfo is the capture info shown in the dashboard.
|
||||
type CaptureInfo struct {
|
||||
Request string `json:"request"`
|
||||
Response string `json:"response"`
|
||||
Curl string `json:"curl"`
|
||||
}
|
||||
|
||||
// DashboardItem is an item in the dashboard's list
|
||||
// DashboardItem is an item in the dashboard's list.
|
||||
type DashboardItem struct {
|
||||
ID int `json:"id"`
|
||||
Path string `json:"path"`
|
||||
|
@ -60,7 +60,7 @@ type DashboardItem struct {
|
|||
Elapsed time.Duration `json:"elapsed"`
|
||||
}
|
||||
|
||||
// NewCaptureService creates a new service of captures
|
||||
// NewCaptureService creates a new service of captures.
|
||||
func NewCaptureService(maxItems int) *CaptureService {
|
||||
return &CaptureService{
|
||||
maxItems: maxItems,
|
||||
|
@ -68,7 +68,7 @@ func NewCaptureService(maxItems int) *CaptureService {
|
|||
}
|
||||
}
|
||||
|
||||
// Insert inserts a new capture
|
||||
// Insert inserts a new capture.
|
||||
func (s *CaptureService) Insert(capture Capture) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
@ -82,7 +82,7 @@ func (s *CaptureService) Insert(capture Capture) {
|
|||
s.signalsUpdate()
|
||||
}
|
||||
|
||||
// Find finds a capture by its id
|
||||
// Find finds a capture by its ID.
|
||||
func (s *CaptureService) Find(captureID string) *Capture {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
@ -96,7 +96,7 @@ func (s *CaptureService) Find(captureID string) *Capture {
|
|||
return nil
|
||||
}
|
||||
|
||||
// RemoveAll removes all the captures
|
||||
// RemoveAll removes all the captures.
|
||||
func (s *CaptureService) RemoveAll() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
@ -105,7 +105,7 @@ func (s *CaptureService) RemoveAll() {
|
|||
s.signalsUpdate()
|
||||
}
|
||||
|
||||
// DashboardItems returns the dashboard's list of items
|
||||
// DashboardItems returns the dashboard's list of items.
|
||||
func (s *CaptureService) DashboardItems() []DashboardItem {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
@ -123,14 +123,14 @@ func (s *CaptureService) DashboardItems() []DashboardItem {
|
|||
return metadatas
|
||||
}
|
||||
|
||||
// signalsUpdate fires an update signal
|
||||
// signalsUpdate fires an update signal.
|
||||
func (s *CaptureService) signalsUpdate() {
|
||||
close(s.updated)
|
||||
s.updated = make(chan struct{})
|
||||
}
|
||||
|
||||
// Updated signals any change in this service,
|
||||
// like inserting or removing captures
|
||||
// like inserting or removing captures.
|
||||
func (s *CaptureService) Updated() <-chan struct{} {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"flag"
|
||||
)
|
||||
|
||||
// Config has all the configuration parsed from the command line
|
||||
// Config has all the configuration parsed from the command line.
|
||||
type Config struct {
|
||||
TargetURL string
|
||||
ProxyPort string
|
||||
|
@ -12,7 +12,7 @@ type Config struct {
|
|||
MaxCaptures int
|
||||
}
|
||||
|
||||
// ReadConfig reads the arguments from the command line
|
||||
// ReadConfig reads the arguments from the command line.
|
||||
func ReadConfig() Config {
|
||||
targetURL := flag.String("url", "https://jsonplaceholder.typicode.com", "Required. Set the url you want to proxy")
|
||||
proxyPort := flag.String("port", "9000", "Set the proxy port")
|
||||
|
|
27
main.go
27
main.go
|
@ -21,7 +21,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// StatusInternalProxyError is any unknown proxy error
|
||||
// StatusInternalProxyError is any unknown proxy error.
|
||||
const StatusInternalProxyError = 999
|
||||
|
||||
//go:embed dashboard.html
|
||||
|
@ -55,7 +55,7 @@ func NewDashboardHandler(h http.HandlerFunc, srv *CaptureService, cfg Config) ht
|
|||
}
|
||||
|
||||
// NewDashboardConnHandler opens an event stream connection with the dashboard
|
||||
// so that it is notified everytime a new capture arrives
|
||||
// so that it is notified everytime a new capture arrives.
|
||||
func NewDashboardConnHandler(srv *CaptureService, cfg Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
if _, ok := w.(http.Flusher); !ok {
|
||||
|
@ -86,7 +86,7 @@ func NewDashboardConnHandler(srv *CaptureService, cfg Config) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// NewDashboardClearHandler clears all the captures
|
||||
// NewDashboardClearHandler clears all the captures.
|
||||
func NewDashboardClearHandler(srv *CaptureService) http.HandlerFunc {
|
||||
return func(rw http.ResponseWriter, req *http.Request) {
|
||||
srv.RemoveAll()
|
||||
|
@ -94,7 +94,7 @@ func NewDashboardClearHandler(srv *CaptureService) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// NewDashboardHTMLHandler returns the dashboard html page
|
||||
// NewDashboardHTMLHandler returns the dashboard html page.
|
||||
func NewDashboardHTMLHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
|
@ -111,13 +111,13 @@ func NewDashboardHTMLHandler() http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// NewDashboardRetryHandler retries a request
|
||||
// NewDashboardRetryHandler retries a request.
|
||||
func NewDashboardRetryHandler(srv *CaptureService, next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(rw http.ResponseWriter, req *http.Request) {
|
||||
id := path.Base(req.URL.Path)
|
||||
capture := srv.Find(id)
|
||||
|
||||
// creates a new request based on the current one
|
||||
// Create a new request based on the current one.
|
||||
r, _ := http.NewRequest(capture.Req.Method, capture.Req.Url, bytes.NewReader(capture.Req.Body))
|
||||
r.Header = capture.Req.Header
|
||||
|
||||
|
@ -125,7 +125,7 @@ func NewDashboardRetryHandler(srv *CaptureService, next http.HandlerFunc) http.H
|
|||
}
|
||||
}
|
||||
|
||||
// NewDashboardInfoHandler returns the full capture info
|
||||
// NewDashboardInfoHandler returns the full capture info.
|
||||
func NewDashboardInfoHandler(srv *CaptureService) http.HandlerFunc {
|
||||
return func(rw http.ResponseWriter, req *http.Request) {
|
||||
id := path.Base(req.URL.Path)
|
||||
|
@ -135,7 +135,8 @@ func NewDashboardInfoHandler(srv *CaptureService) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// NewPluginHandler loads plugin files in the current directory. They are loaded sorted by filename.
|
||||
// NewPluginHandler loads plugin files in the current directory.
|
||||
// They are loaded sorted by filename.
|
||||
func NewPluginHandler(next http.HandlerFunc) http.HandlerFunc {
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
|
@ -175,7 +176,7 @@ func NewPluginHandler(next http.HandlerFunc) http.HandlerFunc {
|
|||
return next
|
||||
}
|
||||
|
||||
// NewRecorderHandler records all the traffic data
|
||||
// NewRecorderHandler records all the traffic data.
|
||||
func NewRecorderHandler(srv *CaptureService, next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
|
@ -225,7 +226,7 @@ func NewRecorderHandler(srv *CaptureService, next http.HandlerFunc) http.Handler
|
|||
}
|
||||
}
|
||||
|
||||
// NewProxyHandler is the reverse proxy handler
|
||||
// NewProxyHandler is the reverse proxy handler.
|
||||
func NewProxyHandler(URL string) http.HandlerFunc {
|
||||
url, _ := url.Parse(URL)
|
||||
proxy := httputil.NewSingleHostReverseProxy(url)
|
||||
|
@ -283,13 +284,13 @@ func dumpBody(dst *strings.Builder, header http.Header, body []byte) {
|
|||
|
||||
func dumpCurl(req Req) string {
|
||||
var b strings.Builder
|
||||
// build cmd
|
||||
// Build cmd.
|
||||
fmt.Fprintf(&b, "curl -X %s %s", req.Method, req.Url)
|
||||
// build headers
|
||||
// Build headers.
|
||||
for k, v := range req.Header {
|
||||
fmt.Fprintf(&b, " \\\n -H '%s: %s'", k, strings.Join(v, " "))
|
||||
}
|
||||
// build body
|
||||
// Build body.
|
||||
if len(req.Body) > 0 {
|
||||
fmt.Fprintf(&b, " \\\n -d '%s'", req.Body)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue