-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipaddrs.go
183 lines (165 loc) · 4.54 KB
/
ipaddrs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) 2022 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package net
import (
"fmt"
"net"
"net/http"
"regexp"
"strings"
)
var rxSplitForwardedFor = regexp.MustCompile(`\s*,\s*`)
func GetProxyIpFromRequest(r *http.Request) (ip string, err error) {
xForwardedFor := r.Header.Get("X-Forwarded-FOR")
switch {
case xForwardedFor != "":
if parts := rxSplitForwardedFor.Split(xForwardedFor, -1); len(parts) > 0 {
// the first non-remote-addr IP address
if len(parts) == 1 {
ip = parts[0]
} else {
ip = parts[1]
}
}
return
}
err = fmt.Errorf("request not proxied")
return
}
func GetIpFromRequest(r *http.Request) (ip string, err error) {
// See: https://golangbyexample.com/golang-ip-address-http-request/
// Get IP from CF-Connecting-IP header
ip = r.Header.Get("Cf-Connecting-Ip")
if netIP := net.ParseIP(ip); netIP != nil {
ip = netIP.String()
// log.DebugF("cf-connecting-ip: %v", ip)
return
}
// Get IP from X-FORWARDED-FOR header
xff := r.Header.Get("X-Forwarded-For")
ips := rxSplitForwardedFor.Split(xff, -1)
if len(ips) > 0 {
if netIP := net.ParseIP(ips[0]); netIP != nil {
ip = netIP.String()
// log.DebugF("x-forward-for: %v", ip)
return
}
}
// Get IP from the X-REAL-IP header
ip = r.Header.Get("X-Real-IP")
if netIP := net.ParseIP(ip); netIP != nil {
ip = netIP.String()
// log.DebugF("x-real-ip: %v", ip)
return
}
// Get IP from RemoteAddr
if ip = GetRemoteAddr(r); ip != "" {
// log.DebugF("remoteAddr: %v", ip)
return
}
err = fmt.Errorf("remote address not found")
return
}
func GetRemoteAddr(r *http.Request) (ip string) {
if sip, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
if netIP := net.ParseIP(sip); netIP != nil {
ip = netIP.String()
return
}
}
if netIP := net.ParseIP(r.RemoteAddr); netIP != nil {
ip = netIP.String()
}
return
}
func ParseIpFromRequest(r *http.Request) (ip net.IP, err error) {
var address string
if address, err = GetIpFromRequest(r); err == nil {
if host, _, ee := net.SplitHostPort(address); ee == nil {
address = host
}
return net.ParseIP(address), nil
}
return
}
func IsIpInRange(ip string, cidr string) bool {
if netIp := net.ParseIP(ip); netIp != nil {
if _, subnet, err := net.ParseCIDR(cidr); err == nil {
return subnet.Contains(netIp)
}
}
return false
}
func IsNetIpInRange(ip net.IP, cidr string) bool {
if _, subnet, err := net.ParseCIDR(cidr); err == nil {
return subnet.Contains(ip)
}
return false
}
// CheckRequestIpWithList returns TRUE if the remote IP in an http.Request is
// from an IP in the given list of CIDR ranges
func CheckRequestIpWithList(req *http.Request, list []string) bool {
if ip, err := ParseIpFromRequest(req); err == nil {
for _, cidr := range list {
if IsNetIpInRange(ip, cidr) {
return true
}
}
}
return false
}
// IsNetIpPrivate returns true if the IP address given is within any of the
// local or private network CIDR ranges (not public IP address space)
func IsNetIpPrivate(ip net.IP) (private bool) {
for _, cidr := range PrivateNetworks {
if private = cidr.Contains(ip); private {
break
}
}
return
}
func ParseIP(input ...string) (ips []net.IP, err error) {
for _, lines := range input {
for _, line := range strings.Split(lines, "\n") {
for _, part := range strings.Split(line, " ") {
if cleaned := strings.TrimSpace(part); cleaned != "" {
if parsed := net.ParseIP(cleaned); parsed == nil {
err = fmt.Errorf("%q is not an address", cleaned)
return
} else {
ips = append(ips, parsed)
}
}
}
}
}
return
}
func ParseCIDR(input ...string) (cidrs []*net.IPNet, err error) {
for _, lines := range input {
for _, line := range strings.Split(lines, "\n") {
for _, part := range strings.Split(line, " ") {
if cleaned := strings.TrimSpace(part); cleaned != "" {
if _, network, ee := net.ParseCIDR(cleaned); ee != nil {
err = fmt.Errorf("error parsing CIDR %q: %w", cleaned, ee)
return
} else {
cidrs = append(cidrs, network)
}
}
}
}
}
return
}