feat(remote): 重构远程协助鉴权与会话管理,新增管理员直连控制

This commit is contained in:
tongtong committed 2026-11-09 19:06:30 +08:00
1 parent 9b427e1149
commit 6cf9959a09
14 files changed
+342 -140

No files matched your search

+1
View File
@@ -50,6 +50,7 @@
<!-- WebRTC 远程控制:系统签名触摸/按键注入(sharedUserId=android.uid.system 下生效) -->
<uses-permission android:name="android.permission.INJECT_EVENTS" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.MANAGE_MEDIA_PROJECTION" />
<!--baidumap start-->
@@ -8,7 +8,7 @@ import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.databinding.ActivitySettingsAssistBinding;
import com.ttstd.dialer.view.SwitchButton;
import com.ttstd.dialer.webrtc.signaling.WebSocketClient;
import com.ttstd.dialer.webrtc.service.ScreenCaptureService;
import org.jetbrains.annotations.Nullable;
@@ -44,24 +44,13 @@ public class SettingsAssistActivity extends BaseMvvmActivity<SettingsAssistViewM
@Override
public void onCheckedChanged(@Nullable SwitchButton view, boolean isChecked) {
mMMKV.encode(CommonConfig.KEY_REMOTE_ASSISTANCE_ENABLE, isChecked ? 1 : 0);
if (isChecked) {
// 开启:后台建立全局信令连接
WebSocketClient client = WebSocketClient.getInstance();
client.setServerUrl(CommonConfig.SIGNAL_SERVER_URL);
client.connect();
} else {
// 关闭:断开全局信令连接
WebSocketClient.getInstance().disconnect();
if (!isChecked) {
ScreenCaptureService service = ScreenCaptureService.getInstance();
if (service != null) service.stopFamilyRemoteControl();
}
}
});
mViewDataBinding.siAutoRemoteAssistance.setOnToggleChanged(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(@Nullable SwitchButton view, boolean isChecked) {
mMMKV.encode(CommonConfig.KEY_AUTO_REMOTE_ASSISTANCE_ENABLE, isChecked ? 1 : 0);
}
});
}
@@ -75,7 +64,6 @@ public class SettingsAssistActivity extends BaseMvvmActivity<SettingsAssistViewM
super.onResume();
// 还原开关状态
mViewDataBinding.siRemoteAssistance.setToggleStatu(mMMKV.decodeInt(CommonConfig.KEY_REMOTE_ASSISTANCE_ENABLE, 0) == 1);
mViewDataBinding.siAutoRemoteAssistance.setToggleStatu(mMMKV.decodeInt(CommonConfig.KEY_AUTO_REMOTE_ASSISTANCE_ENABLE, 0) == 1);
}
public class BtnClick {
@@ -25,7 +25,7 @@ public final class FeaturePermissionRepository {
public Completable restore(boolean lan, boolean exact) {
return Completable.fromAction(() -> {
MMKV preferences = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
if (lan && preferences.decodeInt(CommonConfig.KEY_REMOTE_ASSISTANCE_ENABLE, 0) == 1) {
if (lan) {
WebSocketClient client = WebSocketClient.getInstance();
if (!client.isConnected()) {
client.setServerUrl(CommonConfig.SIGNAL_SERVER_URL);
@@ -8,10 +8,10 @@ import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
@@ -26,10 +26,9 @@ import android.widget.Toast;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.BuildConfig;
import com.ttstd.dialer.R;
import com.ttstd.dialer.activity.settings.utils.SettingsUtilsActivity;
import com.ttstd.dialer.alarmclock.AlarmSyncManager;
import com.ttstd.dialer.bean.ApkInstalledInfo;
import com.ttstd.dialer.bean.BaseResponse;
@@ -50,21 +49,18 @@ import com.ttstd.dialer.network.OkHttpManager;
import com.ttstd.dialer.network.UrlConstants;
import com.ttstd.dialer.utils.ApkUtils;
import com.ttstd.dialer.utils.CameraUtil;
import com.ttstd.dialer.utils.SystemUtils;
import com.ttstd.dialer.utils.CmdUtil;
import com.ttstd.dialer.utils.Logger;
import com.ttstd.dialer.utils.RebootUtils;
import com.ttstd.dialer.utils.SystemUtils;
import com.ttstd.dialer.webrtc.service.ScreenCaptureActivity;
import com.ttstd.dialer.webrtc.service.ScreenCaptureService;
import com.tencent.mmkv.MMKV;
import com.ttstd.iconloader.utils.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
@@ -76,7 +72,6 @@ import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
@@ -770,7 +765,6 @@ public class PushExecutor {
Logger.e(TAG, "mirror: 收到屏幕镜像推送,extra=" + extra);
int remoteKey = mMMKV.decodeInt(CommonConfig.KEY_REMOTE_ASSISTANCE_ENABLE, 0);
int acceptKey = mMMKV.decodeInt(CommonConfig.KEY_AUTO_REMOTE_ASSISTANCE_ENABLE, 0);
// 非远程协助请求:不弹出采集界面
if (remoteKey != 1) {
@@ -786,15 +780,7 @@ public class PushExecutor {
return;
}
if (acceptKey == 1) {
// 自动接收并采集窗口串流(推送指定自动,或本地已开启自动接收开关)
Logger.d(TAG, "mirror: 自动接收远程协助,启动采集");
ScreenCaptureActivity.start(mContext);
} else {
// 需用户确认:弹出询问对话框
Logger.d(TAG, "mirror: 需用户确认,弹出询问对话框");
showRemoteAssistanceConfirmDialog();
}
showRemoteAssistanceConfirmDialog();
} catch (Exception e) {
Logger.e(TAG, "mirror: 启动屏幕串流失败: " + e.getMessage());
}
@@ -824,8 +810,7 @@ public class PushExecutor {
}
dialog.show();
} catch (Exception e) {
Logger.e(TAG, "mirror: 显示询问对话框失败,直接启动采集: " + e.getMessage());
ScreenCaptureActivity.start(mContext);
Logger.e(TAG, "mirror: 显示询问对话框失败: " + e.getMessage());
}
});
}
@@ -16,22 +16,19 @@ import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
import com.blankj.utilcode.util.NetworkUtils;
import com.kongzue.dialogx.dialogs.PopTip;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.kongzue.dialogx.dialogs.PopTip;
import com.shehuan.niv.NiceImageView;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.BuildConfig;
import com.ttstd.dialer.R;
import com.ttstd.dialer.activity.main.MainActivity;
import com.ttstd.dialer.activity.settings.assist.SettingsAssistActivity;
import com.ttstd.dialer.base.BaseService;
import com.ttstd.dialer.bean.req.SnLocationReq;
import com.ttstd.dialer.config.CommonConfig;
@@ -141,14 +138,10 @@ public class MainService extends BaseService implements NetworkUtils.OnNetworkSt
if (enableFloatWindow) {
showFloatWindow();
}
boolean remote = mMMKV.decodeInt(CommonConfig.KEY_REMOTE_ASSISTANCE_ENABLE, 0) == 1;
Logger.e(TAG, "onCreate: remote = " + remote);
if (remote) {
// 开启:后台建立全局信令连接
WebSocketClient client = WebSocketClient.getInstance();
client.setServerUrl(CommonConfig.SIGNAL_SERVER_URL);
client.connect();
}
// The authenticated signal socket also receives administrator sessions.
WebSocketClient client = WebSocketClient.getInstance();
client.setServerUrl(CommonConfig.SIGNAL_SERVER_URL);
client.connect();
}
@@ -59,19 +59,19 @@ public class InputCommandHandler {
switch (action) {
case TOUCH:
handleTouch(command);
if (!supportsMotionEvents()) handleTouch(command);
break;
case KEY:
handleKey(command);
break;
case SWIPE:
handleSwipe(command);
if (!supportsMotionEvents()) handleSwipe(command);
break;
case LONG_PRESS:
handleLongPress(command);
if (!supportsMotionEvents()) handleLongPress(command);
break;
case MOTION_EVENT:
handleMotionEvent(command);
if (supportsMotionEvents()) handleMotionEvent(command);
break;
case SET_RESOLUTION:
if (resolutionListener != null) {
@@ -96,6 +96,10 @@ public class InputCommandHandler {
}
}
public boolean supportsMotionEvents() {
return inputExecutor instanceof SystemInputUtils;
}
public void release() {
if (inputExecutor != null) {
inputExecutor.release();
@@ -0,0 +1,86 @@
package com.ttstd.dialer.webrtc.service;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Process;
import android.util.Log;
import android.view.Display;
import com.ttstd.dialer.webrtc.signaling.SignalMessage;
import com.ttstd.dialer.webrtc.signaling.WebSocketClient;
/**
* Starts a one-session system projection for an authenticated administrator offer.
*/
public final class AdminScreenCapture {
static final String EXTRA_SESSION_ID = "admin_capture_session_id";
private static final String MANAGE_PROJECTION = "android.permission.MANAGE_MEDIA_PROJECTION";
private static final String PROJECTION_EXTRA = "android.media.projection.extra.EXTRA_MEDIA_PROJECTION";
private AdminScreenCapture() {
}
public static void start(Context context, SignalMessage offer) {
WebSocketClient signaling = WebSocketClient.getInstance();
if (!offer.isAdminDirectOffer() || !signaling.hasPendingAdminOffer(offer.getSessionId()))
return;
try {
if (context == null
|| context.checkSelfPermission(MANAGE_PROJECTION) != PackageManager.PERMISSION_GRANTED) {
reject(signaling, offer);
return;
}
if (ScreenCaptureService.getInstance() != null) return;
Intent projection = createProjectionResult(context);
if (!signaling.hasPendingAdminOffer(offer.getSessionId())) return;
Intent service = new Intent(context, ScreenCaptureService.class);
service.putExtra(EXTRA_SESSION_ID, offer.getSessionId());
service.putExtra(ScreenCaptureService.EXTRA_RESULT_CODE, Activity.RESULT_OK);
service.putExtra(ScreenCaptureService.EXTRA_RESULT_DATA, projection);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(service);
} else {
context.startService(service);
}
} catch (ReflectiveOperationException | RuntimeException error) {
Log.w("AdminScreenCapture", "Privileged screen capture unavailable", error);
reject(signaling, offer);
}
}
private static Intent createProjectionResult(Context context) throws ReflectiveOperationException {
IBinder binder = (IBinder) Class.forName("android.os.ServiceManager")
.getMethod("getService", String.class).invoke(null, "media_projection");
Class<?> manager = Class.forName("android.media.projection.IMediaProjectionManager");
Object service = Class.forName(manager.getName() + "$Stub")
.getMethod("asInterface", IBinder.class).invoke(null, binder);
Object projection;
try {
projection = manager.getMethod("createProjection", int.class, String.class,
int.class, boolean.class, int.class)
.invoke(service, Process.myUid(), context.getPackageName(), 0, false, Display.DEFAULT_DISPLAY);
} catch (NoSuchMethodException olderFramework) {
projection = manager.getMethod("createProjection", int.class, String.class, int.class, boolean.class)
.invoke(service, Process.myUid(), context.getPackageName(), 0, false);
}
if (!(projection instanceof IInterface))
throw new IllegalStateException("Missing projection token");
Bundle extras = new Bundle();
extras.putBinder(PROJECTION_EXTRA, ((IInterface) projection).asBinder());
return new Intent().putExtras(extras);
}
static void reject(WebSocketClient signaling, SignalMessage offer) {
SignalMessage rejection = new SignalMessage("CONNECTION_REJECTED", null, offer.getFromDeviceId(),
"{\"reason\":\"设备无法启动管理员屏幕采集\"}");
rejection.setSessionId(offer.getSessionId());
signaling.sendMessage(rejection);
}
}
@@ -30,7 +30,6 @@ import com.ttstd.control.ControlMessage;
import com.ttstd.dialer.activity.main.MainActivity;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.mdm.DeviceManagerService;
import com.ttstd.dialer.utils.Logger;
import com.ttstd.dialer.webrtc.input.InputCommandHandler;
import com.ttstd.dialer.webrtc.input.InputExecutor;
import com.ttstd.dialer.webrtc.input.RootShellInputUtils;
@@ -99,6 +98,30 @@ public class ScreenCaptureService extends Service {
private String pendingControllerName;
private String pendingOfferSdp;
private String controllingName;
private String sessionId;
private String peerId;
private String adminCaptureSessionId;
private volatile boolean adminSession;
private String turnConfig;
private android.app.AlertDialog requestDialog;
public void stopRemoteControl() {
isShuttingDown = true;
stopSelf();
}
public void stopFamilyRemoteControl() {
if (!adminSession && adminCaptureSessionId == null) stopRemoteControl();
}
public static boolean isFamilyAssistanceEnabled() {
return com.tencent.mmkv.MMKV.mmkvWithID(CommonConfig.MMKV_ID, com.tencent.mmkv.MMKV.MULTI_PROCESS_MODE)
.decodeInt(CommonConfig.KEY_REMOTE_ASSISTANCE_ENABLE, 0) == 1;
}
private boolean canControl() {
return adminSession || isFamilyAssistanceEnabled();
}
private int realScreenWidth;
private int realScreenHeight;
@@ -109,7 +132,7 @@ public class ScreenCaptureService extends Service {
private List<Integer> supportedFpsList = new ArrayList<>();
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private boolean isShuttingDown = false;
private volatile boolean isShuttingDown = false;
private int resultCode;
private Intent resultDataIntent;
@@ -183,7 +206,6 @@ public class ScreenCaptureService extends Service {
@Override
public void onConnected() {
Log.i(TAG, "Connected to signal server");
isShuttingDown = false;
updateNotification("远程控制服务正在运行");
if (stateListener != null) stateListener.onSignalConnected();
}
@@ -202,7 +224,7 @@ public class ScreenCaptureService extends Service {
@Override
public void onMessage(SignalMessage message) {
handleSignalMessage(message);
mainHandler.post(() -> handleSignalMessage(message));
}
@Override
@@ -268,7 +290,7 @@ public class ScreenCaptureService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
if (intent == null || isShuttingDown) {
stopSelf();
return START_NOT_STICKY;
}
@@ -285,9 +307,18 @@ public class ScreenCaptureService extends Service {
return START_NOT_STICKY;
}
int resultCode = (sResultCode != Activity.RESULT_CANCELED)
adminCaptureSessionId = intent.getStringExtra(AdminScreenCapture.EXTRA_SESSION_ID);
boolean captureAllowed = adminCaptureSessionId != null
? WebSocketClient.getInstance().hasPendingAdminOffer(adminCaptureSessionId)
: isFamilyAssistanceEnabled() || adminSession;
if (!captureAllowed) {
stopRemoteControl();
return START_NOT_STICKY;
}
int resultCode = (adminCaptureSessionId == null && sResultCode != Activity.RESULT_CANCELED)
? sResultCode : intent.getIntExtra(EXTRA_RESULT_CODE, Activity.RESULT_CANCELED);
Intent resultData = (sResultData != null)
Intent resultData = (adminCaptureSessionId == null && sResultData != null)
? sResultData : intent.getParcelableExtra(EXTRA_RESULT_DATA);
this.resultCode = resultCode;
@@ -301,7 +332,15 @@ public class ScreenCaptureService extends Service {
}
isInitialized = true;
startScreenCapture(resultCode, resultData);
try {
startScreenCapture(resultCode, resultData);
} catch (RuntimeException error) {
Log.e(TAG, "Screen capture startup failed", error);
WebSocketClient signaling = WebSocketClient.getInstance();
SignalMessage offer = signaling.getPendingAdminOffer(adminCaptureSessionId);
if (offer != null) AdminScreenCapture.reject(signaling, offer);
stopRemoteControl();
}
// 按需启动:远程控制会话结束(onControllerDisconnected)即 stopSelf,
// 且不随系统重启自动复活,避免空转占用前台服务与 MediaProjection。
return START_NOT_STICKY;
@@ -337,13 +376,11 @@ public class ScreenCaptureService extends Service {
webRtcClient = new WebRtcClient(this, wsClient, deviceId);
webRtcClient.initialize(eglBase);
// WebRTC 就绪后,若 OFFER 已先到(竞态缓存),立即补应答,保证连接不因时序而中断
if (pendingOfferSdp != null && pendingControllerId != null) {
Log.i(TAG, "WebRTC 就绪,补应答先到的 OFFER");
mainHandler.post(this::acceptConnection);
}
webRtcClient.setSupportedFpsList(supportedFpsList);
webRtcClient.setInputCallback(bytes -> inputHandler.handleCommand(bytes));
webRtcClient.setInputCallback(bytes -> {
InputCommandHandler handler = inputHandler;
if (!isShuttingDown && canControl() && handler != null) handler.handleCommand(bytes);
});
webRtcClient.setIceEventListener(() -> mainHandler.post(() -> {
if (currentStreamMode == SelfCodecEncoder.STREAM_MODE_SELF_CODEC && selfEncoder != null) {
selfEncoder.requestKeyFrame();
@@ -356,6 +393,7 @@ public class ScreenCaptureService extends Service {
mainHandler.post(() -> onControllerDisconnected(controllerId)));
inputHandler = createInputCommandHandler();
webRtcClient.setTouchMode(inputHandler.supportsMotionEvents());
inputHandler.setResolutionRequestListener(this::onRemoteResolutionRequested);
inputHandler.setStreamModeListener(this::onStreamModeRequested);
@@ -363,7 +401,7 @@ public class ScreenCaptureService extends Service {
@Override
public void onStop() {
super.onStop();
Log.e(TAG, "MediaProjection stopped (ScreenCapturerAndroid)");
stopRemoteControl();
}
});
@@ -373,6 +411,9 @@ public class ScreenCaptureService extends Service {
// 避免静止画面(无触摸/无变化)下不推帧导致控制端黑屏。
mainHandler.removeCallbacks(heartbeatRunnable);
mainHandler.post(heartbeatRunnable);
mainHandler.postDelayed(() -> {
if (controllingName == null) stopRemoteControl();
}, 2 * 60 * 1000L);
}
/** 解析信令服务器地址(ws:// 或 wss://)。 */
@@ -410,19 +451,32 @@ public class ScreenCaptureService extends Service {
private void handleSignalMessage(SignalMessage message) {
String type = message.getType();
if (type == null) return;
if (type == null || isShuttingDown) return;
switch (type.toUpperCase()) {
case "OFFER":
handleOffer(message);
break;
case "SESSION_ENDED":
if ("server".equals(message.getFromDeviceId())
&& java.util.Objects.equals(sessionId, message.getSessionId()))
stopRemoteControl();
break;
case "ICE_CANDIDATE":
handleIceCandidate(message);
if (sessionId != null && sessionId.equals(message.getSessionId())
&& java.util.Objects.equals(peerId, message.getFromDeviceId()))
handleIceCandidate(message);
break;
}
}
private void handleOffer(SignalMessage message) {
if (pendingControllerId != null) {
if (!"CONTROLLER".equals(message.getDeviceType())
|| message.getSessionId() == null || message.getFromDeviceId() == null) return;
boolean administrator = message.isAdminDirectOffer();
if (!administrator && !isFamilyAssistanceEnabled()) return;
if (adminCaptureSessionId != null && !adminCaptureSessionId.equals(message.getSessionId()))
return;
if (sessionId != null) {
Log.i(TAG, "Ignore OFFER from " + message.getFromDeviceId() + ": a request is already pending");
return;
}
@@ -432,6 +486,7 @@ public class ScreenCaptureService extends Service {
try {
JsonObject payload = gson.fromJson(message.getPayload(), JsonObject.class);
offerSdp = payload.get("sdp").getAsString();
turnConfig = payload.has("turn") ? payload.get("turn").getAsString() : null;
if (payload.has("username") && !payload.get("username").isJsonNull()) {
controllerName = payload.get("username").getAsString();
}
@@ -446,18 +501,35 @@ public class ScreenCaptureService extends Service {
? controllerName : controllerId;
pendingOfferSdp = offerSdp;
// 鉴权模式:目标平台由信令服务统一鉴权(client JWT + 绑定校验),设备端不重复校验,
// 仅保留"是否允许远程控制"的总开关(由设备端设置,默认允许)。
if (connectionListener != null) {
connectionListener.onConnectionRequested(controllerId, pendingControllerName, offerSdp);
adminSession = administrator;
sessionId = message.getSessionId();
mainHandler.postDelayed(this::stopRemoteControl, 15 * 60 * 1000L);
peerId = controllerId;
if (administrator) {
acceptConnection();
} else {
// 未接入确认回调:直接接受(跟随平台"允许远程控制"总开关)。
mainHandler.post(this::acceptConnection);
try {
requestDialog = new android.app.AlertDialog.Builder(
new android.view.ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert))
.setTitle("远程协助")
.setMessage(pendingControllerName + " 请求查看屏幕并控制设备")
.setPositiveButton("允许", (dialog, which) -> acceptConnection())
.setNegativeButton("拒绝", (dialog, which) -> rejectConnection())
.setOnCancelListener(dialog -> rejectConnection())
.create();
requestDialog.getWindow().setType(android.os.Build.VERSION.SDK_INT >= 26
? android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
: android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
requestDialog.show();
} catch (RuntimeException e) {
rejectConnection();
}
}
}
/** 接受连接:创建 Answer 完成连接。 */
public void acceptConnection() {
if (isShuttingDown || !canControl()) return;
// 竞态保护:WebRTC 尚未初始化完成时(OFFER 先于 webRtcClient 到达),延迟重试。
if (pendingOfferSdp != null && pendingControllerId != null && webRtcClient == null) {
Log.w(TAG, "acceptConnection: webRtcClient 未就绪,延迟重试");
@@ -470,7 +542,7 @@ public class ScreenCaptureService extends Service {
controllingName = pendingControllerName;
clearPendingRequest();
updateNotification("正在接受 " + controllingName + " 的控制...");
webRtcClient.createPeerConnectionAndAnswer(offerSdp, controllerId);
webRtcClient.createPeerConnectionAndAnswer(offerSdp, controllerId, sessionId, turnConfig);
}
}
@@ -480,6 +552,7 @@ public class ScreenCaptureService extends Service {
String controllerId = pendingControllerId;
clearPendingRequest();
sendConnectionRejected(controllerId);
stopRemoteControl();
}
}
@@ -506,14 +579,7 @@ public class ScreenCaptureService extends Service {
if (isShuttingDown) return;
Log.i(TAG, "Signal server disconnected");
if (stateListener != null) stateListener.onSignalDisconnected();
// 信令已断开:若当前无控制端在观看,采集服务已无意义,直接停止;
// 全局信令单例会自行重连,待下次命令再拉起本服务。
if (controllingName == null || controllingName.isEmpty()) {
Log.i(TAG, "No active controller, stopping screen capture service");
stopSelf();
} else {
updateNotification("信令断开,正在重连...");
}
stopRemoteControl();
}
private void onControllerDisconnected(String controllerId) {
@@ -524,13 +590,14 @@ public class ScreenCaptureService extends Service {
if (stateListener != null) stateListener.onControllerDisconnected(name);
controllingName = null;
// 远程控制会话已结束:停止前台采集服务,释放 MediaProjection / WebRTC 等资源。
// 全局信令 WebSocket 由 siRemoteAssistance 开关控制,不受本服务停止影响。
// 全局信令 WebSocket 不受本服务停止影响。
stopSelf();
}
private void sendConnectionRejected(String controllerId) {
SignalMessage msg = new SignalMessage();
msg.setType("CONNECTION_REJECTED");
msg.setSessionId(sessionId);
msg.setFromDeviceId(deviceId);
msg.setToDeviceId(controllerId);
msg.setDeviceType("CONTROLLED");
@@ -734,8 +801,14 @@ public class ScreenCaptureService extends Service {
public void onDestroy() {
super.onDestroy();
isShuttingDown = true;
if (wsClient != null && sessionId != null && peerId != null) {
SignalMessage end = new SignalMessage("SESSION_END", deviceId, peerId, null);
end.setSessionId(sessionId);
wsClient.sendMessage(end);
}
// 停止心脏起搏器
mainHandler.removeCallbacks(heartbeatRunnable);
mainHandler.removeCallbacksAndMessages(null);
if (requestDialog != null) requestDialog.dismiss();
sResultCode = Activity.RESULT_CANCELED;
sResultData = null;
if (instance == this) instance = null;
@@ -751,8 +824,7 @@ public class ScreenCaptureService extends Service {
if (webRtcClient != null) {
webRtcClient.close();
}
// 仅移除本会话的监听器,不关闭全局单例连接
// (全局连接由 siRemoteAssistance 开关控制,见 SettingsAssistActivity)。
// 仅移除本会话的监听器,保留全局信令连接。
if (wsClient != null && signalListener != null) {
wsClient.removeSignalListener(signalListener);
}
@@ -7,9 +7,34 @@ public class SignalMessage {
private String toDeviceId;
private String deviceType;
private String payload;
private String sessionId;
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
private String authType;
private String authValue;
/**
* The signaling server reconstructs this field from the authorized ticket.
*/
public boolean isAdminDirectOffer() {
if (!"OFFER".equals(type) || !"CONTROLLER".equals(deviceType)
|| sessionId == null || fromDeviceId == null) return false;
try {
com.google.gson.JsonObject offer = com.google.gson.JsonParser.parseString(payload).getAsJsonObject();
com.google.gson.JsonElement flag = offer.get("adminDirectControl");
return flag != null && flag.isJsonPrimitive() && flag.getAsJsonPrimitive().isBoolean()
&& flag.getAsBoolean();
} catch (RuntimeException e) {
return false;
}
}
public SignalMessage() {}
public SignalMessage(String type, String fromDeviceId, String toDeviceId, String payload) {
@@ -9,8 +9,6 @@ import com.ttstd.dialer.network.SecurityUtils;
import com.ttstd.dialer.utils.NativeUtils;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@@ -56,8 +54,8 @@ public class WebSocketClient {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private OkHttpClient client;
private WebSocket ws;
private boolean manualClose = false;
private volatile WebSocket ws;
private volatile boolean manualClose = false;
private int reconnectAttempts = 0;
private static final int MAX_RECONNECT_DELAY = 30_000;
@@ -96,6 +94,16 @@ public class WebSocketClient {
* 若在注册前已有缓存的 OFFER(监听器晚于 OFFER 到达时注册),立即重放,
* 确保 ScreenCaptureService 能处理因时序早到的远程连接请求。
*/
public boolean hasPendingAdminOffer(String sessionId) {
return getPendingAdminOffer(sessionId) != null;
}
public SignalMessage getPendingAdminOffer(String sessionId) {
SignalMessage offer = pendingOffer;
return offer != null && sessionId != null && sessionId.equals(offer.getSessionId())
&& offer.isAdminDirectOffer() ? offer : null;
}
public void addSignalListener(SignalListener listener) {
if (listener == null) return;
if (!listeners.contains(listener)) {
@@ -133,14 +141,15 @@ public class WebSocketClient {
this.token = token;
}
public void connect() {
public synchronized void connect() {
if (ws != null) return;
android.content.Context context = com.ttstd.dialer.base.BaseApplication.getContext();
if (context != null && !com.ttstd.dialer.permissions.FeaturePermissionController.hasLan(context)) {
notifyError("远程协助需要局域网权限,请在应用权限中允许附近设备");
return;
}
if (serverUrl == null || serverUrl.isEmpty()) {
notifyError("服务器地址为空");
if (serverUrl == null || !serverUrl.startsWith("wss://")) {
notifyError("远程协助需要 WSS 信令地址");
return;
}
manualClose = false;
@@ -157,18 +166,20 @@ public class WebSocketClient {
if (sn != null && !sn.isEmpty()) {
String nonce = NativeUtils.getNonce();
long timestamp = System.currentTimeMillis();
SortedMap<String, String> headersMap = new TreeMap<>();
headersMap.put("X-Device-SN", sn);
headersMap.put("X-Nonce", nonce);
headersMap.put("X-Timestamp", String.valueOf(timestamp));
String sign = SecurityUtils.generateSign(headersMap);
String canonical = "GET|/ws/signal|" + sn + "|" + timestamp + "|" + nonce;
String sign = SecurityUtils.signWithEcdsa(canonical);
if (sign == null || sign.isEmpty()) {
notifyError("设备私钥不可用");
return;
}
reqBuilder.addHeader("X-Device-SN", sn);
reqBuilder.addHeader("X-Nonce", nonce);
reqBuilder.addHeader("X-Timestamp", String.valueOf(timestamp));
reqBuilder.addHeader("X-Sign", sign);
}
} catch (Exception e) {
Log.w(TAG, "附加 SN 签名头失败,继续尝试连接: " + e.getMessage());
notifyError("无法签署设备信令握手");
return;
}
// 可选:Bearer token(家属端/管理端控制时)
@@ -180,6 +191,7 @@ public class WebSocketClient {
ws = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
if (!isCurrent(webSocket)) return;
Log.d(TAG, "WebSocket 已连接");
reconnectAttempts = 0;
for (SignalListener l : listeners) {
@@ -189,7 +201,7 @@ public class WebSocketClient {
@Override
public void onMessage(WebSocket webSocket, String text) {
Log.d(TAG, "WebSocket 收到消息: " + text);
if (!isCurrent(webSocket)) return;
SignalMessage msg = parse(text);
if (msg == null) return;
if ("REGISTER_SUCCESS".equals(msg.getType()) && msg.getFromDeviceId() != null) {
@@ -199,10 +211,23 @@ public class WebSocketClient {
}
// 缓存最近一次 OFFER,供后注册的监听器(如 ScreenCaptureService)重放
if ("OFFER".equalsIgnoreCase(msg.getType())) {
if (!msg.isAdminDirectOffer() && !com.ttstd.dialer.webrtc.service.ScreenCaptureService.isFamilyAssistanceEnabled()) {
SignalMessage rejection = new SignalMessage("CONNECTION_REJECTED", null, msg.getFromDeviceId(), null);
rejection.setSessionId(msg.getSessionId());
sendMessage(rejection);
return;
}
pendingOffer = msg;
Log.d(TAG, "缓存 OFFER: from=" + msg.getFromDeviceId());
if (msg.isAdminDirectOffer()) {
new android.os.Handler(android.os.Looper.getMainLooper()).post(() -> {
if (isCurrent(webSocket) && hasPendingAdminOffer(msg.getSessionId())) {
com.ttstd.dialer.webrtc.service.AdminScreenCapture.start(context, msg);
}
});
}
} else if ("CONNECTION_REJECTED".equalsIgnoreCase(msg.getType())
|| "TARGET_OFFLINE".equalsIgnoreCase(msg.getType())) {
|| "TARGET_OFFLINE".equalsIgnoreCase(msg.getType())
|| "SESSION_ENDED".equals(msg.getType())) {
// 连接已终结/被拒,清空缓存,避免旧 OFFER 重放导致错连
pendingOffer = null;
}
@@ -213,6 +238,7 @@ public class WebSocketClient {
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
if (!isCurrent(webSocket)) return;
Log.w(TAG, "WebSocket onClosing code=" + code + " reason=" + reason);
if (code == 4001) {
for (SignalListener l : listeners) {
@@ -234,17 +260,20 @@ public class WebSocketClient {
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
if (!clearCurrent(webSocket)) return;
Log.d(TAG, "WebSocket 已关闭 code=" + code);
pendingOffer = null;
if (manualClose) {
notifyDisconnected();
return;
}
notifyDisconnected();
handleReconnect();
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
if (!clearCurrent(webSocket)) return;
Log.e(TAG, "WebSocket 连接失败: " + t.getMessage(), t);
if (manualClose) {
notifyDisconnected();
@@ -277,17 +306,29 @@ public class WebSocketClient {
}, delay, TimeUnit.MILLISECONDS);
}
public void disconnect() {
private synchronized boolean isCurrent(WebSocket socket) {
return ws == socket;
}
private synchronized boolean clearCurrent(WebSocket socket) {
if (ws != socket) return false;
ws = null;
pendingOffer = null;
return true;
}
public synchronized void disconnect() {
manualClose = true;
pendingOffer = null;
if (ws != null) {
ws.close(1000, "用户断开");
ws = null;
WebSocket current = ws;
ws = null;
if (current != null) {
current.close(1000, "用户断开");
notifyDisconnected();
}
if (client != null) {
client.dispatcher().executorService().shutdown();
}
scheduler.shutdownNow();
}
private SignalMessage parse(String text) {
@@ -298,6 +339,7 @@ public class WebSocketClient {
if (json.has("fromDeviceId")) msg.setFromDeviceId(getString(json, "fromDeviceId"));
if (json.has("toDeviceId")) msg.setToDeviceId(getString(json, "toDeviceId"));
if (json.has("deviceType")) msg.setDeviceType(getString(json, "deviceType"));
msg.setSessionId(getString(json, "sessionId"));
if (json.has("payload")) msg.setPayload(getString(json, "payload"));
if (json.has("authType")) msg.setAuthType(getString(json, "authType"));
if (json.has("authValue")) msg.setAuthValue(getString(json, "authValue"));
@@ -324,9 +366,8 @@ public class WebSocketClient {
}
public void send(String message) {
if (ws != null) {
ws.send(message);
}
WebSocket current = ws;
if (current != null) current.send(message);
}
public boolean isConnected() {
@@ -196,6 +196,11 @@ public class WebRtcClient {
}
private List<Integer> supportedFpsList;
private int touchMode;
public void setTouchMode(boolean supportsMotionEvents) {
touchMode = supportsMotionEvents ? 2 : 1;
}
public void setSupportedFpsList(List<Integer> fpsList) {
this.supportedFpsList = fpsList;
@@ -212,6 +217,7 @@ public class WebRtcClient {
.setWidth(width)
.setHeight(height)
.setFps(fps);
builder.setTouchMode(touchMode);
if (supportedFpsList != null && !supportedFpsList.isEmpty()) {
builder.addAllSupportedFps(supportedFpsList);
}
@@ -241,7 +247,8 @@ public class WebRtcClient {
return eglBase != null ? eglBase.getEglBaseContext() : null;
}
public void createPeerConnectionAndAnswer(String offerSdp, String controllerId) {
public void createPeerConnectionAndAnswer(String offerSdp, String controllerId,
String sessionId, String turnConfig) {
if (peerConnection != null) {
Log.d(TAG, "Closing previous peer connection");
suppressDisconnectNotify = true;
@@ -254,16 +261,22 @@ public class WebRtcClient {
this.pendingRemoteCandidates.clear();
List<PeerConnection.IceServer> iceServers = new ArrayList<>();
// ICE 服务器(与 webrtc_controller_flutter 的 ice_servers.dart 完全一致,含 STUN + TURN)。
iceServers.add(PeerConnection.IceServer.builder("stun:stun.l.google.com:19302").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("stun:175.178.213.60:3478").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("turns:175.178.213.60:5349").setUsername("fanhuitong").setPassword("Fan19961907..").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("turn:175.178.213.60:3478?transport=tcp").setUsername("fanhuitong").setPassword("Fan19961907..").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("turn:175.178.213.60:3478").setUsername("fanhuitong").setPassword("Fan19961907..").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("stun:47.242.112.133:3478").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("turn:47.242.112.133:3478").setUsername("ttstd").setPassword("fanhuitong").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("stun:192.168.5.224:3478").createIceServer());
iceServers.add(PeerConnection.IceServer.builder("turn:192.168.5.224:3478").setUsername("tt").setPassword("fht").createIceServer());
if (turnConfig != null) {
try {
JsonObject turn = new com.google.gson.Gson().fromJson(turnConfig, JsonObject.class);
if (turn.has("urls")) {
for (com.google.gson.JsonElement url : turn.getAsJsonArray("urls")) {
iceServers.add(PeerConnection.IceServer.builder(url.getAsString())
.setUsername(turn.get("username").getAsString())
.setPassword(turn.get("credential").getAsString())
.createIceServer());
}
}
} catch (RuntimeException e) {
Log.w(TAG, "Invalid TURN configuration");
}
}
PeerConnection.RTCConfiguration config = new PeerConnection.RTCConfiguration(iceServers);
config.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;
@@ -274,7 +287,7 @@ public class WebRtcClient {
PeerObserver peerObserver = new PeerObserver(new PeerObserver.PeerEventListener() {
@Override
public void onIceCandidate(IceCandidate candidate) {
sendIceCandidate(candidate, controllerId);
sendIceCandidate(candidate, controllerId, sessionId);
}
@Override
@@ -336,7 +349,7 @@ public class WebRtcClient {
@Override
public void onSetSuccess() {
Log.d(TAG, "onSetSuccess: modifiedSdp = " + modifiedSdpDescription);
sendAnswer(modifiedSdpDescription, controllerId);
sendAnswer(modifiedSdpDescription, controllerId, sessionId);
}
@Override
@@ -586,11 +599,12 @@ public class WebRtcClient {
return newSdp.toString();
}
private void sendAnswer(String sdp, String controllerId) {
private void sendAnswer(String sdp, String controllerId, String sessionId) {
SignalMessage msg = new SignalMessage();
msg.setType("ANSWER");
msg.setFromDeviceId(deviceId);
msg.setToDeviceId(controllerId);
msg.setSessionId(sessionId);
msg.setDeviceType("CONTROLLED");
JsonObject payload = new JsonObject();
@@ -600,11 +614,12 @@ public class WebRtcClient {
wsClient.sendMessage(msg);
}
private void sendIceCandidate(IceCandidate candidate, String controllerId) {
private void sendIceCandidate(IceCandidate candidate, String controllerId, String sessionId) {
SignalMessage msg = new SignalMessage();
msg.setType("ICE_CANDIDATE");
msg.setFromDeviceId(deviceId);
msg.setToDeviceId(controllerId);
msg.setSessionId(sessionId);
msg.setDeviceType("CONTROLLED");
JsonObject payload = new JsonObject();
+2
View File
@@ -54,4 +54,6 @@ message ControlMessage {
// 被控端支持的帧率档位列表(REPORT_RESOLUTION 上报,按屏幕刷新率筛选,升序)。
repeated int32 supported_fps = 16;
// 0 = 旧版本未上报,1 = 离散手势,2 = 实时触摸事件。
int32 touch_mode = 17;
}
@@ -91,16 +91,6 @@
app:linkageName="remote_assistance"
app:optionsText="@string/options_text_remote_assistance" />
<com.ttstd.dialer.view.SettingItem
android:id="@+id/si_auto_remote_assistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:configKey="@string/auto_accept_remote_assistance_key"
app:enabledBy="remote_assistance"
app:hintText="@string/hint_text_auto_accept_remote"
app:linkageName="@string/auto_accept_remote_assistance_key"
app:optionsText="@string/options_text_auto_accept_remote" />
</LinearLayout>
</ScrollView>
+2 -2
View File
@@ -43,8 +43,8 @@
<!--utils end-->
<!--assist start-->
<string name="options_text_remote_assistance">远程协助</string>
<string name="hint_text_remote_assistance">家人远程协助服务</string>
<string name="options_text_remote_assistance">家属远程协助</string>
<string name="hint_text_remote_assistance">开启后家属可申请协助,管理员可独立连接</string>
<string name="remote_assistance_key">remote_assistance_key</string>
<string name="options_text_auto_accept_remote">自动接受</string>