OCT 1. 增加初始化SCG服务功能

2. 重新选择HTTP库,解决C++ HTTP请求报错问题
This commit is contained in:
黄昕 2023-10-19 18:08:07 +08:00
parent 1dd2cbbfa1
commit 2258e3e315
15 changed files with 708 additions and 9888 deletions

View File

@ -1,10 +1,25 @@
<component name="InspectionProjectProfileManager"> <component name="InspectionProjectProfileManager">
<profile version="1.0"> <profile version="1.0">
<option name="myName" value="Project Default" /> <option name="myName" value="Project Default" />
<inspection_tool class="ArrayIndexOutOfBounds" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ConstantConditionsOC" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ConstantFunctionResult" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ConstantParameter" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="DanglingPointer" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="EndlessLoop" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="InfiniteRecursion" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="LocalValueEscapesScope" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="LoopDoesntUseConditionVariable" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="NullDereference" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false"> <inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" /> <option name="processCode" value="true" />
<option name="processLiterals" value="true" /> <option name="processLiterals" value="true" />
<option name="processComments" value="true" /> <option name="processComments" value="true" />
</inspection_tool> </inspection_tool>
<inspection_tool class="UnreachableCallsOfFunction" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="UnreachableCode" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="UnusedLocalVariable" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="UnusedParameter" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="UnusedValue" enabled="false" level="WARNING" enabled_by_default="false" />
</profile> </profile>
</component> </component>

View File

@ -14,6 +14,10 @@ android {
versionName = "1.0" versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk {
//abiFilters.add("armeabi-v7a")
}
} }
buildTypes { buildTypes {

View File

@ -3,6 +3,10 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"

View File

@ -5,6 +5,8 @@
# Sets the minimum CMake version required for this project. # Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1) cmake_minimum_required(VERSION 3.22.1)
OPTION(SCG_ENABLE "Enable SCG Proxy Service" OFF)
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME}, # Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible # Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level # with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
@ -13,6 +15,7 @@ project("sccproxy")
INCLUDE_DIRECTORIES(include ./) INCLUDE_DIRECTORIES(include ./)
FILE(GLOB C_HEADS ./include/*.h ./cJSON/cJSON.h) FILE(GLOB C_HEADS ./include/*.h ./cJSON/cJSON.h)
ADD_DEFINITIONS(-D_LINUX)
# Creates and names a library, sets it as either STATIC # Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code. # or SHARED, and provides the relative paths to its source code.
@ -27,14 +30,25 @@ FILE(GLOB C_HEADS ./include/*.h ./cJSON/cJSON.h)
# System.loadLibrary() and pass the name of the library defined here; # System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be # for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file. # used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED ADD_LIBRARY(${CMAKE_PROJECT_NAME} SHARED
# List C/C++ source files with relative paths to this CMakeLists.txt. # List C/C++ source files with relative paths to this CMakeLists.txt.
native-lib.cpp srcs/ipcalc.cpp srcs/scc-service.cpp srcs/tunnel-proxy.cpp cJSON/cJSON.c ${C_HEADS}) native-lib.cpp http/http-client.cpp srcs/ipcalc.cpp srcs/scc-service.cpp srcs/tunnel-proxy.cpp cJSON/cJSON.c ${C_HEADS})
MESSAGE(STATUS "SRC DIRECTORY: ${CMAKE_SOURCE_DIR}")
# Specifies libraries CMake should link to your target library. You # Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this # can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries. # build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME} IF (SCG_ENABLE)
# List libraries link to the target library ADD_DEFINITIONS(-DSCG_ON)
android TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}
log) # List libraries link to the target library
android
log
${CMAKE_SOURCE_DIR}/scg/libjwae.a)
ELSE()
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}
# List libraries link to the target library
android
log)
ENDIF ()

View File

@ -0,0 +1,267 @@
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <cstring>
#include "log.h"
#include "http-client.h"
#include "sccproxy.h"
#include "scg-service.h"
#define MY_HTTP_DEFAULT_PORT 80
#define BUFFER_SIZE 1024
#define HTTP_POST "POST /%s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n"\
"Content-Type:application/json\r\nContent-Length: %d\r\n\r\n%s"
#define HTTP_GET "GET /%s HTTP/1.1\r\nHOST: %s:%d\r\nAccept: */*\r\n\r\n"
static void send_scg_head(int sock, int vmId) {
int ret;
unsigned char vmid[4];
unsigned char *p;
const unsigned int id = htonl(vmId);
const auto svrId = static_cast<unsigned char>(WG_CTRL_SCG_ID);
unsigned char scgProxy[] = {0x01, // VERSION
0x09, // Length
0xF0, // ++++++ INFO[0] TYPE
0x04, // INFO[0] LENGTH
0, // INFO[0] VMID[0]
0, // INFO[0] VMID[1]
0, // INFO[0] VMID[2]
0, // INFO[0] VMID[3]
0xF1, // INFO[1] TYPE
0x01, // INFO[1] LENGTH
svrId}; // ------ INFO[1] SCG Service ID
p = scgProxy;
memcpy(vmid, &id, 4);
scgProxy[4] = vmid[0];
scgProxy[5] = vmid[1];
scgProxy[6] = vmid[2];
scgProxy[7] = vmid[3];
ret = send(sock, reinterpret_cast<const char *>(p), sizeof(scgProxy), 0);
if (ret != sizeof(scgProxy)) {
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Service Connected To SCG Server(vmid = %d, SCGAppId = %d) error: %d\n",
vmId, WG_CTRL_SCG_ID, ret);
}
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "Service Connected To SCG Server:vmid = %d, SCGAppId = %d: %d\n",
vmId, WG_CTRL_SCG_ID, sock);
}
static int http_tcpclient_create(const char *host, int port) {
struct hostent *he;
struct sockaddr_in server_addr {};
int socket_fd;
if ((he = gethostbyname(host)) == nullptr) {
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *) he->h_addr);
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
return -1;
}
if (connect(socket_fd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == -1) {
return -1;
}
//if(get_scg_cur_vmid() > 0) {
send_scg_head(socket_fd, get_scg_cur_vmid());
//}
return socket_fd;
}
static void http_tcpclient_close(int socket) {
close(socket);
}
static int http_parse_url(const char *url, char *host, char *file, int *port) {
char *ptr1, *ptr2;
int len = 0;
if (!url || !host || !file || !port) {
return -1;
}
ptr1 = (char *) url;
if (!strncmp(ptr1, "http://", strlen("http://"))) {
ptr1 += strlen("http://");
} else {
return -1;
}
ptr2 = strchr(ptr1, '/');
if (ptr2) {
len = strlen(ptr1) - strlen(ptr2);
memcpy(host, ptr1, len);
host[len] = '\0';
if (*(ptr2 + 1)) {
memcpy(file, ptr2 + 1, strlen(ptr2) - 1);
file[strlen(ptr2) - 1] = '\0';
}
} else {
memcpy(host, ptr1, strlen(ptr1));
host[strlen(ptr1)] = '\0';
}
//get host and ip
ptr1 = strchr(host, ':');
if (ptr1) {
*ptr1++ = '\0';
*port = atoi(ptr1);
} else {
*port = MY_HTTP_DEFAULT_PORT;
}
return 0;
}
static int http_tcpclient_recv(int socket, char *lpbuff) {
int recvnum = 0;
recvnum = recv(socket, lpbuff, BUFFER_SIZE * 4, 0);
return recvnum;
}
static int http_tcpclient_send(int socket, char *buff, int size) {
int sent = 0, tmpres = 0;
while (sent < size) {
tmpres = send(socket, buff + sent, size - sent, 0);
if (tmpres == -1) {
return -1;
}
sent += tmpres;
}
return sent;
}
static char *http_parse_result(const char *lpbuf) {
char *ptmp = nullptr;
char *response = nullptr;
ptmp = (char *) strstr(lpbuf, "HTTP/1.1");
if (!ptmp) {
printf("http/1.1 not faind\n");
return nullptr;
}
if (atoi(ptmp + 9) != 200) {
printf("result:\n%s\n", lpbuf);
return nullptr;
}
ptmp = (char *) strstr(lpbuf, "\r\n\r\n");
if (!ptmp) {
printf("ptmp is nullptr\n");
return nullptr;
}
response = (char *) malloc(strlen(ptmp) + 1);
if (!response) {
printf("malloc failed \n");
return nullptr;
}
strcpy(response, ptmp + 4);
return response;
}
char *http_post(const char *url, const char *post_str) {
char post[BUFFER_SIZE] = {'\0'};
int socket_fd = -1;
char lpbuf[BUFFER_SIZE * 4] = {'\0'};
char *ptmp;
char host_addr[BUFFER_SIZE] = {'\0'};
char file[BUFFER_SIZE] = {'\0'};
int port = 0;
int len = 0;
char *response = nullptr;
if (!url || !post_str) {
printf(" failed!\n");
return nullptr;
}
if (http_parse_url(url, host_addr, file, &port)) {
printf("http_parse_url failed!\n");
return nullptr;
}
//printf("host_addr : %s\tfile:%s\t,%d\n",host_addr,file,port);
socket_fd = http_tcpclient_create(host_addr, port);
if (socket_fd < 0) {
printf("http_tcpclient_create failed\n");
return nullptr;
}
sprintf(lpbuf, HTTP_POST, file, host_addr, port, static_cast<int>(strlen(post_str)), post_str);
if (http_tcpclient_send(socket_fd, lpbuf, static_cast<int>(strlen(lpbuf))) < 0) {
printf("http_tcpclient_send failed..\n");
return nullptr;
}
//printf("发送请求:\n%s\n",lpbuf);
/*it's time to recv from server*/
if (http_tcpclient_recv(socket_fd, lpbuf) <= 0) {
printf("http_tcpclient_recv failed\n");
return nullptr;
}
http_tcpclient_close(socket_fd);
return http_parse_result(lpbuf);
}
char *http_get(const char *url) {
char post[BUFFER_SIZE] = {'\0'};
int socket_fd = -1;
char lpbuf[BUFFER_SIZE * 4] = {'\0'};
char *ptmp;
char host_addr[BUFFER_SIZE] = {'\0'};
char file[BUFFER_SIZE] = {'\0'};
int port = 0;
int len = 0;
if (!url) {
printf(" failed!\n");
return nullptr;
}
if (http_parse_url(url, host_addr, file, &port)) {
printf("http_parse_url failed!\n");
return nullptr;
}
//printf("host_addr : %s\tfile:%s\t,%d\n",host_addr,file,port);
socket_fd = http_tcpclient_create(host_addr, port);
if (socket_fd < 0) {
printf("http_tcpclient_create failed\n");
return nullptr;
}
sprintf(lpbuf, HTTP_GET, file, host_addr, port);
if (http_tcpclient_send(socket_fd, lpbuf, static_cast<int>(strlen(lpbuf))) < 0) {
printf("http_tcpclient_send failed..\n");
return nullptr;
}
// printf("发送请求:\n%s\n",lpbuf);
if (http_tcpclient_recv(socket_fd, lpbuf) <= 0) {
printf("http_tcpclient_recv failed\n");
return nullptr;
}
http_tcpclient_close(socket_fd);
return http_parse_result(lpbuf);
}

