forked from ServiceStackV3/ServiceStack.Contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpointHandlerBase.cs
More file actions
145 lines (119 loc) · 4.45 KB
/
EndpointHandlerBase.cs
File metadata and controls
145 lines (119 loc) · 4.45 KB
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Web;
using ServiceStack.Common.Extensions;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
namespace ServiceStack.WebHost.Endpoints.Support
{
public class EndpointHandlerBase
{
private static readonly Dictionary<byte[], byte[]> NetworkInterfaceIpv4Addresses = new Dictionary<byte[], byte[]>();
private static readonly byte[][] NetworkInterfaceIpv6Addresses;
static EndpointHandlerBase()
{
IPAddressExtensions.GetAllNetworkInterfaceIpv4Addresses().ForEach((x, y) => NetworkInterfaceIpv4Addresses[x.GetAddressBytes()] = y.GetAddressBytes());
NetworkInterfaceIpv6Addresses = IPAddressExtensions.GetAllNetworkInterfaceIpv6Addresses().ConvertAll(x => x.GetAddressBytes()).ToArray();
}
protected static bool DefaultHandledRequest(HttpContext context)
{
if (context.Request.HttpMethod == HttpMethods.Options)
{
foreach (var globalResponseHeader in EndpointHost.Config.GlobalResponseHeaders)
{
context.Response.AddHeader(globalResponseHeader.Key, globalResponseHeader.Value);
}
return true;
}
return false;
}
protected static object ExecuteService(object request, EndpointAttributes endpointAttributes)
{
return EndpointHost.ExecuteService(request, endpointAttributes);
}
//Execute SOAP requests
protected static string ExecuteXmlService(string xmlRequest, EndpointAttributes endpointAttributes)
{
return EndpointHost.ExecuteXmlService(xmlRequest, endpointAttributes);
}
public EndpointAttributes GetEndpointAttributes(System.ServiceModel.OperationContext operationContext)
{
if (!EndpointHost.Config.EnableAccessRestrictions) return default(EndpointAttributes);
var portRestrictions = default(EndpointAttributes);
var ipAddress = GetIpAddress(operationContext);
portRestrictions |= GetIpAddressEndpointAttributes(ipAddress);
//TODO: work out if the request was over a secure channel
//portRestrictions |= request.IsSecureConnection ? PortRestriction.Secure : PortRestriction.InSecure;
return portRestrictions;
}
public static IPAddress GetIpAddress(System.ServiceModel.OperationContext context)
{
#if !MONO
var prop = context.IncomingMessageProperties;
if (context.IncomingMessageProperties.ContainsKey(System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name))
{
var endpoint = prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name]
as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
if (endpoint != null)
{
return IPAddress.Parse(endpoint.Address);
}
}
#endif
return null;
}
public EndpointAttributes GetEndpointAttributes(HttpRequest request)
{
var portRestrictions = EndpointAttributes.None;
portRestrictions |= HttpMethods.GetEndpointAttribute(request.RequestType);
portRestrictions |= request.IsSecureConnection ? EndpointAttributes.Secure : EndpointAttributes.InSecure;
var ipAddress = IPAddress.Parse(request.UserHostAddress);
portRestrictions |= GetIpAddressEndpointAttributes(ipAddress);
return portRestrictions;
}
private static EndpointAttributes GetIpAddressEndpointAttributes(IPAddress ipAddress)
{
if (IPAddress.IsLoopback(ipAddress))
return EndpointAttributes.Localhost;
return IsInLocalSubnet(ipAddress)
? EndpointAttributes.LocalSubnet
: EndpointAttributes.External;
}
private static bool IsInLocalSubnet(IPAddress ipAddress)
{
var ipAddressBytes = ipAddress.GetAddressBytes();
switch (ipAddress.AddressFamily)
{
case AddressFamily.InterNetwork:
foreach (var localIpv4AddressAndMask in NetworkInterfaceIpv4Addresses)
{
if (ipAddressBytes.IsInSameIpv4Subnet(localIpv4AddressAndMask.Key, localIpv4AddressAndMask.Value))
{
return true;
}
}
break;
case AddressFamily.InterNetworkV6:
foreach (var localIpv6Address in NetworkInterfaceIpv6Addresses)
{
if (ipAddressBytes.IsInSameIpv6Subnet(localIpv6Address))
{
return true;
}
}
break;
}
return false;
}
protected static void AssertOperationExists(string operationName, Type type)
{
if (type == null)
{
throw new NotImplementedException(
string.Format("The operation '{0}' does not exist for this service", operationName));
}
}
}
}