Coder Social home page Coder Social logo

Comments (5)

guonaihong avatar guonaihong commented on May 18, 2024

Add Proxy() function
Fake code

g := gout.New(nil)
s := ""

err := g.GET("http://www.google.com").SetProxy("http://127.0.0.1").BindBody(&s).Do()
if err != nil {
}

Can such an API be?

from gout.

zheeeng avatar zheeeng commented on May 18, 2024

It there an API for changing request origin ? For example proxy https://www.elgoog.com/search?q=test is equivalent to https://www.google.com/search?q=test.

g.Proxy(request, g.ProxyOptopn{
     Url: func (url: http.Url) {
        // modify the url from elgoog to google
     },
     Header: map[string]string{origin: 'www.google.com'}   // merge and override the origianl request's headers
})

proxy keeps the original methods and other headers unchanged.

from gout.

guonaihong avatar guonaihong commented on May 18, 2024

There are only two APIs for implementing the proxy function in golang. From the documentation point of view, the proxy client is transparent, and only forwards the original request to the proxy server. It seems that the client does not need to do additional processing.

func ProxyFromEnvironment(req *Request) (*url.URL, error)
func ProxyURL(fixedURL *url.URL) func(*Request) (*url.URL, error)

doc

https://golang.google.cn/pkg/net/http/#ProxyURL

from gout.

guonaihong avatar guonaihong commented on May 18, 2024

The proxy function is developed. If there is no problem, I plan to merge it into the main branch.
Below is the test code I wrote.

  • proxy-server
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/guonaihong/gout"
)

func main() {
	r := gin.Default()
	r.Any("/*action", func(c *gin.Context) {
		fmt.Printf("%#v\n", c.Request)
		var s string
		fmt.Printf("url = %s\n", c.Request.URL.String())
		err := gout.Def().GET(c.Request.URL.String()).BindBody(&s).Do()
		if err != nil {
			c.String(200, err.Error())
			return
		}
		c.String(200, s)
	})

	r.Run(":7000")
}
  • proxy-client.go
package main

import (
	"fmt"
	"github.com/guonaihong/gout"
	"log"
)

func main() {
	var s string
	err := gout.Def().GET("www.qq.com").SetProxy("http://127.0.0.1:7000").BindBody(&s).Do()
	if err != nil {
		log.Println(err)
		return
	}

	fmt.Println(s)
}

from gout.

guonaihong avatar guonaihong commented on May 18, 2024

I am closed, if you have any questions, you can reopen it.

from gout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.