View File

@ -0,0 +1,5 @@
#pragma once
char * http_get(const char *url);
char * http_post(const char *url,const char * post_str);

File diff suppressed because it is too large Load Diff

View File

@ -38,11 +38,16 @@ extern "C" {
int init_scgproxy_service(const char *pSCGIpAddr, int scgPort); int init_scgproxy_service(const char *pSCGIpAddr, int scgPort);
/** /**
* @brief * @brief
* @param[in] vmId VMID * @param[in] vmId VMID
*/
void remote_connect_vm_service(int vmId);
/**
* @brief
* @return 0: 0 @see USER_ERRNO * @return 0: 0 @see USER_ERRNO
*/ */
int remote_tunnel_service_start(int vmId); int remote_tunnel_service_start();
/** /**
* @brief * @brief

View File

@ -4,17 +4,51 @@
#include "sccproxy.h" #include "sccproxy.h"
#include "log.h" #include "log.h"
#include "usrerr.h" #include "usrerr.h"
#include "./scg/jwae.h"
#ifdef SCG_ON
static void connect_scg_server() {
//1、设置token
jwae_set_token("token1234");
//2、init scg服务
StartConfig start_config;
start_config.mode = 2; //0:tcp 1:udp 2:tcp and udp
start_config.server_ip = "112.17.28.215";
start_config.client_ip = "127.0.0.1";
start_config.path = "/sdcard/";
start_config.auth_type = 2;
start_config.server_port = 10800;
start_config.client_port = 10800;
start_config.encrypt = 0;
int ret = jwae_start(&start_config);
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "connect_scg_server: %d\n", ret);
}
#endif
extern "C" JNIEXPORT jstring JNICALL extern "C" JNIEXPORT jstring JNICALL
Java_com_example_sccproxy_MainActivity_stringFromJNI( Java_com_example_sccproxy_MainActivity_stringFromJNI(
JNIEnv *env, JNIEnv *env,
jobject /* this */) { jobject /* this */) {
int ret; int ret;
int vmId = 123; int vmId = 61;
char svrTunnelNetwork[256]; char svrTunnelNetwork[256];
unsigned short tunnelProxyPort = 0; unsigned short tunnelProxyPort = 0;
std::string hello = "Hello from C++"; std::string hello = "Hello from C++";
#ifdef SCG_ON
connect_scg_server();
#endif
// struct http_response *hresp = http_get("http://xajhuang.com:32400", "User-agent:MyUserAgent\r\n");
//
// if(hresp == nullptr) {
// ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "http_get error\n");
// }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// 系统初始化, 程序启动时调用一次 // 系统初始化, 程序启动时调用一次
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -38,9 +72,13 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
goto error; goto error;
} }
#if 1
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// 隧道控制,每次启动/停止 SCC 服务时调用 // 隧道控制,每次启动/停止 SCC 服务时调用
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// SCC Service Step 0. 设置客户端 SCC 服务需要连接虚拟机
remote_connect_vm_service(vmId);
// SCC Service Step 1. 客户端获取隧道配置并连接到虚拟机后,首先要对虚拟机里面的隧道进行配置 // SCC Service Step 1. 客户端获取隧道配置并连接到虚拟机后,首先要对虚拟机里面的隧道进行配置
/** /**
{ {
@ -90,7 +128,7 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
*/ */
// SCC Service Step 3. 启动虚拟机内服务端隧道 // SCC Service Step 3. 启动虚拟机内服务端隧道
ret = remote_tunnel_service_start(vmId); ret = remote_tunnel_service_start();
if (ret == ERR_SUCCESS) { if (ret == ERR_SUCCESS) {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "remote_tunnel_service_start: SUCCESSED, Current Connect VMID: %d\n", vmId); ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "remote_tunnel_service_start: SUCCESSED, Current Connect VMID: %d\n", vmId);
} else { } else {
@ -134,7 +172,7 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
// UnInit step 4. 停止客户端 Wireguard 隧道 UDP 代理服务 // UnInit step 4. 停止客户端 Wireguard 隧道 UDP 代理服务
stop_wireguard_proxy_server(); stop_wireguard_proxy_server();
#endif
error: error:
return env->NewStringUTF(hello.c_str()); return env->NewStringUTF(hello.c_str());
} }

