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/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/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/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/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/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/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/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 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':netbare-core', ':netbare-injector'
2 |
--------------------------------------------------------------------------------