mirror of
https://github.com/containers/podman.git
synced 2025-06-23 18:59:30 +08:00
fix(machine): throw connect: connection refused
after set proxy
When the `machine start` command is executed, Podman automatically retrieves the current host's `*_PROXY` environment variable and assigns it directly to the virtual machine in QEMU. However, most `*_PROXY` variables are set with `127.0.0.1` or `localhost`, such as `127.0.0.1:8888`. This causes failures in network-related operations within the virtual machine due to incorrect proxy settings. Fixes: #14087 Signed-off-by: Black-Hole1 <bh@bugs.cc>
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/config"
|
"github.com/containers/common/pkg/config"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -613,7 +614,14 @@ func GetProxyVariables() map[string]string {
|
|||||||
proxyOpts := make(map[string]string)
|
proxyOpts := make(map[string]string)
|
||||||
for _, variable := range config.ProxyEnv {
|
for _, variable := range config.ProxyEnv {
|
||||||
if value, ok := os.LookupEnv(variable); ok {
|
if value, ok := os.LookupEnv(variable); ok {
|
||||||
proxyOpts[variable] = value
|
if value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: use constants for host.containers.internal
|
||||||
|
v := strings.ReplaceAll(value, "127.0.0.1", "host.containers.internal")
|
||||||
|
v = strings.ReplaceAll(v, "localhost", "host.containers.internal")
|
||||||
|
proxyOpts[variable] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return proxyOpts
|
return proxyOpts
|
||||||
|
@ -25,9 +25,55 @@ func TestEditCmd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPropagateHostEnv(t *testing.T) {
|
func TestPropagateHostEnv(t *testing.T) {
|
||||||
t.Setenv("SSL_CERT_FILE", "/some/foo.cert")
|
tests := map[string]struct {
|
||||||
t.Setenv("SSL_CERT_DIR", "/some/my/certs")
|
value string
|
||||||
t.Setenv("HTTP_PROXY", "proxy")
|
expect string
|
||||||
|
}{
|
||||||
|
"HTTP_PROXY": {
|
||||||
|
"proxy",
|
||||||
|
"equal",
|
||||||
|
},
|
||||||
|
"ftp_proxy": {
|
||||||
|
"domain.com:8888",
|
||||||
|
"equal",
|
||||||
|
},
|
||||||
|
"FTP_PROXY": {
|
||||||
|
"proxy",
|
||||||
|
"equal",
|
||||||
|
},
|
||||||
|
"NO_PROXY": {
|
||||||
|
"localaddress",
|
||||||
|
"equal",
|
||||||
|
},
|
||||||
|
"HTTPS_PROXY": {
|
||||||
|
"",
|
||||||
|
"unset",
|
||||||
|
},
|
||||||
|
"no_proxy": {
|
||||||
|
"",
|
||||||
|
"unset",
|
||||||
|
},
|
||||||
|
"http_proxy": {
|
||||||
|
"127.0.0.1:8888",
|
||||||
|
"host.containers.internal:8888",
|
||||||
|
},
|
||||||
|
"https_proxy": {
|
||||||
|
"localhost:8888",
|
||||||
|
"host.containers.internal:8888",
|
||||||
|
},
|
||||||
|
"SSL_CERT_FILE": {
|
||||||
|
"/some/f=oo.cert",
|
||||||
|
fmt.Sprintf("%s/f=oo.cert", machine.UserCertsTargetPath),
|
||||||
|
},
|
||||||
|
"SSL_CERT_DIR": {
|
||||||
|
"/some/my/certs",
|
||||||
|
machine.UserCertsTargetPath,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, item := range tests {
|
||||||
|
t.Setenv(key, item.value)
|
||||||
|
}
|
||||||
|
|
||||||
cmdLine := propagateHostEnv(make([]string, 0))
|
cmdLine := propagateHostEnv(make([]string, 0))
|
||||||
|
|
||||||
@ -36,5 +82,26 @@ func TestPropagateHostEnv(t *testing.T) {
|
|||||||
tokens := strings.Split(cmdLine[1], ",string=")
|
tokens := strings.Split(cmdLine[1], ",string=")
|
||||||
decodeString, err := base64.StdEncoding.DecodeString(tokens[1])
|
decodeString, err := base64.StdEncoding.DecodeString(tokens[1])
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, fmt.Sprintf("HTTP_PROXY=\"proxy\"|SSL_CERT_FILE=\"%s/foo.cert\"|SSL_CERT_DIR=%q", machine.UserCertsTargetPath, machine.UserCertsTargetPath), string(decodeString))
|
|
||||||
|
// envsRawArr looks like: ["BAR=\"bar\"", "FOO=\"foo\""]
|
||||||
|
envsRawArr := strings.Split(string(decodeString), "|")
|
||||||
|
// envs looks like: {"BAR": "bar", "FOO": "foo"}
|
||||||
|
envs := make(map[string]string)
|
||||||
|
for _, env := range envsRawArr {
|
||||||
|
item := strings.SplitN(env, "=", 2)
|
||||||
|
envs[item[0]] = strings.Trim(item[1], "\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, test := range tests {
|
||||||
|
switch test.expect {
|
||||||
|
case "equal":
|
||||||
|
assert.Equal(t, envs[key], test.value)
|
||||||
|
case "unset":
|
||||||
|
if _, ok := envs[key]; ok {
|
||||||
|
t.Errorf("env %s should not be set", key)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
assert.Equal(t, envs[key], test.expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user