HTTP request to IP Address using Host header

Hi all,

I’m making a tool, that can check a CIDR and see if a domain belongs to one server in the CIDR.
I wanted to do it similar as I do with cUrl, doing the request to the ip address, for example: and setting the Host Header:

So far the code I have is always returning Status Code 200, but the host exist just in one of the IP I have set. Why is this behavior and how can I get the right behavior from my http request?

Here is the code

package main

import (

func main() {

	var domain = ""

	fmt.Println("Analyzing Domain: ", domain)

	ipAddresses, err := Hosts("")
	if err != nil {

	for _, ips := range ipAddresses {
		req, err := http.NewRequest("GET", "http://"+ips, nil)
		if err != nil {
			log.Fatal("Error reading request. ", err)

		fmt.Println("Using IP: ", ips)

		req.Host = domain
		req.URL.Host = domain
		//req.Header.Set("Host", domain)

		client := &http.Client{Timeout: time.Second * 10}

		resp, err := client.Do(req)
		if err != nil {
			log.Fatal("Error reading response. ", err)
		defer resp.Body.Close()

		//body, err := ioutil.ReadAll(resp.Body)
		//if err != nil {
		//	log.Fatal("Error reading body", err)

		fmt.Println("HTTP Status: ", resp.StatusCode)
		fmt.Println("Content Length: ", resp.ContentLength)

// Hosts function read a CIDR and create a slide with all the IP addresses contained in it
func Hosts(cidr string) ([]string, error) {
	ip, ipnet, err := net.ParseCIDR(cidr)
	if err != nil {
		return nil, err

	var ips []string
	for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
		ips = append(ips, ip.String())

	// remove network address and broadcast address
	lenIPs := len(ips)
	switch {
	case lenIPs < 2:
		return ips, nil

		return ips[1 : len(ips)-1], nil

func inc(ip net.IP) {
	for j := len(ip) - 1; j >= 0; j-- {
		if ip[j] > 0 {

Thanks in advance

I tried this example , but the result is the same

You set the request.URL.Host to the same hostname every time and not just the request http header. You are basically changing the url on the request made for the IP address.

Also, you should check if resp.Body is nil before calling Close on it :slight_smile:

1 Like

Hey, You can use below code to get IP from HTTP req

 package main
 import ("encoding/json"
func main() {
 http.HandleFunc("/", ExampleHandler)
 if err: = http.ListenAndServe("[](", nil);
 err != nil {
func ExampleHandler(w http.ResponseWriter, r * http.Request) {
 var ip string
 w.Header().Add("Content-Type", "application/json")
 forwarded: = r.Header.Get("X-FORWARDED-FOR")
 if forwarded != "" {
     ip = forwarded
 } else {
     log.Print("Not forwarded")
     ip = r.RemoteAddr
 resp, _: = json.Marshal(map[string] string {
     "ip": ip,

Hi Boris,

thanks for your answer.

Then If I set request.URL.Host it will overwrite the url I provide?

thanks and all the best!

Yes. The requests url is made up of different segments, a schema, possibly username and password, a host, a path, some query string parameters and a url fragment. The Host in the URL object is not the same as the Host on the request object, which is the one you want to change.

1 Like

Hi Boris,

thanks for the clarification, now everything is more clear about this header options.

Then I just need to set req.Host but not this is great!

Just using req.Host it is working, but when the IP does not contain the page, I get the error

Error reading response. Get EOF

I guess this is what you mentioned before

Also, you should check if resp.Body is nil before calling Close on it



Hey Boris,

I manage with your help to make that part of the code, work properly.

thank you very much :pray:

Happy to help :blush:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.