Skip to content

Commit 4b8191c

Browse files
authored
Merge pull request #110 from fengyhack/master
新增"时间戳防盗链",部分优化与改进
2 parents d2f7b1a + dc9add3 commit 4b8191c

File tree

11 files changed

+878
-293
lines changed

11 files changed

+878
-293
lines changed

Qiniu/Fusion/FusionManager.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Security.Cryptography;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -7,6 +8,7 @@
78
using System.Net;
89
using Qiniu.Http;
910
using Newtonsoft.Json;
11+
using System.Text.RegularExpressions;
1012

1113
namespace Qiniu.Fusion
1214
{
@@ -206,5 +208,23 @@ public LogListResult LogList(LogListRequest request)
206208

207209
return result;
208210
}
211+
212+
/// <summary>
213+
/// 时间戳防盗链
214+
/// </summary>
215+
/// <param name="request"></param>
216+
/// <returns></returns>
217+
public string HotLink(HotLinkRequest request)
218+
{
219+
string RAW = request.RawUrl;
220+
221+
string key = request.Key;
222+
string path = Uri.EscapeUriString(request.Path);
223+
string file = request.File;
224+
string ts = (int.Parse(request.Timestamp)).ToString("x");
225+
string SIGN = StringUtils.md5Hash(key + path + file + ts);
226+
227+
return string.Format("{0}&sign={1}&t={2}", RAW, SIGN, ts);
228+
}
209229
}
210230
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Qiniu.Util;
6+
7+
namespace Qiniu.Fusion.Model
8+
{
9+
public class HotLinkRequest
10+
{
11+
public string RawUrl
12+
{
13+
get
14+
{
15+
return Host + Path + File + Query;
16+
}
17+
}
18+
19+
public string Host { get; set; }
20+
21+
public string Path { get; set; }
22+
23+
public string File { get; set; }
24+
25+
public string Query { get; set; }
26+
27+
public string Key { get; set; }
28+
29+
public string Timestamp { get; set; }
30+
31+
public HotLinkRequest()
32+
{
33+
Host = "";
34+
Path = "";
35+
File = "";
36+
Query = "";
37+
Key = "";
38+
Timestamp = "";
39+
}
40+
41+
public HotLinkRequest(string url, string key, int expire)
42+
{
43+
string host, path, file, query;
44+
UrlHelper.UrlSplit(url, out host, out path, out file, out query);
45+
46+
Host = host;
47+
Path = path;
48+
File = file;
49+
Query = query;
50+
Key = key;
51+
52+
SetLinkExpire(expire);
53+
}
54+
55+
public void SetLinkExpire(int seconds)
56+
{
57+
DateTime dt0 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
58+
DateTime dt1 = DateTime.Now.AddSeconds(seconds);
59+
TimeSpan tsx = dt1.Subtract(dt0);
60+
string sts = tsx.Ticks.ToString();
61+
Timestamp = sts.Substring(0, sts.Length - 7);
62+
}
63+
64+
public void SetLinkExpire(DateTime stopAt)
65+
{
66+
DateTime dt0 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
67+
TimeSpan tsx = stopAt.Subtract(dt0);
68+
string sts = tsx.Ticks.ToString();
69+
Timestamp = sts.Substring(0, sts.Length - 7);
70+
}
71+
}
72+
}

Qiniu/Fusion/Model/UrlHelper.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,26 @@ public static string GetNormalUrl(string _url)
3131
return m.Value;
3232
}
3333

34+
public static void UrlSplit(string url, out string host, out string path, out string file, out string query)
35+
{
36+
int start = 0;
37+
38+
Regex regHost = new Regex(@"(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+");
39+
host = regHost.Match(url, start).Value;
40+
start += host.Length;
41+
42+
Regex regPath = new Regex(@"(/(\w|\-)*)+/");
43+
path = regPath.Match(url, start).Value;
44+
if(string.IsNullOrEmpty(path))
45+
{
46+
path = "/";
47+
}
48+
start += path.Length;
49+
50+
int index = url.IndexOf('?', start);
51+
file = url.Substring(start, index - start);
52+
53+
query = url.Substring(index);
54+
}
3455
}
3556
}

Qiniu/Http/CompletionHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ namespace Qiniu.Http
77
/// <param name="respInfo">请求回复信息</param>
88
/// <param name="response">请求回复内容</param>
99
public delegate void CompletionHandler(ResponseInfo respInfo, string response);
10+
11+
/// <summary>
12+
/// HTTP请求结果(非json/二进制数据)
13+
/// </summary>
14+
/// <param name="respInfo"></param>
15+
/// <param name="respData"></param>
16+
public delegate void RecvDataHandler(ResponseInfo respInfo, byte[] respData);
17+
1018
}

0 commit comments

Comments
 (0)