HTTP Proxy in Go

132 views
Skip to first unread message

Van Fury

unread,
Apr 22, 2021, 3:07:00 AM4/22/21
to golang-nuts

Hi,

I have to implement an HTTP forwarding proxy in Go which can be used in production and need advice from Go experts.
Any advice like which Go library is best to use and other things to take not of when implementing it.

BR
Abraham

wilk

unread,
Apr 22, 2021, 9:55:26 AM4/22/21
to golan...@googlegroups.com
On 22-04-2021, Van Fury wrote:
> ------=_Part_322_998422299.1619075183467
> Content-Type: multipart/alternative;
> boundary="----=_Part_323_2099960923.1619075183467"
>
> ------=_Part_323_2099960923.1619075183467
> Content-Type: text/plain; charset="UTF-8"
I'm not an expert but I use the proxy of httputil without any issue. I'm
using forwarding to legacy app with Go new app in front.

--
wilk

Van Fury

unread,
Apr 22, 2021, 12:13:08 PM4/22/21
to golang-nuts
Hi,

Thanks, I  have created these two proxy using the httputil. How can i send JSON request body from proxyA to proxyB.
Any help or idea?

ProxyA:

const (
  ServerB = "<address of B>"
  Port = "<proxy A port>"
)

func main() {
  // start server
  http.HandleFunc("/", proxyPass)
  log.Fatal(http.ListenAndServe(":" + Port, nil))
}

func proxyPass(res http.ResponseWriter, req  *http.Request) {
 
 // JSON message

msg := Data{ "key1": "v1", "key2":"v2"}
 
  url, _ := url.Parse(ServerB)
  proxy := httputil.NewSingleHostReverseProxy(url)
  proxy.ServeHTTP(res, req)
}


For proxy B:

const (
  Server = "<address of server>"
  Port = "<proxy B port>"
)

func main() {
  // start server
  http.HandleFunc("/", proxyPass)
  log.Fatal(http.ListenAndServe(":" + Port, nil))
}

func proxyPass(res http.ResponseWriter, req  *http.Request) {
  // Decrypt Request here
  // ...

  url, _ := url.Parse(Server)
  proxy := httputil.NewSingleHostReverseProxy(url)
  proxy.ServeHTTP(res, req)
}

Dang Dien

unread,
Apr 22, 2021, 1:01:53 PM4/22/21
to golang-nuts

I recommend the fasthttp package. I have implemented an HTTP forwarding proxy in Go and use in production with high traffic (~40 rpm).
Reply all
Reply to author
Forward
0 new messages