forked from AirenSoft/OvenMediaEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigned_policy_url_generator.java
104 lines (88 loc) · 3.79 KB
/
signed_policy_url_generator.java
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
package kr.hvy;
import com.google.gson.Gson;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.time.Instant;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class OvenMediaEngineKeygen {
private static final String SECRET_KEY = "ome_is_the_best";
private static final String BASE_RTMP_URL = "rtmp://localhost:1935/app/stream";
private static final String BASE_WS_URL = "ws://localhost:3333/app/stream";
private static final String POLICY_NAME = "policy";
private static final String SIGNATURE_NAME = "signature";
private static final HmacUtils hmacUtils = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, SECRET_KEY);
private static final Gson gson = new Gson();
private static final Base64.Encoder bEncode = java.util.Base64.getEncoder();
public static void main(String[] args) throws UnsupportedEncodingException {
// usage sample
String rtmpUrl = generate(BASE_RTMP_URL,
Timestamp.from(Instant.now().plusSeconds(100)).getTime(),
Timestamp.from(Instant.now().plusSeconds(0)).getTime(),
Timestamp.from(Instant.now().plusSeconds(5 * 60)).getTime(),
null);
System.out.println(rtmpUrl);
String wsUrl = generate(BASE_WS_URL,
Timestamp.from(Instant.now().plusSeconds(20)).getTime(),
Timestamp.from(Instant.now().plusSeconds(0)).getTime(),
null,
null);
System.out.println(wsUrl);
}
/**
* To use the code below, you need to add a dependency.
*
* <pre>
* {@code
* <dependency>
* <groupId>org.apache.commons</groupId>
* <artifactId>commons-lang3</artifactId>
* <version>3.12.0</version>
* </dependency>
* <dependency>
* <groupId>commons-codec</groupId>
* <artifactId>commons-codec</artifactId>
* <version>1.15</version>
* </dependency>
* <dependency>
* <groupId>com.google.code.gson</groupId>
* <artifactId>gson</artifactId>
* <version>2.10</version>
* </dependency>
* }
* </pre>
* <p>
* Usage example main function
* </pre>
*/
public static String generate(String rtmpUrl, Long url_expire, Long url_activate, Long stream_expire, String allow_ip) throws UnsupportedEncodingException {
Map<String, Object> policyMap = new HashMap<String, Object>();
policyMap.put("url_expire", url_expire);
if (url_activate != null)
policyMap.put("url_activate", url_activate);
if (stream_expire != null)
policyMap.put("stream_expire", stream_expire);
if (StringUtils.isNotBlank(allow_ip))
policyMap.put("allow_ip", allow_ip);
String policy = gson.toJson(policyMap);
String encodePolicy = encode(policy);
String streamUrl = MessageFormat.format("{0}?{1}={2}", rtmpUrl, POLICY_NAME, encodePolicy);
String signature = makeDigest(streamUrl);
return MessageFormat.format("{0}&{1}={2}", streamUrl, SIGNATURE_NAME, signature);
}
private static String encode(String message) throws UnsupportedEncodingException {
return encode(message.getBytes("UTF-8"));
}
private static String encode(byte[] message) {
String enc = bEncode.encodeToString(message);
return StringUtils.stripEnd(enc, "=").replaceAll("\\+", "-").replaceAll("/", "_");
}
private static String makeDigest(String message) {
return encode(hmacUtils.hmac(message));
}
}