/
ipcheck.go
79 lines (67 loc) · 1.47 KB
/
ipcheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"errors"
"fmt"
"log"
"net"
"strings"
)
func parseAllowedIPs(allowedIPs string) (map[uint32]struct{}, error) {
if len(allowedIPs) == 0 {
return nil, nil
}
m := make(map[uint32]struct{})
for _, ipStr := range strings.Split(allowedIPs, ",") {
ip := net.ParseIP(ipStr)
if ip == nil {
return nil, fmt.Errorf("invalid ip %q", ipStr)
}
ip = ip.To4()
if ip == nil {
return nil, fmt.Errorf("invalid IPv4 %q", ipStr)
}
n := ip2Uint32(ip)
m[n] = struct{}{}
}
return m, nil
}
type ipCheckListener struct {
net.Listener
allowedIPs map[uint32]struct{}
}
func (ln *ipCheckListener) Accept() (net.Conn, error) {
for {
conn, err := ln.accept()
if err == errDisallowedIP {
log.Printf("%q<->%q: %s", conn.RemoteAddr(), conn.LocalAddr(), err)
conn.Close()
continue
}
return conn, err
}
}
func (ln *ipCheckListener) accept() (net.Conn, error) {
conn, err := ln.Listener.Accept()
if err != nil {
return nil, err
}
raddr := conn.RemoteAddr()
tcpAddr, ok := raddr.(*net.TCPAddr)
if !ok {
return conn, nil
}
ip := tcpAddr.IP.To4()
if ip == nil {
return conn, nil
}
n := ip2Uint32(ip)
if _, ok := ln.allowedIPs[n]; !ok {
// The connection is closed by the caller
return conn, errDisallowedIP
}
return conn, nil
}
var errDisallowedIP = errors.New("disallowed access for the given client ip")
func ip2Uint32(ip []byte) uint32 {
return uint32(ip[3]) | (uint32(ip[2]) << 8) | (uint32(ip[1]) << 16) | (uint32(ip[0]) << 24)
}