Compare commits

..
5 changed files with 80 additions and 17 deletions
+47
View File
@@ -0,0 +1,47 @@
// Copyright (C) 2017 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package connections
import (
"testing"
"github.com/syncthing/syncthing/lib/config"
)
func TestIsLANHost(t *testing.T) {
cases := []struct {
addr string
lan bool
}{
// loopback
{"127.0.0.1:22000", true},
{"127.0.0.1", true},
// local nets
{"10.20.30.40:22000", true},
{"10.20.30.40", true},
// neither
{"192.0.2.1:22000", false},
{"192.0.2.1", false},
// doesn't resolve
{"[banana::phone]:hello", false},
{"„‹›fl´fi·‰ˇ¨Á˝", false},
}
cfg := config.Wrap("/dev/null", config.Configuration{
Options: config.OptionsConfiguration{
AlwaysLocalNets: []string{"10.20.30.0/24"},
},
})
s := &Service{cfg: cfg}
for _, tc := range cases {
res := s.isLANHost(tc.addr)
if res != tc.lan {
t.Errorf("isLANHost(%q) => %v, expected %v", tc.addr, res, tc.lan)
}
}
}
+24 -11
View File
@@ -424,23 +424,36 @@ func (s *Service) connect() {
}
func (s *Service) isLANHost(host string) bool {
if noPort, _, err := net.SplitHostPort(host); err == nil && noPort != "" {
host = noPort
// Probably we are called with an ip:port combo which we can resolve as
// a TCP address.
if addr, err := net.ResolveTCPAddr("tcp", host); err == nil {
return s.isLAN(addr)
}
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return false
// ... but this function looks general enough that someone might try
// with just an IP as well in the future so lets allow that.
if addr, err := net.ResolveIPAddr("ip", host); err == nil {
return s.isLAN(addr)
}
return s.isLAN(addr)
return false
}
func (s *Service) isLAN(addr net.Addr) bool {
tcpaddr, ok := addr.(*net.TCPAddr)
if !ok {
var ip net.IP
switch addr := addr.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.TCPAddr:
ip = addr.IP
case *net.UDPAddr:
ip = addr.IP
default:
// From the standard library, just Unix sockets.
// If you invent your own, handle it.
return false
}
if tcpaddr.IP.IsLoopback() {
if ip.IsLoopback() {
return true
}
@@ -450,14 +463,14 @@ func (s *Service) isLAN(addr net.Addr) bool {
l.Debugln("Network", lan, "is malformed:", err)
continue
}
if ipnet.Contains(tcpaddr.IP) {
if ipnet.Contains(ip) {
return true
}
}
lans, _ := osutil.GetLans()
for _, lan := range lans {
if lan.Contains(tcpaddr.IP) {
if lan.Contains(ip) {
return true
}
}
+3 -1
View File
@@ -7,6 +7,9 @@ import (
)
type FilteredConn struct {
// Alignment
deadline atomic.Value
source *PacketFilter
priority int
@@ -14,7 +17,6 @@ type FilteredConn struct {
filter Filter
deadline atomic.Value
closed chan struct{}
}
+4 -3
View File
@@ -26,13 +26,14 @@ func NewPacketFilter(conn net.PacketConn) *PacketFilter {
// PacketFilter embeds a net.PacketConn to perform the filtering.
type PacketFilter struct {
// Alignment
dropped uint64
overflow uint64
net.PacketConn
conns []*FilteredConn
mut sync.Mutex
dropped uint64
overflow uint64
}
// NewConn returns a new net.PacketConn object which filters packets based
+2 -2
View File
@@ -29,7 +29,7 @@
"importpath": "github.com/AudriusButkevicius/pfilter",
"repository": "https://github.com/AudriusButkevicius/pfilter",
"vcs": "git",
"revision": "09b3cfdd04de89f0196caecb0b335d7149a6593a",
"revision": "56143fe9cebe95636de1275acf30fcca36a1383d",
"branch": "master",
"notests": true
},
@@ -679,4 +679,4 @@
"notests": true
}
]
}
}