|
|
|
|
@@ -16,6 +16,7 @@ import ir.peeco.pline.repositories.SipProfileDetailsRepository;
|
|
|
|
|
import ir.peeco.pline.repositories.SipProfilesRepository;
|
|
|
|
|
import ir.peeco.pline.repositories.plineUser.UserAccessRepository;
|
|
|
|
|
import jakarta.servlet.http.HttpSession;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
|
|
@@ -23,6 +24,8 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.nio.file.InvalidPathException;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
@@ -31,6 +34,8 @@ import java.nio.file.attribute.PosixFilePermissions;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
|
|
|
|
/** Manages protected Nginx, Asterisk WebRTC transport and TLS settings. */
|
|
|
|
|
@RestController
|
|
|
|
|
@@ -46,6 +51,7 @@ public class NginxManagementController {
|
|
|
|
|
private static final Path CANDIDATE_DTLS_CERT = STATE_DIR.resolve("dtls-fullchain.candidate");
|
|
|
|
|
private static final Path CANDIDATE_DTLS_KEY = STATE_DIR.resolve("dtls-private-key.candidate");
|
|
|
|
|
private static final Pattern HOST_PATTERN = Pattern.compile("(?:_|\\[[0-9A-Fa-f:]+\\]|[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*|(?:[0-9]{1,3}\\.){3}[0-9]{1,3})");
|
|
|
|
|
private static final Pattern IP_LITERAL_PATTERN = Pattern.compile("[0-9A-Fa-f:.]+(?:/[0-9]{1,3})?");
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
private final UserLogger userLogger;
|
|
|
|
|
@@ -79,6 +85,7 @@ public class NginxManagementController {
|
|
|
|
|
result.addResult("sslContents", readSslContents());
|
|
|
|
|
result.addResult("host", readHostSettings());
|
|
|
|
|
result.addResult("webrtc", readWebRtcConfiguration());
|
|
|
|
|
result.addResult("rtp", readRtpConfiguration());
|
|
|
|
|
result.addResult("baseConfigurations", List.of(
|
|
|
|
|
Map.of("id", "webrtc", "locked", true),
|
|
|
|
|
Map.of("id", "host-ssl", "locked", true)));
|
|
|
|
|
@@ -185,6 +192,31 @@ public class NginxManagementController {
|
|
|
|
|
throw new IllegalArgumentException("WebRTC port must be between 1 and 65535");
|
|
|
|
|
next.serverName = request.serverName.trim();
|
|
|
|
|
next.port = request.port;
|
|
|
|
|
next.rtpStart = previous.rtpStart;
|
|
|
|
|
next.rtpEnd = previous.rtpEnd;
|
|
|
|
|
next.iceEnabled = true;
|
|
|
|
|
next.stunHost = previous.stunHost;
|
|
|
|
|
next.stunPort = previous.stunPort;
|
|
|
|
|
next.clientStunHost = previous.clientStunHost;
|
|
|
|
|
next.clientStunPort = previous.clientStunPort;
|
|
|
|
|
next.iceHostCandidates = previous.iceHostCandidates;
|
|
|
|
|
next.includeLocalIceCandidates = previous.includeLocalIceCandidates;
|
|
|
|
|
next.turnEnabled = previous.turnEnabled;
|
|
|
|
|
next.turnHost = previous.turnHost;
|
|
|
|
|
next.turnPort = previous.turnPort;
|
|
|
|
|
next.turnTlsPort = previous.turnTlsPort;
|
|
|
|
|
next.turnUdpEnabled = previous.turnUdpEnabled;
|
|
|
|
|
next.turnTcpEnabled = previous.turnTcpEnabled;
|
|
|
|
|
next.turnTlsEnabled = previous.turnTlsEnabled;
|
|
|
|
|
next.turnRelayStart = previous.turnRelayStart;
|
|
|
|
|
next.turnRelayEnd = previous.turnRelayEnd;
|
|
|
|
|
next.turnRealm = previous.turnRealm;
|
|
|
|
|
next.turnInternalIp = previous.turnInternalIp;
|
|
|
|
|
next.turnExternalIp = previous.turnExternalIp;
|
|
|
|
|
next.turnTlsCertPath = previous.turnTlsCertPath;
|
|
|
|
|
next.turnTlsPrivateKeyPath = previous.turnTlsPrivateKeyPath;
|
|
|
|
|
next.turnCredentialTtlSeconds = previous.turnCredentialTtlSeconds;
|
|
|
|
|
next.turnSharedSecret = previous.turnSharedSecret;
|
|
|
|
|
next.httpPort = http.getBindPort();
|
|
|
|
|
next.tlsPort = http.getTlsBindPort();
|
|
|
|
|
next.upstreamHost = normalizeLoopbackTarget(http.getBindAddr());
|
|
|
|
|
@@ -201,7 +233,7 @@ public class NginxManagementController {
|
|
|
|
|
Map<String, Object> currentSsl = readCertificateInfo();
|
|
|
|
|
if (!Boolean.TRUE.equals(currentSsl.get("valid")))
|
|
|
|
|
throw new IllegalStateException("Apply a valid host SSL certificate before using direct HTTPS to Asterisk");
|
|
|
|
|
String keys = plineTools.getConfigPath() + "keys/";
|
|
|
|
|
String keys = plineTools.getAsteriskConfigPath() + "keys/";
|
|
|
|
|
http.setTlsEnable(true);
|
|
|
|
|
http.setTlsBindPort(request.port);
|
|
|
|
|
next.tlsPort = request.port;
|
|
|
|
|
@@ -229,7 +261,9 @@ public class NginxManagementController {
|
|
|
|
|
runManager("apply-webrtc");
|
|
|
|
|
httpRepository.save(http);
|
|
|
|
|
updateWebRtcProfiles(request.mode, request.dtlsMode);
|
|
|
|
|
fileGenerator.GenerateFiles();
|
|
|
|
|
if (!fileGenerator.GenerateFiles())
|
|
|
|
|
throw new IllegalStateException(
|
|
|
|
|
"Could not apply WebRTC settings. Check Asterisk configuration permissions and logs.");
|
|
|
|
|
plineTools.execAsteriskCli("http reload", true);
|
|
|
|
|
result.addResult("webrtc", readWebRtcConfiguration());
|
|
|
|
|
result.setHasError(false);
|
|
|
|
|
@@ -261,6 +295,127 @@ public class NginxManagementController {
|
|
|
|
|
return ResponseEntity.ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/rtp")
|
|
|
|
|
public synchronized ResponseEntity<ApiResult> saveRtpSettings(HttpSession session,
|
|
|
|
|
@RequestBody RtpSettingsRequest request) {
|
|
|
|
|
ApiResult result = new ApiResult();
|
|
|
|
|
WebRtcState previous = null;
|
|
|
|
|
byte[] previousState = null;
|
|
|
|
|
boolean stateExisted = false;
|
|
|
|
|
boolean stateSnapshotTaken = false;
|
|
|
|
|
try {
|
|
|
|
|
requireAccess(session, UserAccessAction.Update);
|
|
|
|
|
if (request == null) throw new IllegalArgumentException("RTP settings are required");
|
|
|
|
|
validateRtpSettings(request.rtpStart, request.rtpEnd, request.stunHost, request.stunPort,
|
|
|
|
|
request.clientStunHost, request.clientStunPort, request.iceHostCandidates);
|
|
|
|
|
|
|
|
|
|
previous = readWebRtcState();
|
|
|
|
|
stateExisted = Files.isRegularFile(WEBRTC_FILE);
|
|
|
|
|
if (stateExisted) previousState = Files.readAllBytes(WEBRTC_FILE);
|
|
|
|
|
stateSnapshotTaken = true;
|
|
|
|
|
request.turnSharedSecret = request.turnEnabled
|
|
|
|
|
? (!trimToEmpty(request.turnSharedSecret).isBlank()
|
|
|
|
|
? request.turnSharedSecret.trim() : trimToEmpty(previous.turnSharedSecret))
|
|
|
|
|
: "";
|
|
|
|
|
validateTurnSettings(request);
|
|
|
|
|
WebRtcState next = previous;
|
|
|
|
|
next.rtpStart = request.rtpStart;
|
|
|
|
|
next.rtpEnd = request.rtpEnd;
|
|
|
|
|
next.iceEnabled = true;
|
|
|
|
|
next.stunHost = trimToEmpty(request.stunHost);
|
|
|
|
|
next.stunPort = request.stunPort;
|
|
|
|
|
next.clientStunHost = trimToEmpty(request.clientStunHost);
|
|
|
|
|
next.clientStunPort = request.clientStunPort;
|
|
|
|
|
next.iceHostCandidates = parseIceHostCandidates(request.iceHostCandidates);
|
|
|
|
|
next.includeLocalIceCandidates = request.includeLocalIceCandidates;
|
|
|
|
|
next.turnEnabled = request.turnEnabled;
|
|
|
|
|
next.turnHost = trimToEmpty(request.turnHost);
|
|
|
|
|
next.turnPort = request.turnPort;
|
|
|
|
|
next.turnTlsPort = request.turnTlsPort;
|
|
|
|
|
next.turnUdpEnabled = request.turnUdpEnabled;
|
|
|
|
|
next.turnTcpEnabled = request.turnTcpEnabled;
|
|
|
|
|
next.turnTlsEnabled = request.turnTlsEnabled;
|
|
|
|
|
next.turnRelayStart = request.turnRelayStart;
|
|
|
|
|
next.turnRelayEnd = request.turnRelayEnd;
|
|
|
|
|
next.turnRealm = trimToEmpty(request.turnRealm).isBlank()
|
|
|
|
|
? next.turnHost : request.turnRealm.trim();
|
|
|
|
|
next.turnInternalIp = trimToEmpty(request.turnInternalIp);
|
|
|
|
|
next.turnExternalIp = trimToEmpty(request.turnExternalIp);
|
|
|
|
|
next.turnTlsCertPath = trimToEmpty(request.turnTlsCertPath);
|
|
|
|
|
next.turnTlsPrivateKeyPath = trimToEmpty(request.turnTlsPrivateKeyPath);
|
|
|
|
|
next.turnCredentialTtlSeconds = request.turnCredentialTtlSeconds;
|
|
|
|
|
next.turnSharedSecret = request.turnSharedSecret;
|
|
|
|
|
|
|
|
|
|
Files.createDirectories(STATE_DIR);
|
|
|
|
|
writePrivate(WEBRTC_FILE, objectMapper.writeValueAsString(next) + "\n");
|
|
|
|
|
if (!fileGenerator.GenerateFiles())
|
|
|
|
|
throw new IllegalStateException(
|
|
|
|
|
"Could not apply RTP settings. Check Asterisk configuration permissions and logs.");
|
|
|
|
|
result.addResult("rtp", readRtpConfiguration());
|
|
|
|
|
result.setHasError(false);
|
|
|
|
|
userLogger.addLog("Updated Asterisk RTP, ICE, and TURN client settings");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (e instanceof ResponseStatusException responseStatusException) throw responseStatusException;
|
|
|
|
|
if (stateSnapshotTaken) {
|
|
|
|
|
try {
|
|
|
|
|
if (stateExisted && previousState != null) Files.write(WEBRTC_FILE, previousState);
|
|
|
|
|
else if (!stateExisted) Files.deleteIfExists(WEBRTC_FILE);
|
|
|
|
|
fileGenerator.GenerateFiles();
|
|
|
|
|
} catch (Exception ignored) { }
|
|
|
|
|
}
|
|
|
|
|
result.setHasError(true);
|
|
|
|
|
result.addMessage(safeMessage(e));
|
|
|
|
|
userLogger.addLog("Failed to update Asterisk RTP, ICE, and TURN client settings: " + safeMessage(e));
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/client-settings")
|
|
|
|
|
public ResponseEntity<ApiResult> getClientSettings(HttpSession session,
|
|
|
|
|
HttpServletRequest servletRequest, @RequestParam String username) {
|
|
|
|
|
ApiResult result = new ApiResult();
|
|
|
|
|
try {
|
|
|
|
|
requireSipUserRead(session);
|
|
|
|
|
if (username == null || !username.matches("[A-Za-z0-9_.-]{1,64}"))
|
|
|
|
|
throw new IllegalArgumentException("SIP username is invalid");
|
|
|
|
|
if (!Files.isRegularFile(WEBRTC_FILE))
|
|
|
|
|
throw new IllegalStateException("Save the WebRTC transport settings before generating a client QR code");
|
|
|
|
|
WebRtcState state = readWebRtcState();
|
|
|
|
|
String host = "_".equals(state.serverName) || state.serverName.isBlank()
|
|
|
|
|
? servletRequest.getServerName() : state.serverName;
|
|
|
|
|
String path = "proxy".equals(state.mode) ? "/asterisk-ws" : "/ws";
|
|
|
|
|
String signalingUrl = "wss://" + host
|
|
|
|
|
+ (state.port == 443 ? "" : ":" + state.port) + path;
|
|
|
|
|
Map<String, Object> turn = Map.of("urls", List.of(), "username", "", "credential", "",
|
|
|
|
|
"expiresAt", 0L);
|
|
|
|
|
if (state.turnEnabled) {
|
|
|
|
|
long expiresAt = System.currentTimeMillis() / 1000L + state.turnCredentialTtlSeconds;
|
|
|
|
|
String turnUsername = expiresAt + ":" + username;
|
|
|
|
|
String credential = createTurnCredential(turnUsername, state.turnSharedSecret);
|
|
|
|
|
turn = Map.of("urls", buildTurnUrls(state), "username", turnUsername,
|
|
|
|
|
"credential", credential, "expiresAt", expiresAt);
|
|
|
|
|
}
|
|
|
|
|
List<Map<String, Object>> iceServers = new ArrayList<>();
|
|
|
|
|
if (!state.clientStunHost.isBlank()) {
|
|
|
|
|
iceServers.add(Map.of("urls", "stun:" + formatIceServerHost(state.clientStunHost)
|
|
|
|
|
+ ":" + state.clientStunPort));
|
|
|
|
|
}
|
|
|
|
|
if (state.turnEnabled) {
|
|
|
|
|
iceServers.add(Map.of("urls", buildTurnUrls(state), "username", turn.get("username"),
|
|
|
|
|
"credential", turn.get("credential")));
|
|
|
|
|
}
|
|
|
|
|
result.addResult("signalingUrl", signalingUrl);
|
|
|
|
|
result.addResult("turn", turn);
|
|
|
|
|
result.addResult("iceServers", iceServers);
|
|
|
|
|
result.setHasError(false);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (e instanceof ResponseStatusException responseStatusException) throw responseStatusException;
|
|
|
|
|
result.setHasError(true);
|
|
|
|
|
result.addMessage(safeMessage(e));
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> readWebRtcConfiguration() throws Exception {
|
|
|
|
|
WebRtcState state = readWebRtcState();
|
|
|
|
|
Map<String, Object> configuration = new HashMap<>();
|
|
|
|
|
@@ -274,6 +429,31 @@ public class NginxManagementController {
|
|
|
|
|
configuration.put("proxyUrl", "wss://" + state.serverName + ":" + state.port + "/asterisk-ws");
|
|
|
|
|
configuration.put("directUrl", "wss://" + state.serverName + ":" + state.port + "/ws");
|
|
|
|
|
configuration.put("upstreamHost", state.upstreamHost);
|
|
|
|
|
configuration.put("rtpStart", state.rtpStart);
|
|
|
|
|
configuration.put("rtpEnd", state.rtpEnd);
|
|
|
|
|
configuration.put("iceEnabled", state.iceEnabled);
|
|
|
|
|
configuration.put("stunHost", state.stunHost);
|
|
|
|
|
configuration.put("stunPort", state.stunPort);
|
|
|
|
|
configuration.put("clientStunHost", state.clientStunHost);
|
|
|
|
|
configuration.put("clientStunPort", state.clientStunPort);
|
|
|
|
|
configuration.put("iceHostCandidates", state.iceHostCandidates);
|
|
|
|
|
configuration.put("includeLocalIceCandidates", state.includeLocalIceCandidates);
|
|
|
|
|
configuration.put("turnEnabled", state.turnEnabled);
|
|
|
|
|
configuration.put("turnHost", state.turnHost);
|
|
|
|
|
configuration.put("turnPort", state.turnPort);
|
|
|
|
|
configuration.put("turnTlsPort", state.turnTlsPort);
|
|
|
|
|
configuration.put("turnUdpEnabled", state.turnUdpEnabled);
|
|
|
|
|
configuration.put("turnTcpEnabled", state.turnTcpEnabled);
|
|
|
|
|
configuration.put("turnTlsEnabled", state.turnTlsEnabled);
|
|
|
|
|
configuration.put("turnRelayStart", state.turnRelayStart);
|
|
|
|
|
configuration.put("turnRelayEnd", state.turnRelayEnd);
|
|
|
|
|
configuration.put("turnRealm", state.turnRealm);
|
|
|
|
|
configuration.put("turnInternalIp", state.turnInternalIp);
|
|
|
|
|
configuration.put("turnExternalIp", state.turnExternalIp);
|
|
|
|
|
configuration.put("turnTlsCertPath", state.turnTlsCertPath);
|
|
|
|
|
configuration.put("turnTlsPrivateKeyPath", state.turnTlsPrivateKeyPath);
|
|
|
|
|
configuration.put("turnCredentialTtlSeconds", state.turnCredentialTtlSeconds);
|
|
|
|
|
configuration.put("turnSecretConfigured", !trimToEmpty(state.turnSharedSecret).isBlank());
|
|
|
|
|
configuration.put("http", httpRepository.getFirstByEnable().map(value -> Map.of(
|
|
|
|
|
"enabled", value.isEnabled(),
|
|
|
|
|
"bindAddress", value.getBindAddr(),
|
|
|
|
|
@@ -284,6 +464,37 @@ public class NginxManagementController {
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Object> readRtpConfiguration() throws Exception {
|
|
|
|
|
WebRtcState state = readWebRtcState();
|
|
|
|
|
Map<String, Object> configuration = new HashMap<>();
|
|
|
|
|
configuration.put("rtpStart", state.rtpStart);
|
|
|
|
|
configuration.put("rtpEnd", state.rtpEnd);
|
|
|
|
|
configuration.put("iceEnabled", state.iceEnabled);
|
|
|
|
|
configuration.put("stunHost", state.stunHost);
|
|
|
|
|
configuration.put("stunPort", state.stunPort);
|
|
|
|
|
configuration.put("clientStunHost", state.clientStunHost);
|
|
|
|
|
configuration.put("clientStunPort", state.clientStunPort);
|
|
|
|
|
configuration.put("iceHostCandidates", state.iceHostCandidates);
|
|
|
|
|
configuration.put("includeLocalIceCandidates", state.includeLocalIceCandidates);
|
|
|
|
|
configuration.put("turnEnabled", state.turnEnabled);
|
|
|
|
|
configuration.put("turnHost", state.turnHost);
|
|
|
|
|
configuration.put("turnPort", state.turnPort);
|
|
|
|
|
configuration.put("turnTlsPort", state.turnTlsPort);
|
|
|
|
|
configuration.put("turnUdpEnabled", state.turnUdpEnabled);
|
|
|
|
|
configuration.put("turnTcpEnabled", state.turnTcpEnabled);
|
|
|
|
|
configuration.put("turnTlsEnabled", state.turnTlsEnabled);
|
|
|
|
|
configuration.put("turnRelayStart", state.turnRelayStart);
|
|
|
|
|
configuration.put("turnRelayEnd", state.turnRelayEnd);
|
|
|
|
|
configuration.put("turnRealm", state.turnRealm);
|
|
|
|
|
configuration.put("turnInternalIp", state.turnInternalIp);
|
|
|
|
|
configuration.put("turnExternalIp", state.turnExternalIp);
|
|
|
|
|
configuration.put("turnTlsCertPath", state.turnTlsCertPath);
|
|
|
|
|
configuration.put("turnTlsPrivateKeyPath", state.turnTlsPrivateKeyPath);
|
|
|
|
|
configuration.put("turnCredentialTtlSeconds", state.turnCredentialTtlSeconds);
|
|
|
|
|
configuration.put("turnSecretConfigured", !trimToEmpty(state.turnSharedSecret).isBlank());
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private WebRtcState readWebRtcState() throws Exception {
|
|
|
|
|
if (Files.isRegularFile(WEBRTC_FILE))
|
|
|
|
|
return objectMapper.readValue(WEBRTC_FILE.toFile(), WebRtcState.class);
|
|
|
|
|
@@ -313,8 +524,8 @@ public class NginxManagementController {
|
|
|
|
|
|
|
|
|
|
private void updateWebRtcProfiles(String mode, String dtlsMode) {
|
|
|
|
|
String transportProtocol = "direct".equals(mode) ? "wss" : "ws";
|
|
|
|
|
String dtlsCert = plineTools.getConfigPath() + "keys/raymand-webrtc-dtls.pem";
|
|
|
|
|
String dtlsKey = plineTools.getConfigPath() + "keys/raymand-webrtc-dtls-key.pem";
|
|
|
|
|
String dtlsCert = plineTools.getAsteriskConfigPath() + "keys/raymand-webrtc-dtls.pem";
|
|
|
|
|
String dtlsKey = plineTools.getAsteriskConfigPath() + "keys/raymand-webrtc-dtls-key.pem";
|
|
|
|
|
List<TblSipProfileDetails> updates = new ArrayList<>();
|
|
|
|
|
List<TblSipProfileDetails> removals = new ArrayList<>();
|
|
|
|
|
for (TblSipProfile profile : sipProfilesRepository.findAll()) {
|
|
|
|
|
@@ -328,6 +539,15 @@ public class NginxManagementController {
|
|
|
|
|
List<TblSipProfileDetails> transport = sipProfileDetailsRepository
|
|
|
|
|
.getBySipProfileIdAndType(profile.getId(), "transport");
|
|
|
|
|
setProfileValue(profile, transport, "protocol", transportProtocol, updates, removals);
|
|
|
|
|
setProfileValue(profile, transport, "allow_reload", "yes", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "ice_support", "yes", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "use_avpf", "yes", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "media_encryption", "dtls", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "dtls_verify", "fingerprint", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "dtls_setup", "actpass", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "media_use_received_transport", "yes", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "rtcp_mux", "yes", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "direct_media", "no", updates, removals);
|
|
|
|
|
setProfileValue(profile, endpoint, "dtls_auto_generate_cert",
|
|
|
|
|
"auto".equals(dtlsMode) ? "yes" : "no", updates, removals);
|
|
|
|
|
if ("custom".equals(dtlsMode)) {
|
|
|
|
|
@@ -342,6 +562,133 @@ public class NginxManagementController {
|
|
|
|
|
if (!updates.isEmpty()) sipProfileDetailsRepository.saveAll(updates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateRtpSettings(int rtpStart, int rtpEnd, String stunHost, int stunPort,
|
|
|
|
|
String clientStunHost, int clientStunPort, String iceHostCandidates) {
|
|
|
|
|
if (rtpStart < 1024 || rtpEnd > 65535 || rtpStart >= rtpEnd)
|
|
|
|
|
throw new IllegalArgumentException("RTP ports must be between 1024 and 65535, with the end port greater than the start port");
|
|
|
|
|
if (stunPort < 1 || stunPort > 65535)
|
|
|
|
|
throw new IllegalArgumentException("STUN port must be between 1 and 65535");
|
|
|
|
|
if (!trimToEmpty(stunHost).isBlank()) validateWebRtcHost(stunHost, "STUN host");
|
|
|
|
|
if (clientStunPort < 1 || clientStunPort > 65535)
|
|
|
|
|
throw new IllegalArgumentException("Browser STUN port must be between 1 and 65535");
|
|
|
|
|
if (!trimToEmpty(clientStunHost).isBlank())
|
|
|
|
|
validateWebRtcHost(clientStunHost, "Browser STUN host");
|
|
|
|
|
List<String> candidates = parseIceHostCandidates(iceHostCandidates);
|
|
|
|
|
if (!candidates.isEmpty() && !trimToEmpty(stunHost).isBlank())
|
|
|
|
|
throw new IllegalArgumentException("Use either a STUN server or static ICE host mappings for Asterisk, not both");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateTurnSettings(RtpSettingsRequest request) {
|
|
|
|
|
if (!request.turnEnabled) return;
|
|
|
|
|
validateWebRtcHost(request.turnHost, "TURN host");
|
|
|
|
|
if (!trimToEmpty(request.turnRealm).isBlank())
|
|
|
|
|
validateWebRtcHost(request.turnRealm, "TURN realm");
|
|
|
|
|
if (!request.turnUdpEnabled && !request.turnTcpEnabled && !request.turnTlsEnabled)
|
|
|
|
|
throw new IllegalArgumentException("Enable at least one TURN transport");
|
|
|
|
|
if (request.turnPort < 1 || request.turnPort > 65535
|
|
|
|
|
|| request.turnTlsPort < 1 || request.turnTlsPort > 65535)
|
|
|
|
|
throw new IllegalArgumentException("TURN ports must be between 1 and 65535");
|
|
|
|
|
if (request.turnRelayStart < 1024 || request.turnRelayEnd > 65535
|
|
|
|
|
|| request.turnRelayStart >= request.turnRelayEnd)
|
|
|
|
|
throw new IllegalArgumentException("TURN relay ports must be between 1024 and 65535, with the end port greater than the start port");
|
|
|
|
|
if (request.turnCredentialTtlSeconds < 60 || request.turnCredentialTtlSeconds > 86400)
|
|
|
|
|
throw new IllegalArgumentException("TURN credential lifetime must be between 60 and 86400 seconds");
|
|
|
|
|
if (!trimToEmpty(request.turnInternalIp).isBlank() || !trimToEmpty(request.turnExternalIp).isBlank()) {
|
|
|
|
|
if (trimToEmpty(request.turnInternalIp).isBlank() || trimToEmpty(request.turnExternalIp).isBlank()
|
|
|
|
|
|| !isIpLiteral(request.turnInternalIp) || !isIpLiteral(request.turnExternalIp))
|
|
|
|
|
throw new IllegalArgumentException("Enter both valid internal and public TURN IP addresses, or leave both empty");
|
|
|
|
|
}
|
|
|
|
|
if (request.turnTlsEnabled) {
|
|
|
|
|
validateAbsolutePath(request.turnTlsCertPath, "TURN TLS certificate path");
|
|
|
|
|
validateAbsolutePath(request.turnTlsPrivateKeyPath, "TURN TLS private key path");
|
|
|
|
|
}
|
|
|
|
|
if (trimToEmpty(request.turnSharedSecret).length() < 32)
|
|
|
|
|
throw new IllegalArgumentException("TURN shared secret must contain at least 32 characters");
|
|
|
|
|
if (trimToEmpty(request.turnSharedSecret).length() > 256)
|
|
|
|
|
throw new IllegalArgumentException("TURN shared secret cannot exceed 256 characters");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> parseIceHostCandidates(String value) {
|
|
|
|
|
List<String> candidates = new ArrayList<>();
|
|
|
|
|
if (value == null || value.isBlank()) return candidates;
|
|
|
|
|
for (String line : value.split("\\R")) {
|
|
|
|
|
String candidate = line.trim();
|
|
|
|
|
if (candidate.isEmpty()) continue;
|
|
|
|
|
String[] addresses = candidate.split("=>", -1);
|
|
|
|
|
if (addresses.length != 2 || !isIpLiteral(addresses[0]) || !isIpLiteral(addresses[1]))
|
|
|
|
|
throw new IllegalArgumentException("Each ICE mapping must be an IP address pair in the form local IP => public IP");
|
|
|
|
|
candidates.add(normalizeIpLiteral(addresses[0]) + " => " + normalizeIpLiteral(addresses[1]));
|
|
|
|
|
}
|
|
|
|
|
return candidates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String normalizeIpLiteral(String value) {
|
|
|
|
|
String address = value.trim();
|
|
|
|
|
return address.startsWith("[") && address.endsWith("]")
|
|
|
|
|
? address.substring(1, address.length() - 1) : address;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateWebRtcHost(String value, String label) {
|
|
|
|
|
if (value == null || value.isBlank() || "_".equals(value.trim())
|
|
|
|
|
|| !HOST_PATTERN.matcher(value.trim()).matches())
|
|
|
|
|
throw new IllegalArgumentException(label + " must be a DNS name or IP address");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isIpLiteral(String value) {
|
|
|
|
|
if (value == null) return false;
|
|
|
|
|
String address = value.trim();
|
|
|
|
|
if (address.startsWith("[") && address.endsWith("]"))
|
|
|
|
|
address = address.substring(1, address.length() - 1);
|
|
|
|
|
if (!IP_LITERAL_PATTERN.matcher(address).matches() || address.contains("/")) return false;
|
|
|
|
|
try {
|
|
|
|
|
return InetAddress.getByName(address).getHostAddress() != null;
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void validateAbsolutePath(String value, String label) {
|
|
|
|
|
if (value == null || !value.startsWith("/") || value.contains("\n") || value.contains("\r"))
|
|
|
|
|
throw new IllegalArgumentException(label + " must be an absolute filesystem path");
|
|
|
|
|
try {
|
|
|
|
|
Path.of(value);
|
|
|
|
|
} catch (InvalidPathException e) {
|
|
|
|
|
throw new IllegalArgumentException(label + " is invalid");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String trimToEmpty(String value) {
|
|
|
|
|
return value == null ? "" : value.trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> buildTurnUrls(WebRtcState state) {
|
|
|
|
|
String host = formatIceServerHost(state.turnHost);
|
|
|
|
|
List<String> urls = new ArrayList<>();
|
|
|
|
|
if (state.turnUdpEnabled) urls.add("turn:" + host + ":" + state.turnPort + "?transport=udp");
|
|
|
|
|
if (state.turnTcpEnabled) urls.add("turn:" + host + ":" + state.turnPort + "?transport=tcp");
|
|
|
|
|
if (state.turnTlsEnabled) urls.add("turns:" + host + ":" + state.turnTlsPort + "?transport=tcp");
|
|
|
|
|
return urls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String formatIceServerHost(String host) {
|
|
|
|
|
return host.contains(":") && !host.startsWith("[") ? "[" + host + "]" : host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String createTurnCredential(String username, String secret) throws Exception {
|
|
|
|
|
Mac mac = Mac.getInstance("HmacSHA1");
|
|
|
|
|
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));
|
|
|
|
|
return Base64.getEncoder().encodeToString(mac.doFinal(username.getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void requireSipUserRead(HttpSession session) {
|
|
|
|
|
Object current = session.getAttribute("user");
|
|
|
|
|
if (!(current instanceof TblPlineUser user))
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Login required");
|
|
|
|
|
if (user.getId() != null && user.getId() == 1L) return;
|
|
|
|
|
if (userAccessRepository.findAllByPlineUserAndMenuIdAndAction(user, "2_1", UserAccessAction.Read).isEmpty())
|
|
|
|
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Permission denied");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setProfileValue(TblSipProfile profile, List<TblSipProfileDetails> details,
|
|
|
|
|
String key, String value, List<TblSipProfileDetails> updates,
|
|
|
|
|
List<TblSipProfileDetails> removals) {
|
|
|
|
|
@@ -470,6 +817,33 @@ public class NginxManagementController {
|
|
|
|
|
public String dtlsPrivateKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class RtpSettingsRequest {
|
|
|
|
|
public int rtpStart = 10000;
|
|
|
|
|
public int rtpEnd = 20000;
|
|
|
|
|
public String stunHost = "";
|
|
|
|
|
public int stunPort = 3478;
|
|
|
|
|
public String clientStunHost = "";
|
|
|
|
|
public int clientStunPort = 3478;
|
|
|
|
|
public String iceHostCandidates = "";
|
|
|
|
|
public boolean includeLocalIceCandidates;
|
|
|
|
|
public boolean turnEnabled;
|
|
|
|
|
public String turnHost = "";
|
|
|
|
|
public int turnPort = 3478;
|
|
|
|
|
public int turnTlsPort = 5349;
|
|
|
|
|
public boolean turnUdpEnabled = true;
|
|
|
|
|
public boolean turnTcpEnabled;
|
|
|
|
|
public boolean turnTlsEnabled;
|
|
|
|
|
public int turnRelayStart = 49152;
|
|
|
|
|
public int turnRelayEnd = 65535;
|
|
|
|
|
public String turnRealm = "";
|
|
|
|
|
public String turnInternalIp = "";
|
|
|
|
|
public String turnExternalIp = "";
|
|
|
|
|
public String turnTlsCertPath = "";
|
|
|
|
|
public String turnTlsPrivateKeyPath = "";
|
|
|
|
|
public int turnCredentialTtlSeconds = 3600;
|
|
|
|
|
public String turnSharedSecret = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class WebRtcState {
|
|
|
|
|
public String mode = "proxy";
|
|
|
|
|
public String dtlsMode = "auto";
|
|
|
|
|
@@ -482,6 +856,31 @@ public class NginxManagementController {
|
|
|
|
|
public boolean originalTlsEnabled;
|
|
|
|
|
public String originalTlsCert = "";
|
|
|
|
|
public String originalTlsKey = "";
|
|
|
|
|
public int rtpStart = 10000;
|
|
|
|
|
public int rtpEnd = 20000;
|
|
|
|
|
public boolean iceEnabled = true;
|
|
|
|
|
public String stunHost = "";
|
|
|
|
|
public int stunPort = 3478;
|
|
|
|
|
public String clientStunHost = "";
|
|
|
|
|
public int clientStunPort = 3478;
|
|
|
|
|
public List<String> iceHostCandidates = new ArrayList<>();
|
|
|
|
|
public boolean includeLocalIceCandidates;
|
|
|
|
|
public boolean turnEnabled;
|
|
|
|
|
public String turnHost = "";
|
|
|
|
|
public int turnPort = 3478;
|
|
|
|
|
public int turnTlsPort = 5349;
|
|
|
|
|
public boolean turnUdpEnabled = true;
|
|
|
|
|
public boolean turnTcpEnabled;
|
|
|
|
|
public boolean turnTlsEnabled;
|
|
|
|
|
public int turnRelayStart = 49152;
|
|
|
|
|
public int turnRelayEnd = 65535;
|
|
|
|
|
public String turnRealm = "";
|
|
|
|
|
public String turnInternalIp = "";
|
|
|
|
|
public String turnExternalIp = "";
|
|
|
|
|
public String turnTlsCertPath = "";
|
|
|
|
|
public String turnTlsPrivateKeyPath = "";
|
|
|
|
|
public int turnCredentialTtlSeconds = 3600;
|
|
|
|
|
public String turnSharedSecret = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class SslRequest {
|
|
|
|
|
|