forked from gin-gonic/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(proxy): Add forward proxy (gin-gonic#134)
* add forward proxy * Del.idea
1 parent
4925663
commit d9e5ae3
Showing
3 changed files
with
74 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
*.dll | ||
*.so | ||
*.dylib | ||
|
||
.idea | ||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# A proxy integrate both forward and reverse | ||
|
||
Run the server and make following reqeust to test forward function. Remember to set "forward"="ok' in the header so the middleware can tell which one is for forward and which one is for reverse. The demo can be adapted to puer forward proxy. | ||
|
||
``` | ||
import requests | ||
def test_forward(): | ||
res=requests.get("http://www.baidu.com",headers={"forward":"ok"},proxies={"http":"http://127.0.0.1:8888"}) | ||
print (res.text) | ||
test_forward() | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"io" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
) | ||
|
||
func main() { | ||
r := gin.Default() | ||
|
||
r.Use(ForwardMid) | ||
|
||
//Create a catchall route | ||
r.Any("/*proxyPath", Reverse) | ||
|
||
log.Info("启动http") | ||
r.Run(":8888") | ||
} | ||
|
||
func ForwardMid(c *gin.Context) { | ||
// !!! adapt to your request header set | ||
if v, ok := c.Request.Header["Forward"]; ok { | ||
if v[0] == "ok" { | ||
resp, err := http.DefaultTransport.RoundTrip(c.Request) | ||
if err != nil { | ||
http.Error(c.Writer, err.Error(), http.StatusServiceUnavailable) | ||
c.Abort() | ||
return | ||
} | ||
defer resp.Body.Close() | ||
copyHeader(c.Writer.Header(), resp.Header) | ||
c.Writer.WriteHeader(resp.StatusCode) | ||
io.Copy(c.Writer, resp.Body) | ||
c.Abort() | ||
return | ||
} | ||
} | ||
|
||
c.Next() | ||
} | ||
func copyHeader(dst, src http.Header) { | ||
for k, vv := range src { | ||
for _, v := range vv { | ||
dst.Add(k, v) | ||
} | ||
} | ||
} | ||
func Reverse(c *gin.Context) { | ||
remote, _ := url.Parse("http://xxx.xxx.xxx") | ||
proxy := httputil.NewSingleHostReverseProxy(remote) | ||
proxy.Director = func(req *http.Request) { | ||
req.Header = c.Request.Header | ||
req.Host = remote.Host | ||
req.URL.Host = remote.Host | ||
req.URL.Scheme = remote.Scheme | ||
} | ||
proxy.ServeHTTP(c.Writer, c.Request) | ||
} |