MCPcopy Create free account
hub / github.com/kataras/iris / RemoteAddr

Method RemoteAddr

context/context.go:1122–1156  ·  view source on GitHub ↗

RemoteAddr tries to parse and return the real client's request IP. Based on allowed headers names that can be modified from Configuration.RemoteAddrHeaders. If parse based on these headers fail then it will return the Request's `RemoteAddr` field which is filled by the server before the HTTP handl

()

Source from the content-addressed store, hash-verified

1120// Configuration.WithoutRemoteAddrHeader(...) and
1121// Configuration.RemoteAddrPrivateSubnetsW for more.
1122func (ctx *Context) RemoteAddr() string {
1123 if remoteHeaders := ctx.app.ConfigurationReadOnly().GetRemoteAddrHeaders(); len(remoteHeaders) > 0 {
1124 privateSubnets := ctx.app.ConfigurationReadOnly().GetRemoteAddrPrivateSubnets()
1125
1126 for _, headerName := range remoteHeaders {
1127 ipAddresses := strings.Split(ctx.GetHeader(headerName), ",")
1128 if ip, ok := netutil.GetIPAddress(ipAddresses, privateSubnets); ok {
1129 return ip
1130 }
1131 }
1132
1133 if ctx.app.ConfigurationReadOnly().GetRemoteAddrHeadersForce() {
1134 for _, headerName := range remoteHeaders {
1135 // return the first valid IP,
1136 // even if it's a part of a private network.
1137 ipAddresses := strings.Split(ctx.GetHeader(headerName), ",")
1138 for _, addr := range ipAddresses {
1139 if ip, _, err := net.SplitHostPort(addr); err == nil {
1140 return ip
1141 }
1142 }
1143 }
1144 }
1145 }
1146
1147 addr := strings.TrimSpace(ctx.request.RemoteAddr)
1148 if addr != "" {
1149 // if addr has port use the net.SplitHostPort otherwise(error occurs) take as it is
1150 if ip, _, err := net.SplitHostPort(addr); err == nil {
1151 return ip
1152 }
1153 }
1154
1155 return addr
1156}
1157
1158// TrimHeaderValue returns the "v[0:first space or semicolon]".
1159func TrimHeaderValue(v string) string {

Callers 15

StringMethod · 0.95
LogFailureFunction · 0.80
OnChatMethod · 0.80
mainFunction · 0.80
indexFunction · 0.80
IPRateLimitFunction · 0.80
server.goFile · 0.80
beforeSaveFunction · 0.80
beforeSaveFunction · 0.80
logThisMiddlewareFunction · 0.80
mainFunction · 0.80

Calls 6

GetHeaderMethod · 0.95
GetIPAddressFunction · 0.92
GetRemoteAddrHeadersMethod · 0.65
ConfigurationReadOnlyMethod · 0.65

Tested by

no test coverage detected