code format

This commit is contained in:
DosGo
2021-08-22 23:22:57 +08:00
parent 1b6d81ce21
commit 8bb75edff0
8 changed files with 373 additions and 642 deletions

243
ddns.cpp
View File

@@ -1,243 +0,0 @@
#include "ddns.h"
static char * headstring="Accept:*/*\r\nUser-Agent:dnspodc/0.1(dosgo@qq.com)";
string getDomainId(string user,string password,string domain)
{
string DomainId="";
string poststring="login_email="+user+"&login_password="+password+"&format=json&domain="+domain;
struct http_response *hresp = http_post("https://dnsapi.cn/Domain.Info",headstring,(char*)poststring.c_str());
if(hresp!=NULL){
int start=strlpos(hresp->body,"{",0);
int end=strrpos(hresp->body,"}",0);
char*jsonstr=hresp->body+start;
jsonstr[end+1]='\0';
cJSON *json = cJSON_Parse(jsonstr);
if(json)
{
cJSON *status = cJSON_GetObjectItem(json, "status");
if(status)
{
cJSON *code= cJSON_GetObjectItem(status, "code");
if(code)
{
char *codestr=code->valuestring;
if ( strcmp(codestr,"1")==0)
{
cJSON *domain = cJSON_GetObjectItem(json, "domain");
//
char * domain_id=cJSON_GetObjectItem(domain, "id")->valuestring;
DomainId=string(domain_id);
}
}
}
}
cJSON_Delete(json);
}
free(hresp);
return DomainId;
}
string getRecordList(string user,string password,string domain_id)
{
string jsonstr="";
string poststring="login_email="+user+"&login_password="+password+"&format=json&domain_id="+domain_id;
struct http_response *hresp = http_post("https://dnsapi.cn/Record.List",headstring,(char *)poststring.c_str());
if(hresp!=NULL){
int start=strlpos(hresp->body,"{",0);
int end=strrpos(hresp->body,"}",0);
char*json=hresp->body+start;
json[end+1]='\0';
jsonstr=string(json);
}
free(hresp);
return jsonstr;
}
int _ddns(string user,string password,string domain_id,string record_id,string sub_domain,string record_line,string value)
{
string poststring="login_email="+user+"&login_password="+password+"&format=json&lang=en&domain_id="+domain_id+"&record_id="+record_id+"&sub_domain="+sub_domain+"&record_line="+record_line+"&value="+value;
printf("poststring:%s\r\n",poststring.c_str());
struct http_response *hresp = http_post("https://dnsapi.cn/Record.Ddns",headstring,(char *)poststring.c_str());
if(hresp!=NULL){
int start=strlpos(hresp->body,"{",0);
int end=strrpos(hresp->body,"}",0);
char*jsonstr=hresp->body+start;
jsonstr[end+1]='\0';
// printf("jsonstr:%s\r\n",jsonstr);
cJSON *json = cJSON_Parse(jsonstr);
if(!json)
{
cJSON_Delete(json);
return -1;
}
cJSON *status = cJSON_GetObjectItem(json, "status");
if(!status)
{
cJSON_Delete(json);
return -1;
}
char *code= cJSON_GetObjectItem(status, "code")->valuestring;
if ( strcmp(code,"1")==0)
{
cJSON_Delete(json);
return 0;
}
cJSON_Delete(json);
}
free(hresp);
return -1;
}
int ddns(string user,string password,string domain,string sub_domain,string value)
{
string DomainId=getDomainId(user,password,domain);
if(!DomainId.empty())
{
string jsonstr=getRecordList(user,password,DomainId);
if(!jsonstr.empty())
{
cJSON *json = cJSON_Parse(jsonstr.c_str());
if(!json)
{
cJSON_Delete(json);
return -1;
}
cJSON *status = cJSON_GetObjectItem(json, "status");
if(!status)
{
cJSON_Delete(json);
return -1;
}
cJSON *code= cJSON_GetObjectItem(status, "code");
if(!status)
{
cJSON_Delete(json);
return -1;
}
char *codestr=code->valuestring;
if (strcmp(codestr,"1")==0)
{
cJSON*records = cJSON_GetObjectItem(json, "records");
if(!records)
{
cJSON_Delete(json);
return -1;
}
for(int i=0;i<cJSON_GetArraySize(records);i++)
{
cJSON *recordsitem=cJSON_GetArrayItem(records,i);
if(!recordsitem)
{
cJSON_Delete(json);
return -1;
}
char *item_sub_domain =cJSON_GetObjectItem(recordsitem, "name")->valuestring;
char *item_value =cJSON_GetObjectItem(recordsitem, "value")->valuestring;
if(strcmp(sub_domain.c_str(),item_sub_domain)==0&&strcmp(value.c_str(),item_value)!=0)
{
string item_record_line=string(cJSON_GetObjectItem(recordsitem, "line")->valuestring);
string item_record_id =string(cJSON_GetObjectItem(recordsitem, "id")->valuestring);
int err=_ddns(user,password,DomainId,item_record_id,sub_domain,item_record_line,value);
if(err==0)
{
printf("DDNS update OK \r\n");
return 0;
}
printf("DDNS update err \r\n");
return -1;
}
}
}
cJSON_Delete(json);
}
}
printf("DDNS not edit \r\n");
return 0;
}
int strrpos(const char *haystack,const char *needle, int ignorecase = 0)
{
int back=0;
register unsigned char c, needc;
unsigned char const *from, *end;
int len = strlen(haystack);
int needlen = strlen(needle);
from = (unsigned char *)haystack;
end = (unsigned char *)haystack + len;
const char *findreset = needle;
for (int i = 0; from < end; ++i) {
c = *from++;
needc = *needle;
if (ignorecase) {
if (c >= 65 && c < 97)
c += 32;
if (needc >= 65 && needc < 97)
needc += 32;
}
if(c == needc) {
++needle;
if(*needle == '\0') {
if (len == needlen)
{
back=0;
//return 0;
}
else
{
back=i - needlen+1;
//return i - needlen+1;
}
}
}
else {
if(*needle == '\0' && needlen > 0)
{
back=i - needlen+1;
//return i - needlen +1;
}
needle = findreset;
}
}
return back;
}
int strlpos(const char *haystack,const char *needle, int ignorecase = 0)
{
register unsigned char c, needc;
unsigned char const *from, *end;
int len = strlen(haystack);
int needlen = strlen(needle);
from = (unsigned char *)haystack;
end = (unsigned char *)haystack + len;
const char *findreset = needle;
for (int i = 0; from < end; ++i) {
c = *from++;
needc = *needle;
if (ignorecase) {
if (c >= 65 && c < 97)
c += 32;
if (needc >= 65 && needc < 97)
needc += 32;
}
if(c == needc) {
++needle;
if(*needle == '\0') {
if (len == needlen)
return 0;
else
return i - needlen+1;
}
}
else {
if(*needle == '\0' && needlen > 0)
return i - needlen +1;
needle = findreset;
}
}
return 0;
}

13
ddns.h
View File

@@ -1,13 +0,0 @@
#ifndef DDNS_H_INCLUDED
#define DDNS_H_INCLUDED
#include <string>
#include "cJSON.h"
#include "https.h";
using namespace std;
string getDomainId(string user,string password,string domain);
string getRecordList(string user,string password,string domain_id);
int _ddns(string user,string password,string domain_id,string record_id,string sub_domain,string record_line,string value);
int ddns(string user,string password,string domain,string sub_domain,string value);
int strrpos(const char *haystack,const char *needle, int ignorecase);
int strlpos(const char *haystack,const char *needle, int ignorecase);
#endif // DDNS_H_INCLUDED

View File

@@ -18,7 +18,7 @@ typedef long long __int64;
using namespace std;
/*<EFBFBD><EFBFBD><EFBFBD><EFBFBD>udp*/
/*控制udp*/
int ControlUdp(int port){
int sockfd;
struct sockaddr_in my_addr;
@@ -53,13 +53,13 @@ int UdpCmd(int udpsocket){
if(udpbuflen>0){
cJSON *json = cJSON_Parse( buf );
cJSON *cmd = cJSON_GetObjectItem( json, "cmd" );
//<EFBFBD>˳<EFBFBD>
//退出
if ( strcmp( cmd->valuestring, "exit" ) == 0 ){
cJSON_Delete( json );
exit(0);
}
if ( strcmp( cmd->valuestring, "ping" ) == 0 ){
//<EFBFBD>ظ<EFBFBD>
//回复
char sendbuf[255]="{\"cmd\":\"pong\"}";
sendto(udpsocket,sendbuf,strlen(sendbuf),0,(struct sockaddr *)&udpaddr,sizeof(udpaddr));
cJSON_Delete( json );
@@ -105,7 +105,7 @@ int InitTunnelList(){
}
//<EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
//释放所有通道信息
map<string, TunnelReq*>::iterator it3;
for ( it3 = mainInfo.G_TunnelAddr.begin(); it3 != mainInfo.G_TunnelAddr.end(); )
{
@@ -130,7 +130,7 @@ int SetLocalAddrInfo(char *url,char *ReqId,int regstate){
sscanf(url,"%[^:]://%[^:]:%[0-9]",protocol,host,portstr);
port=atoi(portstr);
sscanf(host,"%[^.].",subdomain);
//<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//进行迭代遍历
for(iter = G_TunnelList.begin(); iter !=G_TunnelList.end(); iter++)
{
TunnelInfo *tunnelinfo =(TunnelInfo*)*iter;
@@ -213,7 +213,7 @@ int RemoteSslInit(Sockinfo *tempinfo){
#if OPENSSL
setnonblocking(tempinfo->sock,1);
#endif
/* ssl <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* ssl 初始化失败,移除连接 */
clearsock(tempinfo );
return -1;
}
@@ -223,7 +223,7 @@ int RemoteSslInit(Sockinfo *tempinfo){
int LocalToRemote(Sockinfo *tempinfo,ssl_info *sslinfo){
int readlen;
int bufsize=1024*15;//15K //oolarssl SSL_MAX_CONTENT_LEN 16384
//oolarssl <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD>Ȳ<EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD><EFBFBD>16K<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹij<EFBFBD>15<EFBFBD><EFBFBD>
//oolarssl 最大发送长度不能超过16K。。还是改成15吧
char buf[bufsize+1];
memset(buf,0,bufsize+1);
#if WIN32
@@ -233,7 +233,7 @@ int LocalToRemote(Sockinfo *tempinfo,ssl_info *sslinfo){
#endif
if ( readlen > 0&&sslinfo!=NULL )
{
//<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Զ<EFBFBD><EFBFBD>
//发送到远程
#if OPENSSL
sendremote(tempinfo->tosock,sslinfo->ssl,buf,readlen,1);
#else
@@ -251,7 +251,7 @@ int LocalToRemote(Sockinfo *tempinfo,ssl_info *sslinfo){
int RemoteToLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
int readlen,sendlen;
int bufsize=1024*15;//15K //oolarssl SSL_MAX_CONTENT_LEN 16384
//oolarssl <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳ<EFBFBD><EFBFBD>Ȳ<EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD><EFBFBD>16K<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹij<EFBFBD>15<EFBFBD><EFBFBD>
//oolarssl 最大发送长度不能超过16K。。还是改成15吧
char buf[bufsize+1];
memset(buf,0,bufsize+1);
#if OPENSSL
@@ -269,7 +269,7 @@ int RemoteToLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
int tosock=tempinfo->tosock;
shutdown( tosock, 2 );
clearsock(tempinfo );
//<EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><EFBFBD>Բ<EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD>ssl<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>٣<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//这行绝对不能删除用标记ssl已经销毁删除会导致崩溃。
if(G_SockList.count(tosock)==1)
{
G_SockList[tosock]->sslinfo=NULL;
@@ -284,20 +284,20 @@ int RemoteToLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
char remotehost[256] = { 0 };
char httpline[3]="\r\n";
sscanf(tunnelreq->url,"%[^:]://%[^\n]",protocol,remotehost);
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>tcp<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫת<EFBFBD><EFBFBD>
//不是tcp才需要转发
if(strncmp(protocol,"tcp",3)!=0){
//<EFBFBD><EFBFBD>Ҫhostͷת<EFBFBD><EFBFBD>
//需要host头转发
if(strlen(tunnelreq->hostheader)>0){
//ƴ<EFBFBD><EFBFBD>\r\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD>httpͷrfcЭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//拼接\r\n用于识别httprfc协议描述的
memcpy(remotehost+strlen(remotehost),httpline,2);
char *p=strstr(buf,remotehost);
char srchost[256] = { 0 };
memcpy(srchost,tunnelreq->hostheader,strlen(tunnelreq->hostheader));
memcpy(srchost+strlen(srchost),httpline,2);
//<EFBFBD><EFBFBD>ˡ<EFBFBD><EFBFBD><EFBFBD>
//查到了。。
if(p!=NULL){
//<EFBFBD>http<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
//替换http请求头
str_replace(p,strlen(remotehost),srchost);
}
}
@@ -315,7 +315,7 @@ int RemoteToLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
}
int ConnectLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>Ϊ<EFBFBD>ձ<EFBFBD><EFBFBD><EFBFBD>
//避免指针为空崩溃
if(sslinfo==NULL){
clearsock(tempinfo);
return -1;
@@ -346,16 +346,16 @@ int ConnectLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
if ( readlen ==-1)
{
return 0;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1<><31>Ȼ<EFBFBD><EFBFBD>£<EFBFBD>map<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return 0;//这里不能用-1不然会导致map迭代器不自增
}
//<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>readlen<EFBFBD><EFBFBD><EFBFBD><EFBFBD>-76<37><36><EFBFBD>±<EFBFBD><C2B1><EFBFBD>
//有时候readlen变成-76导致崩溃
if ( readlen <1)
{
clearsock(tempinfo);
return -1;
}
/* copy<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* copy到临时缓存区 */
if ( tempinfo->packbuflen == 0 )
{
tempinfo->packbuf = (unsigned char *) malloc( MAXBUF );
@@ -413,7 +413,7 @@ int ConnectLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
sinfo->tosock = tempinfo->sock;
sinfo->sock = tcp;
G_SockList.insert( map<int, Sockinfo*> :: value_type( tcp, sinfo ) );
/* Զ<EFBFBD>̵Ĵ<EFBFBD><EFBFBD>ϱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* 远程的带上本地链接 */
tempinfo->tosock = tcp;
tempinfo->tunnelreq = tunnelreq;
tempinfo->isconnectlocal= 1;
@@ -434,7 +434,7 @@ int ConnectLocal(ssl_info *sslinfo,Sockinfo *tempinfo){
int CmdSock(int *mainsock,Sockinfo *tempinfo,struct sockaddr_in server_addr){
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Ͽ<EFBFBD>
//检测是否断开
if(check_sock(*mainsock)!= 0)
{
return -1;
@@ -470,7 +470,7 @@ int CmdSock(int *mainsock,Sockinfo *tempinfo,struct sockaddr_in server_addr){
/* copy<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/* copy到临时缓存区 */
if ( tempinfo->packbuflen == 0 )
{
tempinfo->packbuf = (unsigned char *) malloc( MAXBUF );

View File

@@ -1,88 +1,84 @@
#include "nonblocking.h"
#if OPENSSL
void clearsock(Sockinfo * sock_info)
void clearsock(Sockinfo *sock_info)
{
if(sock_info->istype==1)
if (sock_info->istype == 1)
{
if(sock_info->packbuflen>0&&sock_info->packbuf!=NULL)
{
free(sock_info->packbuf);
sock_info->packbuf=NULL;
}
if (sock_info->packbuflen > 0 && sock_info->packbuf != NULL)
{
free(sock_info->packbuf);
sock_info->packbuf = NULL;
}
if(sock_info->sslinfo!=NULL)
{
ssl_free_info(sock_info->sslinfo);
free(sock_info->sslinfo);
sock_info->sslinfo=NULL;
}
if (sock_info->sslinfo != NULL)
{
ssl_free_info(sock_info->sslinfo);
free(sock_info->sslinfo);
sock_info->sslinfo = NULL;
}
}
// net_close(sock);
// net_close(sock);
shutdown(sock_info->sock,2);
//closesocket(sock);
#if WIN32
shutdown(sock_info->sock, 2);
//closesocket(sock);
#if WIN32
closesocket(sock_info->sock);
#else
#else
close(sock_info->sock);
#endif
//ÊÍ·ÅÄÚ´æ
if(sock_info!=NULL)
#endif
//脢脥路脜脛脷麓忙
if (sock_info != NULL)
{
free(sock_info);
sock_info=NULL;
free(sock_info);
sock_info = NULL;
}
}
#else
void clearsock(Sockinfo * sock_info)
void clearsock(Sockinfo *sock_info)
{
if(sock_info->istype==1)
if (sock_info->istype == 1)
{
if(sock_info->packbuflen>0&&sock_info->packbuf!=NULL)
{
free(sock_info->packbuf);
sock_info->packbuf=NULL;
}
if (sock_info->packbuflen > 0 && sock_info->packbuf != NULL)
{
free(sock_info->packbuf);
sock_info->packbuf = NULL;
}
if(sock_info->sslinfo!=NULL)
{
#if ISMBEDTLS
mbedtls_ssl_close_notify(&sock_info->sslinfo->ssl);
#else
ssl_close_notify(&sock_info->sslinfo->ssl);
#endif // ISMBEDTLS
ssl_free_info(sock_info->sslinfo);
free(sock_info->sslinfo);
sock_info->sslinfo=NULL;
}
if (sock_info->sslinfo != NULL)
{
#if ISMBEDTLS
mbedtls_ssl_close_notify(&sock_info->sslinfo->ssl);
#else
ssl_close_notify(&sock_info->sslinfo->ssl);
#endif // ISMBEDTLS
ssl_free_info(sock_info->sslinfo);
free(sock_info->sslinfo);
sock_info->sslinfo = NULL;
}
}
#if ISMBEDTLS
shutdown(sock_info->sock,2);
//closesocket(sock_info->sock);
#if WIN32
#if ISMBEDTLS
shutdown(sock_info->sock, 2);
//closesocket(sock_info->sock);
#if WIN32
closesocket(sock_info->sock);
#else
#else
close(sock_info->sock);
#endif
#endif
#else
shutdown(sock_info->sock,2);
#else
shutdown(sock_info->sock, 2);
net_close(sock_info->sock);
#endif
#endif
//ÊÍ·ÅÄÚ´æ
if(sock_info!=NULL)
//
if (sock_info != NULL)
{
free(sock_info);
sock_info=NULL;
free(sock_info);
sock_info = NULL;
}
}
#endif

View File

@@ -2,7 +2,7 @@
#define NONBLOCKING_H_INCLUDED
#include "config.h"
#include <string.h>
#if WIN32
#if WIN32
#include <winsock.h>
#include <winsock2.h>
#include <windows.h>
@@ -22,43 +22,45 @@ using namespace std;
#if WIN32
struct tcp_keepalive {
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
struct tcp_keepalive
{
u_long onoff;
u_long keepalivetime;
u_long keepaliveinterval;
};
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define SIO_RCVALL_MCAST _WSAIOW(IOC_VENDOR,2)
#define SIO_RCVALL_IGMPMCAST _WSAIOW(IOC_VENDOR,3)
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
#define SIO_ABSORB_RTRALERT _WSAIOW(IOC_VENDOR,5)
#define SIO_UCAST_IF _WSAIOW(IOC_VENDOR,6)
#define SIO_LIMIT_BROADCASTS _WSAIOW(IOC_VENDOR,7)
#define SIO_INDEX_BIND _WSAIOW(IOC_VENDOR,8)
#define SIO_INDEX_MCASTIF _WSAIOW(IOC_VENDOR,9)
#define SIO_INDEX_ADD_MCAST _WSAIOW(IOC_VENDOR,10)
#define SIO_INDEX_DEL_MCAST _WSAIOW(IOC_VENDOR,11)
#define SIO_RCVALL _WSAIOW(IOC_VENDOR, 1)
#define SIO_RCVALL_MCAST _WSAIOW(IOC_VENDOR, 2)
#define SIO_RCVALL_IGMPMCAST _WSAIOW(IOC_VENDOR, 3)
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR, 4)
#define SIO_ABSORB_RTRALERT _WSAIOW(IOC_VENDOR, 5)
#define SIO_UCAST_IF _WSAIOW(IOC_VENDOR, 6)
#define SIO_LIMIT_BROADCASTS _WSAIOW(IOC_VENDOR, 7)
#define SIO_INDEX_BIND _WSAIOW(IOC_VENDOR, 8)
#define SIO_INDEX_MCASTIF _WSAIOW(IOC_VENDOR, 9)
#define SIO_INDEX_ADD_MCAST _WSAIOW(IOC_VENDOR, 10)
#define SIO_INDEX_DEL_MCAST _WSAIOW(IOC_VENDOR, 11)
#define RCVALL_OFF 0
#define RCVALL_ON 1
#define RCVALL_SOCKETLEVELONLY 2
inline int SetKeepAlive(int sock){
inline int SetKeepAlive(int sock)
{
BOOL bKeepAlive = TRUE;
int nRet =setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&bKeepAlive, sizeof(bKeepAlive));
int nRet = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&bKeepAlive, sizeof(bKeepAlive));
if (nRet == SOCKET_ERROR)
{
return -1;
return -1;
}
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>KeepAlive<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// 设置KeepAlive参数
tcp_keepalive alive_in = {0};
tcp_keepalive alive_out = {0};
alive_in.keepalivetime =60000; // <20><>ʼ<EFBFBD>״<EFBFBD>KeepAlive̽<65><CCBD>ǰ<EFBFBD><C7B0>TCP<43>ձ<EFBFBD>ʱ<EFBFBD><CAB1>
alive_in.keepaliveinterval =60000; // <EFBFBD><EFBFBD><EFBFBD><EFBFBD>KeepAlive̽<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
alive_in.keepalivetime = 60000; // 开始首次KeepAlive探测前的TCP空闭时间
alive_in.keepaliveinterval = 60000; // 两次KeepAlive探测间的时间间隔
alive_in.onoff = TRUE;
unsigned long ulBytesReturn =0;
unsigned long ulBytesReturn = 0;
//nRet = WSAIoctl(sock, SIO_KEEPALIVE_VALS, &alive_in, sizeof(alive_in),&alive_out, sizeof(alive_out), &ulBytesReturn, NULL, NULL);
if (nRet == SOCKET_ERROR)
{
@@ -69,74 +71,72 @@ inline int SetKeepAlive(int sock){
#else
#include <netinet/in.h>
#include <netinet/tcp.h>
inline int SetKeepAlive(int sock){
/*<2A><><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>뵥λ<EBB5A5><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>Ǻ<EFBFBD><C7BA>뵥λ*/
int keepalive = 1; // <20><><EFBFBD><EFBFBD>keepalive<76><65><EFBFBD><EFBFBD>
int keepidle = 60000; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>60<36><30><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>κ<EFBFBD><CEBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̽<EFBFBD><CCBD>
int keepinterval = 60000; // ̽<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ5 <20><>
int keepcount = 1; // ̽<EFBFBD><EFBFBD>ԵĴ<EFBFBD><EFBFBD><EFBFBD>.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>̽<EFBFBD><CCBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD>Ӧ<EFBFBD><D3A6>,<2C><><EFBFBD><EFBFBD>2<EFBFBD>εIJ<CEB5><C4B2>ٷ<EFBFBD>.
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive , sizeof(keepalive ));
setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (void*)&keepidle , sizeof(keepidle ));
setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, (void *)&keepinterval , sizeof(keepinterval ));
setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (void *)&keepcount , sizeof(keepcount ));
return 0;
inline int SetKeepAlive(int sock)
{
/*有人说是秒单位。。有人说是毫秒单位*/
int keepalive = 1; // 开启keepalive属性
int keepidle = 60000; // 如该连接在60秒内没有任何数据往来,则进行探测
int keepinterval = 60000; // 探测时发包的时间间隔为5 秒
int keepcount = 1; // 探测尝试的次数.如果第1次探测包就收到响应了,则后2次的不再发.
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive, sizeof(keepalive));
setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, (void *)&keepidle, sizeof(keepidle));
setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, (void *)&keepinterval, sizeof(keepinterval));
setsockopt(sock, SOL_TCP, TCP_KEEPCNT, (void *)&keepcount, sizeof(keepcount));
return 0;
}
#endif // WIN32
inline int setnonblocking(int sServer,int _nMode)
inline int setnonblocking(int sServer, int _nMode)
{
#if WIN32
#if WIN32
DWORD nMode = _nMode;
return ioctlsocket( sServer, FIONBIO,&nMode);
#else
if(_nMode==1)
return ioctlsocket(sServer, FIONBIO, &nMode);
#else
if (_nMode == 1)
{
return fcntl(sServer,F_SETFL,O_NONBLOCK);
return fcntl(sServer, F_SETFL, O_NONBLOCK);
}
else
{
return fcntl(sServer,F_SETFL, _nMode);
return fcntl(sServer, F_SETFL, _nMode);
}
#endif
#endif
}
inline int net_dns( struct sockaddr_in *server_addr, const char *host, int port )
inline int net_dns(struct sockaddr_in *server_addr, const char *host, int port)
{
struct hostent *server_host;
if((server_host = gethostbyname(host)) == NULL )
if ((server_host = gethostbyname(host)) == NULL)
{
return -1;
}
memcpy((void*)&server_addr->sin_addr,(void*)server_host->h_addr,server_host->h_length);
memcpy((void *)&server_addr->sin_addr, (void *)server_host->h_addr, server_host->h_length);
server_addr->sin_family = AF_INET;
server_addr->sin_port = htons( port );
server_addr->sin_port = htons(port);
return 0;
}
inline int check_sock(int sock)
{
int error=-1;
#if WIN32
int len ;
#else
int error = -1;
#if WIN32
int len;
#else
socklen_t len;
#endif
#endif
len = sizeof(error);
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
return error;
}
void clearsock(Sockinfo * sock_info);
void clearsock(Sockinfo *sock_info);
inline int SetBufSize(int sock)
{
//<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int opt=25*1024;//30K
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&opt,sizeof(opt));
//<EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD>ǧ<EFBFBD><C7A7><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
// setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&opt,sizeof(opt));
//接收缓冲区
int opt = 25 * 1024; //30K
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char *)&opt, sizeof(opt));
//发送缓冲区 (这个千万不要。。发送不需要缓存区)
// setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&opt,sizeof(opt));
return 0;
}

View File

@@ -9,155 +9,155 @@
typedef long long __int64;
#endif
#include "sendmsg.h"
#include<stdlib.h>
#include <stdlib.h>
using namespace std;
char *rand_str(char *str,const int len)
char *rand_str(char *str, const int len)
{
int i;
for(i=0;i<len;++i)
str[i]='A'+rand()%26;
str[++i]='\0';
return str;
int i;
for (i = 0; i < len; ++i)
str[i] = 'A' + rand() % 26;
str[++i] = '\0';
return str;
}
int SendReqTunnel(int sock,ssl_context *ssl,TunnelInfo *tunnelInfo)
int SendReqTunnel(int sock, ssl_context *ssl, TunnelInfo *tunnelInfo)
{
char guid[20]={0};
rand_str(guid,5);
char str[1024];
memset(str,0,1024);
memset(tunnelInfo->ReqId,0,20);
//copy
memcpy(tunnelInfo->ReqId,guid,strlen(guid));
sprintf(str,"{\"Type\":\"ReqTunnel\",\"Payload\":{\"Protocol\":\"%s\",\"ReqId\":\"%s\",\"Hostname\": \"%s\",\"Subdomain\":\"%s\",\"HttpAuth\":\"%s\",\"RemotePort\":%d,\"authtoken\":\"%s\"}}",tunnelInfo->protocol,tunnelInfo->ReqId,tunnelInfo->hostname,tunnelInfo->subdomain,tunnelInfo->httpauth,tunnelInfo->remoteport,mainInfo.authtoken);
tunnelInfo->regtime=getUnixTime();//<EFBFBD>ѷ<EFBFBD>
return sendpack(sock,ssl,str,1);
char guid[20] = {0};
rand_str(guid, 5);
char str[1024];
memset(str, 0, 1024);
memset(tunnelInfo->ReqId, 0, 20);
//copy
memcpy(tunnelInfo->ReqId, guid, strlen(guid));
sprintf(str, "{\"Type\":\"ReqTunnel\",\"Payload\":{\"Protocol\":\"%s\",\"ReqId\":\"%s\",\"Hostname\": \"%s\",\"Subdomain\":\"%s\",\"HttpAuth\":\"%s\",\"RemotePort\":%d,\"authtoken\":\"%s\"}}", tunnelInfo->protocol, tunnelInfo->ReqId, tunnelInfo->hostname, tunnelInfo->subdomain, tunnelInfo->httpauth, tunnelInfo->remoteport, mainInfo.authtoken);
tunnelInfo->regtime = getUnixTime(); //已发
return sendpack(sock, ssl, str, 1);
}
int loadargs( int argc, char **argv )
int loadargs(int argc, char **argv)
{
if ( argc > 1 )
if (argc > 1)
{
char jsonstr[1024];
char temp[255];
int pos = 0;
char jsonstr[1024];
char temp[255];
int pos = 0;
char *argvstr;
int xpos;
int run = 1;
for ( int i = 1; i < argc; i++ )
int xpos;
int run = 1;
for (int i = 1; i < argc; i++)
{
argvstr = argv[i];
memset( jsonstr, 0, 1024 );
memset( temp, 0, 255 );
pos = strpos( argvstr, '[' );
if ( pos == -1 )
memset(jsonstr, 0, 1024);
memset(temp, 0, 255);
pos = strpos(argvstr, '[');
if (pos == -1)
{
printf( "argv error:%s", argvstr );
}else {
if ( strncmp( argvstr, "-SER", 4 ) == 0 )
printf("argv error:%s", argvstr);
}
else
{
if (strncmp(argvstr, "-SER", 4) == 0)
{
run = 1;
while ( run )
while (run)
{
memset( jsonstr, 0, 1024 );
xpos = strpos( argvstr + pos + 1, ',' );
if ( xpos == -1 )
memset(jsonstr, 0, 1024);
xpos = strpos(argvstr + pos + 1, ',');
if (xpos == -1)
{
xpos = strpos( argvstr + pos + 1, ']' );
memcpy( jsonstr, argvstr + pos + 1, xpos );
xpos = strpos(argvstr + pos + 1, ']');
memcpy(jsonstr, argvstr + pos + 1, xpos);
run = 0;
}else {
memcpy( jsonstr, argvstr + pos + 1, xpos );
}
getvalue(jsonstr,"Shost",mainInfo.shost);
if(getvalue(jsonstr,"Sport",temp)==0)
{
mainInfo.sport = atoi(temp);
else
{
memcpy(jsonstr, argvstr + pos + 1, xpos);
}
getvalue(jsonstr,"Atoken",mainInfo.authtoken);
getvalue(jsonstr,"Password",mainInfo.pwdc);
getvalue(jsonstr, "Shost", mainInfo.shost);
if (getvalue(jsonstr, "Sport", temp) == 0)
{
mainInfo.sport = atoi(temp);
}
getvalue(jsonstr, "Atoken", mainInfo.authtoken);
getvalue(jsonstr, "Password", mainInfo.pwdc);
if(getvalue(jsonstr,"Cid",temp)==0)
{
mainInfo.ClientId = string( temp );
if (getvalue(jsonstr, "Cid", temp) == 0)
{
mainInfo.ClientId = string(temp);
}
pos = pos + xpos + 1;
}
}
#if UDPTUNNEL
if ( strncmp( argvstr, "-UDPSER", 7 ) == 0 )
#if UDPTUNNEL
if (strncmp(argvstr, "-UDPSER", 7) == 0)
{
run = 1;
while ( run )
while (run)
{
memset( jsonstr, 0, 1024 );
xpos = strpos( argvstr + pos + 1, ',' );
if ( xpos == -1 )
memset(jsonstr, 0, 1024);
xpos = strpos(argvstr + pos + 1, ',');
if (xpos == -1)
{
xpos = strpos( argvstr + pos + 1, ']' );
memcpy( jsonstr, argvstr + pos + 1, xpos );
xpos = strpos(argvstr + pos + 1, ']');
memcpy(jsonstr, argvstr + pos + 1, xpos);
run = 0;
}else {
memcpy( jsonstr, argvstr + pos + 1, xpos );
}
getvalue(jsonstr,"Shost",mainInfo.udphost);
if(getvalue(jsonstr,"Sport",temp)==0)
{
mainInfo.udpport = atoi(temp);
else
{
memcpy(jsonstr, argvstr + pos + 1, xpos);
}
getvalue(jsonstr, "Shost", mainInfo.udphost);
if (getvalue(jsonstr, "Sport", temp) == 0)
{
mainInfo.udpport = atoi(temp);
}
pos = pos + xpos + 1;
}
mainInfo.udp=1;
mainInfo.udp = 1;
}
#endif
if ( strncmp( argvstr, "-AddTun", 7 ) == 0 )
#endif
if (strncmp(argvstr, "-AddTun", 7) == 0)
{
run = 1;
TunnelInfo *tunnelinfo = (TunnelInfo *) malloc( sizeof(TunnelInfo) );
memset( tunnelinfo, 0, sizeof(TunnelInfo) );
TunnelInfo *tunnelinfo = (TunnelInfo *)malloc(sizeof(TunnelInfo));
memset(tunnelinfo, 0, sizeof(TunnelInfo));
while ( run )
while (run)
{
memset( jsonstr, 0, 1024 );
xpos = strpos( argvstr + pos + 1, ',' );
if ( xpos == -1 )
memset(jsonstr, 0, 1024);
xpos = strpos(argvstr + pos + 1, ',');
if (xpos == -1)
{
xpos = strpos( argvstr + pos + 1, ']' );
memcpy( jsonstr, argvstr + pos + 1, xpos );
xpos = strpos(argvstr + pos + 1, ']');
memcpy(jsonstr, argvstr + pos + 1, xpos);
run = 0;
}else {
memcpy( jsonstr, argvstr + pos + 1, xpos );
}
else
{
memcpy(jsonstr, argvstr + pos + 1, xpos);
}
getvalue(jsonstr, "Lhost", tunnelinfo->localhost);
getvalue(jsonstr, "Type", tunnelinfo->protocol);
memset(temp, 0, strlen(temp));
if (getvalue(jsonstr, "Lport", temp) == 0)
{
tunnelinfo->localport = atoi(temp);
}
memset(temp, 0, strlen(temp));
if (getvalue(jsonstr, "Rport", temp) == 0)
{
tunnelinfo->remoteport = atoi(temp);
}
getvalue(jsonstr,"Lhost",tunnelinfo->localhost);
getvalue(jsonstr,"Type",tunnelinfo->protocol);
memset( temp, 0, strlen( temp ) );
if(getvalue(jsonstr,"Lport",temp)==0)
{
tunnelinfo->localport = atoi( temp );
}
memset( temp,0,strlen(temp));
if(getvalue(jsonstr,"Rport",temp)==0)
{
tunnelinfo->remoteport = atoi( temp );
}
getvalue(jsonstr,"Sdname",tunnelinfo->subdomain);
getvalue(jsonstr,"Hostheader",tunnelinfo->hostheader);
getvalue(jsonstr,"Httpauth",tunnelinfo->httpauth);
//printf("httpAuth:%s\r\n",tunnelinfo->httpauth);
getvalue(jsonstr,"Hostname",tunnelinfo->hostname);
getvalue(jsonstr, "Sdname", tunnelinfo->subdomain);
getvalue(jsonstr, "Hostheader", tunnelinfo->hostheader);
getvalue(jsonstr, "Httpauth", tunnelinfo->httpauth);
//printf("httpAuth:%s\r\n",tunnelinfo->httpauth);
getvalue(jsonstr, "Hostname", tunnelinfo->hostname);
pos = pos + xpos + 1;
}
@@ -165,14 +165,14 @@ int loadargs( int argc, char **argv )
}
}
}
}else {
echo( "use " );
echo("%s",argv[0]);
echo( " -SER[Shost:ngrokd.ngrok.com,Sport:443,Atoken:xx,Password:xx] -AddTun[Type:tcp,Lhost:127.0.0.1,Lport:80,Rport:50199,Hostheader:localhost,Httpauth:\"test:test\"]" );
echo( "\r\n" );
exit( 1 );
}
else
{
echo("use ");
echo("%s", argv[0]);
echo(" -SER[Shost:ngrokd.ngrok.com,Sport:443,Atoken:xx,Password:xx] -AddTun[Type:tcp,Lhost:127.0.0.1,Lport:80,Rport:50199,Hostheader:localhost,Httpauth:\"test:test\"]");
echo("\r\n");
exit(1);
}
return 0;
}

246
sendmsg.h
View File

@@ -11,198 +11,190 @@
#else
#include <errno.h>
#include <netinet/in.h>
//typedef unsigned long long __int64;
#include <string.h>
#ifndef __UCLIBC__
#include <sys/select.h>
#endif
//typedef unsigned long long __int64;
#include <string.h>
#ifndef __UCLIBC__
#include <sys/select.h>
#endif
#endif
#include "bytestool.h"
using namespace std;
inline int getUnixTime()
{
time_t now;
return time(&now);
}
int loadargs( int argc, char **argv);
char *rand_str(char *str,const int len);
int loadargs(int argc, char **argv);
char *rand_str(char *str, const int len);
inline int strpos( char *str, char c )
inline int strpos(char *str, char c)
{
char *sc = strchr( str, c );
if ( sc == NULL )
return(-1);
return(sc - str);
char *sc = strchr(str, c);
if (sc == NULL)
return (-1);
return (sc - str);
}
inline void str_replace(char * cp, int n, char * str)
inline void str_replace(char *cp, int n, char *str)
{
int lenofstr;
char * tmp;
char *tmp;
lenofstr = strlen(str);
//str3<EFBFBD><EFBFBD>str2<EFBFBD>̣<EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ƶ<EFBFBD>
if(lenofstr < n)
//str3str2短,往前移动
if (lenofstr < n)
{
tmp = cp+n;
while(*tmp)
tmp = cp + n;
while (*tmp)
{
*(tmp-(n-lenofstr)) = *tmp; //n-lenofstr<EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD>ľ<EFBFBD><EFBFBD><EFBFBD>
*(tmp - (n - lenofstr)) = *tmp; //n-lenofstr是移动的距离
tmp++;
}
*(tmp-(n-lenofstr)) = *tmp; //move '\0'
*(tmp - (n - lenofstr)) = *tmp; //move '\0'
}
else
//str3<EFBFBD><EFBFBD>str2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
if(lenofstr > n)
//str3str2长,往后移动
if (lenofstr > n)
{
tmp = cp;
while (*tmp)
tmp++;
while (tmp >= cp + n)
{
tmp = cp;
while(*tmp) tmp++;
while(tmp>=cp+n)
{
*(tmp+(lenofstr-n)) = *tmp;
tmp--;
}
*(tmp + (lenofstr - n)) = *tmp;
tmp--;
}
strncpy(cp,str,lenofstr);
}
strncpy(cp, str, lenofstr);
}
inline int getvalue(char * str,const char *key,char * value){
int ypos=0;
if ( strncmp(str,key,strlen(key)) == 0 )
inline int getvalue(char *str, const char *key, char *value)
{
int ypos = 0;
if (strncmp(str, key, strlen(key)) == 0)
{
ypos = strpos( str, ':' );
if ( ypos != -1 )
ypos = strpos(str, ':');
if (ypos != -1)
{
memcpy(value, str + ypos + 1, strlen( str + ypos ));
memcpy(value, str + ypos + 1, strlen(str + ypos));
return 0;
}
}
return -1;
}
inline int sendremote(int sock,ssl_context *ssl,const char *buf,int buflen,int isblock){
int sendlen=0;
if(isblock)
{
setnonblocking(sock,0);
}
#if OPENSSL
#if OPENSSLDL
sendlen=SslWrite(ssl, buf, buflen );
#else
sendlen=SSL_write(ssl, buf, buflen );
#endif // OPENSSLDL
#else
#if ISMBEDTLS
sendlen=mbedtls_ssl_write( ssl, (unsigned char *)buf, buflen );
#else
sendlen=ssl_write( ssl,(unsigned char *) buf, buflen );
#endif // ISMBEDTLS
#endif
if(isblock)
{
setnonblocking(sock,1);
}
return sendlen;
}
inline int sendlocal(int sock,const char *buf,int buflen,int isblock){
int sendlen=0;
if(isblock)
inline int sendremote(int sock, ssl_context *ssl, const char *buf, int buflen, int isblock)
{
int sendlen = 0;
if (isblock)
{
setnonblocking(sock,0);
setnonblocking(sock, 0);
}
#if WIN32
sendlen=send( sock, (char *) buf, buflen, 0 );
#else
sendlen=send( sock, buf, buflen, 0 );
#endif
if(isblock)
#if OPENSSL
#if OPENSSLDL
sendlen = SslWrite(ssl, buf, buflen);
#else
sendlen = SSL_write(ssl, buf, buflen);
#endif // OPENSSLDL
#else
#if ISMBEDTLS
sendlen = mbedtls_ssl_write(ssl, (unsigned char *)buf, buflen);
#else
sendlen = ssl_write(ssl, (unsigned char *)buf, buflen);
#endif // ISMBEDTLS
#endif
if (isblock)
{
setnonblocking(sock,1);
setnonblocking(sock, 1);
}
return sendlen;
}
inline int sendpack(int sock,ssl_context *ssl,const char *msgstr,int isblock)
inline int sendlocal(int sock, const char *buf, int buflen, int isblock)
{
unsigned char buffer[strlen(msgstr)+9];
memset(buffer,0,strlen(msgstr)+9);
#if WIN32
int sendlen = 0;
if (isblock)
{
setnonblocking(sock, 0);
}
#if WIN32
sendlen = send(sock, (char *)buf, buflen, 0);
#else
sendlen = send(sock, buf, buflen, 0);
#endif
if (isblock)
{
setnonblocking(sock, 1);
}
return sendlen;
}
inline int sendpack(int sock, ssl_context *ssl, const char *msgstr, int isblock)
{
unsigned char buffer[strlen(msgstr) + 9];
memset(buffer, 0, strlen(msgstr) + 9);
#if WIN32
unsigned __int64 packlen;
#else
#else
unsigned long long packlen;
#endif
packlen=strlen(msgstr);
packlen=LittleEndian_64(packlen);
memcpy(buffer,&packlen,8);
memcpy(buffer+8,msgstr,strlen(msgstr));
if(isblock)
#endif
packlen = strlen(msgstr);
packlen = LittleEndian_64(packlen);
memcpy(buffer, &packlen, 8);
memcpy(buffer + 8, msgstr, strlen(msgstr));
if (isblock)
{
setnonblocking(sock,0);
setnonblocking(sock, 0);
}
#if OPENSSL
#if OPENSSLDL
int len=SslWrite( ssl, buffer, 8+strlen(msgstr));
#else
int len=SSL_write( ssl, buffer, 8+strlen(msgstr));
#endif // OPENSSL
#else
#if ISMBEDTLS
int len=mbedtls_ssl_write(ssl, buffer, 8+strlen(msgstr));
#else
int len=ssl_write(ssl, buffer, 8+strlen(msgstr));
#endif // ISM
#endif // ISM
if(isblock)
#if OPENSSL
#if OPENSSLDL
int len = SslWrite(ssl, buffer, 8 + strlen(msgstr));
#else
int len = SSL_write(ssl, buffer, 8 + strlen(msgstr));
#endif // OPENSSL
#else
#if ISMBEDTLS
int len = mbedtls_ssl_write(ssl, buffer, 8 + strlen(msgstr));
#else
int len = ssl_write(ssl, buffer, 8 + strlen(msgstr));
#endif // ISM
#endif // ISM
if (isblock)
{
setnonblocking(sock,1);
setnonblocking(sock, 1);
}
return len;
return len;
}
inline int SendAuth(int sock,ssl_context *ssl)
inline int SendAuth(int sock, ssl_context *ssl)
{
// string str="{\"Type\":\"Auth\",\"Payload\":{\"Version\":\"2\",\"MmVersion\":\"1.7\",\"User\":\""+user+"\",\"Password\": \"\",\"OS\":\"darwin\",\"Arch\":\"amd64\",\"ClientId\":\""+ClientId+"\"}}";
// string str="{\"Type\":\"Auth\",\"Payload\":{\"Version\":\"2\",\"MmVersion\":\"1.7\",\"User\":\""+user+"\",\"Password\": \"\",\"OS\":\"darwin\",\"Arch\":\"amd64\",\"ClientId\":\""+ClientId+"\"}}";
char str[1024];
memset(str,0,1024);
sprintf(str,"{\"Type\":\"Auth\",\"Payload\":{\"Version\":\"2\",\"MmVersion\":\"1.7\",\"User\":\"%s\",\"Password\": \"%s\",\"OS\":\"darwin\",\"Arch\":\"amd64\",\"ClientId\":\"%s\"}}",mainInfo.authtoken,mainInfo.pwdc,mainInfo.ClientId.c_str());
memset(str, 0, 1024);
sprintf(str, "{\"Type\":\"Auth\",\"Payload\":{\"Version\":\"2\",\"MmVersion\":\"1.7\",\"User\":\"%s\",\"Password\": \"%s\",\"OS\":\"darwin\",\"Arch\":\"amd64\",\"ClientId\":\"%s\"}}", mainInfo.authtoken, mainInfo.pwdc, mainInfo.ClientId.c_str());
return sendpack(sock,ssl,str,1);
return sendpack(sock, ssl, str, 1);
}
inline int SendRegProxy(int sock,ssl_context *ssl)
inline int SendRegProxy(int sock, ssl_context *ssl)
{
char str[255];
memset(str,0,255);
sprintf(str,"{\"Type\":\"RegProxy\",\"Payload\":{\"ClientId\":\"%s\"}}",mainInfo.ClientId.c_str());
return sendpack(sock,ssl,str,1);
memset(str, 0, 255);
sprintf(str, "{\"Type\":\"RegProxy\",\"Payload\":{\"ClientId\":\"%s\"}}", mainInfo.ClientId.c_str());
return sendpack(sock, ssl, str, 1);
}
inline int SendPing(int sock,ssl_context *ssl)
inline int SendPing(int sock, ssl_context *ssl)
{
return sendpack(sock,ssl,"{\"Type\":\"Ping\",\"Payload\":{}}",1);
return sendpack(sock, ssl, "{\"Type\":\"Ping\",\"Payload\":{}}", 1);
}
inline int SendPong(int sock,ssl_context *ssl)
inline int SendPong(int sock, ssl_context *ssl)
{
return sendpack(sock,ssl,"{\"Type\":\"Pong\",\"Payload\":{}}",1);
return sendpack(sock, ssl, "{\"Type\":\"Pong\",\"Payload\":{}}", 1);
}
int SendReqTunnel(int sock,ssl_context *ssl,TunnelInfo *tunnelInfo);
int SendReqTunnel(int sock, ssl_context *ssl, TunnelInfo *tunnelInfo);
//#endif
#endif

15
udp.h
View File

@@ -5,7 +5,7 @@
#include <time.h>
#include <list>
#if WIN32
#include <winsock.h>//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD>bytestool.hǰ<EFBFBD><EFBFBD>
#include <winsock.h> //这个得放bytestool.h前面
#endif
#include "string.h"
#include "cJSON.h"
@@ -13,20 +13,19 @@
#include "base64.h"
#include "bytestool.h"
using namespace std;
int SendUdpReqTunnel(int sock, struct sockaddr_in servAddr,TunnelInfo *tunnelinfo);
int SendUdpPack(int sock, struct sockaddr_in servAddr,const char *msgstr);
int SendUdpReqTunnel(int sock, struct sockaddr_in servAddr, TunnelInfo *tunnelinfo);
int SendUdpPack(int sock, struct sockaddr_in servAddr, const char *msgstr);
int SendUdpPing(int sock, struct sockaddr_in servAddr);
int CheckUdpPing(int sock, struct sockaddr_in servAddr);
int SendUdpAuth(int sock,struct sockaddr_in servAddr);
int SendUdpProxy(int sock,struct sockaddr_in servAddr,char* Data,char* Url,const char* ClientAddr);
int GetUdpRemoteAddr(int localport,char *url);
int SendUdpAuth(int sock, struct sockaddr_in servAddr);
int SendUdpProxy(int sock, struct sockaddr_in servAddr, char *Data, char *Url, const char *ClientAddr);
int GetUdpRemoteAddr(int localport, char *url);
int UdpClient();
int initUdp();
int CheckUdpPing(int sock);
int CheckUdpAuth(int sock);
int CheckRegTunnel(int sock);
int UdpRecv(fd_set* readSet);
int UdpRecv(fd_set *readSet);
#endif // UDP_H_INCLUDED