View File

@ -0,0 +1,80 @@
{
"ver": "0.3.2",
"log": {
"level": "info"
},
"monitor_server": {
"addr": "127.0.0.1",
"port": 12000
},
"local_setting": {
"spec": {
"max_rule_cache_size": 10000,
"max_tcp_session_size": 10000,
"max_udp_session_size": 10000
},
"connection_pool": {
"connection_pool_size": 1,
"new_connection_period": 10,
"choose_connection_method": "random"
},
"session": {
"tcp_session_timeout": 259200,
"udp_session_timeout": 259200
},
"xe_init": {
"mtu": 1450,
"sender_slidwnd_size": 40000,
"receiver_slidwnd_size": 40000,
"syn_queue_size": 512,
"nodelay": false,
"handshake_timeout": 5,
"retry_handshake_num": 5,
"ack_loop_period": 5000,
"conn_info_loop_period":200000,
"encrypt": false
},
"keepalive": {
"keepalive_timeout": 30,
"heartbeat_period": 6
},
"drc": {
"drc_enable": false,
"drc_time_wnd": 100,
"drc_count_max": 10,
"drc_loss_rate": 0.0001
},
"rate": {
"cycle": 500,
"loop_period": 200,
"sin_a": 0.96,
"sin_b": 0.53
},
"stream_options": [
{
"port": 0,
"protocol": "tcp",
"ordering": true,
"retransmit": true,
"congestion_ctl": true
},
{
"port": 0,
"protocol": "udp",
"ordering": false,
"retransmit": true,
"retransmit_timeout": 300,
"congestion_ctl": false
}
]
},
"inbounds": [
{
"tag": "redirect_socket1",
"listening_address": "0.0.0.0",
"listening_port": 10800,
"role": "redirect_socket",
"next": "aac1"
}
]
}

View File

@ -0,0 +1,47 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct StartConfig {
unsigned int mode;
unsigned int auth_type;
const char *server_ip;
unsigned int server_port;
const char *client_ip;
unsigned int client_port;
const char *path;
unsigned int encrypt;
} StartConfig;
typedef struct ConnInfo {
unsigned long long send_rate;
unsigned long long recv_rate;
unsigned long long rtt_mean;
float loss_rate;
} ConnInfo;
int jwae_start(struct StartConfig *config);
int jwae_stop(void);
void jwae_set_user_passwd(const char *username, const char *password);
void jwae_set_token(const char *token);
int jwae_set_log(bool log_conf_flag,
const char *level,
const char *path,
uint64_t size,
uint32_t count);
int32_t jwae_get_errno(int32_t *errno, char *err_buff, uint32_t buff_len);
int jwae_get_connection_info(struct ConnInfo *conn_info);
#ifdef __cplusplus
}
#endif

Binary file not shown.

View File

@ -1,20 +1,25 @@
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include "usrerr.h" #include "usrerr.h"
#include "httplib.h"
#include "log.h" #include "log.h"
#include "sccproxy.h" #include "sccproxy.h"
#include "ipcalc.h" #include "ipcalc.h"
#include "scg-service.h" #include "scg-service.h"
#include "../cJSON/cJSON.h" #include "../cJSON/cJSON.h"
#include "../http/http-client.h"
#define SET_CLIENTCFG_PATH ("/tunnel/setconfig") #define SET_CLIENTCFG_PATH ("/tunnel/setconfig")
#define SET_CLIENTSTART_TUNNEL ("/tunnel/start") #define SET_CLIENTSTART_TUNNEL ("/tunnel/start")
typedef struct { typedef struct {
char scgProxyIpAddr[MAX_PATH]; char scgProxyUrl[MAX_PATH];
int scgProxyPort; char scgProxyIpAddr[MAX_PATH];
int curConnVmId; int scgProxyPort;
int isEnable; int curConnVmId;
httplib::Client *g_tunCtrlCtx; int isEnable;
//httplib::Client *g_tunCtrlCtx;
} SCGPROXY_CONFIG, *PSCGPROXY_CONFIG; } SCGPROXY_CONFIG, *PSCGPROXY_CONFIG;
static SCGPROXY_CONFIG g_scgProxyCfg; static SCGPROXY_CONFIG g_scgProxyCfg;
@ -40,7 +45,6 @@ unsigned short get_scg_proxy_port() {
int init_scgproxy_service(const char *pSCGIpAddr, int scgPort) { int init_scgproxy_service(const char *pSCGIpAddr, int scgPort) {
IP_INFO ipInfo; IP_INFO ipInfo;
int ret; int ret;
char scgProxyUrl[MAX_PATH * 4];
memset(&g_scgProxyCfg, 0, sizeof(SCGPROXY_CONFIG)); memset(&g_scgProxyCfg, 0, sizeof(SCGPROXY_CONFIG));
@ -54,29 +58,36 @@ int init_scgproxy_service(const char *pSCGIpAddr, int scgPort) {
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Enable SCG Proxy: %s:%d, Tunnel Control VMID = %d, Tunnel Data VMID = %d \n", ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Enable SCG Proxy: %s:%d, Tunnel Control APPID = %d, Tunnel Data APPID = %d \n",
pSCGIpAddr, scgPort, WG_CTRL_SCG_ID, WG_TUNNEL_SCG_ID); pSCGIpAddr, scgPort, WG_CTRL_SCG_ID, WG_TUNNEL_SCG_ID);
if ((ret = get_ipv4_info_from_hostname(pSCGIpAddr, &ipInfo)) != ERR_SUCCESS) { if ((ret = get_ipv4_info_from_hostname(pSCGIpAddr, &ipInfo)) != ERR_SUCCESS) {
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "get_ipv4_info_from_hostname error: %d\n", ret); ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "get_ipv4_info_from_hostname error: %d\n", ret);
return ret; return ret;
} }
#if 0
httplib::Client* cli = new httplib::Client("http://xajhuang.com:32400");
auto res = cli->Get("/");
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "GET: %d\n", res->status);
#endif
strncpy(g_scgProxyCfg.scgProxyIpAddr, ipInfo.hostip, MAX_PATH); strncpy(g_scgProxyCfg.scgProxyIpAddr, ipInfo.hostip, MAX_PATH);
g_scgProxyCfg.scgProxyPort = scgPort; g_scgProxyCfg.scgProxyPort = scgPort;
memset(scgProxyUrl, 0, MAX_PATH * 4); memset(g_scgProxyCfg.scgProxyUrl, 0, MAX_PATH);
snprintf(scgProxyUrl, MAX_PATH * 4 - 1, "http://%s:%d", g_scgProxyCfg.scgProxyIpAddr, g_scgProxyCfg.scgProxyPort); snprintf(g_scgProxyCfg.scgProxyUrl, MAX_PATH, "http://%s:%d", g_scgProxyCfg.scgProxyIpAddr, g_scgProxyCfg.scgProxyPort);
g_scgProxyCfg.g_tunCtrlCtx = new httplib::Client(scgProxyUrl);
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "SCC Service Redirect To SCG Proxy Service: %s\n", g_scgProxyCfg.scgProxyUrl);
#if 0
g_scgProxyCfg.g_tunCtrlCtx = new httplib::Client(scgProxyUrl);
if (g_scgProxyCfg.g_tunCtrlCtx == nullptr) { if (g_scgProxyCfg.g_tunCtrlCtx == nullptr) {
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Create HTTP Proxy error: %s\n", scgProxyUrl); ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Create HTTP Proxy error: %s\n", scgProxyUrl);
return -ERR_SYSTEM_UNINITIALIZE; return -ERR_SYSTEM_UNINITIALIZE;
} else { } else {
g_scgProxyCfg.g_tunCtrlCtx->set_connection_timeout(0, 1000000); // 1 second g_scgProxyCfg.g_tunCtrlCtx->set_connection_timeout(5, 1000000); // 1 second
g_scgProxyCfg.g_tunCtrlCtx->set_read_timeout(5, 0); // 5 seconds g_scgProxyCfg.g_tunCtrlCtx->set_read_timeout(5, 0); // 5 seconds
g_scgProxyCfg.g_tunCtrlCtx->set_write_timeout(5, 0); // 5 seconds g_scgProxyCfg.g_tunCtrlCtx->set_write_timeout(5, 0); // 5 seconds
g_scgProxyCfg.g_tunCtrlCtx->set_keep_alive(true); //g_scgProxyCfg.g_tunCtrlCtx->set_keep_alive(false);
g_scgProxyCfg.g_tunCtrlCtx->set_post_connect_cb([](socket_t sock) { g_scgProxyCfg.g_tunCtrlCtx->set_post_connect_cb([](socket_t sock) {
int ret; int ret;
unsigned char vmid[4]; unsigned char vmid[4];
@ -113,13 +124,20 @@ int init_scgproxy_service(const char *pSCGIpAddr, int scgPort) {
g_scgProxyCfg.curConnVmId, WG_CTRL_SCG_ID, sock); g_scgProxyCfg.curConnVmId, WG_CTRL_SCG_ID, sock);
}); });
} }
#endif
g_scgProxyCfg.isEnable = true; g_scgProxyCfg.isEnable = true;
return ERR_SUCCESS; return ERR_SUCCESS;
} }
static int remote_tunnel_service_control(int vmId, bool start) { static int remote_tunnel_service_control(int vmId, bool start) {
char *pJsonString;
cJSON *pRoot;
cJSON *pMsgContent;
cJSON *rspCode;
char url[1024];
char *pSvrResp;
if (vmId < 0) { if (vmId < 0) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Input pSCGIpAddr params error: %d\n", vmId); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Input pSCGIpAddr params error: %d\n", vmId);
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
@ -130,95 +148,79 @@ static int remote_tunnel_service_control(int vmId, bool start) {
return -ERR_SYSTEM_UNINITIALIZE; return -ERR_SYSTEM_UNINITIALIZE;
} }
if (g_scgProxyCfg.g_tunCtrlCtx == nullptr) { g_scgProxyCfg.curConnVmId = vmId;
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Server Control Service don't connected(g_tunnelHttpCtx is not initialize).");
return -ERR_SYSTEM_UNINITIALIZE;
} else {
char *pJsonString;
cJSON *pRoot;
cJSON *pMsgContent;
cJSON *rspCode;
httplib::Result res;
g_scgProxyCfg.curConnVmId = vmId; pRoot = cJSON_CreateObject();
pRoot = cJSON_CreateObject(); if (pRoot == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Create JSON Error.\n");
if (pRoot == nullptr) { return -ERR_JSON_CREATE;
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Create JSON Error.\n");
return -ERR_JSON_CREATE;
}
cJSON_AddNumberToObject(pRoot, "ver", 1);
cJSON_AddNumberToObject(pRoot, "cryptoType", 0);
cJSON_AddNumberToObject(pRoot, "timeStamp", time(nullptr));
pMsgContent = cJSON_CreateObject();
if (pMsgContent == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Create JSON Error.\n");
cJSON_Delete(pRoot);
return -ERR_JSON_CREATE;
}
cJSON_AddItemToObject(pRoot, "msgContent", pMsgContent);
cJSON_AddBoolToObject(pMsgContent, "isStart", start);
pJsonString = cJSON_PrintUnformatted(pRoot);
cJSON_Delete(pRoot);
res = g_scgProxyCfg.g_tunCtrlCtx->Post(SET_CLIENTSTART_TUNNEL, pJsonString, "application/json");
if (res.error() != httplib::Error::Success) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s error: %s\n", SET_CLIENTSTART_TUNNEL, pJsonString,
httplib::to_string(res.error()).c_str());
free(pJsonString);
return -ERR_HTTP_POST_DATA;
}
if (res->status != 200) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s server return HTTP error: %d\n", SET_CLIENTSTART_TUNNEL,
pJsonString,
res->status);
free(pJsonString);
return -ERR_HTTP_SERVER_RSP;
}
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "+++++ Http Request %s\n---- Http Response %s\n", pJsonString, res->body.c_str());
free(pJsonString);
if (strlen(res->body.c_str()) == 0) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Server response empty message.\n");
return -ERR_READ_FILE;
}
pRoot = cJSON_Parse(res->body.c_str());
if (pRoot == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode JSON %s Error.\n", res->body.c_str());
return -ERR_JSON_DECODE;
}
pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
if (pMsgContent == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent) JSON %s Error.\n", res->body.c_str());
cJSON_Delete(pRoot);
return -ERR_JSON_DECODE;
}
rspCode = cJSON_GetObjectItem(pMsgContent, "errCode");
if (rspCode == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent.rspCode) JSON %s Error.\n", res->body.c_str());
cJSON_Delete(pRoot);
return -ERR_JSON_DECODE;
}
cJSON_Delete(pRoot);
return rspCode->valueint;
} }
cJSON_AddNumberToObject(pRoot, "ver", 1);
cJSON_AddNumberToObject(pRoot, "cryptoType", 0);
cJSON_AddNumberToObject(pRoot, "timeStamp", time(nullptr));
pMsgContent = cJSON_CreateObject();
if (pMsgContent == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Create JSON Error.\n");
cJSON_Delete(pRoot);
return -ERR_JSON_CREATE;
}
cJSON_AddItemToObject(pRoot, "msgContent", pMsgContent);
cJSON_AddBoolToObject(pMsgContent, "isStart", start);
pJsonString = cJSON_PrintUnformatted(pRoot);
cJSON_Delete(pRoot);
memset(url, 0, 1024);
snprintf(url, 1024, "%s%s", g_scgProxyCfg.scgProxyUrl, SET_CLIENTSTART_TUNNEL);
pSvrResp = http_post(url, pJsonString);
// res = g_scgProxyCfg.g_tunCtrlCtx->Post(SET_CLIENTSTART_TUNNEL, pJsonString, "application/json");
if (pSvrResp == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s error\n", SET_CLIENTSTART_TUNNEL, pJsonString);
free(pJsonString);
return -ERR_HTTP_POST_DATA;
}
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "+++++ Http Request %s\n---- Http Response %s\n", pJsonString, pSvrResp);
free(pJsonString);
if (strlen(pSvrResp) == 0) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Server response empty message.\n");
return -ERR_READ_FILE;
}
pRoot = cJSON_Parse(pSvrResp);
if (pRoot == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode JSON %s Error.\n", pSvrResp);
return -ERR_JSON_DECODE;
}
pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
if (pMsgContent == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent) JSON %s Error.\n", pSvrResp);
cJSON_Delete(pRoot);
return -ERR_JSON_DECODE;
}
rspCode = cJSON_GetObjectItem(pMsgContent, "errCode");
if (rspCode == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent.rspCode) JSON %s Error.\n", pSvrResp);
cJSON_Delete(pRoot);
return -ERR_JSON_DECODE;
}
cJSON_Delete(pRoot);
return rspCode->valueint;
} }
/** /**
@ -231,11 +233,19 @@ int remote_tunnel_service_stop() {
/** /**
* @brief * @brief
* @param[in] vmId VMID
* @return 0: 0 @see USER_ERRNO * @return 0: 0 @see USER_ERRNO
*/ */
int remote_tunnel_service_start(int vmId) { int remote_tunnel_service_start() {
return remote_tunnel_service_control(vmId, true); return remote_tunnel_service_control(g_scgProxyCfg.curConnVmId, true);
}
/**
* @brief
* @param[in] vmId VMID
*/
void remote_connect_vm_service(int vmId) {
g_scgProxyCfg.curConnVmId = vmId;
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Current Connect VM Id: %d\n", vmId);
} }
/** /**
@ -252,7 +262,8 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
cJSON *pMsgContent; cJSON *pMsgContent;
cJSON *pRspCode; cJSON *pRspCode;
cJSON *pSvrNet; cJSON *pSvrNet;
httplib::Result res; char url[1024];
char *pSvrResp;
if (pCliPubKey == nullptr || strlen(pCliPubKey) == 0) { if (pCliPubKey == nullptr || strlen(pCliPubKey) == 0) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Input pCliPubKey params error: %s\n", pCliPubKey ? pCliPubKey : "NULL String"); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Input pCliPubKey params error: %s\n", pCliPubKey ? pCliPubKey : "NULL String");
@ -265,7 +276,8 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
} }
if (pCliTunNetwork == nullptr || strlen(pCliTunNetwork) == 0) { if (pCliTunNetwork == nullptr || strlen(pCliTunNetwork) == 0) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Input pCliTunNetwork params error: %s\n", pCliTunNetwork ? pCliTunNetwork : "NULL String"); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Input pCliTunNetwork params error: %s\n",
pCliTunNetwork ? pCliTunNetwork : "NULL String");
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
@ -309,42 +321,50 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
pJsonString = cJSON_PrintUnformatted(pRoot); pJsonString = cJSON_PrintUnformatted(pRoot);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
res = g_scgProxyCfg.g_tunCtrlCtx->Post(SET_CLIENTCFG_PATH, pJsonString, "application/json"); memset(url, 0, 1024);
snprintf(url, 1024, "%s%s", g_scgProxyCfg.scgProxyUrl, SET_CLIENTCFG_PATH);
pSvrResp = http_post(url, pJsonString);
if (res.error() != httplib::Error::Success) { #if 0
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s error: %s\n", SET_CLIENTCFG_PATH, pJsonString, memset(url, 0, 1024);
httplib::to_string(res.error()).c_str()); //sprintf(url, "%s", g_scgProxyCfg.)
snprintf(url, 1024, "http://%s:%d%s", g_scgProxyCfg.scgProxyIpAddr, g_scgProxyCfg.scgProxyPort, SET_CLIENTCFG_PATH);
char *resp = http_post(url, pJsonString);
if (resp == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s recv NULL\n", SET_CLIENTCFG_PATH, pJsonString);
return ERR_HTTP_POST_DATA;
} else {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "[%s]:Post Data %s recv: %s\n", SET_CLIENTCFG_PATH, pJsonString, resp);
return ERR_HTTP_POST_DATA;
}
res = g_scgProxyCfg.g_tunCtrlCtx->Post(SET_CLIENTCFG_PATH, pJsonString, "application/json");
#endif
if (pSvrResp == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s error", SET_CLIENTCFG_PATH, pJsonString);
free(pJsonString); free(pJsonString);
return -ERR_HTTP_POST_DATA; return -ERR_HTTP_POST_DATA;
} }
if (res->status != 200) { ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "+++++ Http Request %s\n---- Http Response %s\n", pJsonString, pSvrResp);
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s server return HTTP error: %d\n", SET_CLIENTCFG_PATH,
pJsonString,
res->status);
free(pJsonString);
return -ERR_HTTP_SERVER_RSP;
}
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "+++++ Http Request %s\n---- Http Response %s\n", pJsonString, res->body.c_str());
free(pJsonString); free(pJsonString);
if (strlen(res->body.c_str()) == 0) { if (strlen(pSvrResp) == 0) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Server response empty message.\n"); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Server response empty message.\n");
return -ERR_READ_FILE; return -ERR_READ_FILE;
} }
pRoot = cJSON_Parse(res->body.c_str()); pRoot = cJSON_Parse(pSvrResp);
if (pRoot == nullptr) { if (pRoot == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode JSON %s Error.\n", res->body.c_str()); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode JSON %s Error.\n", pSvrResp);
return -ERR_JSON_DECODE; return -ERR_JSON_DECODE;
} }
pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent"); pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
if (pMsgContent == nullptr) { if (pMsgContent == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent) JSON %s Error.\n", res->body.c_str()); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent) JSON %s Error.\n", pSvrResp);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return -ERR_JSON_DECODE; return -ERR_JSON_DECODE;
} }
@ -352,7 +372,7 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
pRspCode = cJSON_GetObjectItem(pMsgContent, "errCode"); pRspCode = cJSON_GetObjectItem(pMsgContent, "errCode");
if (pRspCode == nullptr) { if (pRspCode == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent.pRspCode) JSON %s Error.\n", res->body.c_str()); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent.pRspCode) JSON %s Error.\n", pSvrResp);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return -ERR_JSON_DECODE; return -ERR_JSON_DECODE;
} }
@ -365,7 +385,7 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
pSvrNet = cJSON_GetObjectItem(pMsgContent, "svrNetwork"); pSvrNet = cJSON_GetObjectItem(pMsgContent, "svrNetwork");
if (pSvrNet == nullptr) { if (pSvrNet == nullptr) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent.pSvrNet) JSON %s Error.\n", res->body.c_str()); ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "Decode(msgContent.pSvrNet) JSON %s Error.\n", pSvrResp);
cJSON_Delete(pRoot); cJSON_Delete(pRoot);
return -ERR_JSON_DECODE; return -ERR_JSON_DECODE;
} }

View File

@ -34,13 +34,11 @@ static SCG_PROXY_INFO g_scgProxyInfo;
static int pthread_setcancelstate() { static int pthread_setcancelstate() {
sigset_t newState, old; sigset_t newState, old;
int ret;
sigemptyset(&newState); sigemptyset(&newState);
sigaddset(&newState, SIG_CANCEL_SIGNAL); sigaddset(&newState, SIG_CANCEL_SIGNAL);
ret = pthread_sigmask(SIG_BLOCK, &newState, &old); return pthread_sigmask(SIG_BLOCK, &newState, &old);
return ret;
} }
/** /**

View File

@ -1,14 +1,26 @@
package com.example.sccproxy; package com.example.sccproxy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.widget.TextView; import android.widget.TextView;
import android.Manifest;
import com.example.sccproxy.databinding.ActivityMainBinding; import com.example.sccproxy.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1024;
// Used to load the 'sccproxy' library on application startup. // Used to load the 'sccproxy' library on application startup.
static { static {
System.loadLibrary("sccproxy"); System.loadLibrary("sccproxy");
@ -23,11 +35,68 @@ public class MainActivity extends AppCompatActivity {
binding = ActivityMainBinding.inflate(getLayoutInflater()); binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot()); setContentView(binding.getRoot());
requestPermission();
// Example of a call to a native method // Example of a call to a native method
TextView tv = binding.sampleText; TextView tv = binding.sampleText;
tv.setText(stringFromJNI()); tv.setText(stringFromJNI());
} }
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// 先判断有没有权限
if (Environment.isExternalStorageManager()) {
writeFile();
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 先判断有没有权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
writeFile();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
} else {
writeFile();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
writeFile();
} else {
//ToastUtils.show("存储权限获取失败");
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
writeFile();
} else {
//ToastUtils.show("存储权限获取失败");
}
}
}
/**
* 模拟文件写入
*/
private void writeFile() {
//ToastUtils.show("写入文件成功");
}
/** /**
* A native method that is implemented by the 'sccproxy' native library, * A native method that is implemented by the 'sccproxy' native library,
* which is packaged with this application. * which is packaged with this application.