forked from hacklcx/HFish
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hacklcx#9 from ttlbb/dev
add http/https proxy
- Loading branch information
Showing
1 changed file
with
19 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |