OCT 1. SCC 所有业务联调通过

This commit is contained in:
黄昕 2023-10-30 17:38:47 +08:00
parent dd25cb57e4
commit 8fa76172f1
13 changed files with 2059 additions and 31 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="192.168.10.245:5555" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-10-30T09:08:42.548922300Z" />
</component>
</project>

View File

@ -16,7 +16,7 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters.add("armeabi-v7a")
//abiFilters.add("armeabi-v7a")
}
}

View File

@ -11,9 +11,10 @@ cmake_minimum_required(VERSION 3.22.1)
# build script scope).
project("sccproxy")
INCLUDE_DIRECTORIES(include ./)
FILE(GLOB C_HEADS ./include/*.h ./cJSON/cJSON.h)
INCLUDE_DIRECTORIES(include include ./)
FILE(GLOB C_HEADS ./include/*.h ./cJSON/cJSON.h ./include/sds/*.h)
ADD_DEFINITIONS(-D_LINUX)
ADD_DEFINITIONS(-DUSED_DIRECT_PROXY=0)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
@ -30,14 +31,14 @@ ADD_DEFINITIONS(-D_LINUX)
# used in the AndroidManifest.xml file.
ADD_LIBRARY(${CMAKE_PROJECT_NAME} SHARED
# 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 srcs/ipcalc.cpp srcs/log.cpp srcs/sds.c 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
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
IF (TRUE)
IF (FALSE)
ADD_DEFINITIONS(-DSCG_ON)
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}
# List libraries link to the target library

View File

@ -6,3 +6,4 @@
#include <android/log.h>
#define ANDROID_LOG(level, tag, format, args...) (__android_log_print(level, tag, "[%s(%d)] %s:" format, basename(__FILE__), __LINE__, __FUNCTION__, ##args))
char* format_memory_log(const unsigned char* pData, unsigned int nBytes);

View File

@ -3,6 +3,8 @@
//
#pragma once
#define ENABLE_UDP_PROXY_DEBUG (0)
int get_scg_cur_vmid();
const char *get_scg_proxy_ipaddr();
unsigned short get_scg_proxy_port();

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef HIREDIS_ALLOC_H
#define HIREDIS_ALLOC_H
#include <stddef.h> /* for size_t */
#ifdef __cplusplus
extern "C" {
#endif
/* Structure pointing to our actually configured allocators */
typedef struct hiredisAllocFuncs {
void *(*mallocFn)(size_t);
void *(*callocFn)(size_t, size_t);
void *(*reallocFn)(void *, size_t);
char *(*strdupFn)(const char *);
void (*freeFn)(void *);
} hiredisAllocFuncs;
hiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *ha);
void hiredisResetAllocators(void);
#ifndef _WIN32
/* Hiredis' configured allocator function pointer struct */
extern hiredisAllocFuncs hiredisAllocFns;
static inline void *hi_malloc(size_t size) {
return hiredisAllocFns.mallocFn(size);
}
static inline void *hi_calloc(size_t nmemb, size_t size) {
return hiredisAllocFns.callocFn(nmemb, size);
}
static inline void *hi_realloc(void *ptr, size_t size) {
return hiredisAllocFns.reallocFn(ptr, size);
}
static inline char *hi_strdup(const char *str) {
return hiredisAllocFns.strdupFn(str);
}
static inline void hi_free(void *ptr) {
hiredisAllocFns.freeFn(ptr);
}
#else
void *hi_malloc(size_t size);
void *hi_calloc(size_t nmemb, size_t size);
void *hi_realloc(void *ptr, size_t size);
char *hi_strdup(const char *str);
void hi_free(void *ptr);
#endif
#ifdef __cplusplus
}
#endif
#endif /* HIREDIS_ALLOC_H */

View File

@ -0,0 +1,283 @@
/* SDSLib 2.0 -- A C dynamic strings library
*
* Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Oran Agra
* Copyright (c) 2015, Redis Labs, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __SDS_H
#define __SDS_H
#ifdef __cplusplus
extern "C" {
#endif
#define SDS_MAX_PREALLOC (1024 * 1024)
#ifdef _MSC_VER
#define __attribute__(x)
typedef long long ssize_t;
#define SSIZE_MAX (LLONG_MAX >> 1)
#endif
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
typedef char *sds;
/* Note: sdshdr5 is never used, we just access the flags byte directly.
* However is here to document the layout of type 5 SDS strings. */
struct __attribute__((__packed__)) sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[];
};
struct __attribute__((__packed__)) sdshdr8 {
uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__((__packed__)) sdshdr16 {
uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__((__packed__)) sdshdr32 {
uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
struct __attribute__((__packed__)) sdshdr64 {
uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
};
#define SDS_TYPE_5 0
#define SDS_TYPE_8 1
#define SDS_TYPE_16 2
#define SDS_TYPE_32 3
#define SDS_TYPE_64 4
#define SDS_TYPE_MASK 7
#define SDS_TYPE_BITS 3
#define SDS_HDR_VAR(T, s) struct sdshdr##T *sh = (struct sdshdr##T *)((s) - (sizeof(struct sdshdr##T)));
#define SDS_HDR(T, s) ((struct sdshdr##T *)((s) - (sizeof(struct sdshdr##T))))
#define SDS_TYPE_5_LEN(f) ((f) >> SDS_TYPE_BITS)
static inline size_t sdslen(const sds s) {
unsigned char flags = s[-1];
switch (flags & SDS_TYPE_MASK) {
case SDS_TYPE_5:
return SDS_TYPE_5_LEN(flags);
case SDS_TYPE_8:
return SDS_HDR(8, s)->len;
case SDS_TYPE_16:
return SDS_HDR(16, s)->len;
case SDS_TYPE_32:
return SDS_HDR(32, s)->len;
case SDS_TYPE_64:
return SDS_HDR(64, s)->len;
}
return 0;
}
static inline size_t sdsavail(const sds s) {
unsigned char flags = s[-1];
switch (flags & SDS_TYPE_MASK) {
case SDS_TYPE_5: {
return 0;
}
case SDS_TYPE_8: {
SDS_HDR_VAR(8, s);
return sh->alloc - sh->len;
}
case SDS_TYPE_16: {
SDS_HDR_VAR(16, s);
return sh->alloc - sh->len;
}
case SDS_TYPE_32: {
SDS_HDR_VAR(32, s);
return sh->alloc - sh->len;
}
case SDS_TYPE_64: {
SDS_HDR_VAR(64, s);
return sh->alloc - sh->len;
}
}
return 0;
}
static inline void sdssetlen(sds s, size_t newlen) {
unsigned char flags = s[-1];
switch (flags & SDS_TYPE_MASK) {
case SDS_TYPE_5: {
unsigned char *fp = ((unsigned char *)s) - 1;
*fp = (unsigned char)(SDS_TYPE_5 | (newlen << SDS_TYPE_BITS));
} break;
case SDS_TYPE_8:
SDS_HDR(8, s)->len = (uint8_t)newlen;
break;
case SDS_TYPE_16:
SDS_HDR(16, s)->len = (uint16_t)newlen;
break;
case SDS_TYPE_32:
SDS_HDR(32, s)->len = (uint32_t)newlen;
break;
case SDS_TYPE_64:
SDS_HDR(64, s)->len = (uint64_t)newlen;
break;
}
}
static inline void sdsinclen(sds s, size_t inc) {
unsigned char flags = s[-1];
switch (flags & SDS_TYPE_MASK) {
case SDS_TYPE_5: {
unsigned char *fp = ((unsigned char *)s) - 1;
unsigned char newlen = SDS_TYPE_5_LEN(flags) + (unsigned char)inc;
*fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);
} break;
case SDS_TYPE_8:
SDS_HDR(8, s)->len += (uint8_t)inc;
break;
case SDS_TYPE_16:
SDS_HDR(16, s)->len += (uint16_t)inc;
break;
case SDS_TYPE_32:
SDS_HDR(32, s)->len += (uint32_t)inc;
break;
case SDS_TYPE_64:
SDS_HDR(64, s)->len += (uint64_t)inc;
break;
}
}
/* sdsalloc() = sdsavail() + sdslen() */
static inline size_t sdsalloc(const sds s) {
unsigned char flags = s[-1];
switch (flags & SDS_TYPE_MASK) {
case SDS_TYPE_5:
return SDS_TYPE_5_LEN(flags);
case SDS_TYPE_8:
return SDS_HDR(8, s)->alloc;
case SDS_TYPE_16:
return SDS_HDR(16, s)->alloc;
case SDS_TYPE_32:
return SDS_HDR(32, s)->alloc;
case SDS_TYPE_64:
return SDS_HDR(64, s)->alloc;
}
return 0;
}
static inline void sdssetalloc(sds s, size_t newlen) {
unsigned char flags = s[-1];
switch (flags & SDS_TYPE_MASK) {
case SDS_TYPE_5:
/* Nothing to do, this type has no total allocation info. */
break;
case SDS_TYPE_8:
SDS_HDR(8, s)->alloc = (uint8_t)newlen;
break;
case SDS_TYPE_16:
SDS_HDR(16, s)->alloc = (uint16_t)newlen;
break;
case SDS_TYPE_32:
SDS_HDR(32, s)->alloc = (uint32_t)newlen;
break;
case SDS_TYPE_64:
SDS_HDR(64, s)->alloc = (uint64_t)newlen;
break;
}
}
sds sdsnewlen(const void *init, size_t initlen);
sds sdsnew(const char *init);
sds sdsempty(void);
sds sdsdup(const sds s);
void sdsfree(sds s);
sds sdsgrowzero(sds s, size_t len);
sds sdscatlen(sds s, const void *t, size_t len);
sds sdscat(sds s, const char *t);
sds sdscatsds(sds s, const sds t);
sds sdscpylen(sds s, const char *t, size_t len);
sds sdscpy(sds s, const char *t);
sds sdscatvprintf(sds s, const char *fmt, va_list ap);
#ifdef __GNUC__
sds sdscatprintf(sds s, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
#else
sds sdscatprintf(sds s, const char *fmt, ...);
#endif
sds sdscatfmt(sds s, char const *fmt, ...);
sds sdstrim(sds s, const char *cset);
int sdsrange(sds s, ssize_t start, ssize_t end);
void sdsupdatelen(sds s);
void sdsclear(sds s);
int sdscmp(const sds s1, const sds s2);
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
void sdsfreesplitres(sds *tokens, int count);
void sdstolower(sds s);
void sdstoupper(sds s);
sds sdsfromlonglong(long long value);
sds sdscatrepr(sds s, const char *p, size_t len);
sds *sdssplitargs(const char *line, int *argc);
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
sds sdsjoin(char **argv, int argc, char *sep);
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
/* Low level functions exposed to the user API */
sds sdsMakeRoomFor(sds s, size_t addlen);
void sdsIncrLen(sds s, int incr);
sds sdsRemoveFreeSpace(sds s);
size_t sdsAllocSize(sds s);
void *sdsAllocPtr(sds s);
/* Export the allocator used by SDS to the program using SDS.
* Sometimes the program SDS is linked to, may use a different set of
* allocators, but may want to allocate or free things that SDS will
* respectively free or allocate. */
void *sds_malloc(size_t size);
void *sds_realloc(void *ptr, size_t size);
void sds_free(void *ptr);
#ifdef REDIS_TEST
int sdsTest(int argc, char *argv[]);
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,44 @@
/* SDSLib 2.0 -- A C dynamic strings library
*
* Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Oran Agra
* Copyright (c) 2015, Redis Labs, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* SDS allocator selection.
*
* This file is used in order to change the SDS allocator at compile time.
* Just define the following defines to what you want to use. Also add
* the include of your alternate allocator if needed (not needed in order
* to use the default libc allocator). */
#include "alloc.h"
#define s_malloc hi_malloc
#define s_realloc hi_realloc
#define s_free hi_free

View File

@ -7,17 +7,27 @@
#include "./scg/jwae.h"
#ifdef SCG_ON
static void connect_scg_server() {
//1、设置token
jwae_set_token("token1234");
#if 0
jwae_set_token(
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiIxNzE3NzUwNjk2OTY2MTYwMzg0Iiwic2NvcGUiOlsiYWxsIl0s"
"InVzZXJUeXBlIjoidXNlciIsImV4cCI6MTcwMTIzODEzNywiYXV0aG9yaXRpZXMiOlsiUk9MRV9SRVRBSU58LTM2NjQiXSwianRpIjoiSS"
"1sOThUQ1Bja2thNjlLbkduNWxGOFZQSXNBIiwiYWNjb3VudCI6IjE4ODY3MTIwOTYzIiwiY2xpZW50X2lkIjoic2MtdmRpc2RrLXZleHow"
"dW16azAybnFhbGEifQ.h25B8fy_IqKB1F2WJaldHNP2pQ0KypNl1WJOtubWZGp6m7s7pdyJMlmaeV5K61SXPTXtr7Ed53HvZ5Bl__0iLhjp"
"RUDstoTMR5ZHeee7l_Bzs_d2dlCPZeJpFoFpj99UQnkP1QQYZaLflk723OIhIlbw7U5CqEJunwekeGanKI2PfkBnCjKOiO6jDqZ0Cw3Up5EY"
"2N7hBkn4rn98bmKWsRdIxlvVMY1dGVNu5kBY1nQv--X88Wqt7qeAvaVN_k1fCxPAK73TlN_xpklR1GA8xllDAyEMaR5a-oSsz2NWQiJeFYXy"
"tuyJVPxRE8oOwZVVrmy4SXFwWPJ6Ny4N8fyJZw");
#endif
//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.path = "/sdcard/";
start_config.auth_type = 2;
start_config.server_port = 10800;
@ -27,13 +37,14 @@ static void connect_scg_server() {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "connect_scg_server: %d\n", ret);
}
#endif
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_sccproxy_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
int ret;
int vmId = 61;
int vmId = 258;
char svrTunnelNetwork[256];
unsigned short tunnelProxyPort = 0;
@ -52,7 +63,12 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
// 系统初始化, 程序启动时调用一次
//-----------------------------------------------------------------------
// Init step 1. 初始化 SCG 代理服务
#if USED_DIRECT_PROXY
ret = init_scgproxy_service("101.35.234.160", 10010);
#else
ret = init_scgproxy_service("127.0.0.1", 10800);
#endif
if (ret == ERR_SUCCESS) {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "init_scgproxy_service: SUCCESSED\n");
} else {
@ -71,7 +87,7 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
goto error;
}
#if 1
#if 0
//-----------------------------------------------------------------------
// 隧道控制,每次启动/停止 SCC 服务时调用
//-----------------------------------------------------------------------
@ -103,7 +119,7 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
"message": "操作成功"
}
*/
ret = remote_tunnel_set_params("fcosdnZe7PQjkjpFNNZUxv9Dtl36XubAj6d6cDU6R34=", "192.168.100.0/24", "10.10.10.0/24", svrTunnelNetwork);
ret = remote_tunnel_set_params("C3JlasgsO4TUYS1XF+i8LnER0RE0s9O6cDUJhjD5Hhc=", "192.168.10.0/24", "10.10.10.0/24", svrTunnelNetwork);
if (ret == ERR_SUCCESS) {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "remote_tunnel_set_params: SUCCESSED, Server Tunnel Network: %s\n", svrTunnelNetwork);
@ -140,7 +156,7 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
// SCC Service Step 5. 配置客户端 iptables 规则,启动 SCC 内网转发
// TODO
#if 0
// SCC Service Step 6. 端口虚拟机,停止服务端隧道
ret = remote_tunnel_service_stop();
if (ret == ERR_SUCCESS) {
@ -171,6 +187,7 @@ Java_com_example_sccproxy_MainActivity_stringFromJNI(
// UnInit step 4. 停止客户端 Wireguard 隧道 UDP 代理服务
stop_wireguard_proxy_server();
#endif
#endif
error:
return env->NewStringUTF(hello.c_str());

View File

@ -0,0 +1,55 @@
//
// Created by HuangXin on 2023/10/27.
//
#include <cstdlib>
#include <cstring>
#include <cctype>
#include "sds/sds.h"
#include "log.h"
char *format_memory_log(const unsigned char *pData, unsigned int nBytes) {
int i, j;
unsigned int nLen;
unsigned int nMemSize;
if (nBytes > 1500) {
nBytes = 1500;
}
nLen = (nBytes + 15) / 16;
nMemSize = nLen * 74;
sds s = sdsnew("\n");
int nOffset = 0;
int nPos = 0;
for (i = 0; i < nLen; i++) {
char ascii[18] = {0};
s = sdscatprintf(s, "%04X ", nOffset);
for (j = 0; j < 16 && nPos < nBytes; j++) {
nPos = i * 16 + j;
s = sdscatprintf(s, "%02X ", pData[nPos]);
}
s = sdscat(s, " ");
for (j = 0; j < 16 && nPos < nBytes; j++) {
nPos = i * 16 + j;
if (isprint(pData[nPos])) {
s = sdscatprintf(s, "%c", pData[nPos]);
} else {
s = sdscat(s, ".");
}
}
s = sdscat(s, "\n");
nOffset += 16;
}
char *p = strdup(s);
sdsfree(s);
return p;
}

View File

@ -82,6 +82,9 @@ int init_scgproxy_service(const char *pSCGIpAddr, int scgPort) {
IP_INFO ipInfo;
int ret;
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "libsccproxy(%s) library information: (Build: %s %s GCC Ver:%s) With %u(bits) OS\n",
ENABLE_UDP_PROXY_DEBUG ? "DEBUG" : "RELEASE", __DATE__, __TIME__, __VERSION__, (unsigned int)(sizeof(int *) * 8));
memset(&g_scgProxyCfg, 0, sizeof(SCGPROXY_CONFIG));
if (pSCGIpAddr == nullptr || strlen(pSCGIpAddr) == 0 || strlen(pSCGIpAddr) >= MAX_IPV4_LEN) {
@ -213,13 +216,18 @@ static int remote_tunnel_service_control(int vmId, bool start) {
memset(url, 0, 1024);
snprintf(url, 1024, "%s%s", g_scgProxyCfg.scgProxyUrl, SET_CLIENTSTART_TUNNEL);
resp = sion::Request()
.SetUrl(url)
.SetHttpMethod(sion::Method::Post)
.SetHeader("Content-type", "application/json")
.SetBody(pJsonString)
.SetConnectedCb(http_connected_cb)
.Send();
try {
resp = sion::Request()
.SetUrl(url)
.SetHttpMethod(sion::Method::Post)
.SetHeader("Content-type", "application/json")
.SetBody(pJsonString)
.SetConnectedCb(http_connected_cb)
.Send();
} catch (std::exception &e) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s Exception: %s\n", SET_CLIENTCFG_PATH, pJsonString, e.what());
return -ERR_HTTP_POST_DATA;
}
if (resp.Status() != "OK") {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s error: %s\n", SET_CLIENTCFG_PATH, pJsonString, resp.Status().c_str());
@ -236,7 +244,6 @@ static int remote_tunnel_service_control(int vmId, bool start) {
}
ANDROID_LOG(ANDROID_LOG_DEBUG, "SCGPROXY", "+++++ Http Request %s\n---- Http Response %s\n", pJsonString, resp.StrBody().c_str());
free(pJsonString);
if (strlen(resp.Code().c_str()) == 0) {
@ -364,7 +371,7 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
cJSON_AddItemToObject(pRoot, "msgContent", pMsgContent);
cJSON_AddStringToObject(pMsgContent, "cliPublicKey", pCliPubKey);
cJSON_AddStringToObject(pMsgContent, "pCliNetwork", pCliNetwork);
cJSON_AddStringToObject(pMsgContent, "cliNetwork", pCliNetwork);
cJSON_AddStringToObject(pMsgContent, "cliTunnelAddr", pCliTunNetwork);
pJsonString = cJSON_PrintUnformatted(pRoot);
@ -373,13 +380,18 @@ int remote_tunnel_set_params(const char *pCliPubKey, const char *pCliNetwork, co
memset(url, 0, 1024);
snprintf(url, 1024, "%s%s", g_scgProxyCfg.scgProxyUrl, SET_CLIENTCFG_PATH);
resp = sion::Request()
.SetUrl(url)
.SetHttpMethod(sion::Method::Post)
.SetHeader("Content-type", "application/json")
.SetBody(pJsonString)
.SetConnectedCb(http_connected_cb)
.Send();
try {
resp = sion::Request()
.SetUrl(url)
.SetHttpMethod(sion::Method::Post)
.SetHeader("Content-type", "application/json")
.SetBody(pJsonString)
.SetConnectedCb(http_connected_cb)
.Send();
} catch (std::exception &e) {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s Exception: %s\n", SET_CLIENTCFG_PATH, pJsonString, e.what());
return -ERR_HTTP_POST_DATA;
}
if (resp.Status() != "OK") {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", "[%s]:Post Data %s error: %s\n", SET_CLIENTCFG_PATH, pJsonString, resp.Status().c_str());

1460
app/src/main/cpp/srcs/sds.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@
#include <cstring>
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include "usrerr.h"
#include "sccproxy.h"
@ -57,16 +58,16 @@ void stop_wireguard_proxy_server() {
pProxy->exitNow = true;
if (pProxy->hProxyTunnelThread) {
pthread_kill(pProxy->hProxyTunnelThread, SIG_CANCEL_SIGNAL);
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Proxy Tunnel Thread finished(%ld)...... \n",
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Proxy Tunnel Thread finished(0x%0lX)...... \n",
pProxy->hProxyTunnelThread);
pthread_kill(pProxy->hProxyTunnelThread, SIG_CANCEL_SIGNAL);
pProxy->hProxyTunnelThread = 0;
close(pProxy->udpProxySock);
}
if (pProxy->hProxySCGThread) {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Proxy SCG Thread finished(0x%0lX)...... \n", pProxy->hProxySCGThread);
pthread_kill(pProxy->hProxySCGThread, SIG_CANCEL_SIGNAL);
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXY", "Proxy SCG Thread finished(%ld)...... \n", pProxy->hProxySCGThread);
pProxy->hProxySCGThread = 0;
close(pProxy->scgGwSock);
}
@ -95,6 +96,12 @@ static void *UDPProxvRemoteThread(void *lpParameter) {
memset(ipAddr, 0, MAX_IPV4_LEN);
inet_ntop(AF_INET, &remoteWgAddr.sin_addr.s_addr, ipAddr, MAX_IPV4_LEN);
#if ENABLE_UDP_PROXY_DEBUG
ANDROID_LOG(ANDROID_LOG_VERBOSE, "SCGPROXYUDP", ">>> Scoket In %d Recv %d bytes from %s:%d",
pProxy->udpProxySock, iRecvBytes,
ipAddr,
ntohs(remoteWgAddr.sin_port));
#endif
if (iRecvBytes != -1) {
int sendBytes = sendto(pProxy->udpProxySock,
recvBuf,
@ -105,6 +112,14 @@ static void *UDPProxvRemoteThread(void *lpParameter) {
if (sendBytes != iRecvBytes) {
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Porxy Send Data Error(%d): %d/%d\n", errno, sendBytes, iRecvBytes);
}
#if ENABLE_UDP_PROXY_DEBUG
memset(ipAddr, 0, MAX_IPV4_LEN);
inet_ntop(AF_INET, &pPeerSock->sin_addr.s_addr, ipAddr, MAX_IPV4_LEN);
ANDROID_LOG(ANDROID_LOG_VERBOSE, "SCGPROXYUDP", "<<< Scoket In Send %d bytes to %s:%d",
iRecvBytes,
ipAddr,
ntohs(remoteWgAddr.sin_port));
#endif
} else {
ANDROID_LOG(ANDROID_LOG_ERROR, "SCGPROXY", ">>> Scoket In %d Recv %d bytes from %s:%d error %d\n", iRecvBytes,
pProxy->scgGwSock,
@ -161,6 +176,13 @@ static void *UDPProxyRecvThread(void *UNUSED(lpParameter)) {
inet_ntop(AF_INET, &localWgAddr.sin_addr.s_addr, ipAddr, MAX_IPV4_LEN);
#if ENABLE_UDP_PROXY_DEBUG
ANDROID_LOG(ANDROID_LOG_VERBOSE, "SCGPROXYUDP", ">>> Scoket Out %d Recv %d bytes from %s:%d",
pProxy->udpProxySock, iRecvBytes,
ipAddr,
ntohs(localWgAddr.sin_port));
#endif
if (iRecvBytes != -1) {
int sendBytes;
const unsigned int id = htonl(get_scg_cur_vmid());
@ -187,10 +209,25 @@ static void *UDPProxyRecvThread(void *UNUSED(lpParameter)) {
recvBuf[7] = vmid[3]; // INFO[0] VMID[3]
// 增加SCG包头数据长度
#if !USED_DIRECT_PROXY
iRecvBytes += 11;
#else
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Porxy Send Data Not Add SCG UDP HEAD\n");
#endif
#if ENABLE_UDP_PROXY_DEBUG
char *p = format_memory_log(reinterpret_cast<const unsigned char *>(pRecBuf), iRecvBytes);
if (p) {
ANDROID_LOG(ANDROID_LOG_INFO, "SCGPROXYUDP", "UDP Proxy SCG(vmid: %d) \n%s", get_scg_cur_vmid(), p);
free(p);
}
#endif
sendBytes = sendto(pProxy->scgGwSock,
#if USED_DIRECT_PROXY
reinterpret_cast<char *>(pRecBuf),
#else
reinterpret_cast<char *>(recvBuf),
#endif
iRecvBytes,
0,
reinterpret_cast<struct sockaddr *>(&scgAddr),
@ -199,8 +236,16 @@ static void *UDPProxyRecvThread(void *UNUSED(lpParameter)) {
if (sendBytes != iRecvBytes) {
ANDROID_LOG(ANDROID_LOG_WARN, "SCGPROXY", "Porxy Send Data Error(%d): %d/%d\n", errno, sendBytes, iRecvBytes);
}
}
#if ENABLE_UDP_PROXY_DEBUG
memset(ipAddr, 0, MAX_IPV4_LEN);
inet_ntop(AF_INET, &scgAddr.sin_addr.s_addr, ipAddr, MAX_IPV4_LEN);
ANDROID_LOG(ANDROID_LOG_VERBOSE, "SCGPROXYUDP", "<<< Scoket Out Send %d bytes to %s:%d",
sendBytes,
ipAddr,
ntohs(scgAddr.sin_port));
#endif
}
usleep(1000);
}