Initial commit after restructure
This commit is contained in:
16
network/netutils/cleandns.go
Normal file
16
network/netutils/cleandns.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package netutils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
// cleanDomainRegex = regexp.MustCompile("^(((?!-))(xn--)?[a-z0-9-_]{0,61}[a-z0-9]{1,1}\\.)*(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\\.[a-z]{2,}\\.)$")
|
||||
cleanDomainRegex = regexp.MustCompile("^((xn--)?[a-z0-9-_]{0,61}[a-z0-9]{1,1}\\.)*(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\\.[a-z]{2,}\\.)$")
|
||||
)
|
||||
|
||||
func IsValidFqdn(fqdn string) bool {
|
||||
return cleanDomainRegex.MatchString(fqdn)
|
||||
}
|
||||
98
network/netutils/ip.go
Normal file
98
network/netutils/ip.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package netutils
|
||||
|
||||
import "net"
|
||||
|
||||
// IP types
|
||||
const (
|
||||
hostLocal int8 = iota
|
||||
linkLocal
|
||||
siteLocal
|
||||
global
|
||||
localMulticast
|
||||
globalMulticast
|
||||
invalid
|
||||
)
|
||||
|
||||
func classifyAddress(ip net.IP) int8 {
|
||||
if ip4 := ip.To4(); ip4 != nil {
|
||||
// IPv4
|
||||
switch {
|
||||
case ip4[0] == 127:
|
||||
// 127.0.0.0/8
|
||||
return hostLocal
|
||||
case ip4[0] == 169 && ip4[1] == 254:
|
||||
// 169.254.0.0/16
|
||||
return linkLocal
|
||||
case ip4[0] == 10:
|
||||
// 10.0.0.0/8
|
||||
return siteLocal
|
||||
case ip4[0] == 172 && ip4[1]&0xf0 == 16:
|
||||
// 172.16.0.0/12
|
||||
return siteLocal
|
||||
case ip4[0] == 192 && ip4[1] == 168:
|
||||
// 192.168.0.0/16
|
||||
return siteLocal
|
||||
case ip4[0] == 224:
|
||||
// 224.0.0.0/8
|
||||
return localMulticast
|
||||
case ip4[0] >= 225 && ip4[0] <= 239:
|
||||
// 225.0.0.0/8 - 239.0.0.0/8
|
||||
return globalMulticast
|
||||
case ip4[0] >= 240:
|
||||
// 240.0.0.0/8 - 255.0.0.0/8
|
||||
return invalid
|
||||
default:
|
||||
return global
|
||||
}
|
||||
} else if len(ip) == net.IPv6len {
|
||||
// IPv6
|
||||
switch {
|
||||
case ip.Equal(net.IPv6loopback):
|
||||
return hostLocal
|
||||
case ip[0]&0xfe == 0xfc:
|
||||
// fc00::/7
|
||||
return siteLocal
|
||||
case ip[0] == 0xfe && ip[1]&0xc0 == 0x80:
|
||||
// fe80::/10
|
||||
return linkLocal
|
||||
case ip[0] == 0xff && ip[1] <= 0x05:
|
||||
// ff00::/16 - ff05::/16
|
||||
return localMulticast
|
||||
case ip[0] == 0xff:
|
||||
// other ff00::/8
|
||||
return globalMulticast
|
||||
default:
|
||||
return global
|
||||
}
|
||||
}
|
||||
return invalid
|
||||
}
|
||||
|
||||
// IPIsLocal returns true if the given IP is a site-local or link-local address
|
||||
func IPIsLocal(ip net.IP) bool {
|
||||
switch classifyAddress(ip) {
|
||||
case siteLocal:
|
||||
return true
|
||||
case linkLocal:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IPIsGlobal returns true if the given IP is a global address
|
||||
func IPIsGlobal(ip net.IP) bool {
|
||||
return classifyAddress(ip) == global
|
||||
}
|
||||
|
||||
// IPIsLinkLocal returns true if the given IP is a link-local address
|
||||
func IPIsLinkLocal(ip net.IP) bool {
|
||||
return classifyAddress(ip) == linkLocal
|
||||
}
|
||||
|
||||
// IPIsSiteLocal returns true if the given IP is a site-local address
|
||||
func IPIsSiteLocal(ip net.IP) bool {
|
||||
return classifyAddress(ip) == siteLocal
|
||||
}
|
||||
41
network/netutils/ip_test.go
Normal file
41
network/netutils/ip_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package netutils
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIPClassification(t *testing.T) {
|
||||
testClassification(t, net.IPv4(71, 87, 113, 211), global)
|
||||
testClassification(t, net.IPv4(127, 0, 0, 1), hostLocal)
|
||||
testClassification(t, net.IPv4(127, 255, 255, 1), hostLocal)
|
||||
testClassification(t, net.IPv4(192, 168, 172, 24), siteLocal)
|
||||
}
|
||||
|
||||
func testClassification(t *testing.T, ip net.IP, expectedClassification int8) {
|
||||
c := classifyAddress(ip)
|
||||
if c != expectedClassification {
|
||||
t.Errorf("%s is %s, expected %s", ip, classificationString(c), classificationString(expectedClassification))
|
||||
}
|
||||
}
|
||||
|
||||
func classificationString(c int8) string {
|
||||
switch c {
|
||||
case hostLocal:
|
||||
return "hostLocal"
|
||||
case linkLocal:
|
||||
return "linkLocal"
|
||||
case siteLocal:
|
||||
return "siteLocal"
|
||||
case global:
|
||||
return "global"
|
||||
case localMulticast:
|
||||
return "localMulticast"
|
||||
case globalMulticast:
|
||||
return "globalMulticast"
|
||||
case invalid:
|
||||
return "invalid"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
51
network/netutils/tcpassembly.go
Normal file
51
network/netutils/tcpassembly.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright Safing ICS Technologies GmbH. Use of this source code is governed by the AGPL license that can be found in the LICENSE file.
|
||||
|
||||
package netutils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/tcpassembly"
|
||||
)
|
||||
|
||||
type SimpleStreamAssemblerManager struct {
|
||||
InitLock sync.Mutex
|
||||
lastAssembler *SimpleStreamAssembler
|
||||
}
|
||||
|
||||
func (m *SimpleStreamAssemblerManager) New(net, transport gopacket.Flow) tcpassembly.Stream {
|
||||
assembler := new(SimpleStreamAssembler)
|
||||
m.lastAssembler = assembler
|
||||
return assembler
|
||||
}
|
||||
|
||||
func (m *SimpleStreamAssemblerManager) GetLastAssembler() *SimpleStreamAssembler {
|
||||
// defer func() {
|
||||
// m.lastAssembler = nil
|
||||
// }()
|
||||
return m.lastAssembler
|
||||
}
|
||||
|
||||
type SimpleStreamAssembler struct {
|
||||
Cumulated []byte
|
||||
CumulatedLen int
|
||||
Complete bool
|
||||
}
|
||||
|
||||
func NewSimpleStreamAssembler() *SimpleStreamAssembler {
|
||||
return new(SimpleStreamAssembler)
|
||||
}
|
||||
|
||||
// Reassembled implements tcpassembly.Stream's Reassembled function.
|
||||
func (a *SimpleStreamAssembler) Reassembled(reassembly []tcpassembly.Reassembly) {
|
||||
for _, entry := range reassembly {
|
||||
a.Cumulated = append(a.Cumulated, entry.Bytes...)
|
||||
}
|
||||
a.CumulatedLen = len(a.Cumulated)
|
||||
}
|
||||
|
||||
// ReassemblyComplete implements tcpassembly.Stream's ReassemblyComplete function.
|
||||
func (a *SimpleStreamAssembler) ReassemblyComplete() {
|
||||
a.Complete = true
|
||||
}
|
||||
Reference in New Issue
Block a user