Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Special-case 127.0.0.0/8 and ::1 loopbacks
  • Loading branch information
michaelblyons committed May 5, 2017
1 parent 7b68d8e commit a67ff26
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions hosts.tmLanguage
Expand Up @@ -23,6 +23,16 @@
<string>#.*$</string>
</dict>

<!-- a different color for localhost -->
<dict>
<key>comment</key>
<string>Loopback address like 127.0.0.1</string>
<key>match</key>
<string>127(\.((25[0-5])|(2[0-4][0-9])|(1[0-9][0-9])|([1-9][0-9])|[0-9])){3}|::1</string>
<key>name</key>
<string>support.type.built-in.loopback.hosts</string>
</dict>

<!-- a different color for local IP addresses -->
<dict>
<key>comment</key>
Expand Down
14 changes: 14 additions & 0 deletions hosts.tmLanguage.rb
Expand Up @@ -27,6 +27,8 @@
"192\\.168(\\.#{BYTE}){2}" # 192.169.x.x
].map { |addr| "(#{addr})" }.join('|')

LOOPBACK_IP_V4 = "127(\\.#{BYTE}){3}" # 127.x.x.x

# == IPv6 ==

# Luckily David M. Syzdek already did the hard work and we can use it here
Expand All @@ -52,6 +54,8 @@
"(#{SEGMENT}:){1,7}:", # 1:: 1:2:3:4:5:6:7::
].map { |variant| "(#{variant})" }.join('|')

LOOPBACK_IP_V6 = "::1"

# == render the tmLanguage file ==

# binding gives the template access to all local variables
Expand Down Expand Up @@ -85,6 +89,16 @@
<string>#.*$</string>
</dict>

<!-- a different color for localhost -->
<dict>
<key>comment</key>
<string>Loopback address like 127.0.0.1</string>
<key>match</key>
<string><%= LOOPBACK_IP_V4 %>|<%= LOOPBACK_IP_V6 %></string>
<key>name</key>
<string>support.type.built-in.loopback.hosts</string>
</dict>

<!-- a different color for local IP addresses -->
<dict>
<key>comment</key>
Expand Down
3 changes: 3 additions & 0 deletions test/hosts
@@ -1,5 +1,6 @@
# localhost entries using IPv4 and IPv6
127.0.0.1 localhost.localdomain localhost
127.8.8.8 localhost.localdomain localhost
::1 localhost.localdomain localhost

# Private network IPs show in a different colour
Expand All @@ -20,6 +21,8 @@
# an IP in the public range should have different colour than private IP ranges
192.30.252.131 github.com

# an IP containing ::1 should not be partly highlighted as loopback
8::1 unassigned.arpa

# invalid IP addresses should not be highlighted as if they are valid
189.23.290.13 i.cannot.even
Expand Down

0 comments on commit a67ff26

Please sign in to comment.