From 64c32f62b15a0dfe6d4c7ba022ccc4777527e86b Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Mon, 1 Aug 2022 08:34:00 -0700 Subject: [PATCH] Tag sockets in FlipperSocketImpl (#3928) Summary: Resolves https://github.com/facebook/flipper/issues/3926. Open to suggestions on the tag value, just sort of picked one that could be pointed to for anyone that wants to track these in their apps. ## Changelog Tag sockets used by Flipper to fix strict mode warnings about untagged sockets. Pull Request resolved: https://github.com/facebook/flipper/pull/3928 Test Plan: Edit FlipperSampleApplication.java with the following: ``` public class FlipperSampleApplication extends Application { Override public void onCreate() { StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build() ); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build() ); ... ``` That will enable strict mode. Now build/launch the sample application. Use adb logcat to inspect warnings: ``` adb logcat -s StrictMode ``` Without tagging the socket, the following warning should be observed: ``` D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setTrafficStatsTag() to track all network usage ``` After applying the fix, that warning should go away. Reviewed By: lblasa Differential Revision: D38280819 Pulled By: passy fbshipit-source-id: 0c841b13237cbcb0ff8b8226fb44655a74b775c6 --- .../android/DelegatingSocketFactory.java | 70 +++++++++++++++++++ .../flipper/android/FlipperSocketImpl.java | 11 ++- .../facebook/flipper/core/FlipperSocket.java | 3 + 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 android/src/main/java/com/facebook/flipper/android/DelegatingSocketFactory.java diff --git a/android/src/main/java/com/facebook/flipper/android/DelegatingSocketFactory.java b/android/src/main/java/com/facebook/flipper/android/DelegatingSocketFactory.java new file mode 100644 index 000000000..936bf5661 --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/android/DelegatingSocketFactory.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2014 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.facebook.flipper.android; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import javax.net.SocketFactory; + +/** + * A {@link SocketFactory} that delegates calls. Sockets can be configured after creation by + * overriding {@link #configureSocket}. + */ +class DelegatingSocketFactory extends SocketFactory { + private final SocketFactory delegate; + + DelegatingSocketFactory(SocketFactory delegate) { + this.delegate = delegate; + } + + @Override + public Socket createSocket() throws IOException { + Socket socket = delegate.createSocket(); + return configureSocket(socket); + } + + @Override + public Socket createSocket(String host, int port) throws IOException { + Socket socket = delegate.createSocket(host, port); + return configureSocket(socket); + } + + @Override + public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) + throws IOException { + Socket socket = delegate.createSocket(host, port, localAddress, localPort); + return configureSocket(socket); + } + + @Override + public Socket createSocket(InetAddress host, int port) throws IOException { + Socket socket = delegate.createSocket(host, port); + return configureSocket(socket); + } + + @Override + public Socket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort) + throws IOException { + Socket socket = delegate.createSocket(host, port, localAddress, localPort); + return configureSocket(socket); + } + + protected Socket configureSocket(Socket socket) { + // No-op by default. + return socket; + } +} diff --git a/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java b/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java index 586f0037f..2ff095d73 100644 --- a/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java +++ b/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java @@ -7,6 +7,7 @@ package com.facebook.flipper.android; +import android.net.TrafficStats; import android.util.Log; import com.facebook.flipper.BuildConfig; import com.facebook.flipper.core.FlipperObject; @@ -17,6 +18,7 @@ import com.facebook.soloader.SoLoader; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; +import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; import java.security.InvalidAlgorithmParameterException; @@ -100,7 +102,14 @@ class FlipperSocketImpl extends WebSocketClient implements FlipperSocket { SSLSocketFactory factory = sslContext.getSocketFactory(); - this.setSocketFactory(factory); + this.setSocketFactory( + new DelegatingSocketFactory(factory) { + @Override + protected Socket configureSocket(Socket socket) { + TrafficStats.setThreadStatsTag(SOCKET_TAG); + return socket; + } + }); } this.connect(); diff --git a/android/src/main/java/com/facebook/flipper/core/FlipperSocket.java b/android/src/main/java/com/facebook/flipper/core/FlipperSocket.java index bbad12fa1..10600cfa3 100644 --- a/android/src/main/java/com/facebook/flipper/core/FlipperSocket.java +++ b/android/src/main/java/com/facebook/flipper/core/FlipperSocket.java @@ -9,6 +9,9 @@ package com.facebook.flipper.core; public interface FlipperSocket { + /** The value used by Flipper to tag sockets, visible to {@link android.net.TrafficStats}. */ + int SOCKET_TAG = 0x000090000; + /** Connect to the endpoint. */ void flipperConnect();