Add proxy settings to PeerConnection

Only Java SDK, PortAllocator, and TCP sockets are supported.
This commit is contained in:
Oscar Mira 2024-05-08 02:02:38 +02:00
parent 2eb4c30a1d
commit 9a5698762b
No known key found for this signature in database
GPG key ID: B371B98C5DC32237
5 changed files with 156 additions and 0 deletions

View file

@ -149,6 +149,7 @@
#include "rtc_base/ssl_certificate.h"
#include "rtc_base/ssl_stream_adapter.h"
#include "rtc_base/system/rtc_export.h"
#include "rtc_base/proxy_info.h"
#include "rtc_base/thread.h"
namespace rtc {
@ -439,6 +440,7 @@ class RTC_EXPORT PeerConnectionInterface : public webrtc::RefCountInterface {
// TODO(pthatcher): Rename this ice_servers, but update Chromium
// at the same time.
IceServers servers;
rtc::ProxyInfo proxy_info;
// TODO(pthatcher): Rename this ice_transport_type, but update
// Chromium at the same time.
IceTransportsType type = kAll;

View file

@ -455,6 +455,7 @@ bool PeerConnectionInterface::RTCConfiguration::operator==(
// Note: Order matters! Fields must be ordered the same as RTCConfiguration.
struct stuff_being_tested_for_equality {
IceServers servers;
rtc::ProxyInfo proxy_info;
IceTransportsType type;
BundlePolicy bundle_policy;
RtcpMuxPolicy rtcp_mux_policy;

View file

@ -255,6 +255,10 @@ PeerConnectionFactory::CreatePeerConnectionOrError(
port_allocator_flags);
}
if (dependencies.allocator) {
dependencies.allocator->set_proxy("", configuration.proxy_info);
}
if (!dependencies.ice_transport_factory) {
dependencies.ice_transport_factory =
std::make_unique<DefaultIceTransportFactory>();

View file

@ -372,6 +372,96 @@ public class PeerConnection {
}
}
/** Java version of rtc::ProxyInfo */
public static class ProxyInfo {
public final String hostname;
public final int port;
public final String username;
public final String password;
public final ProxyType type;
private ProxyInfo(String hostname, int port, String username, String password,
ProxyType type) {
this.hostname = hostname;
this.port = port;
this.username = username;
this.password = password;
this.type = type;
}
public static Builder builder(String hostname, int port) {
return new Builder(hostname, port);
}
public static class Builder {
private String hostname = "";
private int port;
private ProxyType type = ProxyType.PROXY_HTTPS;
private String username = "";
private String password = "";
private Builder(String hostname, int port) {
if (hostname == null || hostname.isEmpty()) {
throw new IllegalArgumentException("hostname == null || hostname.isEmpty()");
}
this.hostname = hostname;
this.port = port;
}
public Builder setProxyType(ProxyType type) {
this.type = type;
return this;
}
public Builder setUsername(String username) {
this.username = username;
return this;
}
public Builder setPassword(String password) {
this.password = password;
return this;
}
public ProxyInfo createProxyInfo() {
return new ProxyInfo(hostname, port, username, password, type);
}
}
@Nullable
@CalledByNative("ProxyInfo")
String getHostname() {
return hostname;
}
@CalledByNative("ProxyInfo")
int getPort() {
return port;
}
@Nullable
@CalledByNative("ProxyInfo")
String getUsername() {
return username;
}
@Nullable
@CalledByNative("ProxyInfo")
String getPassword() {
return password;
}
@Nullable
@CalledByNative("ProxyInfo")
ProxyType getProxyType() {
return type;
}
}
/** Java version of rtc::ProxyType */
public enum ProxyType { PROXY_NONE, PROXY_HTTPS, PROXY_SOCKS5, PROXY_UNKNOWN }
/** Java version of PeerConnectionInterface.IceTransportsType */
public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
@ -473,6 +563,7 @@ public class PeerConnection {
public static class RTCConfiguration {
public IceTransportsType iceTransportsType;
public List<IceServer> iceServers;
public ProxyInfo proxyInfo;
public BundlePolicy bundlePolicy;
@Nullable public RtcCertificatePem certificate;
public RtcpMuxPolicy rtcpMuxPolicy;
@ -594,6 +685,7 @@ public class PeerConnection {
tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
this.iceServers = iceServers;
proxyInfo = null;
audioJitterBufferMaxPackets = 200;
// RingRTC change to configure the jitter buffer's max target delay.
audioJitterBufferMaxTargetDelayMs = 500;
@ -640,6 +732,11 @@ public class PeerConnection {
return iceServers;
}
@CalledByNative("RTCConfiguration")
ProxyInfo getProxyInfo() {
return proxyInfo;
}
@CalledByNative("RTCConfiguration")
BundlePolicy getBundlePolicy() {
return bundlePolicy;

View file

@ -111,6 +111,53 @@ PeerConnectionInterface::IceServers JavaToNativeIceServers(
return ice_servers;
}
rtc::ProxyType JavaToNativeProxyType(
JNIEnv* jni,
const jni_zero::JavaRef<jobject>& j_proxy_type) {
std::string enum_name = GetJavaEnumName(jni, j_proxy_type);
if (enum_name == "PROXY_NONE") {
return rtc::PROXY_NONE;
}
if (enum_name == "PROXY_HTTPS") {
return rtc::PROXY_HTTPS;
}
if (enum_name == "PROXY_SOCKS5") {
return rtc::PROXY_SOCKS5;
}
RTC_CHECK(false) << " Unexpected ProxyType enum name " << enum_name;
return rtc::PROXY_NONE;
}
rtc::ProxyInfo JavaToNativeProxyInfo(
JNIEnv* jni,
const jni_zero::JavaRef<jobject>& j_proxy_info) {
rtc::ProxyInfo proxy_info;
rtc::InsecureCryptStringImpl pass;
jni_zero::ScopedJavaLocalRef<jstring> hostname =
Java_ProxyInfo_getHostname(jni, j_proxy_info);
jni_zero::ScopedJavaLocalRef<jstring> username =
Java_ProxyInfo_getUsername(jni, j_proxy_info);
jni_zero::ScopedJavaLocalRef<jstring> password =
Java_ProxyInfo_getPassword(jni, j_proxy_info);
jni_zero::ScopedJavaLocalRef<jobject> proxy_type =
Java_ProxyInfo_getProxyType(jni, j_proxy_info);
int port = Java_ProxyInfo_getPort(jni, j_proxy_info);
proxy_info.address = rtc::SocketAddress(
JavaToNativeString(jni, hostname), port);
proxy_info.username = JavaToNativeString(jni, username);
pass.password() = JavaToNativeString(jni, password);
proxy_info.password = rtc::CryptString(pass);
proxy_info.type = JavaToNativeProxyType(jni, proxy_type);
return proxy_info;
}
SdpSemantics JavaToNativeSdpSemantics(
JNIEnv* jni,
const jni_zero::JavaRef<jobject>& j_sdp_semantics) {
@ -163,6 +210,8 @@ void JavaToNativeRTCConfiguration(
Java_RTCConfiguration_getCandidateNetworkPolicy(jni, j_rtc_config);
jni_zero::ScopedJavaLocalRef<jobject> j_ice_servers =
Java_RTCConfiguration_getIceServers(jni, j_rtc_config);
jni_zero::ScopedJavaLocalRef<jobject> j_proxy_info =
Java_RTCConfiguration_getProxyInfo(jni, j_rtc_config);
jni_zero::ScopedJavaLocalRef<jobject> j_continual_gathering_policy =
Java_RTCConfiguration_getContinualGatheringPolicy(jni, j_rtc_config);
jni_zero::ScopedJavaLocalRef<jobject> j_turn_port_prune_policy =
@ -192,6 +241,9 @@ void JavaToNativeRTCConfiguration(
rtc_config->candidate_network_policy =
JavaToNativeCandidateNetworkPolicy(jni, j_candidate_network_policy);
rtc_config->servers = JavaToNativeIceServers(jni, j_ice_servers);
if (!j_proxy_info.is_null()) {
rtc_config->proxy_info = JavaToNativeProxyInfo(jni, j_proxy_info);
}
rtc_config->audio_jitter_buffer_max_packets =
Java_RTCConfiguration_getAudioJitterBufferMaxPackets(jni, j_rtc_config);
// RingRTC change to configure the jitter buffer's max target delay.