├── .gitignore ├── LICENSE ├── README.md ├── assets ├── sample1.png ├── sample2.png ├── va_request.png └── va_response.png ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── netbare-core ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── megatronking │ │ └── netbare │ │ ├── NetBare.java │ │ ├── NetBareConfig.java │ │ ├── NetBareListener.java │ │ ├── NetBareLog.java │ │ ├── NetBareService.java │ │ ├── NetBareThread.java │ │ ├── NetBareUtils.java │ │ ├── NetBareVirtualGateway.java │ │ ├── NetBareXLog.java │ │ ├── gateway │ │ ├── AbstractRequestChain.java │ │ ├── AbstractResponseChain.java │ │ ├── DefaultVirtualGateway.java │ │ ├── DefaultVirtualGatewayFactory.java │ │ ├── IndexedInterceptor.java │ │ ├── Interceptor.java │ │ ├── InterceptorChain.java │ │ ├── InterceptorFactory.java │ │ ├── PendingIndexedInterceptor.java │ │ ├── Request.java │ │ ├── RequestChain.java │ │ ├── Response.java │ │ ├── ResponseChain.java │ │ ├── SSLCodecInterceptor.java │ │ ├── SSLRefluxInterceptor.java │ │ ├── SessionTunnelFlow.java │ │ ├── SpecVirtualGateway.java │ │ ├── TunnelFlow.java │ │ ├── VirtualGateway.java │ │ └── VirtualGatewayFactory.java │ │ ├── http │ │ ├── ContainerHttpInterceptor.java │ │ ├── Http2SniffInterceptor.java │ │ ├── HttpHeaderParseInterceptor.java │ │ ├── HttpHeaderSeparateInterceptor.java │ │ ├── HttpHeaderSniffInterceptor.java │ │ ├── HttpId.java │ │ ├── HttpIndexedInterceptor.java │ │ ├── HttpInterceptor.java │ │ ├── HttpInterceptorFactory.java │ │ ├── HttpInterceptorsFactory.java │ │ ├── HttpMethod.java │ │ ├── HttpMultiplexInterceptor.java │ │ ├── HttpPendingIndexedInterceptor.java │ │ ├── HttpProtocol.java │ │ ├── HttpRequest.java │ │ ├── HttpRequestChain.java │ │ ├── HttpResponse.java │ │ ├── HttpResponseChain.java │ │ ├── HttpSSLCodecInterceptor.java │ │ ├── HttpSSLRefluxInterceptor.java │ │ ├── HttpSSLRequestCodec.java │ │ ├── HttpSSLResponseCodec.java │ │ ├── HttpSession.java │ │ ├── HttpSessionFactory.java │ │ ├── HttpSniffInterceptor.java │ │ ├── HttpVirtualGateway.java │ │ ├── HttpVirtualGatewayFactory.java │ │ ├── HttpZygoteRequest.java │ │ └── HttpZygoteResponse.java │ │ ├── http2 │ │ ├── DecodeCallback.java │ │ ├── EncodeCallback.java │ │ ├── ErrorCode.java │ │ ├── FrameType.java │ │ ├── Hpack.java │ │ ├── Http2.java │ │ ├── Http2DecodeInterceptor.java │ │ ├── Http2EncodeInterceptor.java │ │ ├── Http2Settings.java │ │ ├── Http2Stream.java │ │ ├── Http2Updater.java │ │ └── Huffman.java │ │ ├── ip │ │ ├── Header.java │ │ ├── IcmpHeader.java │ │ ├── IpAddress.java │ │ ├── IpHeader.java │ │ ├── Protocol.java │ │ ├── TcpHeader.java │ │ └── UdpHeader.java │ │ ├── net │ │ ├── DumpCallback.java │ │ ├── Net.java │ │ ├── Session.java │ │ ├── SessionProvider.java │ │ ├── UidDumper.java │ │ └── UidProvider.java │ │ ├── proxy │ │ ├── BaseProxyServer.java │ │ ├── IcmpProxyServerForwarder.java │ │ ├── ProxyServer.java │ │ ├── ProxyServerForwarder.java │ │ ├── TcpProxyServer.java │ │ ├── TcpProxyServerForwarder.java │ │ ├── UdpProxyServer.java │ │ └── UdpProxyServerForwarder.java │ │ ├── ssl │ │ ├── CertificateGenerator.java │ │ ├── CertificateInstallActivity.java │ │ ├── JKS.java │ │ ├── SSLCodec.java │ │ ├── SSLEngineFactory.java │ │ ├── SSLKeyManagerProvider.java │ │ ├── SSLRefluxCallback.java │ │ ├── SSLRequestCodec.java │ │ ├── SSLResponseCodec.java │ │ ├── SSLTrustManagerProvider.java │ │ ├── SSLUtils.java │ │ └── SSLWhiteList.java │ │ ├── tcp │ │ └── TcpVirtualGateway.java │ │ ├── tunnel │ │ ├── ConnectionShutdownException.java │ │ ├── NioCallback.java │ │ ├── NioTunnel.java │ │ ├── TcpProxyTunnel.java │ │ ├── TcpRemoteTunnel.java │ │ ├── TcpTunnel.java │ │ ├── TcpVATunnel.java │ │ ├── Tunnel.java │ │ ├── UdpRemoteTunnel.java │ │ ├── UdpTunnel.java │ │ ├── UdpVATunnel.java │ │ └── VirtualGatewayTunnel.java │ │ ├── udp │ │ └── UdpVirtualGateway.java │ │ └── ws │ │ ├── WebSocketCallback.java │ │ ├── WebSocketProtocol.java │ │ └── WebSocketReader.java │ └── res │ └── values │ └── styles.xml ├── netbare-injector ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── github │ └── megatronking │ └── netbare │ ├── http │ ├── Cookie.java │ ├── HttpBody.java │ ├── HttpHeaderPart.java │ ├── HttpInjectInterceptor.java │ ├── HttpRawBody.java │ ├── HttpRequestHeaderPart.java │ ├── HttpRequestInjectorCallback.java │ ├── HttpResponseHeaderPart.java │ └── HttpResponseInjectorCallback.java │ ├── injector │ ├── BlockedHttpInjector.java │ ├── HttpInjector.java │ ├── InjectorCallback.java │ └── SimpleHttpInjector.java │ ├── io │ ├── ByteBufferInputStream.java │ └── HttpBodyInputStream.java │ ├── stream │ ├── BufferStream.java │ ├── ByteStream.java │ ├── Stream.java │ ├── StringStream.java │ └── TinyFileStream.java │ └── utils │ └── CaseInsensitiveLinkedMap.java ├── netbare-sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── megatronking │ │ └── netbare │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── kotlin │ │ └── com │ │ │ └── github │ │ │ └── megatronking │ │ │ └── netbare │ │ │ └── sample │ │ │ ├── App.kt │ │ │ ├── AppService.kt │ │ │ ├── BaiduLogoInjector.kt │ │ │ ├── HttpUrlPrintInterceptor.kt │ │ │ ├── MainActivity.kt │ │ │ └── WechatLocationInjector.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable-xhdpi │ │ └── netbare_notification.png │ │ ├── drawable-xxhdpi │ │ └── netbare_notification.png │ │ ├── drawable-xxxhdpi │ │ └── netbare_notification.png │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── raw │ │ └── baidu_inject_logo.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── github │ └── megatronking │ └── netbare │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | gradle.properties 9 | gradlew 10 | gradlew.bat 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2019 Megatron King 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /assets/sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/assets/sample1.png -------------------------------------------------------------------------------- /assets/sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/assets/sample2.png -------------------------------------------------------------------------------- /assets/va_request.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/assets/va_request.png -------------------------------------------------------------------------------- /assets/va_response.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/assets/va_response.png -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.11' 5 | 6 | repositories { 7 | google() 8 | jcenter() 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:3.3.0' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.11" 13 | 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | jcenter() 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Feb 04 12:49:28 CET 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /netbare-core/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /netbare-core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | minSdkVersion 21 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | } 12 | 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | 25 | implementation 'com.android.support:appcompat-v7:28.0.0' 26 | 27 | implementation 'org.bouncycastle:bcpkix-jdk15on:1.56' 28 | implementation 'org.bouncycastle:bcprov-jdk15on:1.56' 29 | implementation 'com.google.guava:guava:19.0' 30 | } 31 | -------------------------------------------------------------------------------- /netbare-core/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/NetBareListener.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare; 17 | 18 | /** 19 | * Interface definition for a callback to be invoked when the NetBare service state changes. 20 | * 21 | * @author Megatron King 22 | * @since 2018-10-11 19:44 23 | */ 24 | public interface NetBareListener { 25 | 26 | /** 27 | * Callback method to be invoked when the NetBare service is started. It usual is called after 28 | * {@link NetBare#start(NetBareConfig)}. 29 | */ 30 | void onServiceStarted(); 31 | 32 | /** 33 | * Callback method to be invoked when the NetBare service is stopped. It usual is called after 34 | * {@link NetBare#stop()} or another VPN service is established. 35 | */ 36 | void onServiceStopped(); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/AbstractRequestChain.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * This is a specific chain for all the requests. 22 | * 23 | * @author Megatron King 24 | * @since 2019/4/6 19:45 25 | */ 26 | public abstract class AbstractRequestChain 27 | extends InterceptorChain { 28 | 29 | /** 30 | * Get the current request instance in this chain. 31 | * 32 | * @return An instance of {@link Request}. 33 | */ 34 | public abstract Req request(); 35 | 36 | /** 37 | * Constructs an intercept chain for request. 38 | * 39 | * @param request A {@link Request} implementation. 40 | * @param interceptors A collection of interceptors. 41 | */ 42 | protected AbstractRequestChain(Req request, List interceptors) { 43 | super(request, interceptors); 44 | } 45 | 46 | /** 47 | * Constructs an intercept chain for request. 48 | * 49 | * @param request A {@link Request} implementation. 50 | * @param interceptors A collection of interceptors. 51 | * @param index The head index. 52 | * @param tag The chain's tag. 53 | */ 54 | protected AbstractRequestChain(Req request, List interceptors, int index, Object tag) { 55 | super(request, interceptors, index, tag); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/AbstractResponseChain.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import java.util.List; 19 | 20 | /** 21 | * This is a specific chain for all the responses. 22 | * 23 | * @author Megatron King 24 | * @since 2019/4/6 19:51 25 | */ 26 | public abstract class AbstractResponseChain 27 | extends InterceptorChain { 28 | 29 | /** 30 | * Get the current response instance in this chain. 31 | * 32 | * @return An instance of {@link Response}. 33 | */ 34 | public abstract Res response(); 35 | 36 | /** 37 | * Constructs an intercept chain for response. 38 | * 39 | * @param response A {@link Response} implementation. 40 | * @param interceptors A collection of interceptors. 41 | */ 42 | protected AbstractResponseChain(Res response, List interceptors) { 43 | super(response, interceptors); 44 | } 45 | 46 | /** 47 | * Constructs an intercept chain for response. 48 | * 49 | * @param response A {@link Response} implementation. 50 | * @param interceptors A collection of interceptors. 51 | * @param index The head index. 52 | * @param tag The chain's tag. 53 | */ 54 | protected AbstractResponseChain(Res response, List interceptors, int index, Object tag) { 55 | super(response, interceptors, index, tag); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/DefaultVirtualGateway.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import com.github.megatronking.netbare.net.Session; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * A {@link VirtualGateway} provides the interception service. Interceptors are organized as a list 27 | * in chain, can observe and modify packets. Use {@link DefaultVirtualGatewayFactory} to create an 28 | * instance. 29 | * 30 | * @author Megatron King 31 | * @since 2018-11-01 23:35 32 | */ 33 | /* package */ class DefaultVirtualGateway extends VirtualGateway { 34 | 35 | private final List> mInterceptors; 36 | 37 | /* package */ DefaultVirtualGateway(Session session, Request request, Response response, 38 | List> factories) { 39 | super(session, request, response); 40 | this.mInterceptors = new ArrayList<>(factories.size()); 41 | for (InterceptorFactory factory : factories) { 42 | mInterceptors.add(factory.create()); 43 | } 44 | } 45 | 46 | @Override 47 | public void onRequest(ByteBuffer buffer) throws IOException { 48 | new RequestChain(mRequest, mInterceptors).process(buffer); 49 | } 50 | 51 | @Override 52 | public void onResponse(ByteBuffer buffer) throws IOException { 53 | new ResponseChain(mResponse, mInterceptors).process(buffer); 54 | } 55 | 56 | @Override 57 | public void onRequestFinished() { 58 | for (Interceptor interceptor: mInterceptors) { 59 | interceptor.onRequestFinished(mRequest); 60 | } 61 | } 62 | 63 | @Override 64 | public void onResponseFinished() { 65 | for (Interceptor interceptor: mInterceptors) { 66 | interceptor.onResponseFinished(mResponse); 67 | } 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/DefaultVirtualGatewayFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.net.Session; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * A {@link VirtualGatewayFactory} that produces the {@link DefaultVirtualGateway}. 27 | * 28 | * @author Megatron King 29 | * @since 2018-11-01 23:29 30 | */ 31 | public final class DefaultVirtualGatewayFactory implements VirtualGatewayFactory { 32 | 33 | private List> mFactories; 34 | 35 | private DefaultVirtualGatewayFactory(@NonNull List> factories) { 37 | this.mFactories = factories; 38 | } 39 | 40 | @Override 41 | public VirtualGateway create(Session session, Request request, Response response) { 42 | return new DefaultVirtualGateway(session, request, response, new ArrayList<>(mFactories)); 43 | } 44 | 45 | /** 46 | * Create a {@link VirtualGatewayFactory} instance with a collection of 47 | * {@link InterceptorFactory}. 48 | * 49 | * @param factories a collection of {@link InterceptorFactory}. 50 | * @return A instance of {@link DefaultVirtualGatewayFactory}. 51 | */ 52 | public static VirtualGatewayFactory create(@NonNull List> factories) { 54 | return new DefaultVirtualGatewayFactory(factories); 55 | } 56 | 57 | /** 58 | * Create a {@link VirtualGatewayFactory} instance that not contains {@link Interceptor}. 59 | * 60 | * @return A instance of {@link VirtualGatewayFactory}. 61 | */ 62 | public static VirtualGatewayFactory create() { 63 | return create(new ArrayList>()); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/IndexedInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * Add the index parameter in the {@link #intercept(ReqChain, ByteBuffer)} and 25 | * {@link #intercept(ResChain, ByteBuffer)}, it indicates the packet index in the session. 26 | *

27 | * The index will be reset when the session finished. 28 | *

