How to bind the host in the go code?

Time:2021-10-27

How to bind the host in the go code?

Within the company, for security, there may be domain names that can only be accessed by binding host. If accessed through a browser, we will modify the hosts file of the machine; However, if we want to access such domain names through programs, do we still have to bind host? Of course, the answer is No. moreover, depending on the locally bound host, if the program is deployed to other machines, it must also bind the host on that machine. What if there are many machines?

Just two days ago, someone in the group asked similar questions

Examples of this article:

  • IP: 192.168.1.102, which means you need to access the resources on this machine
  • Domain name: studygolang.com, virtual host configured by nginx
  • URL path: / testhost.txt, the content is welcome to studygolang.com

Requirement: you need to request the testhost.txt resource on the server.

1. Solutions for Linux shell

Curl program under Linux can bind host, so it can be easily implemented in shell, such as curl – H “host: studygolang. Com” http://192.168.1.102/testhost.txt 。

2. PHP solutions

1) Implemented by curl extension

  1. $ch = curl_init(); 
  2. curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Host:studygolang.com’)); 
  3. curl_setopt($ch, CURLOPT_URL, ‘http://192.168.1.102/testhost.txt’); 
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  5. $ret = curl_exec($ch); 
  6. var_dump($ret); 

2) Does not rely on curl extensions

  1. // Create a stream 
  2. $opts = array( 
  3.     ‘http’=>array( 
  4.         ‘method’=>”GET”, 
  5.         ‘header’=>”Host:studygolang.com” 
  6.     ) 
  7. ); 
  8.  
  9. $context = stream_context_create($opts); 
  10.  
  11. // Open the file using the HTTP headers set above 
  12. $ret = file_get_contents(‘http://192.168.1.102/testhost.txt’, false, $context); 
  13. var_dump($ret); 

3. Golang’s solution

Since the go standard library implements the HTTP protocol, find a solution in the net / HTTP package.

Generally, we can request a URL through the following code:

  1. http.Get(url) 

However, in this case, regardless of url =“ http://192.168.1.102/testhost.txt “Or url =” http://studygolang.com/testhost.txt “, cannot request resources (without binding host).

In the request structure in the HTTP package, there is a field: host. We can refer to the above two solutions to set the value of host. The method is as follows:

  1. package main 
  2.  
  3. import ( 
  4.     “net/http” 
  5.     “io/ioutil” 
  6.     “fmt” 
  7.  
  8. func main() { 
  9.     req, err := http.NewRequest(“GET”, “http://192.168.1.102/testhost.txt”, nil) 
  10.     if err != nil { 
  11.         panic(err) 
  12.     } 
  13.     req.Host = “studygolang.com” 
  14.     resp, err := http.DefaultClient.Do(req) 
  15.     if err != nil { 
  16.         panic(err) 
  17.     } 
  18.     defer resp.Body.Close() 
  19.     body, err := ioutil.ReadAll(resp.Body) 
  20.     if err != nil { 
  21.         panic(err) 
  22.     } 
  23.     fmt.Println(string(body)) 

4. Summary

Regardless of the method and language, in the final analysis, you need to tell the server which host is requested. This is the host header of the HTTP protocol. If the host header is not set manually, it is obtained from the requested URL.