breach/breacher/proxy.go

66 lines
1.6 KiB
Go

package breacher
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"github.com/spf13/cobra"
)
var proxyCmd = &cobra.Command{
Use: "proxy [from address] [to url]",
Short: "reverse proxy to url",
Long: `reverse proxy,
ex.
* breacher reverse_proxy :8080 http://vpn.kumoly.io/
* breacher reverse_proxy :8080 https://kumoly.io/`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
targetUrl, err := url.Parse(args[1])
if err != nil {
log.Fatalln(err)
}
director := func(req *http.Request) {
req.URL.Scheme = targetUrl.Scheme
req.URL.Host = targetUrl.Host
req.Host = targetUrl.Host
}
proxy := httputil.NewSingleHostReverseProxy(targetUrl)
proxy.Director = director
if err := http.ListenAndServe(args[0], proxy); err != nil {
log.Fatalln(err)
}
},
}
// func rewriteBody(resp *http.Response) (err error) {
// var reader io.ReadCloser
// switch resp.Header.Get("Content-Encoding") {
// case "gzip":
// reader, err = gzip.NewReader(resp.Body)
// defer reader.Close()
// default:
// reader = resp.Body
// }
// delete(resp.Header, "Content-Encoding")
// b, err := ioutil.ReadAll(reader)
// if err != nil {
// return err
// }
// err = resp.Body.Close()
// if err != nil {
// return err
// }
// b = bytes.Replace(b, []byte("https:\\/\\/"+*remoteHost), []byte(*myApiHostSchema+":\\/\\/"+*myApiHost), -1)
// body := ioutil.NopCloser(bytes.NewReader(b))
// resp.Body = body
// resp.ContentLength = int64(len(b))
// resp.Header.Set("Content-Length", strconv.Itoa(len(b)))
// return nil
// }