Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
bfec2c6e
Commit
bfec2c6e
authored
Mar 08, 2024
by
Jeffrey Morgan
Browse files
simplify host checks
parent
5c143af7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
10 deletions
+8
-10
server/routes.go
server/routes.go
+8
-10
No files found.
server/routes.go
View file @
bfec2c6e
...
@@ -906,7 +906,7 @@ var defaultAllowOrigins = []string{
...
@@ -906,7 +906,7 @@ var defaultAllowOrigins = []string{
}
}
func
allowedHost
(
host
string
)
bool
{
func
allowedHost
(
host
string
)
bool
{
if
host
==
""
||
host
==
"localhost"
{
if
host
==
""
||
host
==
"localhost"
||
host
==
"0.0.0.0"
{
return
true
return
true
}
}
...
@@ -920,18 +920,14 @@ func allowedHost(host string) bool {
...
@@ -920,18 +920,14 @@ func allowedHost(host string) bool {
"internal"
,
"internal"
,
}
}
// check if the host is a local TLD
for
_
,
tld
:=
range
tlds
{
for
_
,
tld
:=
range
tlds
{
if
strings
.
HasSuffix
(
host
,
"."
+
tld
)
{
if
strings
.
HasSuffix
(
host
,
"."
+
tld
)
{
return
true
return
true
}
}
}
}
return
false
// check if the host is a local IP address
}
func
ips
()
[]
string
{
var
ips
[]
string
if
interfaces
,
err
:=
net
.
Interfaces
();
err
==
nil
{
if
interfaces
,
err
:=
net
.
Interfaces
();
err
==
nil
{
for
_
,
iface
:=
range
interfaces
{
for
_
,
iface
:=
range
interfaces
{
addrs
,
err
:=
iface
.
Addrs
()
addrs
,
err
:=
iface
.
Addrs
()
...
@@ -941,13 +937,15 @@ func ips() []string {
...
@@ -941,13 +937,15 @@ func ips() []string {
for
_
,
a
:=
range
addrs
{
for
_
,
a
:=
range
addrs
{
if
ip
,
_
,
err
:=
net
.
ParseCIDR
(
a
.
String
());
err
==
nil
{
if
ip
,
_
,
err
:=
net
.
ParseCIDR
(
a
.
String
());
err
==
nil
{
ips
=
append
(
ips
,
ip
.
String
())
if
host
==
ip
.
String
()
{
return
true
}
}
}
}
}
}
}
}
}
return
ips
return
false
}
}
func
allowedHostsMiddleware
(
addr
net
.
Addr
)
gin
.
HandlerFunc
{
func
allowedHostsMiddleware
(
addr
net
.
Addr
)
gin
.
HandlerFunc
{
...
@@ -969,7 +967,7 @@ func allowedHostsMiddleware(addr net.Addr) gin.HandlerFunc {
...
@@ -969,7 +967,7 @@ func allowedHostsMiddleware(addr net.Addr) gin.HandlerFunc {
}
}
if
addr
,
err
:=
netip
.
ParseAddr
(
host
);
err
==
nil
{
if
addr
,
err
:=
netip
.
ParseAddr
(
host
);
err
==
nil
{
if
addr
.
IsLoopback
()
||
addr
.
IsPrivate
()
||
slices
.
Contains
(
ips
(),
host
)
||
addr
.
String
()
==
"0.0.0.0"
{
if
addr
.
IsLoopback
()
||
addr
.
IsPrivate
()
{
c
.
Next
()
c
.
Next
()
return
return
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment