初始化
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (C) Alibaba Cloud Computing
|
||||
* All rights reserved.
|
||||
*
|
||||
* 版权所有 (C)阿里云计算有限公司
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using Aliyun.Api.LOG.Utilities;
|
||||
|
||||
namespace Aliyun.Api.LOG.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示访问阿里云服务的配置信息。
|
||||
/// </summary>
|
||||
internal class ClientConfiguration : ICloneable
|
||||
{
|
||||
private string _userAgent = LogConsts.CONST_USER_AGENT_PREFIX + typeof(ClientConfiguration).Assembly.GetName().Version.ToString();
|
||||
|
||||
private int _connectionTimeout = LogConsts.DEFAULT_SLS_CONNECT_TIMEOUT;
|
||||
private int _maxErrorRetry = LogConsts.DEFAULT_SLS_RETRY_TIME;
|
||||
private int _readWrtTimeout = LogConsts.DEFAULT_SLS_READWRT_TIMEOUT;
|
||||
private int _retryInterval = LogConsts.DEFAULT_SLS_RETRY_INTERVALBASE;
|
||||
|
||||
public int RetryIntervalBase
|
||||
{
|
||||
get { return _retryInterval; }
|
||||
set { _retryInterval = value; }
|
||||
}
|
||||
|
||||
public int ReadWriteTimeout
|
||||
{
|
||||
get { return _readWrtTimeout; }
|
||||
set { _readWrtTimeout = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设置访问请求的User-Agent。
|
||||
/// </summary>
|
||||
public string UserAgent
|
||||
{
|
||||
get { return _userAgent; }
|
||||
set { _userAgent = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置代理服务器的地址。
|
||||
/// </summary>
|
||||
public string ProxyHost { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置代理服务器的端口。
|
||||
/// </summary>
|
||||
public int ProxyPort {get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置用户名。
|
||||
/// </summary>
|
||||
public string ProxyUserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置密码。
|
||||
/// </summary>
|
||||
public string ProxyPassword { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置代理服务器授权用户所在的域。
|
||||
/// </summary>
|
||||
public string ProxyDomain { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置连接的超时时间,单位为毫秒。
|
||||
/// </summary>
|
||||
public int ConnectionTimeout
|
||||
{
|
||||
get { return _connectionTimeout; }
|
||||
set { _connectionTimeout = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置请求发生错误时最大的重试次数。
|
||||
/// </summary>
|
||||
public int MaxErrorRetry
|
||||
{
|
||||
get { return _maxErrorRetry; }
|
||||
set { _maxErrorRetry = value; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 初始化新的<see cref="ClientConfiguration"/>的实例。
|
||||
/// </summary>
|
||||
public ClientConfiguration()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取该实例的拷贝。
|
||||
/// </summary>
|
||||
/// <returns>该实例的拷贝。</returns>
|
||||
public object Clone()
|
||||
{
|
||||
ClientConfiguration config = new ClientConfiguration();
|
||||
config.ConnectionTimeout = this.ConnectionTimeout;
|
||||
config.MaxErrorRetry = this.MaxErrorRetry;
|
||||
config.ProxyDomain = this.ProxyDomain;
|
||||
config.ProxyHost = this.ProxyHost;
|
||||
config.ProxyPassword = this.ProxyPassword;
|
||||
config.ProxyPort = this.ProxyPort;
|
||||
config.ProxyUserName = this.ProxyUserName;
|
||||
config.UserAgent = this.UserAgent;
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
285
bin/aliyun-log-csharp-sdk-master/SLSSDK/Utilities/ClientTools.cs
Normal file
285
bin/aliyun-log-csharp-sdk-master/SLSSDK/Utilities/ClientTools.cs
Normal file
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* Copyright (C) Alibaba Cloud Computing
|
||||
* All rights reserved.
|
||||
*
|
||||
* 版权所有 (C)阿里云计算有限公司
|
||||
*/
|
||||
using Aliyun.Api.LOG.Common.Authentication;
|
||||
using Aliyun.Api.LOG.Common.Communication;
|
||||
using Aliyun.Api.LOG;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.IO.Compression;
|
||||
using LZ4Sharp;
|
||||
using Lz4;
|
||||
|
||||
namespace Aliyun.Api.LOG.Utilities
|
||||
{
|
||||
internal class LogClientTools
|
||||
{
|
||||
public static byte[] CompressToZlib(byte[] buffer)
|
||||
{
|
||||
using (MemoryStream compressStream = new MemoryStream())
|
||||
{
|
||||
using (ZLibNet.ZLibStream gz = new ZLibNet.ZLibStream(compressStream, ZLibNet.CompressionMode.Compress, ZLibNet.CompressionLevel.Level6, true))
|
||||
{
|
||||
gz.Write(buffer, 0, buffer.Length);
|
||||
compressStream.Seek(0, SeekOrigin.Begin);
|
||||
byte[] compressBuffer = new byte[compressStream.Length];
|
||||
compressStream.Read(compressBuffer, 0, compressBuffer.Length);
|
||||
return compressBuffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static byte[] DecompressFromZlib(Stream stream, int rawSize)
|
||||
{
|
||||
using (stream)
|
||||
{
|
||||
using (ZLibNet.ZLibStream dz = new ZLibNet.ZLibStream(stream, ZLibNet.CompressionMode.Decompress))
|
||||
{
|
||||
byte[] buffer = new byte[rawSize];
|
||||
dz.Read(buffer, 0, buffer.Length);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static byte[] CompressToLz4(byte[] buffer)
|
||||
{
|
||||
var compressor = LZ4CompressorFactory.CreateNew();
|
||||
return compressor.Compress(buffer);
|
||||
}
|
||||
public static byte[] DecompressFromLZ4(Stream stream, int rawLength)
|
||||
{
|
||||
using (stream)
|
||||
{
|
||||
using (Lz4DecoderStream streamInner = new Lz4DecoderStream(stream))
|
||||
{
|
||||
byte[] output = new byte[rawLength];
|
||||
streamInner.Read(output, 0, rawLength);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static string GetMd5Value(byte[] buffer)
|
||||
{
|
||||
return GetMd5Value(buffer,0,buffer.Length);
|
||||
}
|
||||
public static string GetMd5Value(byte[] buffer,int offset, int count)
|
||||
{
|
||||
MD5 hash = MD5.Create(LogConsts.NAME_MD5);
|
||||
StringBuilder returnStr = new StringBuilder();
|
||||
byte[] md5hash = hash.ComputeHash(buffer, offset, count);
|
||||
if (md5hash != null)
|
||||
{
|
||||
for (int i = 0; i < md5hash.Length; i++)
|
||||
{
|
||||
returnStr.Append(md5hash[i].ToString("X2").ToUpper());
|
||||
}
|
||||
}
|
||||
return returnStr.ToString();
|
||||
}
|
||||
|
||||
public static void ResponseErrorCheck(ServiceResponse response, ServiceCredentials credentials)
|
||||
{
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
String requestId = "";
|
||||
response.Headers.TryGetValue(LogConsts.NAME_HEADER_REQUESTID, out requestId);
|
||||
JObject body = ParserResponseToJObject(response.Content);
|
||||
throw new LogException(body[LogConsts.NAME_ERROR_CODE].ToString(), body[LogConsts.NAME_ERROR_MESSAGE].ToString(), requestId);
|
||||
}
|
||||
}
|
||||
internal class KeyValueComparer : IComparer<KeyValuePair<string, string>>
|
||||
{
|
||||
public int Compare(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
|
||||
{
|
||||
int rtu = String.Compare(x.Key, y.Key, StringComparison.Ordinal);
|
||||
return rtu == 0 ? String.Compare(x.Value, y.Value, StringComparison.Ordinal) : rtu;
|
||||
}
|
||||
}
|
||||
private static String MapEnumMethodToString(HttpMethod httpMethod)
|
||||
{
|
||||
switch (httpMethod)
|
||||
{
|
||||
case HttpMethod.Get:
|
||||
return LogConsts.NAME_HTTP_GET;
|
||||
case HttpMethod.Post:
|
||||
return LogConsts.NAME_HTTP_POST;
|
||||
case HttpMethod.Put:
|
||||
return LogConsts.NAME_HTTP_PUT;
|
||||
case HttpMethod.Head:
|
||||
return LogConsts.NAME_HTTP_HEAD;
|
||||
case HttpMethod.Delete:
|
||||
return LogConsts.NAME_HTTP_DELETE;
|
||||
case HttpMethod.Options:
|
||||
return LogConsts.NAME_HTTP_OPTIONS;
|
||||
default:
|
||||
Debug.Assert(false, "invalid http method");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
private static string GetRequestString(IEnumerable<KeyValuePair<string, string>> parameters, String kvDelimiter, String separator)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder("");
|
||||
if (parameters != null)
|
||||
{
|
||||
bool isFirst = true;
|
||||
foreach (var p in parameters)
|
||||
{
|
||||
if (!isFirst)
|
||||
{
|
||||
stringBuilder.Append(separator);
|
||||
}
|
||||
isFirst = false;
|
||||
stringBuilder.Append(p.Key);
|
||||
if (p.Value != null)
|
||||
{
|
||||
stringBuilder.Append(kvDelimiter).Append(p.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
internal static String BuildHeaderSigStr(IDictionary<String, String> headers)
|
||||
{
|
||||
List<KeyValuePair<string, string>> headerLst = new List<KeyValuePair<string, string>>();
|
||||
foreach (KeyValuePair<string, string> pair in headers)
|
||||
{
|
||||
if ((pair.Key.StartsWith("x-log-") && pair.Key.CompareTo(LogConsts.NAME_HEADER_DATE) != 0)
|
||||
|| pair.Key.StartsWith("x-acs-"))
|
||||
{
|
||||
headerLst.Add(new KeyValuePair<String, String>(pair.Key.Trim().ToLower(), pair.Value.Trim()));
|
||||
}
|
||||
}
|
||||
headerLst.Sort(new KeyValueComparer());
|
||||
StringBuilder reqUri = new StringBuilder();
|
||||
reqUri.Append(LogClientTools.GetValeFromDic(headers, LogConsts.NAME_HEADER_MD5)).Append("\n")
|
||||
.Append(LogClientTools.GetValeFromDic(headers, LogConsts.NAME_HEADER_CONTENTTYPE)).Append("\n")
|
||||
.Append(LogClientTools.GetValeFromDic(headers, LogConsts.NAME_HEADER_DATE)).Append("\n")
|
||||
.Append(GetRequestString(headerLst, ":", "\n"));
|
||||
return reqUri.ToString();
|
||||
}
|
||||
internal static String SigInternal(String source,String accessKeyId, String accessKey)
|
||||
{
|
||||
ServiceSignature signAlthm = ServiceSignature.Create();
|
||||
return LogConsts.PREFIX_VALUE_HEADER_AUTH + accessKeyId + ":" + signAlthm.ComputeSignature(accessKey, source);
|
||||
}
|
||||
public static String Signature(IDictionary<String, String> headers, String accessKeyId, String accessKey)
|
||||
{
|
||||
return SigInternal(BuildHeaderSigStr(headers), accessKeyId, accessKey);
|
||||
}
|
||||
public static String Signature(IDictionary<String,String> headers,IDictionary<String,String> paramDic,HttpMethod method, String resource,String accessKeyId,String accessKey)
|
||||
{
|
||||
List<KeyValuePair<string, string>> paramLst = new List<KeyValuePair<string, string>>(paramDic);
|
||||
|
||||
paramLst.Sort(new KeyValueComparer());
|
||||
|
||||
StringBuilder reqUri = new StringBuilder();
|
||||
reqUri.Append(MapEnumMethodToString(method)).Append("\n")
|
||||
.Append(BuildHeaderSigStr(headers)).Append("\n")
|
||||
.Append(resource)
|
||||
.Append((paramLst != null && paramLst.Count > 0) ? ("?" + GetRequestString(paramLst, "=", "&")) : (""));
|
||||
return SigInternal(reqUri.ToString(), accessKeyId, accessKey);
|
||||
}
|
||||
internal static String GetValeFromDic(IDictionary<String, String> dic, String keyName)
|
||||
{
|
||||
String value = null;
|
||||
if (dic.TryGetValue(keyName, out value))
|
||||
return value;
|
||||
return "";
|
||||
}
|
||||
public static JArray ParserResponseToJArray(Stream response)
|
||||
{
|
||||
using (response)
|
||||
{
|
||||
StreamReader sr = null;
|
||||
String json = null;
|
||||
try
|
||||
{
|
||||
sr = new StreamReader(response, Encoding.UTF8);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (sr != null)
|
||||
sr.Close();
|
||||
throw new LogException("LOGBadResponse", "The response from the server is empty", e);
|
||||
}
|
||||
try
|
||||
{
|
||||
json = sr.ReadToEnd();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new LogException("LOGBadResponse", "Io exception happened when parse the response data : ", e);
|
||||
}
|
||||
catch (OutOfMemoryException e)
|
||||
{
|
||||
throw new LogException("LOGBadResponse", "There is not enough memory to continue the execution of parsing the response data : ", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
sr.Close();
|
||||
}
|
||||
try
|
||||
{
|
||||
JArray obj = JArray.Parse(json);
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new LogException("LOGBadResponse", "The response is not valid json string : " + json, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static JObject ParserResponseToJObject(Stream response)
|
||||
{
|
||||
using (response)
|
||||
{
|
||||
StreamReader sr = null;
|
||||
String json = null;
|
||||
try
|
||||
{
|
||||
sr = new StreamReader(response, Encoding.UTF8);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (sr != null)
|
||||
sr.Close();
|
||||
throw new LogException("LOGBadResponse", "The response from the server is empty", e);
|
||||
}
|
||||
try
|
||||
{
|
||||
json = sr.ReadToEnd();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new LogException("LOGBadResponse", "Io exception happened when parse the response data : ", e);
|
||||
}
|
||||
catch (OutOfMemoryException e)
|
||||
{
|
||||
throw new LogException("LOGBadResponse", "There is not enough memory to continue the execution of parsing the response data : ", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
sr.Close();
|
||||
}
|
||||
try
|
||||
{
|
||||
JObject obj = JObject.Parse(json);
|
||||
return obj;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new LogException("LOGBadResponse", "The response is not valid json string : " + json, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
100
bin/aliyun-log-csharp-sdk-master/SLSSDK/Utilities/Consts.cs
Normal file
100
bin/aliyun-log-csharp-sdk-master/SLSSDK/Utilities/Consts.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) Alibaba Cloud Computing
|
||||
* All rights reserved.
|
||||
*
|
||||
* 版权所有 (C)阿里云计算有限公司
|
||||
*/
|
||||
using Aliyun.Api.LOG.Common.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace Aliyun.Api.LOG.Utilities
|
||||
{
|
||||
internal class LogConsts
|
||||
{
|
||||
public const string CONST_USER_AGENT_PREFIX = "log-dotnet-sdk-v-";
|
||||
|
||||
public const int LIMIT_LOG_SIZE = 3 * 1024 * 1024;
|
||||
public const int LIMIT_LOG_COUNT = 4096;
|
||||
public const int DEFAULT_SLS_RETRY_TIME = 3;
|
||||
public const int DEFAULT_SLS_CONNECT_TIMEOUT = 5 * 1000;
|
||||
public const int DEFAULT_SLS_READWRT_TIMEOUT = 20 * 1000;
|
||||
public const int DEFAULT_SLS_RETRY_INTERVALBASE = 100;
|
||||
|
||||
public const String NAME_ERROR_CODE = "errorCode";
|
||||
public const String NAME_ERROR_MESSAGE = "errorMessage";
|
||||
public const String NAME_LISTLOGSTORE_ITEM = "logstores";
|
||||
public const String NAME_LISTLOGSTORE_TOTAL = "count";
|
||||
public const String NAME_LISTTOPIC_COUNT = "count";
|
||||
public const String NAME_LISTTOPIC_TOPICS = "topics";
|
||||
public const String NAME_LISTTOPIC_NEXTTOKEN = "next_token";
|
||||
public const String NAME_GETSTATUS_PROGRESS = "progress";
|
||||
public const String NAME_GETSTATUS_COUNT = "count";
|
||||
public const String NAME_GETSTATUS_FROM = "from";
|
||||
public const String NAME_GETSTATUS_TO = "to";
|
||||
public const String NAME_GETSTATUS_HISTOGRAM = "histograms";
|
||||
public const String NAME_GETDATA_COUNT = "count";
|
||||
public const String NAME_GETDATA_PROGRESS = "progress";
|
||||
public const String NAME_GETDATA_LOGS = "logs";
|
||||
public const String NAME_GETDATA_TIME = "__time__";
|
||||
public const String NAME_GETDATA_SOURCE = "__source__";
|
||||
|
||||
public const string NAME_MD5 = "MD5";
|
||||
|
||||
public const String NAME_HTTP_GET = "GET";
|
||||
public const String NAME_HTTP_POST = "POST";
|
||||
public const String NAME_HTTP_PUT = "PUT";
|
||||
public const String NAME_HTTP_DELETE = "DELETE";
|
||||
public const String NAME_HTTP_PATCH = "PATCH";
|
||||
public const String NAME_HTTP_HEAD = "HEAD";
|
||||
public const String NAME_HTTP_OPTIONS = "OPTIONS";
|
||||
|
||||
public const String NAME_HEADER_AUTH = HttpHeaders.Authorization;
|
||||
public const String PREFIX_VALUE_HEADER_AUTH = "LOG" + " ";
|
||||
public const String NAME_HEADER_CONTENTTYPE = HttpHeaders.ContentType;
|
||||
public const String JSONVALUE_HEADER_CONTENTTYPE = "application/json";
|
||||
public const String PBVALUE_HEADER_CONTENTTYPE = "application/x-protobuf";
|
||||
public const String NAME_HEADER_MD5 = HttpHeaders.ContentMd5;
|
||||
public const String NAME_HEADER_HOST = "Host";
|
||||
public const String NAME_HEADER_APIVERSION = "x-log-apiversion";
|
||||
public const String VALUE_HEADER_APIVERSION = "0.6.0";
|
||||
public const String NAME_HEADER_ACCESSKEYID = "x-log-accesskeyid";
|
||||
public const String NAME_HEADER_COMPRESSTYPE = "x-log-compresstype";
|
||||
public const String NAME_HEADER_REQUESTID = "x-log-requestid";
|
||||
public const String NAME_HEADER_DATE = "x-log-date";
|
||||
public const String NAME_HEADER_X_LOG_COUNT = "x-log-count";
|
||||
public const String NAME_HEADER_X_LOG_NEXT_TOKEN = "x-log-nexttoken";
|
||||
public const String NAME_HEADER_X_LOG_PROGRESS = "x-log-progress";
|
||||
public const String NAME_HEADER_ACCEPT_ENCODING = "Accept-Encoding";
|
||||
public const String NAME_HEADER_ACCEPT = "Accept";
|
||||
public const String VALUE_HEADER_COMPRESSTYPE_DEFLATE = "deflate";
|
||||
public const String VALUE_HEADER_COMPRESSTYPE_LZ4 = "lz4";
|
||||
public const String NAME_HEADER_BODYRAWSIZE = "x-log-bodyrawsize";
|
||||
public const String NAME_HEADER_NEXT_CURSOR = "x-log-cursor";
|
||||
public const String NAME_HEADER_LOG_COUNT = "x-log-count";
|
||||
public const String NAME_HEADER_LOG_BODY_RAW_SIZE = "x-log-bodyrawsize";
|
||||
public const String NAME_HEADER_SIGMETHOD = "x-log-signaturemethod";
|
||||
public const String VALUE_HEADER_SIGMETHOD = "hmac-sha1";
|
||||
public const String NAME_HEADER_ACS_SECURITY_TOKEN = "x-acs-security-token";
|
||||
public const String RESOURCE_SEPARATOR = "/";
|
||||
public const String RESOURCE_LOGSTORES = RESOURCE_SEPARATOR + "logstores";
|
||||
public const String RESOURCE_SHARDS = RESOURCE_SEPARATOR + "shards";
|
||||
public const String PARAMETER_OFFSET = "offset";
|
||||
public const String PARAMETER_LINES = "line";
|
||||
public const String RESOURCE_TOPIC = "topic";
|
||||
public const String PARAMETER_TOKEN = "token";
|
||||
public const String PARAMETER_TYPE = "type";
|
||||
public const String VALUE_TYPE_CONTENT = "log";
|
||||
public const String VALUE_TYPE_STATUS = "histogram";
|
||||
public const String PARAMETER_TOPIC = "topic";
|
||||
public const String PARAMETER_FROM = "from";
|
||||
public const String PARAMETER_TO = "to";
|
||||
public const String PARAMETER_QUERY = "query";
|
||||
public const String PARAMETER_REVERSE = "reverse";
|
||||
|
||||
public const String STATUS_COMPLETE = "Complete";
|
||||
public const String STATUS_INCOMPLETE = "InComplete";
|
||||
}
|
||||
}
|
||||
71
bin/aliyun-log-csharp-sdk-master/SLSSDK/Utilities/IpUtils.cs
Normal file
71
bin/aliyun-log-csharp-sdk-master/SLSSDK/Utilities/IpUtils.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) Alibaba Cloud Computing
|
||||
* All rights reserved.
|
||||
*
|
||||
* 版权所有 (C)阿里云计算有限公司
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Aliyun.Api.LOG.Utilities
|
||||
{
|
||||
internal class IpUtils
|
||||
{
|
||||
public static bool IsInternalIP(IPAddress ip)
|
||||
{
|
||||
//According to RFC 1918 (http://www.faqs.org/rfcs/rfc1918.html). private IP ranges are as bellow
|
||||
// 10.0.0.0 - 10.255.255.255 (10/8 prefix)
|
||||
// 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
|
||||
// 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
|
||||
|
||||
byte[] addrs = ip.GetAddressBytes();
|
||||
if ((addrs[0] == 10) ||
|
||||
(addrs[0] == 192 && addrs[1] == 168) ||
|
||||
(addrs[0] == 172 && (addrs[1] >= 16) && (addrs[1] < 32)))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public static bool IsIpAddress(String str)
|
||||
{
|
||||
Regex regex = new Regex("^(\\d{1,3}\\.){3}\\d{1,3}");
|
||||
return regex.Match(str).Success;
|
||||
}
|
||||
public static string GetLocalMachinePrivateIp()
|
||||
{
|
||||
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
|
||||
string ip = "";
|
||||
foreach (NetworkInterface adapter in adapters)
|
||||
{
|
||||
if (adapter.OperationalStatus == OperationalStatus.Up)
|
||||
{
|
||||
IPInterfaceProperties IPInterfaceProperties = adapter.GetIPProperties();
|
||||
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
|
||||
foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
|
||||
{
|
||||
IPAddress addr = UnicastIPAddressInformation.Address;
|
||||
|
||||
if (!IPAddress.IsLoopback(addr) &&
|
||||
UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
if (IsInternalIP(addr))
|
||||
{
|
||||
ip = addr.ToString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!String.IsNullOrEmpty(ip))
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,544 @@
|
||||
#define CHECK_ARGS
|
||||
#define CHECK_EOF
|
||||
//#define LOCAL_SHADOW
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Lz4
|
||||
{
|
||||
public class Lz4DecoderStream : Stream
|
||||
{
|
||||
public Lz4DecoderStream()
|
||||
{
|
||||
}
|
||||
|
||||
public Lz4DecoderStream(Stream input, long inputLength = long.MaxValue)
|
||||
{
|
||||
Reset(input, inputLength);
|
||||
}
|
||||
|
||||
public void Reset(Stream input, long inputLength = long.MaxValue)
|
||||
{
|
||||
this.inputLength = inputLength;
|
||||
this.input = input;
|
||||
|
||||
phase = DecodePhase.ReadToken;
|
||||
|
||||
decodeBufferPos = 0;
|
||||
|
||||
litLen = 0;
|
||||
matLen = 0;
|
||||
matDst = 0;
|
||||
|
||||
inBufPos = DecBufLen;
|
||||
inBufEnd = DecBufLen;
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
this.input = null;
|
||||
}
|
||||
|
||||
private long inputLength;
|
||||
private Stream input;
|
||||
|
||||
//because we might not be able to match back across invocations,
|
||||
//we have to keep the last window's worth of bytes around for reuse
|
||||
//we use a circular buffer for this - every time we write into this
|
||||
//buffer, we also write the same into our output buffer
|
||||
|
||||
private const int DecBufLen = 0x10000;
|
||||
private const int DecBufMask = 0xFFFF;
|
||||
|
||||
private const int InBufLen = 128;
|
||||
|
||||
private byte[] decodeBuffer = new byte[DecBufLen + InBufLen];
|
||||
private int decodeBufferPos, inBufPos, inBufEnd;
|
||||
|
||||
//we keep track of which phase we're in so that we can jump right back
|
||||
//into the correct part of decoding
|
||||
|
||||
private DecodePhase phase;
|
||||
|
||||
private enum DecodePhase
|
||||
{
|
||||
ReadToken,
|
||||
ReadExLiteralLength,
|
||||
CopyLiteral,
|
||||
ReadOffset,
|
||||
ReadExMatchLength,
|
||||
CopyMatch,
|
||||
}
|
||||
|
||||
//state within interruptable phases and across phase boundaries is
|
||||
//kept here - again, so that we can punt out and restart freely
|
||||
|
||||
private int litLen, matLen, matDst;
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
#if CHECK_ARGS
|
||||
if (buffer == null)
|
||||
throw new ArgumentNullException("buffer");
|
||||
if (offset < 0 || count < 0 || buffer.Length - count < offset)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
if (input == null)
|
||||
throw new InvalidOperationException();
|
||||
#endif
|
||||
int nRead, nToRead = count;
|
||||
|
||||
var decBuf = decodeBuffer;
|
||||
|
||||
//the stringy gotos are obnoxious, but their purpose is to
|
||||
//make it *blindingly* obvious how the state machine transitions
|
||||
//back and forth as it reads - remember, we can yield out of
|
||||
//this routine in several places, and we must be able to re-enter
|
||||
//and pick up where we left off!
|
||||
|
||||
#if LOCAL_SHADOW
|
||||
var phase = this.phase;
|
||||
var inBufPos = this.inBufPos;
|
||||
var inBufEnd = this.inBufEnd;
|
||||
#endif
|
||||
switch (phase)
|
||||
{
|
||||
case DecodePhase.ReadToken:
|
||||
goto readToken;
|
||||
|
||||
case DecodePhase.ReadExLiteralLength:
|
||||
goto readExLiteralLength;
|
||||
|
||||
case DecodePhase.CopyLiteral:
|
||||
goto copyLiteral;
|
||||
|
||||
case DecodePhase.ReadOffset:
|
||||
goto readOffset;
|
||||
|
||||
case DecodePhase.ReadExMatchLength:
|
||||
goto readExMatchLength;
|
||||
|
||||
case DecodePhase.CopyMatch:
|
||||
goto copyMatch;
|
||||
}
|
||||
|
||||
readToken:
|
||||
int tok;
|
||||
if (inBufPos < inBufEnd)
|
||||
{
|
||||
tok = decBuf[inBufPos++];
|
||||
}
|
||||
else
|
||||
{
|
||||
#if LOCAL_SHADOW
|
||||
this.inBufPos = inBufPos;
|
||||
#endif
|
||||
|
||||
tok = ReadByteCore();
|
||||
#if LOCAL_SHADOW
|
||||
inBufPos = this.inBufPos;
|
||||
inBufEnd = this.inBufEnd;
|
||||
#endif
|
||||
#if CHECK_EOF
|
||||
if (tok == -1)
|
||||
goto finish;
|
||||
#endif
|
||||
}
|
||||
|
||||
litLen = tok >> 4;
|
||||
matLen = (tok & 0xF) + 4;
|
||||
|
||||
switch (litLen)
|
||||
{
|
||||
case 0:
|
||||
phase = DecodePhase.ReadOffset;
|
||||
goto readOffset;
|
||||
|
||||
case 0xF:
|
||||
phase = DecodePhase.ReadExLiteralLength;
|
||||
goto readExLiteralLength;
|
||||
|
||||
default:
|
||||
phase = DecodePhase.CopyLiteral;
|
||||
goto copyLiteral;
|
||||
}
|
||||
|
||||
readExLiteralLength:
|
||||
int exLitLen;
|
||||
if (inBufPos < inBufEnd)
|
||||
{
|
||||
exLitLen = decBuf[inBufPos++];
|
||||
}
|
||||
else
|
||||
{
|
||||
#if LOCAL_SHADOW
|
||||
this.inBufPos = inBufPos;
|
||||
#endif
|
||||
exLitLen = ReadByteCore();
|
||||
#if LOCAL_SHADOW
|
||||
inBufPos = this.inBufPos;
|
||||
inBufEnd = this.inBufEnd;
|
||||
#endif
|
||||
|
||||
#if CHECK_EOF
|
||||
if (exLitLen == -1)
|
||||
goto finish;
|
||||
#endif
|
||||
}
|
||||
|
||||
litLen += exLitLen;
|
||||
if (exLitLen == 255)
|
||||
goto readExLiteralLength;
|
||||
|
||||
phase = DecodePhase.CopyLiteral;
|
||||
goto copyLiteral;
|
||||
|
||||
copyLiteral:
|
||||
int nReadLit = litLen < nToRead ? litLen : nToRead;
|
||||
if (nReadLit != 0)
|
||||
{
|
||||
if (inBufPos + nReadLit <= inBufEnd)
|
||||
{
|
||||
int ofs = offset;
|
||||
|
||||
for (int c = nReadLit; c-- != 0; )
|
||||
buffer[ofs++] = decBuf[inBufPos++];
|
||||
|
||||
nRead = nReadLit;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if LOCAL_SHADOW
|
||||
this.inBufPos = inBufPos;
|
||||
#endif
|
||||
nRead = ReadCore(buffer, offset, nReadLit);
|
||||
#if LOCAL_SHADOW
|
||||
inBufPos = this.inBufPos;
|
||||
inBufEnd = this.inBufEnd;
|
||||
#endif
|
||||
#if CHECK_EOF
|
||||
if (nRead == 0)
|
||||
goto finish;
|
||||
#endif
|
||||
}
|
||||
|
||||
offset += nRead;
|
||||
nToRead -= nRead;
|
||||
|
||||
litLen -= nRead;
|
||||
|
||||
if (litLen != 0)
|
||||
goto copyLiteral;
|
||||
}
|
||||
|
||||
if (nToRead == 0)
|
||||
goto finish;
|
||||
|
||||
phase = DecodePhase.ReadOffset;
|
||||
goto readOffset;
|
||||
|
||||
readOffset:
|
||||
if (inBufPos + 1 < inBufEnd)
|
||||
{
|
||||
matDst = (decBuf[inBufPos + 1] << 8) | decBuf[inBufPos];
|
||||
inBufPos += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if LOCAL_SHADOW
|
||||
this.inBufPos = inBufPos;
|
||||
#endif
|
||||
matDst = ReadOffsetCore();
|
||||
#if LOCAL_SHADOW
|
||||
inBufPos = this.inBufPos;
|
||||
inBufEnd = this.inBufEnd;
|
||||
#endif
|
||||
#if CHECK_EOF
|
||||
if (matDst == -1)
|
||||
goto finish;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (matLen == 15 + 4)
|
||||
{
|
||||
phase = DecodePhase.ReadExMatchLength;
|
||||
goto readExMatchLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
phase = DecodePhase.CopyMatch;
|
||||
goto copyMatch;
|
||||
}
|
||||
|
||||
readExMatchLength:
|
||||
int exMatLen;
|
||||
if (inBufPos < inBufEnd)
|
||||
{
|
||||
exMatLen = decBuf[inBufPos++];
|
||||
}
|
||||
else
|
||||
{
|
||||
#if LOCAL_SHADOW
|
||||
this.inBufPos = inBufPos;
|
||||
#endif
|
||||
exMatLen = ReadByteCore();
|
||||
#if LOCAL_SHADOW
|
||||
inBufPos = this.inBufPos;
|
||||
inBufEnd = this.inBufEnd;
|
||||
#endif
|
||||
#if CHECK_EOF
|
||||
if (exMatLen == -1)
|
||||
goto finish;
|
||||
#endif
|
||||
}
|
||||
|
||||
matLen += exMatLen;
|
||||
if (exMatLen == 255)
|
||||
goto readExMatchLength;
|
||||
|
||||
phase = DecodePhase.CopyMatch;
|
||||
goto copyMatch;
|
||||
|
||||
copyMatch:
|
||||
int nCpyMat = matLen < nToRead ? matLen : nToRead;
|
||||
if (nCpyMat != 0)
|
||||
{
|
||||
nRead = count - nToRead;
|
||||
|
||||
int bufDst = matDst - nRead;
|
||||
if (bufDst > 0)
|
||||
{
|
||||
//offset is fairly far back, we need to pull from the buffer
|
||||
|
||||
int bufSrc = decodeBufferPos - bufDst;
|
||||
if (bufSrc < 0)
|
||||
bufSrc += DecBufLen;
|
||||
int bufCnt = bufDst < nCpyMat ? bufDst : nCpyMat;
|
||||
|
||||
for (int c = bufCnt; c-- != 0; )
|
||||
buffer[offset++] = decBuf[bufSrc++ & DecBufMask];
|
||||
}
|
||||
else
|
||||
{
|
||||
bufDst = 0;
|
||||
}
|
||||
|
||||
int sOfs = offset - matDst;
|
||||
for (int i = bufDst; i < nCpyMat; i++)
|
||||
buffer[offset++] = buffer[sOfs++];
|
||||
|
||||
nToRead -= nCpyMat;
|
||||
matLen -= nCpyMat;
|
||||
}
|
||||
|
||||
if (nToRead == 0)
|
||||
goto finish;
|
||||
|
||||
phase = DecodePhase.ReadToken;
|
||||
goto readToken;
|
||||
|
||||
finish:
|
||||
nRead = count - nToRead;
|
||||
|
||||
int nToBuf = nRead < DecBufLen ? nRead : DecBufLen;
|
||||
int repPos = offset - nToBuf;
|
||||
|
||||
if (nToBuf == DecBufLen)
|
||||
{
|
||||
Buffer.BlockCopy(buffer, repPos, decBuf, 0, DecBufLen);
|
||||
decodeBufferPos = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
int decPos = decodeBufferPos;
|
||||
|
||||
while (nToBuf-- != 0)
|
||||
decBuf[decPos++ & DecBufMask] = buffer[repPos++];
|
||||
|
||||
decodeBufferPos = decPos & DecBufMask;
|
||||
}
|
||||
|
||||
#if LOCAL_SHADOW
|
||||
this.phase = phase;
|
||||
this.inBufPos = inBufPos;
|
||||
#endif
|
||||
return nRead;
|
||||
}
|
||||
|
||||
private int ReadByteCore()
|
||||
{
|
||||
var buf = decodeBuffer;
|
||||
|
||||
if (inBufPos == inBufEnd)
|
||||
{
|
||||
int nRead = input.Read(buf, DecBufLen,
|
||||
InBufLen < inputLength ? InBufLen : (int)inputLength);
|
||||
|
||||
#if CHECK_EOF
|
||||
if (nRead == 0)
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
inputLength -= nRead;
|
||||
|
||||
inBufPos = DecBufLen;
|
||||
inBufEnd = DecBufLen + nRead;
|
||||
}
|
||||
|
||||
return buf[inBufPos++];
|
||||
}
|
||||
|
||||
private int ReadOffsetCore()
|
||||
{
|
||||
var buf = decodeBuffer;
|
||||
|
||||
if (inBufPos == inBufEnd)
|
||||
{
|
||||
int nRead = input.Read(buf, DecBufLen,
|
||||
InBufLen < inputLength ? InBufLen : (int)inputLength);
|
||||
|
||||
#if CHECK_EOF
|
||||
if (nRead == 0)
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
inputLength -= nRead;
|
||||
|
||||
inBufPos = DecBufLen;
|
||||
inBufEnd = DecBufLen + nRead;
|
||||
}
|
||||
|
||||
if (inBufEnd - inBufPos == 1)
|
||||
{
|
||||
buf[DecBufLen] = buf[inBufPos];
|
||||
|
||||
int nRead = input.Read(buf, DecBufLen + 1,
|
||||
InBufLen - 1 < inputLength ? InBufLen - 1 : (int)inputLength);
|
||||
|
||||
#if CHECK_EOF
|
||||
if (nRead == 0)
|
||||
{
|
||||
inBufPos = DecBufLen;
|
||||
inBufEnd = DecBufLen + 1;
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
inputLength -= nRead;
|
||||
|
||||
inBufPos = DecBufLen;
|
||||
inBufEnd = DecBufLen + nRead + 1;
|
||||
}
|
||||
|
||||
int ret = (buf[inBufPos + 1] << 8) | buf[inBufPos];
|
||||
inBufPos += 2;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int ReadCore(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int nToRead = count;
|
||||
|
||||
var buf = decodeBuffer;
|
||||
int inBufLen = inBufEnd - inBufPos;
|
||||
|
||||
int fromBuf = nToRead < inBufLen ? nToRead : inBufLen;
|
||||
if (fromBuf != 0)
|
||||
{
|
||||
var bufPos = inBufPos;
|
||||
|
||||
for (int c = fromBuf; c-- != 0; )
|
||||
buffer[offset++] = buf[bufPos++];
|
||||
|
||||
inBufPos = bufPos;
|
||||
nToRead -= fromBuf;
|
||||
}
|
||||
|
||||
if (nToRead != 0)
|
||||
{
|
||||
int nRead;
|
||||
|
||||
if (nToRead >= InBufLen)
|
||||
{
|
||||
nRead = input.Read(buffer, offset,
|
||||
nToRead < inputLength ? nToRead : (int)inputLength);
|
||||
nToRead -= nRead;
|
||||
}
|
||||
else
|
||||
{
|
||||
nRead = input.Read(buf, DecBufLen,
|
||||
InBufLen < inputLength ? InBufLen : (int)inputLength);
|
||||
|
||||
inBufPos = DecBufLen;
|
||||
inBufEnd = DecBufLen + nRead;
|
||||
|
||||
fromBuf = nToRead < nRead ? nToRead : nRead;
|
||||
|
||||
var bufPos = inBufPos;
|
||||
|
||||
for (int c = fromBuf; c-- != 0; )
|
||||
buffer[offset++] = buf[bufPos++];
|
||||
|
||||
inBufPos = bufPos;
|
||||
nToRead -= fromBuf;
|
||||
}
|
||||
|
||||
inputLength -= nRead;
|
||||
}
|
||||
|
||||
return count - nToRead;
|
||||
}
|
||||
|
||||
#region Stream internals
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) Alibaba Cloud Computing
|
||||
* All rights reserved.
|
||||
*
|
||||
* 版权所有 (C)阿里云计算有限公司
|
||||
*/
|
||||
using Aliyun.Api.LOG.Common.Authentication;
|
||||
using Aliyun.Api.LOG.Common.Communication;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace Aliyun.Api.LOG.Utilities
|
||||
{
|
||||
internal class LogRequestSigner : IRequestSigner
|
||||
{
|
||||
private readonly String httpResource;
|
||||
private readonly HttpMethod httpMethod;
|
||||
|
||||
public LogRequestSigner(String httpResource, HttpMethod httpMethod)
|
||||
{
|
||||
this.httpResource = httpResource;
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
public void Sign(ServiceRequest request, ServiceCredentials credentials)
|
||||
{
|
||||
request.Headers.Add(LogConsts.NAME_HEADER_AUTH, LogClientTools.Signature(request.Headers, request.Parameters, this.httpMethod, this.httpResource, credentials.AccessId, credentials.AccessKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user