Toxiproxy server not connectiong to upstream non localhost addresses

  Kiến thức lập trình

When I create a proxy in toxiproxy via golang and set the upstream server to a remote address, I get a HTTP 404 error.

I am running toxiproxy:

toxiproxy-server -port 8474

I then create a proxy via golang:

package main

import (
    "log"
    "net/http"

    toxiproxy "github.com/Shopify/toxiproxy/v2/client"
)

func main() {
    client := toxiproxy.NewClient("localhost:8474")

    proxy, err := client.CreateProxy("my_http_proxy", "localhost:8000", "example.com:80")
    if err != nil {
        log.Fatal(err)
    }
    defer proxy.Delete()
    _, err = proxy.AddToxic("latency", "latency", "downstream", 1.0, toxiproxy.Attributes{
        "latency": 1000,
        "jitter":  0,
    })
    if err != nil {
        log.Fatal(err)
    }
    resp, err := http.Get("http://localhost:8000/")
    if err != nil {
        log.Println(err)
    }
    defer resp.Body.Close()
    log.Println(resp.StatusCode)
}

I am expecting the output of the code to be 200 but instead I get a 404 (HTTP).

If I change example.com:80 in the code to a http server running on localhost then it all works as expected. It seems that it cannot access the internet, only servers on localhost.

If I curl the address:

curl localhost:8000

I get the output:

 curl -vvv localhost:8000
*   Trying [::1]:8000...
* connect to ::1 port 8000 failed: Connection refused
*   Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/html
< Date: Sat, 27 Apr 2024 19:19:41 GMT
< Server: ECAcc (dcd/7D26)
< Content-Length: 345
<
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
                <title>404 - Not Found</title>
        </head>
        <body>
                <h1>404 - Not Found</h1>
        </body>
</html>
* Connection #0 to host localhost left intact

New contributor

face-palm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT