Skip to content

Commit

Permalink
Merge pull request hacklcx#9 from ttlbb/dev
Browse files Browse the repository at this point in the history
add http/https proxy
  • Loading branch information
sanjinhub authored Aug 16, 2019
2 parents 72949e4 + 919d072 commit beed40b
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions core/protocol/httpx/http.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
package httpx

import (
"fmt"
"io"
"net"
"net/http"
"strings"
"github.com/elazarl/goproxy"
"net/url"
"fmt"
)

/*http 正向代理*/

type Pxy struct{}

func (p *Pxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("Received request %s %s %s\n", req.Method, req.Host, req.RemoteAddr)

transport := http.DefaultTransport

// step 1
outReq := new(http.Request)
*outReq = *req // this only does shallow copies of maps
func Start(addr string, proxyUrl string) {

if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
if prior, ok := outReq.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
outReq.Header.Set("X-Forwarded-For", clientIP)
gp := goproxy.NewProxyHttpServer()
pu, err := url.Parse(proxyUrl)
if err == nil {
gp.Tr.Proxy = http.ProxyURL(&url.URL{
Scheme: pu.Scheme,
Host: pu.Host,
})
}

// step 2
res, err := transport.RoundTrip(outReq)
if err != nil {
rw.WriteHeader(http.StatusBadGateway)
return
}

// step 3
for key, value := range res.Header {
for _, v := range value {
rw.Header().Add(key, v)
}
}

rw.WriteHeader(res.StatusCode)
io.Copy(rw, res.Body)
res.Body.Close()
}

func Start(addr string) {
http.Handle("/", &Pxy{})
http.ListenAndServe(addr, nil)
}
gp.OnRequest().HandleConnect(goproxy.AlwaysMitm)
gp.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
// Report Send
fmt.Println(req.RemoteAddr)
return req, nil
})
http.ListenAndServe(addr, gp)
}

0 comments on commit beed40b

Please sign in to comment.