29 | * 30 | * @author Megatron King 31 | * @since 2018-12-03 21:00 32 | */ 33 | public abstract class IndexedInterceptor, 34 | Res extends Response, ResChain extends AbstractResponseChain> 35 | implements Interceptor { 36 | 37 | private int mRequestIndex; 38 | private int mResponseIndex; 39 | 40 | /** 41 | * The same like {@link #intercept(ReqChain, ByteBuffer)}. 42 | * 43 | * @param chain The request chain, call {@linkplain ReqChain#process(ByteBuffer)} to 44 | * delivery the packet. 45 | * @param buffer A nio buffer contains the packet data. 46 | * @param index The packet index, started from 0. 47 | * @throws IOException If an I/O error has occurred. 48 | */ 49 | protected abstract void intercept(@NonNull ReqChain chain, @NonNull ByteBuffer buffer, 50 | int index) throws IOException; 51 | 52 | /** 53 | * The same like {@link #intercept(ResChain, ByteBuffer)}. 54 | * 55 | * @param chain The response chain, call {@linkplain ResChain#process(ByteBuffer)} to 56 | * delivery the packet. 57 | * @param buffer A nio buffer contains the packet data. 58 | * @param index The packet index, started from 0. 59 | * @throws IOException If an I/O error has occurred. 60 | */ 61 | protected abstract void intercept(@NonNull ResChain chain, @NonNull ByteBuffer buffer, 62 | int index) throws IOException; 63 | 64 | @Override 65 | public final void intercept(@NonNull ReqChain chain, @NonNull ByteBuffer buffer) 66 | throws IOException { 67 | intercept(chain, buffer, mRequestIndex++); 68 | } 69 | 70 | @Override 71 | public final void intercept(@NonNull ResChain chain, @NonNull ByteBuffer buffer) 72 | throws IOException { 73 | intercept(chain, buffer, mResponseIndex++); 74 | } 75 | 76 | @Override 77 | public void onRequestFinished(@NonNull Req request) { 78 | mRequestIndex = 0; 79 | } 80 | 81 | @Override 82 | public void onResponseFinished(@NonNull Res response) { 83 | mResponseIndex = 0; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/InterceptorFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | /** 21 | * Factory used by developer to create their own interceptor for virtual gateway. 22 | * 23 | * @author Megatron King 24 | * @since 2018-11-02 23:46 25 | */ 26 | public interface InterceptorFactory, 27 | Res extends Response, ResChain extends AbstractResponseChain> { 28 | 29 | /** 30 | * Creates an interceptor instance and immediately returns it, it must not be null. 31 | * 32 | * @return A newly created interceptor. 33 | */ 34 | @NonNull 35 | Interceptor create(); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/Request.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import com.github.megatronking.netbare.tunnel.Tunnel; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * A client requester, it connects to the remote server tunnel directly. We can send packet to the 25 | * remote server using {@link #process(ByteBuffer)}. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-05 22:18 29 | */ 30 | public class Request extends SessionTunnelFlow { 31 | 32 | private Tunnel mTunnel; 33 | 34 | public Request() { 35 | } 36 | 37 | public Request(Tunnel tunnel) { 38 | this.mTunnel = tunnel; 39 | } 40 | 41 | @Override 42 | public void process(ByteBuffer buffer) throws IOException { 43 | if (mTunnel != null) { 44 | mTunnel.write(buffer); 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/RequestChain.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | import java.util.List; 23 | 24 | /** 25 | * A request chain, responsible for intercepting request packets. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-14 23:18 29 | */ 30 | public class RequestChain extends AbstractRequestChain> { 32 | 33 | private Request mRequest; 34 | 35 | public RequestChain(Request request, List> interceptors) { 37 | super(request, interceptors); 38 | mRequest = request; 39 | } 40 | 41 | private RequestChain(Request request, List> interceptors, int index, Object tag) { 43 | super(request, interceptors, index, tag); 44 | mRequest = request; 45 | } 46 | 47 | @Override 48 | protected void processNext(ByteBuffer buffer, Request request, List> interceptors, 50 | int index, Object tag) throws IOException { 51 | Interceptor interceptor = interceptors.get(index); 52 | if (interceptor != null) { 53 | interceptor.intercept(new RequestChain(request, interceptors, ++index, tag), buffer); 54 | } 55 | } 56 | 57 | @Override 58 | @NonNull 59 | public Request request() { 60 | return mRequest; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/Response.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import com.github.megatronking.netbare.tunnel.Tunnel; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * A server response, it connects to VPN file descriptor. We can send packet to the client using 25 | * {@link #process(ByteBuffer)}. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-05 22:24 29 | */ 30 | public class Response extends SessionTunnelFlow { 31 | 32 | private Tunnel mTunnel; 33 | 34 | public Response() { 35 | } 36 | 37 | public Response(Tunnel tunnel) { 38 | this.mTunnel = tunnel; 39 | } 40 | 41 | @Override 42 | public void process(ByteBuffer buffer) throws IOException { 43 | if (mTunnel != null) { 44 | mTunnel.write(buffer); 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/ResponseChain.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | import java.util.List; 23 | 24 | /** 25 | * A response chain, responsible for intercepting response packets. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-14 23:19 29 | */ 30 | public class ResponseChain extends AbstractResponseChain> { 32 | 33 | private Response mResponse; 34 | 35 | public ResponseChain(Response response, List> interceptors) { 37 | super(response, interceptors); 38 | mResponse = response; 39 | } 40 | 41 | private ResponseChain(Response response, List> interceptors, int index, Object tag) { 43 | super(response, interceptors, index, tag); 44 | mResponse = response; 45 | } 46 | 47 | @Override 48 | protected void processNext(ByteBuffer buffer, Response response, List> interceptors, 50 | int index, Object tag) throws IOException { 51 | Interceptor interceptor = interceptors.get(index); 52 | if (interceptor != null) { 53 | interceptor.intercept(new ResponseChain(response, interceptors, ++index, tag), buffer); 54 | } 55 | } 56 | 57 | @Override 58 | @NonNull 59 | public Response response() { 60 | return mResponse; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/SSLRefluxInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.ssl.SSLRefluxCallback; 21 | 22 | import java.io.IOException; 23 | import java.nio.ByteBuffer; 24 | 25 | /** 26 | * An interceptor locates at the last layer of the interceptors. It is responsible for send 27 | * plaintext packets to {@link SSLCodecInterceptor}. 28 | * 29 | * @author Megatron King 30 | * @since 2018-11-15 15:39 31 | */ 32 | public abstract class SSLRefluxInterceptor, 33 | Res extends Response, ResChain extends AbstractResponseChain> 34 | implements Interceptor { 35 | 36 | private SSLRefluxCallback mRefluxCallback; 37 | 38 | /** 39 | * Should reflux the request buffer to SSL codec if the buffer is origin decrypted. 40 | * 41 | * @param chain The request chain. 42 | * @return True if needs to encrypt again. 43 | */ 44 | protected abstract boolean shouldReflux(ReqChain chain); 45 | 46 | /** 47 | * Should reflux the response buffer to SSL codec if the buffer is origin decrypted. 48 | * 49 | * @param chain The response chain. 50 | * @return True if needs to encrypt again. 51 | */ 52 | protected abstract boolean shouldReflux(ResChain chain); 53 | 54 | public SSLRefluxInterceptor(SSLRefluxCallback refluxCallback) { 55 | this.mRefluxCallback = refluxCallback; 56 | } 57 | 58 | @Override 59 | public void intercept(@NonNull ReqChain chain, @NonNull ByteBuffer buffer) 60 | throws IOException { 61 | if (shouldReflux(chain)) { 62 | mRefluxCallback.onRequest(chain.request(), buffer); 63 | } else { 64 | chain.process(buffer); 65 | } 66 | } 67 | 68 | @Override 69 | public void intercept(@NonNull ResChain chain, @NonNull ByteBuffer buffer) 70 | throws IOException { 71 | if (shouldReflux(chain)) { 72 | mRefluxCallback.onResponse(chain.response(), buffer); 73 | } else { 74 | chain.process(buffer); 75 | } 76 | } 77 | 78 | @Override 79 | public void onRequestFinished(@NonNull Req request) { 80 | } 81 | 82 | @Override 83 | public void onResponseFinished(@NonNull Res response) { 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/SessionTunnelFlow.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import com.github.megatronking.netbare.NetBareConfig; 19 | import com.github.megatronking.netbare.NetBareUtils; 20 | import com.github.megatronking.netbare.ip.Protocol; 21 | import com.github.megatronking.netbare.net.Session; 22 | 23 | /** 24 | * A tunnel flow contains the session information. 25 | * 26 | * @author Megatron King 27 | * @since 2018-11-05 21:43 28 | */ 29 | public abstract class SessionTunnelFlow implements TunnelFlow { 30 | 31 | private Session mSession; 32 | 33 | /* package */ void setSession(Session session) { 34 | mSession = session; 35 | } 36 | 37 | /** 38 | * Returns the session's unique id. 39 | * 40 | * @return The session id. 41 | */ 42 | public String id() { 43 | return mSession.id; 44 | } 45 | 46 | /** 47 | * Returns the session created time, you can think of it as the start time of the request. 48 | * 49 | * @return Session created time. 50 | */ 51 | public long time() { 52 | return mSession.time; 53 | } 54 | 55 | /** 56 | * Returns the identifier of this session's process uid. This value is not guaranteed, it is up 57 | * to {@link NetBareConfig#dumpUid}. And if dumps the uid failed, it will return 0. 58 | * 59 | * @return The session's process uid. 60 | */ 61 | public int uid() { 62 | return mSession.uid; 63 | } 64 | 65 | /** 66 | * Returns the remote server's IPV4 address. 67 | * 68 | * @return The remote server's IPV4 address. 69 | */ 70 | public String ip() { 71 | return NetBareUtils.convertIp(mSession.remoteIp); 72 | } 73 | 74 | /** 75 | * Returns the remote server's host name. 76 | * 77 | * @return The remote server's host name. 78 | */ 79 | public String host() { 80 | return mSession.host; 81 | } 82 | 83 | /** 84 | * Returns the remote server's port. 85 | * 86 | * @return The remote server's port. 87 | */ 88 | public int port() { 89 | return NetBareUtils.convertPort(mSession.remotePort); 90 | } 91 | 92 | /** 93 | * Returns the IP protocol. 94 | * 95 | * @return IP protocol. 96 | */ 97 | public Protocol protocol() { 98 | return mSession.protocol; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/TunnelFlow.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * A tunnel flow interface, the implement class should bind a tunnel. 23 | * 24 | * @author Megatron King 25 | * @since 2018-11-05 20:20 26 | */ 27 | /* package */ interface TunnelFlow { 28 | 29 | /** 30 | * Send a packet to remote tunnel, and the tunnel will send it to the terminal. 31 | * 32 | * @param buffer A net packet buffer. 33 | * @throws IOException If an I/O error has occurred. 34 | */ 35 | void process(ByteBuffer buffer) throws IOException; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/VirtualGateway.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import com.github.megatronking.netbare.net.Session; 19 | 20 | import java.io.IOException; 21 | import java.nio.ByteBuffer; 22 | 23 | /** 24 | * Virtual Gateway is a virtual net packets interception distributor, all packets will flow through 25 | * it. We can define our own virtual gateway to decode and encode the packets. The Virtual 26 | * Gateway wraps a request tunnel {@link Request} and a response tunnel {@link Response}, these 27 | * tunnels are responsible for communicating with the terminal(client and server). 28 | * 29 | * @author Megatron King 30 | * @since 2018-11-01 23:48 31 | */ 32 | public class VirtualGateway { 33 | 34 | /** 35 | * The request tunnel connects to the server terminal. We can call 36 | * {@link Request#process(ByteBuffer)} to send data. 37 | */ 38 | protected Request mRequest; 39 | 40 | /** 41 | * The response tunnel connects to the client terminal. We can call 42 | * {@link Response#process(ByteBuffer)} to send data. 43 | */ 44 | protected Response mResponse; 45 | 46 | /** 47 | * Constructs a VirtualGateway object with the net session, request tunnel and response tunnel. 48 | * 49 | * @param session The net session contains basic net information such as IPs and ports. 50 | * @param request The request tunnel connects to the server terminal. 51 | * @param response The response tunnel connects to the client terminal. 52 | */ 53 | public VirtualGateway(Session session, Request request, Response response) { 54 | request.setSession(session); 55 | response.setSession(session); 56 | this.mRequest = request; 57 | this.mResponse = response; 58 | } 59 | 60 | /** 61 | * Send a packet to server terminal through the request tunnel. 62 | * 63 | * @param buffer A byte buffer contains the net packet data. 64 | * @throws IOException If an I/O error has occurred. 65 | */ 66 | public void onRequest(ByteBuffer buffer) throws IOException { 67 | mRequest.process(buffer); 68 | } 69 | 70 | /** 71 | * Send a packet to client terminal through the response tunnel. 72 | * 73 | * @param buffer A byte buffer contains the net packet data. 74 | * @throws IOException If an I/O error has occurred. 75 | */ 76 | public void onResponse(ByteBuffer buffer) throws IOException { 77 | mResponse.process(buffer); 78 | } 79 | 80 | /** 81 | * Notify virtual gateway that no longer has data sent to the server. 82 | */ 83 | public void onRequestFinished() { 84 | } 85 | 86 | /** 87 | * Notify virtual gateway that no longer has data sent to the client. 88 | */ 89 | public void onResponseFinished() { 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/gateway/VirtualGatewayFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.gateway; 17 | 18 | import com.github.megatronking.netbare.net.Session; 19 | 20 | /** 21 | * A factory that produces the {@link VirtualGateway}. 22 | * 23 | * @author Megatron King 24 | * @since 2018-11-01 23:23 25 | */ 26 | public interface VirtualGatewayFactory { 27 | 28 | /** 29 | * Returns a new {@link VirtualGateway} for the given arguments. 30 | * 31 | * @param session A network session. 32 | * @param request A request connects to the remote server tunnel. 33 | * @param response A response connects to VPN file descriptor 34 | */ 35 | VirtualGateway create(Session session, Request request, Response response); 36 | 37 | } 38 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpId.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import java.util.UUID; 19 | 20 | /** 21 | * Regenerated http unique id for multi-sessions in one connection. 22 | * 23 | * @author Megatron King 24 | * @since 2018-12-19 16:35 25 | */ 26 | public class HttpId { 27 | 28 | public String id; 29 | public long time; 30 | public int streamId; 31 | 32 | public HttpId() { 33 | this(-1); 34 | } 35 | 36 | public HttpId(int streamId) { 37 | this.id = UUID.randomUUID().toString(); 38 | this.time = System.currentTimeMillis(); 39 | this.streamId = streamId; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpIndexedInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.gateway.IndexedInterceptor; 19 | 20 | /** 21 | * Indicates the packet index in the http session. 22 | * 23 | * @author Megatron King 24 | * @since 2018-12-03 21:00 25 | */ 26 | public abstract class HttpIndexedInterceptor extends IndexedInterceptor implements HttpInterceptor { 28 | } 29 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.gateway.Interceptor; 19 | 20 | /** 21 | * A specific interceptor designed for {@link HttpVirtualGateway}, it focuses on the http protocol 22 | * packets. The interceptor is an implement of {@link Interceptor}, methods are thread-safety and 23 | * runs in local proxy server threads. 24 | * 25 | *

26 | * Use {@link HttpInterceptorFactory} to create an http interceptor instance. 27 | *

28 | * 29 | * @author Megatron King 30 | * @since 2018-11-15 19:40 31 | */ 32 | public interface HttpInterceptor extends Interceptor { 34 | } 35 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpInterceptorFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.gateway.InterceptorFactory; 21 | 22 | /** 23 | * Factory used by developer to create their own interceptor for {@link HttpVirtualGateway}. 24 | * 25 | * @author Megatron King 26 | * @since 2018-11-15 21:58 27 | */ 28 | public interface HttpInterceptorFactory extends InterceptorFactory { 29 | 30 | /** 31 | * Creates a http interceptor instance and immediately returns it, it must not be null. 32 | * 33 | * @return A newly created http interceptor. 34 | */ 35 | @NonNull 36 | @Override 37 | HttpInterceptor create(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpInterceptorsFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * Factory creates a collection {@link HttpInterceptor}s. 24 | * 25 | * @author Megatron King 26 | * @since 2018-11-15 21:58 27 | */ 28 | /* package */ interface HttpInterceptorsFactory { 29 | 30 | /** 31 | * Creates a collection of http interceptor instances and immediately returns it, 32 | * it must not be null. 33 | * 34 | * @return A http interceptor list. 35 | */ 36 | @NonNull 37 | List create(); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpMethod.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | /** 21 | * HTTP defines a set of request methods to indicate the desired action to be performed for a given 22 | * resource. 23 | * 24 | * See https://tools.ietf.org/html/rfc7231#section-4 25 | * 26 | * @author Megatron King 27 | * @since 2018-10-15 19:59 28 | */ 29 | public enum HttpMethod { 30 | 31 | /** 32 | * It means NetBare does not know the method. 33 | */ 34 | UNKNOWN, 35 | 36 | /** 37 | * The GET method requests a representation of the specified resource. Requests using GET 38 | * should only retrieve data. 39 | */ 40 | GET, 41 | 42 | /** 43 | * The HEAD method asks for a response identical to that of a GET request, but without the 44 | * response body. 45 | */ 46 | HEAD, 47 | 48 | /** 49 | * The POST method is used to submit an entity to the specified resource, often causing 50 | * a change in state or side effects on the server. 51 | */ 52 | POST, 53 | 54 | /** 55 | * The PUT method replaces all current representations of the target resource with the request 56 | * payload. 57 | */ 58 | PUT, 59 | 60 | /** 61 | * The DELETE method deletes the specified resource. 62 | */ 63 | DELETE, 64 | 65 | /** 66 | * The CONNECT method establishes a tunnel to the server identified by the target resource. 67 | */ 68 | CONNECT, 69 | 70 | /** 71 | * The OPTIONS method is used to describe the communication options for the target resource. 72 | */ 73 | OPTIONS, 74 | 75 | /** 76 | * The TRACE method performs a message loop-back test along the path to the target resource. 77 | */ 78 | TRACE, 79 | 80 | /** 81 | * The PATCH method is used to apply partial modifications to a resource. 82 | */ 83 | PATCH; 84 | 85 | /** 86 | * Returns the request method enum. 87 | * 88 | * @param methodValue A string method presents in request line. 89 | * @return A HttpMethod enum. 90 | */ 91 | @NonNull 92 | public static HttpMethod parse(@NonNull String methodValue) { 93 | for (HttpMethod method : values()) { 94 | if (method.name().equals(methodValue)) { 95 | return method; 96 | } 97 | } 98 | return UNKNOWN; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpMultiplexInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.NetBareXLog; 21 | import com.github.megatronking.netbare.ip.Protocol; 22 | 23 | import java.io.IOException; 24 | import java.nio.ByteBuffer; 25 | 26 | /** 27 | * If a HTTP connection is keep-alive, there will be multiple sessions go through the same virtual 28 | * gateway. Those sessions are saw as one and not distinguished, this will increase the difficulty 29 | * of interception. We use this interceptor to separate them into independent sessions and 30 | * intercept them one by one. 31 | * 32 | * @author Megatron King 33 | * @since 2018-12-15 15:17 34 | */ 35 | /* package */ class HttpMultiplexInterceptor extends HttpIndexedInterceptor { 36 | 37 | private final HttpZygoteRequest mZygoteRequest; 38 | private final HttpZygoteResponse mZygoteResponse; 39 | 40 | private int mResponseIndex; 41 | private NetBareXLog mLog; 42 | 43 | private boolean mWebSocket; 44 | 45 | /* package */ HttpMultiplexInterceptor(HttpZygoteRequest zygoteRequest, 46 | HttpZygoteResponse zygoteResponse) { 47 | this.mZygoteRequest = zygoteRequest; 48 | this.mZygoteResponse = zygoteResponse; 49 | } 50 | 51 | @Override 52 | protected void intercept(@NonNull HttpRequestChain chain, @NonNull ByteBuffer buffer, 53 | int index) throws IOException { 54 | if (chain.request().httpProtocol() != HttpProtocol.HTTP_1_1) { 55 | chain.process(buffer); 56 | return; 57 | } 58 | // Check the protocol is web socket 59 | if (!mWebSocket) { 60 | mWebSocket = mZygoteResponse.isWebSocket(); 61 | } 62 | if (mResponseIndex > 0 && !mWebSocket) { 63 | if (mLog == null) { 64 | mLog = new NetBareXLog(Protocol.TCP, chain.request().ip(), chain.request().port()); 65 | } 66 | mResponseIndex = 0; 67 | mLog.w("Multiplex is found in one connection."); 68 | // Multiplex sessions. 69 | HttpId newId = new HttpId(); 70 | mZygoteRequest.zygote(newId); 71 | mZygoteResponse.zygote(newId); 72 | } 73 | chain.process(buffer); 74 | } 75 | 76 | @Override 77 | protected void intercept(@NonNull HttpResponseChain chain, @NonNull ByteBuffer buffer, 78 | int index) throws IOException { 79 | mResponseIndex++; 80 | chain.process(buffer); 81 | } 82 | 83 | @Override 84 | public void onResponseFinished(@NonNull HttpResponse response) { 85 | mResponseIndex = 0; 86 | super.onResponseFinished(response); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpPendingIndexedInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.gateway.PendingIndexedInterceptor; 19 | 20 | /** 21 | * An abstract interceptor provides multi-apis for packet pending. The packet will be stored in a 22 | * queue, and you can merge them with another packet. 23 | * 24 | * @author Megatron King 25 | * @since 2018-12-09 12:07 26 | */ 27 | public abstract class HttpPendingIndexedInterceptor extends PendingIndexedInterceptor implements HttpInterceptor { 29 | } 30 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpRequestChain.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.gateway.AbstractRequestChain; 21 | 22 | import java.io.IOException; 23 | import java.nio.ByteBuffer; 24 | import java.util.List; 25 | 26 | /** 27 | * Http request chain, responsible for intercepting http request packets. 28 | * 29 | * @author Megatron King 30 | * @since 2018-11-16 23:21 31 | */ 32 | public class HttpRequestChain extends AbstractRequestChain { 33 | 34 | private HttpZygoteRequest mZygoteRequest; 35 | 36 | /* package */ HttpRequestChain(HttpZygoteRequest request, List interceptors) { 37 | this(request, interceptors, 0, null); 38 | } 39 | 40 | /* package */ HttpRequestChain(HttpZygoteRequest request, List interceptors, 41 | int index, Object tag) { 42 | super(request, interceptors, index, tag); 43 | this.mZygoteRequest = request; 44 | } 45 | 46 | HttpZygoteRequest zygoteRequest() { 47 | return mZygoteRequest; 48 | } 49 | 50 | @Override 51 | protected void processNext(ByteBuffer buffer, HttpRequest request, 52 | List interceptors, int index, Object tag) throws IOException { 53 | HttpInterceptor interceptor = interceptors.get(index); 54 | if (interceptor != null) { 55 | interceptor.intercept(new HttpRequestChain(mZygoteRequest, interceptors, ++index, tag), buffer); 56 | } 57 | } 58 | 59 | @Override 60 | @NonNull 61 | public HttpRequest request() { 62 | HttpRequest active = mZygoteRequest.getActive(); 63 | return active != null ? active : mZygoteRequest; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpResponseChain.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.gateway.AbstractResponseChain; 21 | 22 | import java.io.IOException; 23 | import java.nio.ByteBuffer; 24 | import java.util.List; 25 | 26 | /** 27 | * Http response chain, responsible for intercepting http response packets. 28 | * 29 | * @author Megatron King 30 | * @since 2018-11-16 23:21 31 | */ 32 | public class HttpResponseChain extends AbstractResponseChain { 33 | 34 | private HttpZygoteResponse mZygoteResponse; 35 | 36 | /* package */ HttpResponseChain(HttpZygoteResponse response, List interceptors) { 37 | this(response, interceptors, 0, null); 38 | } 39 | 40 | /* package */ HttpResponseChain(HttpZygoteResponse response, List interceptors, 41 | int index, Object tag) { 42 | super(response, interceptors, index, tag); 43 | this.mZygoteResponse = response; 44 | } 45 | 46 | HttpZygoteResponse zygoteResponse() { 47 | return mZygoteResponse; 48 | } 49 | 50 | @Override 51 | protected void processNext(ByteBuffer buffer, HttpResponse response, 52 | List interceptors, int index, Object tag) throws IOException { 53 | HttpInterceptor interceptor = interceptors.get(index); 54 | if (interceptor != null) { 55 | interceptor.intercept(new HttpResponseChain(mZygoteResponse, interceptors, ++index, tag), buffer); 56 | } 57 | } 58 | 59 | @Override 60 | @NonNull 61 | public HttpResponse response() { 62 | HttpResponse active = mZygoteResponse.getActive(); 63 | return active != null ? active : mZygoteResponse; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSSLRefluxInterceptor.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.gateway.SSLRefluxInterceptor; 19 | import com.github.megatronking.netbare.ssl.SSLRefluxCallback; 20 | 21 | /** 22 | * An interceptor locates at the last layer of the interceptors. It is responsible for send 23 | * plaintext packets to {@link HttpSSLCodecInterceptor}. 24 | * 25 | * @author Megatron King 26 | * @since 2018-11-15 15:39 27 | */ 28 | /* package */ class HttpSSLRefluxInterceptor extends 29 | SSLRefluxInterceptor 30 | implements HttpInterceptor { 31 | 32 | /* package */ HttpSSLRefluxInterceptor(SSLRefluxCallback refluxCallback) { 33 | super(refluxCallback); 34 | } 35 | 36 | @Override 37 | protected boolean shouldReflux(HttpRequestChain chain) { 38 | return chain.request().isHttps(); 39 | } 40 | 41 | @Override 42 | protected boolean shouldReflux(HttpResponseChain chain) { 43 | return chain.response().isHttps(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSession.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.http2.Http2Settings; 19 | 20 | import java.util.LinkedHashMap; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | /** 25 | * Provides HTTP protocol session information. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-10 11:56 29 | */ 30 | /* package */ class HttpSession { 31 | 32 | boolean isHttps; 33 | HttpProtocol protocol; 34 | HttpMethod method; 35 | String path; 36 | Map> requestHeaders = new LinkedHashMap<>(); 37 | Map> responseHeaders = new LinkedHashMap<>(); 38 | int code; 39 | String message; 40 | int reqBodyOffset; 41 | int resBodyOffset; 42 | // Belows is for HTTP2 43 | Http2Settings clientHttp2Settings; 44 | Http2Settings peerHttp2Settings; 45 | boolean requestStreamEnd; 46 | boolean responseStreamEnd; 47 | 48 | } 49 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpSessionFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | /** 22 | * A factory creates {@link HttpSession} instance by id. 23 | * 24 | * @author Megatron King 25 | * @since 2019/1/6 19:42 26 | */ 27 | /* package */ class HttpSessionFactory { 28 | 29 | private final Map mHttpSession; 30 | 31 | /* package */ HttpSessionFactory() { 32 | mHttpSession = new HashMap<>(1); 33 | } 34 | 35 | HttpSession create(String id) { 36 | HttpSession httpSession; 37 | if (mHttpSession.containsKey(id)) { 38 | httpSession = mHttpSession.get(id); 39 | } else { 40 | httpSession = new HttpSession(); 41 | mHttpSession.put(id, httpSession); 42 | } 43 | return httpSession; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpVirtualGatewayFactory.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.gateway.VirtualGateway; 21 | import com.github.megatronking.netbare.gateway.VirtualGatewayFactory; 22 | import com.github.megatronking.netbare.gateway.Request; 23 | import com.github.megatronking.netbare.gateway.Response; 24 | import com.github.megatronking.netbare.net.Session; 25 | import com.github.megatronking.netbare.ssl.JKS; 26 | 27 | import java.util.ArrayList; 28 | import java.util.List; 29 | 30 | /** 31 | * A {@link VirtualGatewayFactory} that produces the {@link HttpVirtualGateway}. 32 | * 33 | * @author Megatron King 34 | * @since 2018-11-20 23:50 35 | */ 36 | public class HttpVirtualGatewayFactory implements VirtualGatewayFactory { 37 | 38 | private List mFactories; 39 | private JKS mJKS; 40 | 41 | /** 42 | * Constructs a {@link HttpVirtualGatewayFactory} instance with {@link JKS} and a collection of 43 | * {@link HttpInterceptorFactory}. 44 | * 45 | * @param factories a collection of {@link HttpInterceptorFactory}. 46 | * @return A instance of {@link HttpVirtualGatewayFactory}. 47 | */ 48 | public HttpVirtualGatewayFactory(@NonNull JKS jks, 49 | @NonNull List factories) { 50 | this.mJKS = jks; 51 | this.mFactories = factories; 52 | } 53 | 54 | @Override 55 | public VirtualGateway create(Session session, Request request, Response response) { 56 | return new HttpVirtualGateway(session, request, response, mJKS, new ArrayList<>(mFactories)); 57 | } 58 | 59 | /** 60 | * Create a {@link HttpVirtualGatewayFactory} instance with {@link JKS} and a collection of 61 | * {@link HttpInterceptorFactory}. 62 | * 63 | * @param factories a collection of {@link HttpInterceptorFactory}. 64 | * @return A instance of {@link HttpVirtualGatewayFactory}. 65 | */ 66 | public static VirtualGatewayFactory create(@NonNull JKS authority, 67 | @NonNull List factories) { 68 | return new HttpVirtualGatewayFactory(authority, factories); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpZygoteRequest.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.gateway.Request; 19 | import com.github.megatronking.netbare.http2.Http2Settings; 20 | import com.github.megatronking.netbare.http2.Http2Updater; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | 25 | /** 26 | * A zygote http request class, it creates the real http request instance. 27 | * 28 | * @author Megatron King 29 | * @since 2019/1/6 17:00 30 | */ 31 | public class HttpZygoteRequest extends HttpRequest implements Http2Updater { 32 | 33 | private final Request mRequest; 34 | private final HttpSessionFactory mSessionFactory; 35 | private final Map mCachedRequests; 36 | 37 | private HttpRequest mActiveRequest; 38 | 39 | /* package */ HttpZygoteRequest(Request request, HttpSessionFactory factory) { 40 | super(request, factory.create(request.id())); 41 | this.mRequest = request; 42 | this.mSessionFactory = factory; 43 | this.mCachedRequests = new HashMap<>(); 44 | } 45 | 46 | public void zygote(HttpId id) { 47 | if (mCachedRequests.containsKey(id.id)) { 48 | mActiveRequest = mCachedRequests.get(id.id); 49 | } else { 50 | HttpSession originSession = session(); 51 | HttpSession session = mSessionFactory.create(id.id); 52 | session.isHttps = originSession.isHttps; 53 | session.protocol = originSession.protocol; 54 | session.clientHttp2Settings = originSession.clientHttp2Settings; 55 | session.peerHttp2Settings = originSession.peerHttp2Settings; 56 | HttpRequest request = new HttpRequest(mRequest, id, session); 57 | mCachedRequests.put(id.id, request); 58 | mActiveRequest = request; 59 | } 60 | } 61 | 62 | @Override 63 | public void onSettingsUpdate(Http2Settings http2Settings) { 64 | session().clientHttp2Settings = http2Settings; 65 | } 66 | 67 | @Override 68 | public void onStreamFinished() { 69 | HttpRequest request = getActive(); 70 | if (request != null) { 71 | request.session().requestStreamEnd = true; 72 | } 73 | } 74 | 75 | HttpRequest getActive() { 76 | return mActiveRequest; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http/HttpZygoteResponse.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.gateway.Response; 19 | import com.github.megatronking.netbare.http2.Http2Settings; 20 | import com.github.megatronking.netbare.http2.Http2Updater; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | 25 | /** 26 | * A zygote http response class, it creates the real http response instance. 27 | * 28 | * @author Megatron King 29 | * @since 2019/1/6 17:09 30 | */ 31 | public class HttpZygoteResponse extends HttpResponse implements Http2Updater { 32 | 33 | private final Response mResponse; 34 | private final HttpSessionFactory mSessionFactory; 35 | private final Map mCachedResponses; 36 | 37 | private HttpResponse mActiveResponse; 38 | 39 | /* package */ HttpZygoteResponse(Response response, HttpSessionFactory factory) { 40 | super(response, factory.create(response.id())); 41 | this.mResponse = response; 42 | this.mSessionFactory = factory; 43 | this.mCachedResponses = new HashMap<>(); 44 | } 45 | 46 | public void zygote(HttpId id) { 47 | if (mCachedResponses.containsKey(id.id)) { 48 | mActiveResponse = mCachedResponses.get(id.id); 49 | } else { 50 | HttpSession originSession = super.session(); 51 | HttpSession session = mSessionFactory.create(id.id); 52 | session.isHttps = originSession.isHttps; 53 | session.protocol = originSession.protocol; 54 | session.clientHttp2Settings = originSession.clientHttp2Settings; 55 | session.peerHttp2Settings = originSession.peerHttp2Settings; 56 | HttpResponse response = new HttpResponse(mResponse, id, session); 57 | mCachedResponses.put(id.id, response); 58 | mActiveResponse = response; 59 | } 60 | } 61 | 62 | @Override 63 | public void onSettingsUpdate(Http2Settings http2Settings) { 64 | session().peerHttp2Settings = http2Settings; 65 | } 66 | 67 | @Override 68 | public void onStreamFinished() { 69 | HttpResponse response = getActive(); 70 | if (response != null) { 71 | response.session().responseStreamEnd = true; 72 | } 73 | } 74 | 75 | HttpResponse getActive() { 76 | return mActiveResponse; 77 | } 78 | 79 | } 80 | 81 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http2/DecodeCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http2; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * A callback for HTTP2 header and data decodes. 23 | * 24 | * @author Megatron King 25 | * @since 2019/1/5 20:36 26 | */ 27 | /* package */ interface DecodeCallback { 28 | 29 | void onPending(ByteBuffer buffer); 30 | 31 | void onResult(ByteBuffer buffer, boolean isFinished) throws IOException; 32 | 33 | void onSkip(ByteBuffer buffer) throws IOException; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http2/EncodeCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http2; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * A callback for HTTP2 header and data encodes. 23 | * 24 | * @author Megatron King 25 | * @since 2019/1/6 21:54 26 | */ 27 | /* package */ interface EncodeCallback { 28 | 29 | void onResult(ByteBuffer buffer) throws IOException; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http2/ErrorCode.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | /* 17 | * Copyright (C) 2013 Square, Inc. 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); 20 | * you may not use this file except in compliance with the License. 21 | * You may obtain a copy of the License at 22 | * 23 | * http://www.apache.org/licenses/LICENSE-2.0 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, 27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 28 | * See the License for the specific language governing permissions and 29 | * limitations under the License. 30 | */ 31 | package com.github.megatronking.netbare.http2; 32 | 33 | /** 34 | * http://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-7 35 | * 36 | * @author Megatron King 37 | * @since 2019/1/11 23:10 38 | */ 39 | /* package */ enum ErrorCode { 40 | 41 | /** 42 | * Not an error! 43 | */ 44 | NO_ERROR(0), 45 | 46 | PROTOCOL_ERROR(1), 47 | 48 | INTERNAL_ERROR(2), 49 | 50 | FLOW_CONTROL_ERROR(3), 51 | 52 | SETTINGS_TIMEOUT(4), 53 | 54 | STREAM_CLOSED(5), 55 | 56 | FRAME_SIZE_ERROR(6), 57 | 58 | REFUSED_STREAM(7), 59 | 60 | CANCEL(8), 61 | 62 | COMPRESSION_ERROR(9), 63 | 64 | CONNECT_ERROR(0xa), 65 | 66 | ENHANCE_YOUR_CALM(0xb), 67 | 68 | INADEQUATE_SECURITY(0xc), 69 | 70 | HTTP_1_1_REQUIRED(0xd); 71 | 72 | public final int httpCode; 73 | 74 | ErrorCode(int httpCode) { 75 | this.httpCode = httpCode; 76 | } 77 | 78 | /* package */ static ErrorCode fromHttp2(int code) { 79 | for (ErrorCode errorCode : ErrorCode.values()) { 80 | if (errorCode.httpCode == code) { 81 | return errorCode; 82 | } 83 | } 84 | return null; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http2/Http2.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http2; 17 | 18 | /** 19 | * HTTP2 protocol constants and common methods. 20 | * 21 | * See https://httpwg.org/specs/rfc7540.html 22 | * 23 | * @author Megatron King 24 | * @since 2019/1/5 14:14 25 | */ 26 | public final class Http2 { 27 | 28 | /** 29 | * In HTTP/2, each endpoint is required to send a connection preface as a final confirmation of 30 | * the protocol in use and to establish the initial settings for the HTTP/2 connection. 31 | */ 32 | public static final byte[] CONNECTION_PREFACE = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".getBytes(); 33 | 34 | static final int FRAME_HEADER_LENGTH = 9; 35 | 36 | /** 37 | * The initial max frame size, applied independently writing to, or reading from the peer. 38 | * 39 | * 0x4000 = 2^14 = 16384 40 | */ 41 | static final int INITIAL_MAX_FRAME_SIZE = 0x4000; 42 | 43 | 44 | static final byte FLAG_NONE = 0x0; 45 | 46 | /** 47 | * Used for settings and ping. 48 | */ 49 | static final byte FLAG_ACK = 0x1; 50 | 51 | /** 52 | * Used for headers and data. 53 | */ 54 | static final byte FLAG_END_STREAM = 0x1; 55 | 56 | /** 57 | * Used for headers and continuation. 58 | */ 59 | static final byte FLAG_END_HEADERS = 0x4; 60 | static final byte FLAG_END_PUSH_PROMISE = 0x4; 61 | 62 | /** 63 | * Used for headers and data. 64 | */ 65 | static final byte FLAG_PADDED = 0x8; 66 | 67 | /** 68 | * Used for headers. 69 | */ 70 | static final byte FLAG_PRIORITY = 0x20; 71 | 72 | /** 73 | * Used for data. 74 | */ 75 | static final byte FLAG_COMPRESSED = 0x20; 76 | 77 | } 78 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http2/Http2Stream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http2; 17 | 18 | /** 19 | * A "stream" is an independent, bidirectional sequence of frames exchanged between the client and 20 | * server within an HTTP/2 connection. 21 | * 22 | * @author Megatron King 23 | * @since 2019/1/5 23:16 24 | */ 25 | /* package */ class Http2Stream { 26 | 27 | public int id; 28 | 29 | /* package */ Http2Stream() { 30 | this.id = -1; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/http2/Http2Updater.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http2; 17 | 18 | /** 19 | * A updater observes changes of HTTP2 session. 20 | * 21 | * @author Megatron King 22 | * @since 2019/1/6 23:23 23 | */ 24 | public interface Http2Updater { 25 | 26 | void onSettingsUpdate(Http2Settings http2Settings); 27 | 28 | void onStreamFinished(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ip/Header.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ip; 17 | 18 | /** 19 | * An abstract header object for ip protocol packets, provides some common apis. 20 | * 21 | * @author Megatron King 22 | * @since 2018-10-09 16:28 23 | */ 24 | /* package */ abstract class Header { 25 | 26 | byte[] packet; 27 | int offset; 28 | 29 | /* package */ Header(byte[] packet, int offset) { 30 | this.packet = packet; 31 | this.offset = offset; 32 | } 33 | 34 | byte readByte(int offset) { 35 | return packet[offset]; 36 | } 37 | 38 | void writeByte(byte value, int offset) { 39 | packet[offset] = value; 40 | } 41 | 42 | short readShort(int offset) { 43 | int r = ((packet[offset] & 0xFF) << 8) | (packet[offset + 1] & 0xFF); 44 | return (short) r; 45 | } 46 | 47 | void writeShort(short value, int offset) { 48 | packet[offset] = (byte) (value >> 8); 49 | packet[offset + 1] = (byte) (value); 50 | } 51 | 52 | int readInt(int offset) { 53 | return ((packet[offset] & 0xFF) << 24) 54 | | ((packet[offset + 1] & 0xFF) << 16) 55 | | ((packet[offset + 2] & 0xFF) << 8) 56 | | (packet[offset + 3] & 0xFF); 57 | } 58 | 59 | void writeInt(int value, int offset) { 60 | packet[offset] = (byte) (value >> 24); 61 | packet[offset + 1] = (byte) (value >> 16); 62 | packet[offset + 2] = (byte) (value >> 8); 63 | packet[offset + 3] = (byte) value; 64 | } 65 | 66 | long getSum(int offset, int len) { 67 | long sum = 0; 68 | while (len > 1) { 69 | sum += readShort(offset) & 0xFFFF; 70 | offset += 2; 71 | len -= 2; 72 | } 73 | 74 | if (len > 0) { 75 | sum += (packet[offset] & 0xFF) << 8; 76 | } 77 | return sum; 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ip/IcmpHeader.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ip; 17 | 18 | /** 19 | * ICMP messages are sent using the basic IP header. The first octet of the data portion of the 20 | * datagram is a ICMP type field; the value of this field determines the format of the remaining 21 | * data. Any field labeled "unused" is reserved for later extensions and must be zero when sent, 22 | * but receivers should not use these fields (except to include them in the checksum). 23 | * Unless otherwise noted under the individual format descriptions, the values of the internet 24 | * header fields are as follows: 25 | * 26 | * 0 1 2 3 27 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 28 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 29 | * | Type | Code | Checksum | 30 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 31 | * | TBD | 32 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 33 | * | Optional | 34 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 35 | * 36 | * See https://tools.ietf.org/html/rfc792 37 | * 38 | * @author Megatron King 39 | * @since 2018-10-10 23:04 40 | */ 41 | public class IcmpHeader extends Header { 42 | 43 | private static final short OFFSET_TYPE = 0; 44 | private static final short OFFSET_CODE = 1; 45 | private static final short OFFSET_CRC = 2; 46 | 47 | private IpHeader mIpHeader; 48 | 49 | public IcmpHeader(IpHeader header, byte[] packet, int offset) { 50 | super(packet, offset); 51 | mIpHeader = header; 52 | } 53 | 54 | public IpHeader getIpHeader() { 55 | return mIpHeader; 56 | } 57 | 58 | public byte getType() { 59 | return readByte(offset + OFFSET_TYPE); 60 | } 61 | 62 | public byte getCode() { 63 | return readByte(offset + OFFSET_CODE); 64 | } 65 | 66 | public short getCrc() { 67 | return readShort(offset + OFFSET_CRC); 68 | } 69 | 70 | public void setCrc(short crc) { 71 | writeShort(crc, offset + OFFSET_CRC); 72 | } 73 | 74 | public void updateChecksum() { 75 | setCrc((short) 0); 76 | setCrc(computeChecksum()); 77 | } 78 | 79 | private short computeChecksum() { 80 | int dataLength = mIpHeader.getDataLength(); 81 | long sum = mIpHeader.getIpSum(); 82 | sum += mIpHeader.getProtocol() & 0xFF; 83 | sum += dataLength; 84 | sum += getSum(offset, dataLength); 85 | while ((sum >> 16) > 0) { 86 | sum = (sum & 0xFFFF) + (sum >> 16); 87 | } 88 | return (short) ~sum; 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ip/IpAddress.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ip; 17 | 18 | import android.os.Parcel; 19 | import android.os.Parcelable; 20 | 21 | import java.util.Objects; 22 | 23 | public class IpAddress implements Parcelable { 24 | 25 | public String address; 26 | public int prefixLength; 27 | 28 | public IpAddress(String address, int prefixLength) { 29 | this.address = address; 30 | this.prefixLength = prefixLength; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return address + "/" + prefixLength; 36 | } 37 | 38 | @Override 39 | public boolean equals(Object o) { 40 | if (this == o) { 41 | return true; 42 | } 43 | if (!(o instanceof IpAddress)) { 44 | return false; 45 | } 46 | // Compare string value. 47 | return toString().equals(o.toString()); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | return Objects.hash(address, prefixLength); 53 | } 54 | 55 | @Override 56 | public int describeContents() { 57 | return 0; 58 | } 59 | 60 | @Override 61 | public void writeToParcel(Parcel dest, int flags) { 62 | dest.writeString(this.address); 63 | dest.writeInt(this.prefixLength); 64 | } 65 | 66 | private IpAddress(Parcel in) { 67 | this.address = in.readString(); 68 | this.prefixLength = in.readInt(); 69 | } 70 | 71 | public static final Creator CREATOR = new Creator() { 72 | @Override 73 | public IpAddress createFromParcel(Parcel source) { 74 | return new IpAddress(source); 75 | } 76 | 77 | @Override 78 | public IpAddress[] newArray(int size) { 79 | return new IpAddress[size]; 80 | } 81 | }; 82 | 83 | } 84 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ip/Protocol.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ip; 17 | 18 | import android.support.annotation.Nullable; 19 | 20 | /** 21 | * The enum defines all supported IP protocols. 22 | * 23 | * Internet Protocol numbers see: 24 | * https://en.wikipedia.org/wiki/List_of_IP_protocol_numbers 25 | * 26 | * @author Megatron King 27 | * @since 2018-10-11 00:13 28 | */ 29 | public enum Protocol { 30 | 31 | /** 32 | * Internet Control Message Protocol. 33 | */ 34 | ICMP((byte)1), 35 | 36 | /** 37 | * Transmission Control Protocol. 38 | */ 39 | TCP((byte)6), 40 | 41 | /** 42 | * User Datagram Protocol. 43 | */ 44 | UDP((byte)17); 45 | 46 | final byte number; 47 | 48 | Protocol(byte number) { 49 | this.number = number; 50 | } 51 | 52 | /** 53 | * Parse the protocol by number. 54 | * 55 | * @param number Protocol number. 56 | * @return The supported protocol number or null. 57 | */ 58 | @Nullable 59 | public static Protocol parse(int number) { 60 | for (Protocol protocol : values()) { 61 | if (protocol.number == number) { 62 | return protocol; 63 | } 64 | } 65 | return null; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/net/DumpCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.net; 17 | 18 | /** 19 | * A callback for dumping net info from /proc/net. 20 | * 21 | * @author Megatron King 22 | * @since 2018-12-01 16:30 23 | */ 24 | /* package*/ interface DumpCallback { 25 | 26 | /** 27 | * Invoked when a net info has dumped. 28 | * 29 | * @param net A dumped net object. 30 | */ 31 | void onDump(Net net); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/net/Net.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.net; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | /** 21 | * A dumped net info class contains IPs, ports and uid. 22 | * 23 | * @author Megatron King 24 | * @since 2018-12-01 22:33 25 | */ 26 | public class Net { 27 | 28 | /** 29 | * The identifier of a process's uid. 30 | */ 31 | public int uid; 32 | 33 | /** 34 | * The local IP. 35 | */ 36 | public String localIp; 37 | 38 | /** 39 | * The local port. 40 | */ 41 | public int localPort; 42 | 43 | /** 44 | * The remote server IP. 45 | */ 46 | public String remoteIp; 47 | 48 | /** 49 | * The remote server port. 50 | */ 51 | public int remotePort; 52 | 53 | /* package */ Net(int uid, String localIp, int localPort, String remoteIp, int remotePort) { 54 | this.uid = uid; 55 | this.localIp = localIp; 56 | this.localPort = localPort; 57 | this.remoteIp = remoteIp; 58 | this.remotePort = remotePort; 59 | } 60 | 61 | @NonNull 62 | @Override 63 | public String toString() { 64 | return uid + " " + localIp + ":" + localPort + " -> " + remoteIp + ":" + remotePort; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/net/Session.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.net; 17 | 18 | import com.github.megatronking.netbare.ip.Protocol; 19 | 20 | import java.util.UUID; 21 | 22 | /** 23 | * This object represents a network session, it contains IPs, ports and IP packet details. 24 | * 25 | * @author Megatron King 26 | * @since 2018-10-14 23:39 27 | */ 28 | public final class Session { 29 | 30 | /** 31 | * IP protocol. 32 | */ 33 | public final Protocol protocol; 34 | 35 | /** 36 | * Local vpn port. 37 | */ 38 | public final short localPort; 39 | 40 | /** 41 | * Remote server port. 42 | */ 43 | public final short remotePort; 44 | 45 | /** 46 | * Remote server IP. 47 | */ 48 | public final int remoteIp; 49 | 50 | /** 51 | * An unique id uses to identify this session. 52 | */ 53 | public String id; 54 | 55 | /** 56 | * Session started time. 57 | */ 58 | public long time; 59 | 60 | /** 61 | * Remote server host. 62 | */ 63 | public String host; 64 | 65 | /** 66 | * The process id that the session belongs to. 67 | */ 68 | public int uid; 69 | 70 | /** 71 | * Packet counts. 72 | */ 73 | public int packetIndex; 74 | 75 | /** 76 | * The total size of the packets that sends to remote server. 77 | */ 78 | public int sendDataSize; 79 | 80 | /** 81 | * The total size of the packets that received from remote server. 82 | */ 83 | public int receiveDataSize; 84 | 85 | /* package */ Session(Protocol protocol, short localPort, short remotePort, int remoteIp) { 86 | this.protocol = protocol; 87 | this.localPort = localPort; 88 | this.remotePort = remotePort; 89 | this.remoteIp = remoteIp; 90 | this.id = UUID.randomUUID().toString(); 91 | this.time = System.currentTimeMillis(); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/net/SessionProvider.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.net; 17 | 18 | import android.support.annotation.NonNull; 19 | import android.support.annotation.Nullable; 20 | 21 | import com.github.megatronking.netbare.ip.Protocol; 22 | 23 | import java.util.Map; 24 | import java.util.concurrent.ConcurrentHashMap; 25 | 26 | /** 27 | * A session provider that provides the session instance query services. 28 | * 29 | * @author Megatron King 30 | * @since 2018-10-15 21:46 31 | */ 32 | public final class SessionProvider { 33 | 34 | private static final int MAX_SESSION = 100; 35 | 36 | private final Map mSessions; 37 | private final UidDumper mDumper; 38 | 39 | /** 40 | * Constructs a session provider with a {@link UidDumper}. 41 | * 42 | * @param dumper Use to dump uid, can be null. 43 | */ 44 | public SessionProvider(UidDumper dumper) { 45 | this.mSessions = new ConcurrentHashMap<>(MAX_SESSION); 46 | this.mDumper = dumper; 47 | } 48 | 49 | /** 50 | * Query a session by local VPN port. 51 | * 52 | * @param localPort The local VPN port. 53 | * @return The instance of {@link Session} if it exists, or null. 54 | */ 55 | @Nullable 56 | public Session query(short localPort) { 57 | Session session = mSessions.get(localPort); 58 | if (mDumper != null && session != null && session.uid == 0) { 59 | // Query uid again. 60 | mDumper.request(session); 61 | } 62 | return session; 63 | } 64 | 65 | /** 66 | * Query or create a session by protocol, ports and remote server IP. 67 | * 68 | * @param protocol IP protocol. 69 | * @param localPort Local VPN port. 70 | * @param remotePort Remote server port. 71 | * @param remoteIp Remote server IP. 72 | * @return An instance of {@link Session}, if the instance not exists, will create a new one. 73 | */ 74 | @NonNull 75 | public Session ensureQuery(Protocol protocol, short localPort, short remotePort, int remoteIp) { 76 | Session session = mSessions.get(localPort); 77 | if (session != null) { 78 | if (session.protocol != protocol || session.localPort != localPort || 79 | session.remotePort != remotePort || session.remoteIp != remoteIp) { 80 | session = null; 81 | } 82 | } 83 | if (session == null) { 84 | session = new Session(protocol, localPort, remotePort, remoteIp); 85 | mSessions.put(localPort, session); 86 | // Dump uid from /proc/net/ 87 | if (mDumper != null) { 88 | mDumper.request(session); 89 | } 90 | } 91 | return session; 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/net/UidProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.net; 2 | 3 | /** 4 | * This interface provides a known uid for a session. 5 | * 6 | * @author Megatron King 7 | * @since 2019/1/27 21:31 8 | */ 9 | public interface UidProvider { 10 | 11 | int UID_UNKNOWN = -1; 12 | 13 | /** 14 | * Returns a known uid for this session, if the uid is unknown should return {@link #UID_UNKNOWN}. 15 | * 16 | * @param session Network session. 17 | * @return A known uid or {@link #UID_UNKNOWN}. 18 | */ 19 | int uid(Session session); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/proxy/BaseProxyServer.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.proxy; 17 | 18 | import com.github.megatronking.netbare.NetBareLog; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * An abstract base class defined for proxy servers. The local proxy server runs a separated thread 24 | * and loop to process packets. The sub class needs to impl {@link #process()} to handle the packets. 25 | * 26 | * @author Megatron King 27 | * @since 2018-10-10 00:31 28 | */ 29 | /* package */ abstract class BaseProxyServer extends ProxyServer implements Runnable { 30 | 31 | /** 32 | * Waiting the specific protocol packets and trying to sent to real remote server. 33 | * 34 | * @throws IOException If an I/O error has occurred. 35 | */ 36 | protected abstract void process() throws IOException; 37 | 38 | private boolean mIsRunning; 39 | 40 | private final Thread mServerThread; 41 | 42 | /* package */ BaseProxyServer(String serverName) { 43 | this.mServerThread = new Thread(this, serverName); 44 | } 45 | 46 | @Override 47 | void startServer() { 48 | mIsRunning = true; 49 | mServerThread.start(); 50 | } 51 | 52 | @Override 53 | void stopServer() { 54 | mIsRunning = false; 55 | mServerThread.interrupt(); 56 | } 57 | 58 | @Override 59 | public void run() { 60 | while (mIsRunning) { 61 | try { 62 | process(); 63 | } catch (IOException e) { 64 | NetBareLog.e(e.getMessage()); 65 | } 66 | } 67 | } 68 | 69 | /* package */ boolean isStopped() { 70 | return !mIsRunning; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/proxy/IcmpProxyServerForwarder.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.proxy; 17 | 18 | import com.github.megatronking.netbare.NetBareLog; 19 | import com.github.megatronking.netbare.ip.IcmpHeader; 20 | import com.github.megatronking.netbare.ip.IpHeader; 21 | 22 | import java.io.OutputStream; 23 | 24 | /** 25 | * Forward the Internet Control Message Protocol (ICMP) to proxy server. 26 | * 27 | * @author Megatron King 28 | * @since 2018-10-09 01:30 29 | */ 30 | public final class IcmpProxyServerForwarder implements ProxyServerForwarder { 31 | 32 | @Override 33 | public void prepare() { 34 | // TODO 35 | } 36 | 37 | @Override 38 | public void forward(byte[] packet, int len, OutputStream output) { 39 | IpHeader ipHeader = new IpHeader(packet, 0); 40 | IcmpHeader icmpHeader = new IcmpHeader(ipHeader, packet, ipHeader.getHeaderLength()); 41 | NetBareLog.v("ICMP type: " + icmpHeader.getType()); 42 | NetBareLog.v("ICMP code: " + icmpHeader.getCode()); 43 | // TODO transfer to proxy server 44 | } 45 | 46 | @Override 47 | public void release() { 48 | // TODO 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/proxy/ProxyServer.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.proxy; 17 | 18 | /** 19 | * A local proxy server receives net packets from VPN and transfer them to the real remote server. 20 | * Every local proxy server runs separated threads and handle specific IP protocols like TCP, UDP 21 | * and so on. The server is managed by {@link ProxyServerForwarder}, use {@link #start()} to 22 | * establish the server and {@link #stop()} to terminate it. 23 | * 24 | * @author Megatron King 25 | * @since 2018-10-10 00:23 26 | */ 27 | public abstract class ProxyServer { 28 | 29 | /** 30 | * Establish the server and start receive packets. 31 | */ 32 | /* package */ abstract void startServer(); 33 | 34 | /** 35 | * Terminate this server. 36 | */ 37 | /* package */ abstract void stopServer(); 38 | 39 | /** 40 | * Returns the proxy server IP. 41 | * 42 | * @return The proxy server IP. 43 | */ 44 | /* package */ abstract int getIp(); 45 | 46 | /** 47 | * Returns the proxy server port. 48 | * 49 | * @return The proxy server port. 50 | */ 51 | /* package */ abstract short getPort(); 52 | 53 | /** 54 | * Establish the proxy server. 55 | */ 56 | public final void start() { 57 | startServer(); 58 | } 59 | 60 | /** 61 | * Terminate the proxy server, release resources and close IOs. 62 | */ 63 | public final void stop() { 64 | stopServer(); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/proxy/ProxyServerForwarder.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.proxy; 17 | 18 | import java.io.OutputStream; 19 | 20 | /** 21 | * An interface needs to be implement by proxy server forwarders. 22 | * 23 | * @author Megatron King 24 | * @since 2018-10-09 01:24 25 | */ 26 | public interface ProxyServerForwarder { 27 | 28 | /** 29 | * Prepare the forwarder. 30 | */ 31 | void prepare(); 32 | 33 | /** 34 | * Forward a packet to local proxy server. 35 | * 36 | * @param packet A data packet, the array length is MTU. 37 | * @param len The actual data length in packet array. 38 | * @param output An output stream connects VPN file descriptor. 39 | */ 40 | void forward(byte[] packet, int len, OutputStream output); 41 | 42 | /** 43 | * Release the forwarder. 44 | */ 45 | void release(); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ssl/CertificateInstallActivity.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ssl; 17 | 18 | import android.app.Activity; 19 | import android.content.ActivityNotFoundException; 20 | import android.content.Intent; 21 | import android.os.Bundle; 22 | import android.security.KeyChain; 23 | import android.support.annotation.Nullable; 24 | 25 | import com.github.megatronking.netbare.NetBareLog; 26 | 27 | import java.io.File; 28 | import java.io.IOException; 29 | 30 | /** 31 | * A translucent activity uses to install self-signed certificate. 32 | * 33 | * @author Megatron King 34 | * @since 2018-11-10 21:18 35 | */ 36 | public class CertificateInstallActivity extends Activity { 37 | 38 | private static final int REQUEST_CODE_INSTALL = 1; 39 | public static final String EXTRA_ALIAS = "jks_alias"; 40 | 41 | @Override 42 | protected void onCreate(@Nullable Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | Bundle bundle = getIntent().getExtras(); 45 | if (bundle == null) { 46 | finish(); 47 | return; 48 | } 49 | Intent intent = KeyChain.createInstallIntent(); 50 | intent.putExtras(bundle); 51 | try { 52 | startActivityForResult(intent, REQUEST_CODE_INSTALL); 53 | } catch (ActivityNotFoundException e) { 54 | NetBareLog.e("Unable to start certificate installer."); 55 | finish(); 56 | } 57 | } 58 | 59 | @Override 60 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 61 | super.onActivityResult(requestCode, resultCode, data); 62 | if (requestCode == REQUEST_CODE_INSTALL && resultCode == RESULT_OK) { 63 | File jsk = new File(getCacheDir(), 64 | getIntent().getStringExtra(EXTRA_ALIAS) + JKS.KEY_JKS_FILE_EXTENSION); 65 | try { 66 | if(!jsk.exists() && !jsk.createNewFile()) { 67 | throw new IOException("Create jks file failed."); 68 | } 69 | } catch (IOException e) { 70 | NetBareLog.wtf(e); 71 | } 72 | } 73 | finish(); 74 | } 75 | 76 | @Override 77 | protected void onSaveInstanceState(Bundle outState) { 78 | super.onSaveInstanceState(outState); 79 | outState.putAll(getIntent().getExtras()); 80 | } 81 | 82 | @Override 83 | protected void onRestoreInstanceState(Bundle savedInstanceState) { 84 | super.onRestoreInstanceState(savedInstanceState); 85 | getIntent().putExtras(savedInstanceState); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ssl/SSLKeyManagerProvider.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ssl; 17 | 18 | import android.support.annotation.Nullable; 19 | 20 | import javax.net.ssl.KeyManager; 21 | 22 | /** 23 | * A security provider provides the sources of authentication keys. The {@link KeyManager[]} 24 | * instance would be used to initialize {@link javax.net.ssl.SSLContext}. 25 | * 26 | * @author Megatron King 27 | * @since 2019/3/31 10:56 28 | */ 29 | public interface SSLKeyManagerProvider { 30 | 31 | /** 32 | * Provides an authentication keys or null. 33 | * 34 | * @param host The peer host. 35 | * @param client Whether the SSLContext is initialized for client. 36 | * @return The sources of authentication keys or null. 37 | */ 38 | @Nullable 39 | KeyManager[] provide(String host, boolean client); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ssl/SSLRefluxCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ssl; 17 | 18 | import com.github.megatronking.netbare.gateway.Request; 19 | import com.github.megatronking.netbare.gateway.Response; 20 | 21 | import java.io.IOException; 22 | import java.nio.ByteBuffer; 23 | 24 | /** 25 | * A callback to receive SSL plaintext packets and input them to {@link javax.net.ssl.SSLEngine}. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-15 14:35 29 | */ 30 | public interface SSLRefluxCallback { 31 | 32 | void onRequest(Req request, ByteBuffer buffer) throws IOException; 33 | 34 | void onResponse(Res response, ByteBuffer buffer) throws IOException; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ssl/SSLRequestCodec.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ssl; 17 | 18 | import com.github.megatronking.netbare.NetBareLog; 19 | import com.github.megatronking.netbare.gateway.Request; 20 | 21 | import java.util.concurrent.ExecutionException; 22 | 23 | import javax.net.ssl.SSLEngine; 24 | 25 | /** 26 | * An implementation of {@link SSLCodec} to codec request SSL packets. This codec creates a MITM 27 | * SSL server engine using {@link SSLEngineFactory}, it requires the remote server host. 28 | * 29 | * @author Megatron King 30 | * @since 2018-11-15 23:23 31 | */ 32 | public class SSLRequestCodec extends SSLCodec { 33 | 34 | private Request mRequest; 35 | private SSLEngine mEngine; 36 | 37 | /** 38 | * Constructs an instance of {@link SSLCodec} by a factory. 39 | * 40 | * @param factory A factory produces {@link SSLEngine}. 41 | */ 42 | public SSLRequestCodec(SSLEngineFactory factory) { 43 | super(factory); 44 | } 45 | 46 | /** 47 | * Bind a {@link Request} to this codec. 48 | * 49 | * @param request A request has terminated remote server host. 50 | */ 51 | public void setRequest(Request request) { 52 | this.mRequest = request; 53 | } 54 | 55 | @Override 56 | protected SSLEngine createEngine(SSLEngineFactory factory) { 57 | if (mEngine == null) { 58 | String host = mRequest.host(); 59 | if (host == null) { 60 | // Unable to get host. 61 | NetBareLog.e("Failed to get SSL host."); 62 | return null; 63 | } 64 | try { 65 | mEngine = factory.createServerEngine(host); 66 | mEngine.setUseClientMode(false); 67 | mEngine.setNeedClientAuth(false); 68 | } catch (ExecutionException e) { 69 | NetBareLog.e("Failed to create server SSLEngine: " + e.getMessage()); 70 | } 71 | } 72 | return mEngine; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ssl/SSLTrustManagerProvider.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ssl; 17 | 18 | import android.support.annotation.Nullable; 19 | 20 | import javax.net.ssl.TrustManager; 21 | 22 | /** 23 | * A security provider provides peer authentication trust decisions. The {@link TrustManager[]} 24 | * * instance would be used to initialize {@link javax.net.ssl.SSLContext}. 25 | * 26 | * @author Megatron King 27 | * @since 2019/3/31 10:56 28 | */ 29 | public interface SSLTrustManagerProvider { 30 | 31 | /** 32 | * Provides peer authentication trust decisions or null. 33 | * 34 | * @param host The peer host. 35 | * @param client Whether the SSLContext is initialized for client. 36 | * @return The sources of peer authentication trust decisions or null. 37 | */ 38 | @Nullable 39 | TrustManager[] provide(String host, boolean client); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ssl/SSLWhiteList.java: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.ssl; 2 | 3 | import java.util.HashSet; 4 | 5 | /** 6 | * ip whitelist for ssl bypass 7 | * @author cuisoap 8 | * @since 2019/08/01 10:00 9 | */ 10 | public class SSLWhiteList { 11 | private static HashSet whitelist = new HashSet<>(); 12 | 13 | public static void add(String ip) { 14 | whitelist.add(ip); 15 | } 16 | 17 | public static boolean contains(String ip) { 18 | return whitelist.contains(ip); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tcp/TcpVirtualGateway.java: -------------------------------------------------------------------------------- 1 | /* 2 | * NetBare - An android network capture and injection library. 3 | * Copyright (C) 2018-2019 Megatron King 4 | * Copyright (C) 2018-2019 GuoShi 5 | * 6 | * NetBare is free software: you can redistribute it and/or modify it under the terms 7 | * of the GNU General Public License as published by the Free Software Found- 8 | * ation, either version 3 of the License, or (at your option) any later version. 9 | * 10 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 | * PURPOSE. See the GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License along with NetBare. 15 | * If not, see . 16 | */ 17 | package com.github.megatronking.netbare.tcp; 18 | 19 | import com.github.megatronking.netbare.gateway.Request; 20 | import com.github.megatronking.netbare.gateway.Response; 21 | import com.github.megatronking.netbare.gateway.SpecVirtualGateway; 22 | import com.github.megatronking.netbare.gateway.VirtualGateway; 23 | import com.github.megatronking.netbare.ip.Protocol; 24 | import com.github.megatronking.netbare.net.Session; 25 | 26 | /** 27 | * A {@link VirtualGateway} that is responsible for TCP protocol packets interception. 28 | * 29 | * @author Megatron King 30 | * @since 2019-04-06 17:03 31 | */ 32 | public abstract class TcpVirtualGateway extends SpecVirtualGateway { 33 | 34 | public TcpVirtualGateway(Session session, Request request, Response response) { 35 | super(Protocol.TCP, session, request, response); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/ConnectionShutdownException.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import java.io.IOException; 19 | 20 | /** 21 | * Thrown when the connection has shutdown due to irresistible reason. 22 | * 23 | * @author Megatron King 24 | * @since 2018-12-20 18:50 25 | */ 26 | public class ConnectionShutdownException extends IOException { 27 | 28 | /** 29 | * Constructs an {@code ConnectionShutdownException} with the specified detail message. 30 | * 31 | * @param message The detail message (which is saved for later retrieval by the 32 | * {@link #getMessage()} method). 33 | */ 34 | public ConnectionShutdownException(String message) { 35 | super(message); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/NioCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import java.io.IOException; 19 | 20 | /** 21 | * A nio selector attachment callback for sending notification to {@link NioTunnel}. 22 | * 23 | * @author Megatron King 24 | * @since 2018-10-15 23:15 25 | */ 26 | public interface NioCallback { 27 | 28 | /** 29 | * Invoked when the connection is connected with the terminal. 30 | * 31 | * @throws IOException If an I/O error has occurred. 32 | */ 33 | void onConnected() throws IOException; 34 | 35 | /** 36 | * Invoked when the socket IO is readable. 37 | * 38 | * @throws IOException If an I/O error has occurred. 39 | */ 40 | void onRead() throws IOException; 41 | 42 | /** 43 | * Invoked when the socket IO is writable. 44 | * 45 | * @throws IOException If an I/O error has occurred. 46 | */ 47 | void onWrite() throws IOException; 48 | 49 | /** 50 | * Invoked when the socket IO is closed. 51 | */ 52 | void onClosed(); 53 | 54 | /** 55 | * Returns the tunnel using nio attachment. 56 | * 57 | * @return A tunnel. 58 | */ 59 | NioTunnel getTunnel(); 60 | 61 | } 62 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/TcpProxyTunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import com.github.megatronking.netbare.NetBareXLog; 19 | import com.github.megatronking.netbare.ip.Protocol; 20 | 21 | import java.io.IOException; 22 | import java.net.InetSocketAddress; 23 | import java.net.Socket; 24 | import java.nio.ByteBuffer; 25 | import java.nio.channels.Selector; 26 | import java.nio.channels.SocketChannel; 27 | 28 | /** 29 | * A TCP tunnel communicates with the VPN service. 30 | * 31 | * @author Megatron King 32 | * @since 2018-11-21 01:40 33 | */ 34 | public class TcpProxyTunnel extends TcpTunnel { 35 | 36 | private NetBareXLog mLog; 37 | 38 | public TcpProxyTunnel(SocketChannel socketChannel, Selector selector, int remotePort) { 39 | super(socketChannel, selector); 40 | Socket socket = socketChannel.socket(); 41 | this.mLog = new NetBareXLog(Protocol.TCP, socket.getInetAddress().getHostAddress(), 42 | remotePort); 43 | } 44 | 45 | @Override 46 | public void connect(InetSocketAddress address) { 47 | // Nothing to connect 48 | } 49 | 50 | @Override 51 | public void onConnected() throws IOException { 52 | mLog.i("Proxy tunnel is connected."); 53 | super.onConnected(); 54 | } 55 | 56 | @Override 57 | public int read(ByteBuffer buffer) throws IOException { 58 | int len = super.read(buffer); 59 | mLog.i("Read from proxy: " + len); 60 | return len; 61 | } 62 | 63 | @Override 64 | public void write(ByteBuffer buffer) throws IOException { 65 | mLog.i("Write to proxy: " + buffer.remaining()); 66 | super.write(buffer); 67 | } 68 | 69 | @Override 70 | public void close() { 71 | mLog.i("Proxy tunnel is closed."); 72 | super.close(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/TcpRemoteTunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import android.net.VpnService; 19 | 20 | import com.github.megatronking.netbare.NetBareXLog; 21 | import com.github.megatronking.netbare.ip.Protocol; 22 | 23 | import java.io.IOException; 24 | import java.net.InetSocketAddress; 25 | import java.nio.ByteBuffer; 26 | import java.nio.channels.Selector; 27 | import java.nio.channels.SocketChannel; 28 | 29 | /** 30 | * A TCP tunnel communicates with the remote server. 31 | * 32 | * @author Megatron King 33 | * @since 2018-11-21 01:41 34 | */ 35 | public class TcpRemoteTunnel extends TcpTunnel { 36 | 37 | private final VpnService mVpnService; 38 | 39 | private NetBareXLog mLog; 40 | 41 | public TcpRemoteTunnel(VpnService vpnService, SocketChannel channel, Selector selector, 42 | String remoteIp, int remotePort) { 43 | super(channel, selector); 44 | this.mVpnService = vpnService; 45 | this.mLog = new NetBareXLog(Protocol.TCP, remoteIp, remotePort); 46 | } 47 | 48 | @Override 49 | public void connect(InetSocketAddress address) throws IOException { 50 | if (mVpnService.protect(socket())) { 51 | super.connect(address); 52 | mLog.i("Connect to remote server %s", address); 53 | } else { 54 | throw new IOException("[TCP]Can not protect remote tunnel socket."); 55 | } 56 | } 57 | 58 | @Override 59 | public void onConnected() throws IOException { 60 | mLog.i("Remote tunnel is connected."); 61 | super.onConnected(); 62 | } 63 | 64 | @Override 65 | public int read(ByteBuffer buffer) throws IOException { 66 | int len = super.read(buffer); 67 | mLog.i("Read from remote: " + len); 68 | return len; 69 | } 70 | 71 | @Override 72 | public void write(ByteBuffer buffer) throws IOException { 73 | mLog.i("Write to remote: " + buffer.remaining()); 74 | super.write(buffer); 75 | } 76 | 77 | @Override 78 | public void close() { 79 | mLog.i("Remote tunnel is closed."); 80 | super.close(); 81 | } 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/TcpTunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import com.github.megatronking.netbare.NetBareLog; 19 | 20 | import java.io.IOException; 21 | import java.net.InetSocketAddress; 22 | import java.net.Socket; 23 | import java.nio.ByteBuffer; 24 | import java.nio.channels.SelectionKey; 25 | import java.nio.channels.Selector; 26 | import java.nio.channels.SocketChannel; 27 | 28 | /** 29 | * A TCP protocol implementation based with {@link NioTunnel}. 30 | * 31 | * @author Megatron King 32 | * @since 2018-11-21 00:36 33 | */ 34 | public abstract class TcpTunnel extends NioTunnel { 35 | 36 | private final SocketChannel mSocketChannel; 37 | private final Selector mSelector; 38 | 39 | TcpTunnel(SocketChannel socketChannel, Selector selector) { 40 | super(socketChannel, selector); 41 | this.mSocketChannel = socketChannel; 42 | this.mSelector = selector; 43 | } 44 | 45 | @Override 46 | public void connect(InetSocketAddress address) throws IOException { 47 | NetBareLog.i("TCP connects to: %s:%s", 48 | address.getAddress().getHostAddress(), address.getPort()); 49 | if (mSocketChannel.isBlocking()) { 50 | mSocketChannel.configureBlocking(false); 51 | } 52 | mSocketChannel.register(mSelector, SelectionKey.OP_CONNECT, this); 53 | mSocketChannel.connect(address); 54 | } 55 | 56 | @Override 57 | public void onConnected() throws IOException { 58 | if (mSocketChannel.finishConnect()) { 59 | super.onConnected(); 60 | } else { 61 | throw new IOException("[TCP]The tunnel socket is not connected."); 62 | } 63 | } 64 | 65 | @Override 66 | public Socket socket() { 67 | return mSocketChannel.socket(); 68 | } 69 | 70 | @Override 71 | protected int channelRead(ByteBuffer buffer) throws IOException { 72 | return mSocketChannel.read(buffer); 73 | } 74 | 75 | @Override 76 | protected int channelWrite(ByteBuffer buffer) throws IOException { 77 | return mSocketChannel.write(buffer); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/Tunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import java.io.IOException; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * A tunnel connects a terminal that can be wrote to. 23 | * 24 | * @author Megatron King 25 | * @since 2018-10-25 17:57 26 | */ 27 | public interface Tunnel { 28 | 29 | /** 30 | * Write a packet buffer to the terminal. 31 | * 32 | * @param buffer A packet buffer. 33 | * @throws IOException If an I/O error has occurred. 34 | */ 35 | void write(ByteBuffer buffer) throws IOException; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/UdpRemoteTunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import android.net.VpnService; 19 | 20 | import com.github.megatronking.netbare.NetBareXLog; 21 | import com.github.megatronking.netbare.ip.Protocol; 22 | 23 | import java.io.IOException; 24 | import java.net.InetSocketAddress; 25 | import java.nio.ByteBuffer; 26 | import java.nio.channels.DatagramChannel; 27 | import java.nio.channels.Selector; 28 | 29 | /** 30 | * A UDP tunnel communicates with the remote server. 31 | * 32 | * @author Megatron King 33 | * @since 2018-12-02 19:49 34 | */ 35 | public class UdpRemoteTunnel extends UdpTunnel { 36 | 37 | private final VpnService mVpnService; 38 | private NetBareXLog mLog; 39 | 40 | public UdpRemoteTunnel(VpnService vpnService, DatagramChannel channel, Selector selector, 41 | String remoteIp, short remotePort) { 42 | super(channel, selector); 43 | this.mVpnService = vpnService; 44 | this.mLog = new NetBareXLog(Protocol.UDP, remoteIp, remotePort); 45 | } 46 | 47 | @Override 48 | public void connect(InetSocketAddress address) throws IOException { 49 | if (mVpnService.protect(socket())) { 50 | super.connect(address); 51 | mLog.i("Connect to remote server %s", address); 52 | } else { 53 | throw new IOException("[UDP]Can not protect remote tunnel socket."); 54 | } 55 | } 56 | 57 | @Override 58 | public int read(ByteBuffer buffer) throws IOException { 59 | int len = super.read(buffer); 60 | mLog.i("Read from remote: " + len); 61 | return len; 62 | } 63 | 64 | @Override 65 | public void write(ByteBuffer buffer) throws IOException { 66 | mLog.i("Write to remote: " + buffer.remaining()); 67 | super.write(buffer); 68 | } 69 | 70 | @Override 71 | public void close() { 72 | mLog.i("Remote tunnel is closed."); 73 | super.close(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/UdpTunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import com.github.megatronking.netbare.NetBareLog; 19 | 20 | import java.io.IOException; 21 | import java.net.DatagramSocket; 22 | import java.net.InetSocketAddress; 23 | import java.nio.ByteBuffer; 24 | import java.nio.channels.DatagramChannel; 25 | import java.nio.channels.Selector; 26 | 27 | /** 28 | * A UDP protocol implementation based with {@link NioTunnel}. 29 | * 30 | * @author Megatron King 31 | * @since 2018-12-02 19:24 32 | */ 33 | public abstract class UdpTunnel extends NioTunnel { 34 | 35 | private final DatagramChannel mDatagramChannel; 36 | 37 | UdpTunnel(DatagramChannel datagramChannel, Selector selector) { 38 | super(datagramChannel, selector); 39 | this.mDatagramChannel = datagramChannel; 40 | } 41 | 42 | @Override 43 | public void connect(InetSocketAddress address) throws IOException { 44 | NetBareLog.i("UDP connects to: %s:%s", 45 | address.getAddress().getHostAddress(), address.getPort()); 46 | if (mDatagramChannel.isBlocking()) { 47 | mDatagramChannel.configureBlocking(false); 48 | } 49 | mDatagramChannel.connect(address); 50 | prepareRead(); 51 | } 52 | 53 | @Override 54 | public DatagramSocket socket() { 55 | return mDatagramChannel.socket(); 56 | } 57 | 58 | @Override 59 | protected int channelWrite(ByteBuffer buffer) throws IOException { 60 | return mDatagramChannel.write(buffer); 61 | } 62 | 63 | @Override 64 | protected int channelRead(ByteBuffer buffer) throws IOException { 65 | return mDatagramChannel.read(buffer); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/tunnel/VirtualGatewayTunnel.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.tunnel; 17 | 18 | import com.github.megatronking.netbare.gateway.VirtualGateway; 19 | 20 | import java.io.Closeable; 21 | import java.io.IOException; 22 | import java.net.InetSocketAddress; 23 | 24 | /** 25 | * A tunnel uses {@link VirtualGateway} to intercept and filter net packets. 26 | * 27 | * @author Megatron King 28 | * @since 2018-11-21 09:00 29 | */ 30 | public abstract class VirtualGatewayTunnel implements Closeable { 31 | 32 | /** 33 | * Connects to the remote server by the given server address. 34 | * 35 | * @param address The server IP socket address. 36 | * @throws IOException If an I/O error has occurred. 37 | */ 38 | public abstract void connect(InetSocketAddress address) throws IOException; 39 | 40 | /** 41 | * Returns the {@link VirtualGateway} this tunnel created. 42 | * 43 | * @return The {@link VirtualGateway} instance. 44 | */ 45 | public abstract VirtualGateway getGateway(); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/udp/UdpVirtualGateway.java: -------------------------------------------------------------------------------- 1 | /* 2 | * NetBare - An android network capture and injection library. 3 | * Copyright (C) 2018-2019 Megatron King 4 | * Copyright (C) 2018-2019 GuoShi 5 | * 6 | * NetBare is free software: you can redistribute it and/or modify it under the terms 7 | * of the GNU General Public License as published by the Free Software Found- 8 | * ation, either version 3 of the License, or (at your option) any later version. 9 | * 10 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 11 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 | * PURPOSE. See the GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License along with NetBare. 15 | * If not, see . 16 | */ 17 | package com.github.megatronking.netbare.udp; 18 | 19 | import com.github.megatronking.netbare.gateway.Request; 20 | import com.github.megatronking.netbare.gateway.Response; 21 | import com.github.megatronking.netbare.gateway.SpecVirtualGateway; 22 | import com.github.megatronking.netbare.gateway.VirtualGateway; 23 | import com.github.megatronking.netbare.ip.Protocol; 24 | import com.github.megatronking.netbare.net.Session; 25 | 26 | /** 27 | * A {@link VirtualGateway} that is responsible for UDP protocol packets interception. 28 | * 29 | * @author Megatron King 30 | * @since 2019-04-06 17:03 31 | */ 32 | public abstract class UdpVirtualGateway extends SpecVirtualGateway { 33 | 34 | public UdpVirtualGateway(Session session, Request request, Response response) { 35 | super(Protocol.UDP, session, request, response); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /netbare-core/src/main/java/com/github/megatronking/netbare/ws/WebSocketCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.ws; 17 | 18 | import java.io.IOException; 19 | 20 | /** 21 | * A callback to be invoked when the message is decoded. 22 | * 23 | * @author Megatron King 24 | * @since 2019/1/18 23:56 25 | */ 26 | public interface WebSocketCallback { 27 | 28 | /** 29 | * Invoked when a text message is decoded. 30 | * 31 | * @param text A text content. 32 | * @throws IOException If an I/O error has occurred. 33 | */ 34 | void onReadMessage(String text) throws IOException; 35 | 36 | /** 37 | * Invoked when a binary message is decoded. 38 | * 39 | * @param binary A binary content. 40 | * @throws IOException If an I/O error has occurred. 41 | */ 42 | void onReadMessage(byte[] binary) throws IOException; 43 | 44 | /** 45 | * Invoked when a ping message is decoded. 46 | * 47 | * @param ping The ping message content. 48 | */ 49 | void onReadPing(byte[] ping); 50 | 51 | /** 52 | * Invoked when a pong message is decoded. 53 | * 54 | * @param pong The pong message content. 55 | */ 56 | void onReadPong(byte[] pong); 57 | 58 | /** 59 | * Invoked when the control frame is closed. 60 | * 61 | * @param code Status code. 62 | * @param reason Close reason. 63 | */ 64 | void onReadClose(int code, String reason); 65 | 66 | } 67 | -------------------------------------------------------------------------------- /netbare-core/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 21 | -------------------------------------------------------------------------------- /netbare-injector/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /netbare-injector/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | minSdkVersion 21 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | } 12 | 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | } 21 | 22 | dependencies { 23 | implementation project(':netbare-core') 24 | implementation 'com.android.support:appcompat-v7:28.0.0' 25 | } -------------------------------------------------------------------------------- /netbare-injector/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/http/HttpBody.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.stream.Stream; 19 | 20 | /** 21 | * An abstract HTTP body type object, it declares that this object represents the HTTP body. 22 | * 23 | * @author Megatron King 24 | * @since 2018-12-15 21:09 25 | */ 26 | public abstract class HttpBody implements Stream { 27 | } 28 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/http/HttpRawBody.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | /** 23 | * HTTP body object contains a raw buffer. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-15 21:15 27 | */ 28 | public class HttpRawBody extends HttpBody { 29 | 30 | private ByteBuffer mRawBuffer; 31 | 32 | public HttpRawBody(ByteBuffer buffer) { 33 | ByteBuffer rawBuffer = ByteBuffer.allocate(buffer.remaining()); 34 | rawBuffer.put(buffer.array(), buffer.position(), buffer.remaining()); 35 | rawBuffer.flip(); 36 | this.mRawBuffer = rawBuffer; 37 | } 38 | 39 | @NonNull 40 | @Override 41 | public ByteBuffer toBuffer() { 42 | return mRawBuffer; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/http/HttpRequestInjectorCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.stream.Stream; 19 | import com.github.megatronking.netbare.injector.InjectorCallback; 20 | 21 | import java.io.IOException; 22 | import java.nio.ByteBuffer; 23 | import java.util.LinkedHashMap; 24 | 25 | /** 26 | * A HTTP request packets injector callback. 27 | * 28 | * @author Megatron King 29 | * @since 2018-12-15 21:55 30 | */ 31 | public class HttpRequestInjectorCallback implements InjectorCallback { 32 | 33 | private HttpRequestChain mChain; 34 | 35 | /** 36 | * Constructs a {@link InjectorCallback} for HTTP request. 37 | * 38 | * @param chain A {@link HttpInterceptor} chain. 39 | */ 40 | public HttpRequestInjectorCallback(HttpRequestChain chain) { 41 | this.mChain = chain; 42 | } 43 | 44 | @Override 45 | public void onFinished(Stream stream) throws IOException { 46 | ByteBuffer byteBuffer = stream.toBuffer(); 47 | if (stream instanceof HttpRequestHeaderPart) { 48 | HttpRequestHeaderPart header = (HttpRequestHeaderPart) stream; 49 | HttpSession session = mChain.request().session(); 50 | session.method = header.method(); 51 | session.protocol = header.protocol(); 52 | session.path = header.path(); 53 | session.requestHeaders = new LinkedHashMap<>(header.headers()); 54 | session.reqBodyOffset = byteBuffer.remaining(); 55 | } 56 | mChain.process(byteBuffer); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/http/HttpResponseInjectorCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.http; 17 | 18 | import com.github.megatronking.netbare.stream.Stream; 19 | import com.github.megatronking.netbare.injector.InjectorCallback; 20 | 21 | import java.io.IOException; 22 | import java.nio.ByteBuffer; 23 | import java.util.LinkedHashMap; 24 | 25 | /** 26 | * A HTTP response packets injector callback. 27 | * 28 | * @author Megatron King 29 | * @since 2018-12-15 21:55 30 | */ 31 | public class HttpResponseInjectorCallback implements InjectorCallback { 32 | 33 | private HttpResponseChain mChain; 34 | 35 | /** 36 | * Constructs a {@link InjectorCallback} for HTTP response. 37 | * 38 | * @param chain A {@link HttpInterceptor} chain. 39 | */ 40 | public HttpResponseInjectorCallback(HttpResponseChain chain) { 41 | this.mChain = chain; 42 | } 43 | 44 | @Override 45 | public void onFinished(Stream stream) throws IOException { 46 | ByteBuffer byteBuffer = stream.toBuffer(); 47 | if (stream instanceof HttpResponseHeaderPart) { 48 | HttpResponseHeaderPart header = (HttpResponseHeaderPart) stream; 49 | HttpSession session = mChain.response().session(); 50 | session.code = header.code(); 51 | session.message = header.message(); 52 | session.responseHeaders = new LinkedHashMap<>(header.headers()); 53 | session.resBodyOffset = byteBuffer.remaining(); 54 | } 55 | mChain.process(byteBuffer); 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/injector/BlockedHttpInjector.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.injector; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.http.HttpBody; 21 | import com.github.megatronking.netbare.http.HttpRequest; 22 | import com.github.megatronking.netbare.http.HttpRequestHeaderPart; 23 | import com.github.megatronking.netbare.http.HttpResponse; 24 | import com.github.megatronking.netbare.http.HttpResponseHeaderPart; 25 | 26 | /** 27 | * An injector can block http request and response. Add your custom rules in 28 | * {@link #sniffRequest(HttpRequest)} or {@link #sniffResponse(HttpResponse)} to filter the request. 29 | * 30 | * @author Megatron King 31 | * @since 2019/1/2 20:50 32 | */ 33 | public abstract class BlockedHttpInjector implements HttpInjector { 34 | 35 | @Override 36 | public final void onRequestInject(@NonNull HttpRequestHeaderPart header, 37 | @NonNull InjectorCallback callback) { 38 | } 39 | 40 | @Override 41 | public final void onResponseInject(@NonNull HttpResponseHeaderPart header, 42 | @NonNull InjectorCallback callback) { 43 | } 44 | 45 | @Override 46 | public final void onRequestInject(@NonNull HttpRequest request, @NonNull HttpBody body, 47 | @NonNull InjectorCallback callback) { 48 | } 49 | 50 | @Override 51 | public final void onResponseInject(@NonNull HttpResponse response, @NonNull HttpBody body, 52 | @NonNull InjectorCallback callback) { 53 | } 54 | 55 | @Override 56 | public void onRequestFinished(@NonNull HttpRequest request) { 57 | } 58 | 59 | @Override 60 | public void onResponseFinished(@NonNull HttpResponse response) { 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/injector/InjectorCallback.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.injector; 17 | 18 | import com.github.megatronking.netbare.stream.Stream; 19 | 20 | import java.io.IOException; 21 | 22 | /** 23 | * A callback invoked when the injector finished the injection. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-15 21:55 27 | */ 28 | public interface InjectorCallback { 29 | 30 | /** 31 | * Invoked when the injector finished the injection. 32 | * 33 | * @param stream The injected stream. 34 | * @throws IOException If an I/O error has occurred. 35 | */ 36 | void onFinished(Stream stream) throws IOException; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/injector/SimpleHttpInjector.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.injector; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.http.HttpBody; 21 | import com.github.megatronking.netbare.http.HttpRequest; 22 | import com.github.megatronking.netbare.http.HttpRequestHeaderPart; 23 | import com.github.megatronking.netbare.http.HttpResponse; 24 | import com.github.megatronking.netbare.http.HttpResponseHeaderPart; 25 | 26 | import java.io.IOException; 27 | 28 | /** 29 | * Convenience class for simple injectors and allows extending only methods that are necessary. 30 | * 31 | * @author Megatron King 32 | * @since 2019/1/2 20:55 33 | */ 34 | public abstract class SimpleHttpInjector implements HttpInjector { 35 | 36 | @Override 37 | public boolean sniffRequest(@NonNull HttpRequest request) { 38 | return false; 39 | } 40 | 41 | @Override 42 | public boolean sniffResponse(@NonNull HttpResponse response) { 43 | return false; 44 | } 45 | 46 | @Override 47 | public void onRequestInject(@NonNull HttpRequestHeaderPart header, 48 | @NonNull InjectorCallback callback) throws IOException { 49 | callback.onFinished(header); 50 | } 51 | 52 | @Override 53 | public void onResponseInject(@NonNull HttpResponseHeaderPart header, 54 | @NonNull InjectorCallback callback) throws IOException { 55 | callback.onFinished(header); 56 | } 57 | 58 | @Override 59 | public void onRequestInject(@NonNull HttpRequest request, @NonNull HttpBody body, 60 | @NonNull InjectorCallback callback) throws IOException { 61 | callback.onFinished(body); 62 | } 63 | 64 | @Override 65 | public void onResponseInject(@NonNull HttpResponse response, @NonNull HttpBody body, 66 | @NonNull InjectorCallback callback) throws IOException { 67 | callback.onFinished(body); 68 | } 69 | 70 | @Override 71 | public void onRequestFinished(@NonNull HttpRequest request) { 72 | } 73 | 74 | @Override 75 | public void onResponseFinished(@NonNull HttpResponse response) { 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/io/ByteBufferInputStream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.io; 17 | 18 | import java.io.ByteArrayInputStream; 19 | import java.nio.ByteBuffer; 20 | 21 | /** 22 | * A {@link ByteArrayInputStream} constructs by a {@link ByteBuffer}. 23 | * 24 | * @author Megatron King 25 | * @since 2018-12-23 15:24 26 | */ 27 | public class ByteBufferInputStream extends ByteArrayInputStream { 28 | 29 | /** 30 | * Constructs a new {@link ByteArrayInputStream} by a {@link ByteBuffer}. 31 | * 32 | * @param buffer A buffer has remaining data. 33 | */ 34 | public ByteBufferInputStream(ByteBuffer buffer) { 35 | super(buffer.array(), buffer.position(), buffer.remaining()); 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/io/HttpBodyInputStream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.io; 17 | 18 | import com.github.megatronking.netbare.http.HttpBody; 19 | 20 | import java.io.ByteArrayInputStream; 21 | 22 | /** 23 | * A {@link ByteArrayInputStream} constructs by a {@link HttpBody}. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-23 15:24 27 | */ 28 | public class HttpBodyInputStream extends ByteBufferInputStream { 29 | 30 | /** 31 | * Constructs a new {@link ByteArrayInputStream} by a {@link HttpBody}. 32 | * 33 | * @param body A HTTP body stream. 34 | */ 35 | public HttpBodyInputStream(HttpBody body) { 36 | super(body.toBuffer()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/stream/BufferStream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.stream; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | /** 23 | * A {@link ByteBuffer} stream data, the buffer will be output with nothing changes. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-23 11:15 27 | */ 28 | public class BufferStream implements Stream { 29 | 30 | private ByteBuffer mBuffer; 31 | 32 | /** 33 | * Constructs a {@link ByteStream} by a {@link ByteBuffer}. 34 | * 35 | * @param buffer A byte buffer. 36 | */ 37 | public BufferStream(@NonNull ByteBuffer buffer) { 38 | this.mBuffer = buffer; 39 | } 40 | 41 | @NonNull 42 | @Override 43 | public ByteBuffer toBuffer() { 44 | return mBuffer; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/stream/ByteStream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.stream; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | /** 23 | * A stream creates by a byte array. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-23 11:15 27 | */ 28 | public class ByteStream implements Stream { 29 | 30 | private final ByteBuffer mBuffer; 31 | 32 | /** 33 | * Constructs a new stream by a byte array. 34 | * 35 | * @param bytes The bytes to be wrapped into {@link ByteBuffer}. 36 | */ 37 | public ByteStream(byte[] bytes) { 38 | this(bytes, 0, bytes.length); 39 | } 40 | 41 | /** 42 | * Constructs a new stream by a byte array. 43 | * 44 | * @param bytes The bytes to be wrapped into {@link ByteBuffer}. 45 | * @param offset The index of the first byte in array. 46 | * @param length The number of bytes. 47 | */ 48 | public ByteStream(byte[] bytes, int offset, int length) { 49 | // Do not use wrap. 50 | ByteBuffer buffer = ByteBuffer.allocate(length); 51 | buffer.put(bytes, offset, length); 52 | buffer.flip(); 53 | this.mBuffer = buffer; 54 | } 55 | 56 | @NonNull 57 | @Override 58 | public ByteBuffer toBuffer() { 59 | return mBuffer; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/stream/Stream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.stream; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | /** 23 | * Stream is special data type using in NetBare. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-15 21:05 27 | */ 28 | public interface Stream { 29 | 30 | /** 31 | * Converts the stream data type to {@link ByteBuffer}. 32 | * 33 | * @return A {@link ByteBuffer}. 34 | */ 35 | @NonNull 36 | ByteBuffer toBuffer(); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/stream/StringStream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.stream; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import java.nio.ByteBuffer; 21 | 22 | /** 23 | * A stream creates by a string. 24 | * 25 | * @author Megatron King 26 | * @since 2018-12-23 11:30 27 | */ 28 | public class StringStream extends ByteStream { 29 | 30 | /** 31 | * Constructs a new stream by a string. 32 | * 33 | * @param string The string to be wrapped into {@link ByteBuffer}. 34 | */ 35 | public StringStream(@NonNull String string) { 36 | super(string.getBytes()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /netbare-injector/src/main/java/com/github/megatronking/netbare/stream/TinyFileStream.java: -------------------------------------------------------------------------------- 1 | /* NetBare - An android network capture and injection library. 2 | * Copyright (C) 2018-2019 Megatron King 3 | * Copyright (C) 2018-2019 GuoShi 4 | * 5 | * NetBare is free software: you can redistribute it and/or modify it under the terms 6 | * of the GNU General Public License as published by the Free Software Found- 7 | * ation, either version 3 of the License, or (at your option) any later version. 8 | * 9 | * NetBare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 10 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 | * PURPOSE. See the GNU General Public License for more details. 12 | * 13 | * You should have received a copy of the GNU General Public License along with NetBare. 14 | * If not, see . 15 | */ 16 | package com.github.megatronking.netbare.stream; 17 | 18 | import android.support.annotation.NonNull; 19 | 20 | import com.github.megatronking.netbare.NetBareUtils; 21 | 22 | import java.io.File; 23 | import java.io.FileInputStream; 24 | import java.io.IOException; 25 | import java.nio.ByteBuffer; 26 | import java.nio.channels.FileChannel; 27 | 28 | /** 29 | * A tiny file stream, the file will be loaded into memory, the file size should be less than 30 | * {@link #MAX_FILE_LENGTH}. 31 | * 32 | * @author Megatron King 33 | * @since 2018-12-23 11:39 34 | */ 35 | public class TinyFileStream implements Stream { 36 | 37 | /** 38 | * The max file size of this stream supports. 39 | */ 40 | public static final int MAX_FILE_LENGTH = 64 * 1024; 41 | 42 | private final File mFile; 43 | 44 | /** 45 | * Constructs a stream by a tiny file. 46 | * 47 | * @param file A tiny file. 48 | * @throws IOException 49 | */ 50 | public TinyFileStream(@NonNull File file) throws IOException { 51 | if (file.length() > MAX_FILE_LENGTH) { 52 | throw new IOException("Only support file size < " + MAX_FILE_LENGTH); 53 | } 54 | this.mFile = file; 55 | } 56 | 57 | @NonNull 58 | @Override 59 | public ByteBuffer toBuffer() { 60 | ByteBuffer byteBuffer; 61 | FileInputStream fis = null; 62 | FileChannel channel = null; 63 | try { 64 | fis = new FileInputStream(mFile); 65 | channel = fis.getChannel(); 66 | byteBuffer = ByteBuffer.allocate(fis.available()); 67 | channel.read(byteBuffer); 68 | byteBuffer.flip(); 69 | } catch (IOException e) { 70 | byteBuffer = ByteBuffer.allocate(0); 71 | } finally { 72 | NetBareUtils.closeQuietly(channel); 73 | NetBareUtils.closeQuietly(fis); 74 | } 75 | return byteBuffer; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /netbare-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /netbare-sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion 28 6 | defaultConfig { 7 | applicationId "com.github.megatronking.netbare.sample" 8 | minSdkVersion 21 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | sourceSets { 21 | main.java.srcDirs += 'src/main/kotlin' 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'com.android.support:appcompat-v7:28.0.0' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 32 | implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.11' 33 | 34 | implementation 'com.google.code.gson:gson:2.8.2' 35 | implementation 'me.weishu:free_reflection:1.2.0' 36 | 37 | // NetBare libraries 38 | implementation project(':netbare-core') 39 | implementation project(':netbare-injector') 40 | } 41 | repositories { 42 | mavenCentral() 43 | } 44 | -------------------------------------------------------------------------------- /netbare-sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /netbare-sample/src/androidTest/java/com/github/megatronking/netbare/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.github.megatronking.netbare", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /netbare-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /netbare-sample/src/main/kotlin/com/github/megatronking/netbare/sample/App.kt: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.sample 2 | 3 | import android.app.Application 4 | import android.content.Context 5 | import com.github.megatronking.netbare.NetBare 6 | import com.github.megatronking.netbare.NetBareUtils 7 | import com.github.megatronking.netbare.ssl.JKS 8 | import me.weishu.reflection.Reflection 9 | 10 | class App : Application() { 11 | 12 | companion object { 13 | const val JSK_ALIAS = "NetBareSample" 14 | 15 | private lateinit var sInstance: App 16 | 17 | fun getInstance(): App { 18 | return sInstance 19 | } 20 | } 21 | 22 | private lateinit var mJKS : JKS 23 | 24 | override fun onCreate() { 25 | super.onCreate() 26 | sInstance = this 27 | // 创建自签证书 28 | mJKS = JKS(this, JSK_ALIAS, JSK_ALIAS.toCharArray(), JSK_ALIAS,JSK_ALIAS, 29 | JSK_ALIAS, JSK_ALIAS, JSK_ALIAS) 30 | 31 | // 初始化NetBare 32 | NetBare.get().attachApplication(this, BuildConfig.DEBUG) 33 | } 34 | 35 | fun getJSK(): JKS { 36 | return mJKS 37 | } 38 | 39 | override fun attachBaseContext(base: Context?) { 40 | super.attachBaseContext(base) 41 | // On android Q, we can't access Java8EngineWrapper with reflect. 42 | if (NetBareUtils.isAndroidQ()) { 43 | Reflection.unseal(base) 44 | } 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /netbare-sample/src/main/kotlin/com/github/megatronking/netbare/sample/AppService.kt: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.sample 2 | 3 | import android.app.Notification 4 | import android.app.NotificationChannel 5 | import android.app.NotificationManager 6 | import android.app.PendingIntent 7 | import android.content.Context 8 | import android.content.Intent 9 | import android.graphics.BitmapFactory 10 | import android.os.Build 11 | import android.support.v4.app.NotificationCompat 12 | import com.github.megatronking.netbare.NetBareService 13 | 14 | class AppService : NetBareService() { 15 | 16 | companion object { 17 | 18 | private const val CHANNEL_ID = "com.github.megatronking.netbare.sample.NOTIFICATION_CHANNEL_ID" 19 | 20 | } 21 | 22 | override fun onCreate() { 23 | super.onCreate() 24 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 25 | val notificationManager = 26 | getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 27 | if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) { 28 | notificationManager.createNotificationChannel(NotificationChannel(CHANNEL_ID, 29 | getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW)) 30 | } 31 | } 32 | } 33 | 34 | override fun notificationId(): Int { 35 | return 100 36 | } 37 | 38 | override fun createNotification(): Notification { 39 | val intent = Intent(this, MainActivity::class.java) 40 | intent.addCategory(Intent.CATEGORY_LAUNCHER) 41 | intent.action = Intent.ACTION_MAIN 42 | val pendingIntent = PendingIntent.getActivity(this, 0, intent, 43 | PendingIntent.FLAG_UPDATE_CURRENT) 44 | return NotificationCompat.Builder(this, CHANNEL_ID) 45 | .setSmallIcon(R.drawable.netbare_notification) 46 | .setContentTitle(getString(R.string.app_name)) 47 | .setContentText(getString(R.string.app_name)) 48 | .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)) 49 | .setOngoing(true) 50 | .setContentIntent(pendingIntent) 51 | .build() 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /netbare-sample/src/main/kotlin/com/github/megatronking/netbare/sample/BaiduLogoInjector.kt: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.sample 2 | 3 | import android.util.Log 4 | import com.github.megatronking.netbare.http.HttpBody 5 | import com.github.megatronking.netbare.http.HttpResponse 6 | import com.github.megatronking.netbare.http.HttpResponseHeaderPart 7 | import com.github.megatronking.netbare.injector.InjectorCallback 8 | import com.github.megatronking.netbare.injector.SimpleHttpInjector 9 | import com.github.megatronking.netbare.stream.BufferStream 10 | import java.nio.ByteBuffer 11 | 12 | /** 13 | * 注入器范例1:替换百度首页logo 14 | * 15 | * 启动NetBare服务后,用浏览器App打开百度首页,Logo将会被替换成此sample项目raw目录下的图片。 16 | * 注意:如果浏览器有图片缓存,记得先把缓存清理掉! 17 | * 18 | * @author Megatron King 19 | * @since 2018/12/30 00:18 20 | */ 21 | class BaiduLogoInjector : SimpleHttpInjector() { 22 | 23 | companion object { 24 | const val TAG = "BaiduLogoInjector" 25 | } 26 | 27 | override fun sniffResponse(response: HttpResponse): Boolean { 28 | // 请求url匹配时才进行注入 29 | val shouldInject = "https://m.baidu.com/static/index/plus/plus_logo.png".equals( 30 | response.url()) 31 | if (shouldInject) { 32 | Log.i(TAG, "Start Miss. Du logo injection!") 33 | } 34 | return shouldInject 35 | } 36 | 37 | override fun onResponseInject(header: HttpResponseHeaderPart, callback: InjectorCallback) { 38 | // 响应体大小变化,一定要先更新header中的content-length 39 | val newHeader = header.newBuilder() 40 | .replaceHeader("content-length", "10764") 41 | .build() 42 | callback.onFinished(newHeader) 43 | Log.i(TAG, "Inject header completed!") 44 | } 45 | 46 | override fun onResponseInject(response: HttpResponse, body: HttpBody, callback: InjectorCallback) { 47 | // 替换图片请求响应体 48 | val injectIOStream = App.getInstance().resources.openRawResource(R.raw.baidu_inject_logo) 49 | val injectStream = BufferStream(ByteBuffer.wrap(injectIOStream.readBytes())) 50 | injectIOStream.close() 51 | callback.onFinished(injectStream) 52 | Log.i(TAG, "Inject body completed!") 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /netbare-sample/src/main/kotlin/com/github/megatronking/netbare/sample/HttpUrlPrintInterceptor.kt: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.sample 2 | 3 | import android.util.Log 4 | import com.github.megatronking.netbare.http.HttpIndexedInterceptor 5 | import com.github.megatronking.netbare.http.HttpInterceptorFactory 6 | import com.github.megatronking.netbare.http.HttpRequestChain 7 | import com.github.megatronking.netbare.http.HttpResponseChain 8 | import java.nio.ByteBuffer 9 | 10 | /** 11 | * 拦截器反例1:打印请求url 12 | * 13 | * @author Megatron King 14 | * @since 2019/1/2 22:05 15 | */ 16 | class HttpUrlPrintInterceptor : HttpIndexedInterceptor() { 17 | 18 | companion object { 19 | const val TAG = "URL" 20 | 21 | fun createFactory(): HttpInterceptorFactory { 22 | return HttpInterceptorFactory { HttpUrlPrintInterceptor() } 23 | } 24 | } 25 | 26 | override fun intercept(chain: HttpRequestChain, buffer: ByteBuffer, index: Int) { 27 | if (index == 0) { 28 | // 一个请求可能会有多个数据包,故此方法会多次触发,这里只在收到第一个包的时候打印 29 | Log.i(TAG, "Request: " + chain.request().url()) 30 | } 31 | // 调用process将数据发射给下一个拦截器,否则数据将不会发给服务器 32 | chain.process(buffer) 33 | } 34 | 35 | override fun intercept(chain: HttpResponseChain, buffer: ByteBuffer, index: Int) { 36 | chain.process(buffer) 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /netbare-sample/src/main/kotlin/com/github/megatronking/netbare/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.megatronking.netbare.sample 2 | 3 | import android.app.Activity 4 | import android.content.Intent 5 | import android.os.Bundle 6 | import android.support.v7.app.AppCompatActivity 7 | import android.widget.Button 8 | import com.github.megatronking.netbare.NetBare 9 | import com.github.megatronking.netbare.NetBareConfig 10 | import com.github.megatronking.netbare.NetBareListener 11 | import com.github.megatronking.netbare.http.HttpInjectInterceptor 12 | import com.github.megatronking.netbare.http.HttpInterceptorFactory 13 | import com.github.megatronking.netbare.ssl.JKS 14 | import java.io.IOException 15 | 16 | class MainActivity : AppCompatActivity(), NetBareListener { 17 | 18 | companion object { 19 | private const val REQUEST_CODE_PREPARE = 1 20 | } 21 | 22 | private lateinit var mNetBare : NetBare 23 | 24 | private lateinit var mActionButton : Button 25 | 26 | override fun onCreate(savedInstanceState: Bundle?) { 27 | super.onCreate(savedInstanceState) 28 | setContentView(R.layout.activity_main) 29 | 30 | mNetBare = NetBare.get() 31 | 32 | mActionButton = findViewById(R.id.action) 33 | mActionButton.setOnClickListener { 34 | if (mNetBare.isActive) { 35 | mNetBare.stop() 36 | } else{ 37 | prepareNetBare() 38 | } 39 | } 40 | 41 | // 监听NetBare服务的启动和停止 42 | mNetBare.registerNetBareListener(this) 43 | } 44 | 45 | override fun onDestroy() { 46 | super.onDestroy() 47 | mNetBare.unregisterNetBareListener(this) 48 | mNetBare.stop() 49 | } 50 | 51 | override fun onServiceStarted() { 52 | runOnUiThread { 53 | mActionButton.setText(R.string.stop_netbare) 54 | } 55 | } 56 | 57 | override fun onServiceStopped() { 58 | runOnUiThread { 59 | mActionButton.setText(R.string.start_netbare) 60 | } 61 | } 62 | 63 | private fun prepareNetBare() { 64 | // 安装自签证书 65 | if (!JKS.isInstalled(this, App.JSK_ALIAS)) { 66 | try { 67 | JKS.install(this, App.JSK_ALIAS, App.JSK_ALIAS) 68 | } catch(e : IOException) { 69 | // 安装失败 70 | } 71 | return 72 | } 73 | // 配置VPN 74 | val intent = NetBare.get().prepare() 75 | if (intent != null) { 76 | startActivityForResult(intent, REQUEST_CODE_PREPARE) 77 | return 78 | } 79 | // 启动NetBare服务 80 | mNetBare.start(NetBareConfig.defaultHttpConfig(App.getInstance().getJSK(), 81 | interceptorFactories())) 82 | } 83 | 84 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 85 | super.onActivityResult(requestCode, resultCode, data) 86 | if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_PREPARE) { 87 | prepareNetBare() 88 | } 89 | } 90 | 91 | private fun interceptorFactories() : List { 92 | // 拦截器范例1:打印请求url 93 | val interceptor1 = HttpUrlPrintInterceptor.createFactory() 94 | // 注入器范例1:替换百度首页logo 95 | val injector1 = HttpInjectInterceptor.createFactory(BaiduLogoInjector()) 96 | // 注入器范例2:修改发朋友圈定位 97 | val injector2 = HttpInjectInterceptor.createFactory(WechatLocationInjector()) 98 | // 可以添加其它的拦截器,注入器 99 | // ... 100 | return listOf(interceptor1, injector1, injector2) 101 | } 102 | 103 | 104 | } 105 | -------------------------------------------------------------------------------- /netbare-sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /netbare-sample/src/main/res/drawable-xhdpi/netbare_notification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/netbare-sample/src/main/res/drawable-xhdpi/netbare_notification.png -------------------------------------------------------------------------------- /netbare-sample/src/main/res/drawable-xxhdpi/netbare_notification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/netbare-sample/src/main/res/drawable-xxhdpi/netbare_notification.png -------------------------------------------------------------------------------- /netbare-sample/src/main/res/drawable-xxxhdpi/netbare_notification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MegatronKing/NetBare-Android/df5b417ea663763932cabf1ef52f15b09ddc2e16/netbare-sample/src/main/res/drawable-xxxhdpi/netbare_notification.png -------------------------------------------------------------------------------- /netbare-sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 |