Skip to content

Commit ed58fbc

Browse files
authored
Merge pull request #1112 from saltbo/fix-localhostregex
Fix the regexp for the localhost proxy match
2 parents d3c2533 + 64de3f9 commit ed58fbc

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

pkg/cidata/cidata.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"net"
8+
"net/url"
79
"os"
810
"path/filepath"
9-
"regexp"
1011
"strconv"
1112
"strings"
1213
"time"
@@ -23,6 +24,16 @@ import (
2324
"github.com/sirupsen/logrus"
2425
)
2526

27+
var netLookupIP = func(host string) []net.IP {
28+
ips, err := net.LookupIP(host)
29+
if err != nil {
30+
logrus.Debugf("net.LookupIP %s: %s", host, err)
31+
return nil
32+
}
33+
34+
return ips
35+
}
36+
2637
func setupEnv(y *limayaml.LimaYAML) (map[string]string, error) {
2738
// Start with the proxy variables from the system settings.
2839
env, err := osutil.ProxySettings()
@@ -50,16 +61,25 @@ func setupEnv(y *limayaml.LimaYAML) (map[string]string, error) {
5061
}
5162
}
5263
}
53-
// Replace "localhost" in proxy settings with the gateway address
54-
localhostRegexes := []*regexp.Regexp{
55-
regexp.MustCompile(`\blocalhost\b`),
56-
regexp.MustCompile(`\b127.0.0.1\b`),
57-
}
64+
// Replace IP that IsLoopback in proxy settings with the gateway address
5865
for _, name := range append(lowerVars, upperVars...) {
5966
value, ok := env[name]
6067
if ok && !strings.EqualFold(name, "no_proxy") {
61-
for _, re := range localhostRegexes {
62-
value = re.ReplaceAllString(value, qemu.SlirpGateway)
68+
u, err := url.Parse(value)
69+
if err != nil {
70+
logrus.Warnf("Ignoring invalid proxy %q=%v: %s", name, value, err)
71+
continue
72+
}
73+
74+
for _, ip := range netLookupIP(u.Hostname()) {
75+
if ip.IsLoopback() {
76+
newHost := qemu.SlirpGateway
77+
if u.Port() != "" {
78+
newHost = net.JoinHostPort(newHost, u.Port())
79+
}
80+
u.Host = newHost
81+
value = u.String()
82+
}
6383
}
6484
if value != env[name] {
6585
logrus.Infof("Replacing %q value %q with %q", name, env[name], value)

pkg/cidata/cidata_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cidata
2+
3+
import (
4+
"net"
5+
"net/url"
6+
"strings"
7+
"testing"
8+
9+
"github.com/lima-vm/lima/pkg/limayaml"
10+
qemu "github.com/lima-vm/lima/pkg/qemu/const"
11+
"github.com/xorcare/pointer"
12+
"gotest.tools/v3/assert"
13+
)
14+
15+
func fakeLookupIP(host string) []net.IP {
16+
return []net.IP{net.IPv4(127, 0, 0, 0)}
17+
}
18+
19+
func TestSetupEnv(t *testing.T) {
20+
netLookupIP = fakeLookupIP
21+
urlMustParse := func(urlStr string) *url.URL {
22+
u, err := url.Parse(urlStr)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
return u
28+
}
29+
30+
httpProxyCases := []*url.URL{
31+
urlMustParse("http://127.0.0.1"),
32+
urlMustParse("http://127.0.0.1:8080"),
33+
urlMustParse("https://127.0.0.1:8080"),
34+
urlMustParse("sock4://127.0.0.1:8080"),
35+
urlMustParse("sock5://127.0.0.1:8080"),
36+
urlMustParse("http://127.0.0.1:8080/"),
37+
urlMustParse("http://127.0.0.1:8080/path"),
38+
urlMustParse("http://localhost:8080"),
39+
urlMustParse("http://localhost:8080/"),
40+
urlMustParse("http://localhost:8080/path"),
41+
urlMustParse("http://docker.for.mac.localhost:8080"),
42+
urlMustParse("http://docker.for.mac.localhost:8080/"),
43+
urlMustParse("http://docker.for.mac.localhost:8080/path"),
44+
}
45+
46+
for _, httpProxy := range httpProxyCases {
47+
t.Run(httpProxy.Host, func(t *testing.T) {
48+
envKey := "http_proxy"
49+
envValue := httpProxy.String()
50+
envs, err := setupEnv(&limayaml.LimaYAML{PropagateProxyEnv: pointer.Bool(false), Env: map[string]string{envKey: envValue}})
51+
assert.NilError(t, err)
52+
assert.Equal(t, envs[envKey], strings.ReplaceAll(envValue, httpProxy.Hostname(), qemu.SlirpGateway))
53+
})
54+
}
55+
}
56+
57+
func TestSetupInvalidEnv(t *testing.T) {
58+
envKey := "http_proxy"
59+
envValue := "://localhost:8080"
60+
envs, err := setupEnv(&limayaml.LimaYAML{PropagateProxyEnv: pointer.Bool(false), Env: map[string]string{envKey: envValue}})
61+
assert.NilError(t, err)
62+
assert.Equal(t, envs[envKey], envValue)
63+
}

0 commit comments

Comments
 (0)