mirror of https://github.com/OpenVidu/openvidu.git
openvidu-server support 'local', 'ngtok' and custom URL mode
parent
c5bb9e9002
commit
83edc43299
|
@ -12,8 +12,7 @@ public class InfoSocketConfig implements WebSocketConfigurer {
|
|||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(infoHandler(), "/info");
|
||||
//.setAllowedOrigins("*");
|
||||
registry.addHandler(infoHandler(), "/info").setAllowedOrigins("*");
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -46,6 +46,7 @@ import io.openvidu.server.kms.FixedOneKmsManager;
|
|||
import io.openvidu.server.rest.NgrokController;
|
||||
import io.openvidu.server.rpc.JsonRpcNotificationService;
|
||||
import io.openvidu.server.rpc.JsonRpcUserControl;
|
||||
import io.openvidu.server.security.OpenviduConfiguration;
|
||||
|
||||
/**
|
||||
* Room server application.
|
||||
|
@ -55,113 +56,126 @@ import io.openvidu.server.rpc.JsonRpcUserControl;
|
|||
* @author Radu Tom Vlad (rvlad@naevatec.com)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Import({JsonRpcConfiguration.class, InfoSocketConfig.class})
|
||||
@Import({ JsonRpcConfiguration.class, InfoSocketConfig.class })
|
||||
@SpringBootApplication
|
||||
public class OpenViduServer implements JsonRpcConfigurer {
|
||||
|
||||
public static final String KMSS_URIS_PROPERTY = "kms.uris";
|
||||
public static final String KMSS_URIS_DEFAULT = "[ \"ws://localhost:8888/kurento\" ]";
|
||||
|
||||
@Value("${kms.uris}")
|
||||
private String KMSS_CUSTOM_URIS;
|
||||
public static final String KMSS_URIS_PROPERTY = "kms.uris";
|
||||
public static final String KMSS_URIS_DEFAULT = "[ \"ws://localhost:8888/kurento\" ]";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(OpenViduServer.class);
|
||||
@Value("${kms.uris}")
|
||||
private String KMSS_CUSTOM_URIS;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KurentoClientProvider kmsManager() {
|
||||
public static String publicUrl;
|
||||
|
||||
JsonArray kmsUris = getPropertyJson(KMSS_URIS_PROPERTY, KMSS_URIS_DEFAULT, JsonArray.class);
|
||||
List<String> kmsWsUris = JsonUtils.toStringList(kmsUris);
|
||||
|
||||
if ((KMSS_CUSTOM_URIS != null) && (!KMSS_CUSTOM_URIS.isEmpty())) {
|
||||
List<String> uris = new Gson().fromJson( KMSS_CUSTOM_URIS, List.class );
|
||||
kmsWsUris.addAll(0, uris);
|
||||
}
|
||||
private static final Logger log = LoggerFactory.getLogger(OpenViduServer.class);
|
||||
|
||||
if (kmsWsUris.isEmpty()) {
|
||||
throw new IllegalArgumentException(KMSS_URIS_PROPERTY
|
||||
+ " should contain at least one kms url");
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public KurentoClientProvider kmsManager() {
|
||||
|
||||
String firstKmsWsUri = kmsWsUris.get(0);
|
||||
JsonArray kmsUris = getPropertyJson(KMSS_URIS_PROPERTY, KMSS_URIS_DEFAULT, JsonArray.class);
|
||||
List<String> kmsWsUris = JsonUtils.toStringList(kmsUris);
|
||||
|
||||
if (firstKmsWsUri.equals("autodiscovery")) {
|
||||
log.info("Using autodiscovery rules to locate KMS on every pipeline");
|
||||
return new AutodiscoveryKurentoClientProvider();
|
||||
} else {
|
||||
log.info("Configuring Kurento Room Server to use first of the following kmss: " + kmsWsUris);
|
||||
return new FixedOneKmsManager(firstKmsWsUri);
|
||||
}
|
||||
}
|
||||
if ((KMSS_CUSTOM_URIS != null) && (!KMSS_CUSTOM_URIS.isEmpty())) {
|
||||
List<String> uris = new Gson().fromJson(KMSS_CUSTOM_URIS, List.class);
|
||||
kmsWsUris.addAll(0, uris);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JsonRpcNotificationService notificationService() {
|
||||
return new JsonRpcNotificationService();
|
||||
}
|
||||
if (kmsWsUris.isEmpty()) {
|
||||
throw new IllegalArgumentException(KMSS_URIS_PROPERTY + " should contain at least one kms url");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NotificationRoomHandler defaultNotificationRoomHandler() {
|
||||
return new DefaultNotificationRoomHandler(notificationService());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RoomManager roomManager() {
|
||||
return new RoomManager();
|
||||
}
|
||||
String firstKmsWsUri = kmsWsUris.get(0);
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NotificationRoomManager notificationRoomManager() {
|
||||
return new NotificationRoomManager();
|
||||
}
|
||||
if (firstKmsWsUri.equals("autodiscovery")) {
|
||||
log.info("Using autodiscovery rules to locate KMS on every pipeline");
|
||||
return new AutodiscoveryKurentoClientProvider();
|
||||
} else {
|
||||
log.info("Configuring Kurento Room Server to use first of the following kmss: " + kmsWsUris);
|
||||
return new FixedOneKmsManager(firstKmsWsUri);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JsonRpcUserControl userControl() {
|
||||
return new JsonRpcUserControl();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JsonRpcNotificationService notificationService() {
|
||||
return new JsonRpcNotificationService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RoomJsonRpcHandler roomHandler() {
|
||||
return new RoomJsonRpcHandler();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NotificationRoomHandler defaultNotificationRoomHandler() {
|
||||
return new DefaultNotificationRoomHandler(notificationService());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerJsonRpcHandlers(JsonRpcHandlerRegistry registry) {
|
||||
registry.addHandler(roomHandler().withPingWatchdog(true), "/room");
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RoomManager roomManager() {
|
||||
return new RoomManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletServerContainerFactoryBean createWebSocketContainer() {
|
||||
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
|
||||
container.setMaxTextMessageBufferSize(1000000); // chars
|
||||
container.setMaxBinaryMessageBufferSize(1000000); // bytes
|
||||
return container;
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NotificationRoomManager notificationRoomManager() {
|
||||
return new NotificationRoomManager();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
start(args);
|
||||
try {
|
||||
NgrokController ngrok = new NgrokController();
|
||||
System.out.println();
|
||||
System.out.println(" PUBLIC IP ");
|
||||
System.out.println("-------------------------");
|
||||
System.out.println(ngrok.getNgrokPublicUrl());
|
||||
System.out.println("-------------------------");
|
||||
System.out.println();
|
||||
} catch(Exception e) {
|
||||
System.out.println(" No ngrok connection ");
|
||||
}
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public JsonRpcUserControl userControl() {
|
||||
return new JsonRpcUserControl();
|
||||
}
|
||||
|
||||
public static ConfigurableApplicationContext start(String[] args) {
|
||||
log.info("Using /dev/urandom for secure random generation");
|
||||
System.setProperty("java.security.egd", "file:/dev/./urandom");
|
||||
return SpringApplication.run(OpenViduServer.class, args);
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RoomJsonRpcHandler roomHandler() {
|
||||
return new RoomJsonRpcHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerJsonRpcHandlers(JsonRpcHandlerRegistry registry) {
|
||||
registry.addHandler(roomHandler().withPingWatchdog(true), "/room");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServletServerContainerFactoryBean createWebSocketContainer() {
|
||||
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
|
||||
container.setMaxTextMessageBufferSize(1000000); // chars
|
||||
container.setMaxBinaryMessageBufferSize(1000000); // bytes
|
||||
return container;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ConfigurableApplicationContext context = start(args);
|
||||
OpenviduConfiguration openviduConf = context.getBean(OpenviduConfiguration.class);
|
||||
OpenViduServer.publicUrl = "wss://localhost:" + openviduConf.getServerPort();
|
||||
|
||||
if (openviduConf.getOpenViduPublicUrl().equals("ngrok")) {
|
||||
try {
|
||||
NgrokController ngrok = new NgrokController();
|
||||
System.out.println();
|
||||
System.out.println(" PUBLIC IP ");
|
||||
System.out.println("-------------------------");
|
||||
System.out.println(ngrok.getNgrokAppUrl());
|
||||
System.out.println("-------------------------");
|
||||
System.out.println();
|
||||
OpenViduServer.publicUrl = ngrok.getNgrokServerUrl().replaceFirst("https://", "wss://");
|
||||
} catch (Exception e) {
|
||||
System.out.println(" No ngrok connection ");
|
||||
}
|
||||
} else if (!openviduConf.getOpenViduPublicUrl().equals("local")) {
|
||||
System.out.println("CUSTOM URL");
|
||||
OpenViduServer.publicUrl = openviduConf.getOpenViduPublicUrl().replaceFirst("https://", "wss://");
|
||||
if (!OpenViduServer.publicUrl.startsWith("wss://")) {
|
||||
OpenViduServer.publicUrl = "wss://" + OpenViduServer.publicUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ConfigurableApplicationContext start(String[] args) {
|
||||
log.info("Using /dev/urandom for secure random generation");
|
||||
System.setProperty("java.security.egd", "file:/dev/./urandom");
|
||||
return SpringApplication.run(OpenViduServer.class, args);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,11 +37,10 @@ import org.kurento.client.WebRtcEndpoint;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import io.openvidu.client.OpenViduException;
|
||||
import io.openvidu.client.OpenViduException.Code;
|
||||
import io.openvidu.server.InfoHandler;
|
||||
import io.openvidu.server.OpenViduServer;
|
||||
import io.openvidu.server.core.api.KurentoClientProvider;
|
||||
import io.openvidu.server.core.api.KurentoClientSessionInfo;
|
||||
import io.openvidu.server.core.api.MutedMediaType;
|
||||
|
@ -50,6 +49,7 @@ import io.openvidu.server.core.api.pojo.UserParticipant;
|
|||
import io.openvidu.server.core.endpoint.SdpType;
|
||||
import io.openvidu.server.core.internal.Participant;
|
||||
import io.openvidu.server.core.internal.Room;
|
||||
import io.openvidu.server.security.OpenviduConfiguration;
|
||||
import io.openvidu.server.security.ParticipantRole;
|
||||
import io.openvidu.server.security.Token;
|
||||
|
||||
|
@ -82,13 +82,13 @@ public class RoomManager {
|
|||
|
||||
@Autowired
|
||||
private KurentoClientProvider kcProvider;
|
||||
|
||||
@Autowired
|
||||
private OpenviduConfiguration openviduConf;
|
||||
|
||||
private final ConcurrentMap<String, Room> rooms = new ConcurrentHashMap<String, Room>();
|
||||
private final ConcurrentMap<String, ConcurrentHashMap<String, Token>> sessionidTokenTokenobj = new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<String, ConcurrentHashMap<String, String>> sessionidUsernameToken = new ConcurrentHashMap<>();
|
||||
|
||||
@Value("${openvidu.security}")
|
||||
private boolean SECURITY_ENABLED;
|
||||
|
||||
private volatile boolean closed = false;
|
||||
|
||||
|
@ -963,7 +963,7 @@ public class RoomManager {
|
|||
}
|
||||
|
||||
public boolean isParticipantInRoom(String token, String roomId) throws OpenViduException {
|
||||
if (SECURITY_ENABLED) {
|
||||
if (openviduConf.getOpenViduSecurity()) {
|
||||
if (this.sessionidTokenTokenobj.get(roomId) != null) {
|
||||
return this.sessionidTokenTokenobj.get(roomId).containsKey(token);
|
||||
} else{
|
||||
|
@ -979,7 +979,7 @@ public class RoomManager {
|
|||
}
|
||||
|
||||
public boolean isPublisherInRoom(String userName, String roomId) {
|
||||
if (SECURITY_ENABLED) {
|
||||
if (openviduConf.getOpenViduSecurity()) {
|
||||
if (this.sessionidUsernameToken.get(roomId) != null){
|
||||
String token = this.sessionidUsernameToken.get(roomId).get(userName);
|
||||
if (token != null){
|
||||
|
@ -1035,7 +1035,8 @@ public class RoomManager {
|
|||
}
|
||||
|
||||
public String newSessionId(){
|
||||
String sessionId = new BigInteger(130, new SecureRandom()).toString(32);
|
||||
String sessionId = OpenViduServer.publicUrl;
|
||||
sessionId += "/" + new BigInteger(130, new SecureRandom()).toString(32);
|
||||
|
||||
this.sessionidTokenTokenobj.put(sessionId, new ConcurrentHashMap<>());
|
||||
this.sessionidUsernameToken.put(sessionId, new ConcurrentHashMap<>());
|
||||
|
@ -1048,7 +1049,7 @@ public class RoomManager {
|
|||
if (this.sessionidUsernameToken.get(roomId) != null && this.sessionidTokenTokenobj.get(roomId) != null) {
|
||||
if(metadataFormatCorrect(metadata)){
|
||||
String token = new BigInteger(130, new SecureRandom()).toString(32);
|
||||
if (SECURITY_ENABLED) { // Store the token only if security is enabled
|
||||
if (openviduConf.getOpenViduSecurity()) { // Store the token only if security is enabled
|
||||
this.sessionidTokenTokenobj.get(roomId).put(token, new Token(token, role, metadata));
|
||||
}
|
||||
showMap();
|
||||
|
@ -1065,7 +1066,7 @@ public class RoomManager {
|
|||
}
|
||||
|
||||
public String newRandomUserName(String token, String roomId) {
|
||||
if (SECURITY_ENABLED) {
|
||||
if (openviduConf.getOpenViduSecurity()) {
|
||||
if (this.sessionidUsernameToken.get(roomId) != null && this.sessionidTokenTokenobj.get(roomId) != null) {
|
||||
if (this.sessionidTokenTokenobj.get(roomId).get(token) != null) {
|
||||
return this.generateAndStoreUserName(token, roomId);
|
||||
|
|
|
@ -10,16 +10,81 @@ import org.apache.http.client.HttpClient;
|
|||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
public class NgrokController {
|
||||
|
||||
private final String NGROK_URL = "http://localhost:4040/api/tunnels";
|
||||
private final String NGROK_APP_NAME = "app";
|
||||
private final String NGROK_SERVER_NAME = "server";
|
||||
|
||||
private String appUrl = "";
|
||||
private String serverUrl = "";
|
||||
private JsonObject json;
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
|
||||
public String getNgrokPublicUrl() throws ClientProtocolException, IOException {
|
||||
public String getNgrokAppUrl() throws ClientProtocolException, IOException {
|
||||
|
||||
if (this.appUrl != null && !this.appUrl.isEmpty()) {
|
||||
return this.appUrl;
|
||||
}
|
||||
|
||||
if (this.json == null) {
|
||||
this.json = this.httpRequest();
|
||||
}
|
||||
|
||||
String appPublicUrl = "";
|
||||
|
||||
JsonArray array = this.json.getAsJsonArray("tunnels");
|
||||
for (JsonElement el : array) {
|
||||
JsonObject elObj = el.getAsJsonObject();
|
||||
String name = elObj.get("name").getAsString();
|
||||
if (name.equals(NGROK_APP_NAME)) {
|
||||
appPublicUrl = elObj.get("public_url").getAsString();
|
||||
appPublicUrl = appPublicUrl.replaceFirst("http://", "https://");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.appUrl = appPublicUrl;
|
||||
|
||||
return appPublicUrl;
|
||||
}
|
||||
|
||||
public String getNgrokServerUrl() throws ClientProtocolException, IOException {
|
||||
|
||||
if (this.serverUrl != null && !this.serverUrl.isEmpty()) {
|
||||
return this.serverUrl;
|
||||
}
|
||||
|
||||
if (this.json == null) {
|
||||
this.json = this.httpRequest();
|
||||
}
|
||||
|
||||
String serverPublicUrl = "";
|
||||
|
||||
JsonArray array = this.json.getAsJsonArray("tunnels");
|
||||
for (JsonElement el : array) {
|
||||
JsonObject elObj = el.getAsJsonObject();
|
||||
String name = elObj.get("name").getAsString();
|
||||
if (name.equals(NGROK_SERVER_NAME)) {
|
||||
serverPublicUrl = elObj.get("public_url").getAsString();
|
||||
serverPublicUrl = serverPublicUrl.replaceFirst("http://", "https://");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.serverUrl = serverPublicUrl;
|
||||
|
||||
return serverPublicUrl;
|
||||
}
|
||||
|
||||
private JsonObject httpRequest() throws ClientProtocolException, IOException {
|
||||
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
|
||||
HttpGet request = new HttpGet(NGROK_URL);
|
||||
|
@ -31,11 +96,10 @@ public class NgrokController {
|
|||
while ((line = rd.readLine()) != null) {
|
||||
result.append(line);
|
||||
}
|
||||
JsonObject json = (JsonObject) new JsonParser().parse(result.toString());
|
||||
String publicUrl = json.getAsJsonArray("tunnels").get(0).getAsJsonObject().get("public_url").getAsString();
|
||||
publicUrl = publicUrl.replaceFirst("http://", "https://");
|
||||
|
||||
return publicUrl;
|
||||
|
||||
JsonObject responseJson = (JsonObject) new JsonParser().parse(result.toString());
|
||||
|
||||
return responseJson;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package io.openvidu.server.security;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class OpenviduConfiguration {
|
||||
|
||||
@Value("${openvidu.publicurl}")
|
||||
private String openviduPublicUrl; //local, ngrok, FINAL_URL
|
||||
|
||||
@Value("${server.port}")
|
||||
private String serverPort;
|
||||
|
||||
@Value("${openvidu.secret}")
|
||||
private String openviduSecret;
|
||||
|
||||
@Value("${openvidu.security}")
|
||||
private boolean openviduSecurity; // true, false
|
||||
|
||||
public String getOpenViduPublicUrl() {
|
||||
return this.openviduPublicUrl;
|
||||
}
|
||||
|
||||
public String getServerPort() {
|
||||
return this.serverPort;
|
||||
}
|
||||
|
||||
public String getOpenViduSecret() {
|
||||
return this.openviduSecret;
|
||||
}
|
||||
|
||||
public boolean getOpenViduSecurity() {
|
||||
return this.openviduSecurity;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package io.openvidu.server.security;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
|
@ -14,13 +13,13 @@ import org.springframework.security.config.http.SessionCreationPolicy;
|
|||
@EnableGlobalAuthentication
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Value("${openvidu.secret}")
|
||||
private String SECRET;
|
||||
@Autowired
|
||||
OpenviduConfiguration openviduConf;
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication()
|
||||
.withUser("OPENVIDUAPP").password(SECRET).roles("ADMIN");
|
||||
.withUser("OPENVIDUAPP").password(openviduConf.getOpenViduSecret()).roles("ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -7,4 +7,5 @@ server.address: 0.0.0.0
|
|||
kms.uris=[\"ws://localhost:8888/kurento\"]
|
||||
|
||||
openvidu.secret: MY_SECRET
|
||||
openvidu.security: false
|
||||
openvidu.security: false
|
||||
openvidu.publicurl: ngrok
|
|
@ -9,4 +9,5 @@ server.ssl.key-alias: openvidu-selfsigned
|
|||
kms.uris=[\"ws://localhost:8888/kurento\"]
|
||||
|
||||
openvidu.secret: MY_SECRET
|
||||
openvidu.security: true
|
||||
openvidu.security: true
|
||||
openvidu.publicurl: local
|
File diff suppressed because one or more lines are too long
|
@ -59,7 +59,9 @@ var AppComponent = (function () {
|
|||
}
|
||||
AppComponent.prototype.ngOnInit = function () {
|
||||
var _this = this;
|
||||
this.websocket = new WebSocket('wss://' + location.hostname + ':8443/info');
|
||||
var protocol = location.protocol.includes('https') ? 'wss://' : 'ws://';
|
||||
var port = (location.port) ? (':' + location.port) : '';
|
||||
this.websocket = new WebSocket(protocol + location.hostname + port + '/info');
|
||||
this.websocket.onopen = function (event) {
|
||||
console.log('Info websocket connected');
|
||||
};
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue