-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package middlewares | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func isLocalIP(ip string) bool { | ||
if ip[0:7] == "192.168" || ip[0:3] == "10." { | ||
return true | ||
} | ||
|
||
if ip[0:4] != "172." { | ||
return false | ||
} | ||
|
||
tokens := strings.Split(ip, ".") | ||
|
||
if len(tokens) != 4 { | ||
return false | ||
} | ||
|
||
i, _ := strconv.Atoi(tokens[1]) | ||
|
||
return i >= 16 && i <= 31 | ||
} | ||
|
||
func LocalOnly(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ip := getIP(r) | ||
|
||
// Start with 172 or 192 only | ||
if !isLocalIP(ip) { | ||
http.Error(w, "Forbidden", http.StatusForbidden) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package middlewares | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsLocalIP(t *testing.T) { | ||
assert.True(t, isLocalIP("192.168.1.112")) | ||
assert.True(t, isLocalIP("192.168.10.123")) | ||
|
||
assert.False(t, isLocalIP("48.123.45.67")) | ||
assert.False(t, isLocalIP("192.166.69.420")) | ||
|
||
assert.True(t, isLocalIP("172.24.0.1")) | ||
assert.True(t, isLocalIP("172.18.0.1")) | ||
|
||
assert.False(t, isLocalIP("172.32.0.0")) | ||
assert.False(t, isLocalIP("172.15.0.0")) | ||
|
||
assert.False(t, isLocalIP("invalid string")) | ||
assert.False(t, isLocalIP("1234:5678:abcd:efgh:ijkl:mnop:qrst:uvwx")) | ||
assert.False(t, isLocalIP("172.24.0")) | ||
} |