Skip to content

Commit dc9add3

Browse files
author
fengyunhai
committed
新增:时间戳防盗链
1 parent 47061af commit dc9add3

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
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/Qiniu.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="Fusion\Model\BandwidthResult.cs" />
5555
<Compile Include="Fusion\Model\FluxRequest.cs" />
5656
<Compile Include="Fusion\Model\FluxResult.cs" />
57+
<Compile Include="Fusion\Model\HotLinkRequest.cs" />
5758
<Compile Include="Fusion\Model\LogListRequest.cs" />
5859
<Compile Include="Fusion\Model\LogListResult.cs" />
5960
<Compile Include="Fusion\Model\RefreshRequest.cs" />

0 commit comments

Comments
 (0)