我想实现一个HTTP请求和响应系统,用于

client ------> A ----------> B --------> (request) HTTPserver
client <------ A <---------- B <-------- (response) HTTPserver
  1. 客户端使用任何 HTTP 方法(POST、PUT 等)向 A 发送 HTTP 请求

  2. 然后A读取请求正文对其进行加密,然后将其转发到B 3 B,然后解密读取HTTP请求正文

  3. B然后用收到的解密体作为有效载荷准备一个HTTP请求并发送到HTTP服务器。5 然后,HTTP 服务器响应 B。

  4. 然后 B 对来自 HTTP 服务器的响应进行加密,然后转发到 A。

  5. A 还解密来自 B 的响应,然后将响应发送回客户端。

我根据前面的建议实现了以下目标。

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) {

 

 // read request body from client

bodybytes, _ := ioutil.ReadAll(req.Body)


defer req.Body.Close()


// encrypt req.Body

object, _ := enc.Encrypt(bodybytes)


// serialize object

serialized, _ := object.CompactSerialize()


// prepare forwarding message

msg := message{reformatedData: serialized}


// encode message 

msgbytes, _ := json.Marshal(&msg)


req.ContentLength = int64(len(msgbytes))

req.Body = ioutil.NopCloser(bytes.NewBuffer(msgbytes))



// How do I read the response data from proxy server B and then send

// response to the client

....

 

  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) {


  var msg message

  HTTPServerurl := http://xxxxx


  // read request body

  bodybytes, _ := ioutil.ReadAll(req.Body)

  

  req.ContentLength = int64(len(bodybytes))

  req.Body = ioutil.NopCloser(bytes.NewBuffer(bodybytes))


我的问题是

  1. 如何将代理 B 中的代理ServHTTP“res”中的“respmsgbytes”写回代理A?

  2. 如何从代理服务器 B 读取响应数据,然后将响应发送到客户端?

有什么帮助吗?我留下了错误检查以使代码简短。