Skip to content

Commit

Permalink
Adding proxy utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jan 25, 2023
1 parent ee9de8d commit 4701095
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
75 changes: 75 additions & 0 deletions http/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package proxy

import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)

// IsBurp checks if the target proxy URL is burp suite
func IsBurp(proxyURL string) (bool, error) {
return getURLWithHTTPProxy("http://burpsuite/", proxyURL, func(resp *http.Response) (bool, error) {
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("unexpected status code (200 wanted): %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}

defer resp.Body.Close()

return bytes.Contains(body, []byte("Burp Suite")), nil
})
}

// ValidateOne returns the first valid proxy from a list of proxies
func ValidateOne(proxies ...string) (string, error) {
for _, proxy := range proxies {
ok, err := getURLWithHTTPProxy("https://scanme.sh", proxy, func(resp *http.Response) (bool, error) {
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("unexpected status code (200 wanted): %d", resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
defer resp.Body.Close()

return len(body) > 0, nil
})
if ok {
return proxy, err
}
}

return "", errors.New("no valid proxy found")
}

func getURLWithHTTPProxy(targetURL, proxyURL string, checkCallback func(resp *http.Response) (bool, error)) (bool, error) {
URL, err := url.Parse(proxyURL)
if err != nil {
return false, err
}
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
Proxy: http.ProxyURL(URL),
},
}

resp, err := httpClient.Get(targetURL)
if err != nil {
return false, err
}

return checkCallback(resp)
}
28 changes: 28 additions & 0 deletions http/proxy/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build proxy

package proxy

// package tests will be executed only with (running proxy is necessary):
// go test -tags proxy

import (
"testing"

"github.com/stretchr/testify/require"
)

const burpURL = "http://127.0.0.1:8080"

// a local instance of burp community is necessary
func TestIsBurp(t *testing.T) {
ok, err := IsBurp(burpURL)
require.Nil(t, err)
require.True(t, ok)
}

// a valid proxy is necessary
func TestValidateOne(t *testing.T) {
proxyURL, err := ValidateOne(burpURL)
require.Nil(t, err)
require.Equal(t, burpURL, proxyURL)
}

0 comments on commit 4701095

Please sign in to comment.