Skip to content

Commit db861f6

Browse files
committed
cherry-pick: add local domain pattern as a host alias (#2030)
1 parent 2c87086 commit db861f6

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

daemon/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
github.com/containerd/containerd v1.7.28
2727
github.com/distribution/distribution/v3 v3.0.0
2828
github.com/dustin/go-humanize v1.0.1
29-
github.com/eball/zeroconf v0.2.1
29+
github.com/eball/zeroconf v0.2.2
3030
github.com/godbus/dbus/v5 v5.1.0
3131
github.com/gofiber/fiber/v2 v2.52.9
3232
github.com/hirochachacha/go-smb2 v1.1.0

daemon/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
8585
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
8686
github.com/eball/echo/v4 v4.13.4-patch h1:5w83KQrEqrxhc1BO0BpRBHssC37vFrWualUM27Rt2sg=
8787
github.com/eball/echo/v4 v4.13.4-patch/go.mod h1:ORgy8LWTq8knpwgaz538rAJMri7WgpoAD6H3zYccn84=
88-
github.com/eball/zeroconf v0.2.1 h1:PZ89f6J2k2Z7q3oSzcZGFXJf97S7NPmj7H04ACw9v8c=
89-
github.com/eball/zeroconf v0.2.1/go.mod h1:eIbIjGYo9sSMaKWLcveHEPRWdyblz7q9ih2R1HnNw5M=
88+
github.com/eball/zeroconf v0.2.2 h1:y23X67tLFlU+b35LyM9THXGsdC88IUz803G+mzfeSeE=
89+
github.com/eball/zeroconf v0.2.2/go.mod h1:eIbIjGYo9sSMaKWLcveHEPRWdyblz7q9ih2R1HnNw5M=
9090
github.com/ebitengine/purego v0.8.4 h1:CF7LEKg5FFOsASUj0+QwaXf8Ht6TlFxg09+S9wz0omw=
9191
github.com/ebitengine/purego v0.8.4/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
9292
github.com/emicklei/go-restful/v3 v3.13.0 h1:C4Bl2xDndpU6nJ4bc1jXd+uTmYPVUwkD6bFY/oTyCes=

daemon/internel/intranet/dns.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net"
66
"slices"
7+
"strings"
78

89
"github.com/beclab/Olares/daemon/pkg/nets"
910
"github.com/eball/zeroconf"
@@ -17,6 +18,7 @@ type DNSConfig struct {
1718
type instanceServer struct {
1819
queryServer *zeroconf.Server
1920
host *DNSConfig
21+
aliases []string
2022
}
2123

2224
type mDNSServer struct {
@@ -66,6 +68,14 @@ func (s *mDNSServer) StartAll() error {
6668
return err
6769
}
6870

71+
// add host alias
72+
domainTokens := strings.Split(domain, ".")
73+
alias := []string{strings.Join(domainTokens, "-") + ".local."}
74+
75+
// TODO: add more alias if needed
76+
klog.Info("add host alias, ", alias[0])
77+
server.AddHostAlias(alias[0])
78+
6979
s.servers[domain] = &instanceServer{
7080
queryServer: server,
7181
host: &DNSConfig{Domain: domain},

daemon/internel/intranet/proxy.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func (p *proxyServer) Start() error {
5757
p.proxy.Use(
5858
func(next echo.HandlerFunc) echo.HandlerFunc {
5959
return func(c echo.Context) error {
60-
if strings.HasSuffix(c.Request().Host, ".olares.local") {
60+
if strings.HasSuffix(c.Request().Host, ".olares.local") ||
61+
strings.HasSuffix(c.Request().Host, "-olares.local") {
6162
if c.IsWebSocket() {
6263
ctx := c.Request().Context()
6364
ctx = context.WithValue(ctx, WSKey, true)
@@ -103,7 +104,25 @@ func (p *proxyServer) Next(c echo.Context) *middleware.ProxyTarget {
103104
if c.IsWebSocket() {
104105
scheme = "wss://"
105106
}
106-
proxyPass, err := url.Parse(scheme + c.Request().Host + ":443")
107+
108+
var (
109+
proxyPass *url.URL
110+
err error
111+
)
112+
requestHost := c.Request().Host
113+
if strings.HasSuffix(requestHost, "-olares.local") {
114+
// intranet request, and host parttern is appid-<username>-olares.local for windows and linux client
115+
tokens := strings.Split(requestHost, "-")
116+
if len(tokens) < 3 {
117+
klog.Error("invalid intranet request host, ", requestHost)
118+
return nil
119+
}
120+
requestHost = strings.Join(tokens, ".")
121+
c.Request().Host = requestHost
122+
proxyPass, err = url.Parse(scheme + requestHost + ":443")
123+
} else {
124+
proxyPass, err = url.Parse(scheme + c.Request().Host + ":443")
125+
}
107126
if err != nil {
108127
klog.Error("parse proxy target error, ", err)
109128
return nil

0 commit comments

Comments
 (0)