kingate代理软件--一个不错的开源代理(不过里面代码好乱),win32调试日记

kingate代理软件--一个不错的开源代理,是学习代理开发很好开源软件,不过里面代码好乱。我这几天我看代码发现,它里面是提供win32代码,作者已经把源代码开源出来了,为什么在windows下还是把几函数实现没有公布出来。真假。。。我试着把它放在vs2008编译。只要实现那几个没实现的函数,或者你把main.cpp中#define _WIN32_SERVICE屏蔽。再把utils.cpp中#include <afxmt.h>修改为#include <time.h>,就可以。下面是我网上找到的那几个函数实现:

 

forwin32.cpp

///

 

 

#include<time.h>

#include "forwin32.h"

#include "kingate.h"

#include "malloc_debug.h"

 

#ifdef _WIN32

 

SERVICE_STATUS        ServiceStatus;

SERVICE_STATUS_HANDLE ServiceStatusHandle;

 

int  pthread_mutex_init(pthread_mutex_t *mutex,void *t)

{

// char tmp[100];

// sprintf(tmp,"lock%d%d",rand(),time(NULL));

*mutex=CreateMutex(NULL,FALSE,NULL);

return 0;

};

int pthread_mutex_destroy(pthread_mutex_t *mutex)

{

CloseHandle(*mutex);

return 0;

}

 

extern void StartAll();

extern void StopAll();

void WINAPI ThreadProc()

{

    StartAll();

}

 

void WINAPI kingateControl(DWORD dwCode)

{

    switch(dwCode)

    {

    case SERVICE_CONTROL_PAUSE:

        ServiceStatus.dwCurrentState = SERVICE_PAUSED;

        break;

 

    case SERVICE_CONTROL_CONTINUE:

        ServiceStatus.dwCurrentState = SERVICE_RUNNING;

        break;

    case SERVICE_CONTROL_STOP:

        StopAll();

        ServiceStatus.dwCurrentState  = SERVICE_STOPPED;

        ServiceStatus.dwWin32ExitCode = 0;

        ServiceStatus.dwCheckPoint    = 0;

        ServiceStatus.dwWaitHint      = 0;

        if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)

        {

            OutputDebugString("SetServiceStatus in CmdControl in Switch Error !/n");

        }

 

        return ;

    case SERVICE_CONTROL_INTERROGATE:

        break;

    default:

        break;

    }

 

    if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)

    {

        OutputDebugString("SetServiceStatus in CmdControl out Switch Error !/n");

    }

}

 

void WINAPI kingateMain(DWORD argc, LPTSTR * argv)

{

    HANDLE    hThread;

    ServiceStatus.dwServiceType             = SERVICE_WIN32;

    ServiceStatus.dwCurrentState            = SERVICE_START_PENDING;

    ServiceStatus.dwControlsAccepted        = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;

    ServiceStatus.dwServiceSpecificExitCode = 0;

    ServiceStatus.dwWin32ExitCode           = 0;

    ServiceStatus.dwCheckPoint              = 0;

    ServiceStatus.dwWaitHint                = 0;

 

    ServiceStatusHandle = RegisterServiceCtrlHandler("kingate",kingateControl);

 

    if(ServiceStatusHandle == 0)

    {

        OutputDebugString("RegisterServiceCtrlHandler Error !/n");

        return ;

    }

 

    ServiceStatus.dwCurrentState = SERVICE_RUNNING;

    ServiceStatus.dwCheckPoint   = 0;

    ServiceStatus.dwWaitHint     = 0;

 

    if(SetServiceStatus(ServiceStatusHandle,&ServiceStatus)==0)

    {

        OutputDebugString("SetServiceStatus in CmdStart Error !/n");

        return ;

    }

 

    hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,NULL,0,NULL);

    if(hThread==NULL)

    {

        OutputDebugString("CreateThread in CmdStart Error !/n");

    }

 

    return ;

}

bool InstallService(const char * szServiceName)

{

    char             szCurrentPath[MAX_PATH];

    GetModuleFileName(NULL,szCurrentPath,MAX_PATH);

SERVICE_STATUS   InstallServiceStatus;

    DWORD dwErrorCode = 0;

    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

    if(schSCManager == NULL)

    {

        printf("Open Service Control Manager Database Failure !/n");

        return false;

    }

 

    printf("Creating Service .... ");

    SC_HANDLE schService=CreateService(schSCManager,szServiceName,szServiceName,SERVICE_ALL_ACCESS,

        SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,

        SERVICE_ERROR_IGNORE,szCurrentPath,NULL,NULL,NULL,NULL,NULL); 

    if(schService==NULL)

    {

        dwErrorCode=GetLastError();

        if(dwErrorCode!=ERROR_SERVICE_EXISTS)

        {

            printf("Failure !/n");

            CloseServiceHandle(schSCManager);

            return false;

        }

        else

        {

            printf("already Exists !/n");

            schService = OpenService(schSCManager,szServiceName,SERVICE_START);

            if(schService==NULL)

            {

                printf("Opening Service .... Failure !/n");

                CloseServiceHandle(schSCManager);

                return false;

            }

        }

    }

    else

    {

        printf("Success !/n");

    }

 

    printf("Starting Service .... ");

    if(StartService(schService,0,NULL)==0)                         

    {

        dwErrorCode=GetLastError();

        if(dwErrorCode==ERROR_SERVICE_ALREADY_RUNNING)

        {

            printf("already Running !/n");

            CloseServiceHandle(schSCManager);  

            CloseServiceHandle(schService);

            return true;

        }

    }

    else

    {

        printf("Pending ... ");

    }

 

    while(QueryServiceStatus(schService,&InstallServiceStatus)!=0)           

    {

        if(InstallServiceStatus.dwCurrentState==SERVICE_START_PENDING)

        {

            Sleep(100);

        }

        else

        {

            break;

        }

    }

    if(InstallServiceStatus.dwCurrentState!=SERVICE_RUNNING)

    {

        printf("Failure !/n");                       

    }

    else

    {

        printf("Success !/n");

    }

 

    CloseServiceHandle(schSCManager);

    CloseServiceHandle(schService);

 

    return InstallServiceStatus.dwCurrentState == SERVICE_RUNNING;

}

bool UninstallService(const char * szServiceName)

{

    DWORD dwErrorCode = 0;

    SERVICE_STATUS   RemoveServiceStatus;

    SC_HANDLE schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

    if(schSCManager == NULL)

    {

        printf("Opening SCM ......... ");

        dwErrorCode = GetLastError();

        if(dwErrorCode != 5)

        {

            printf("Failure !/n"); 

        }

        else

        {

            printf("Failuer ... Access is Denied !/n");

        }

        return false;

    }

 

    SC_HANDLE schService = OpenService(schSCManager,szServiceName,SERVICE_ALL_ACCESS);

    if(schService==NULL) 

    {

        printf("Opening Service ..... ");

        dwErrorCode=GetLastError();

        if(dwErrorCode==1060)

        {

            printf("no Exists !/n");

        }

        else

        {

            printf("Failure !/n");

        }

        CloseServiceHandle(schSCManager);

        return false;

    }

    else

    {

        printf("Stopping Service .... ");

        if(QueryServiceStatus(schService,&RemoveServiceStatus)!=0)

        {

            if(RemoveServiceStatus.dwCurrentState==SERVICE_STOPPED)

            {

                printf("already Stopped !/n"); 

            }

            else

            {

                printf("Pending ... ");

                if(ControlService(schService,SERVICE_CONTROL_STOP,&RemoveServiceStatus)!=0)

                {

                    while(RemoveServiceStatus.dwCurrentState==SERVICE_STOP_PENDING)         

                    {

                        Sleep(10);

                        QueryServiceStatus(schService,&RemoveServiceStatus);

                    }

                    if(RemoveServiceStatus.dwCurrentState==SERVICE_STOPPED)

                    {

                        printf("Success !/n");

                    }

                    else

                    {

                        printf("Failure !/n");

                    }

                }

                else

                {

                    printf("Failure !/n");          

                }

            }

        }

        else

        {

            printf("Query Failure !/n");

        }

 

        printf("Removing Service .... ");     

        if(DeleteService(schService)==0)

        {

            printf("Failure !/n");   

        }

        else

        {

            printf("Success !/n");

        }

    }

 

    CloseServiceHandle(schSCManager);        

    CloseServiceHandle(schService);

    return RemoveServiceStatus.dwCurrentState == SERVICE_STOPPED;

}

 

void Log(const char *format, va_list argptr)

{

    char szMsg[1024];

    _vsnprintf(szMsg, sizeof(szMsg)-2, format, argptr);

    szMsg[sizeof(szMsg)-1] = '/0';

    strcat(szMsg, "/n");

 

    OutputDebugString(szMsg);

}

 

void LogEvent(LPCTSTR pFormat, ...)

{

    va_list args;

    va_start(args,pFormat);

    Log(pFormat, args);

    va_end(args);

}

/*

int

strerror_r(int err, char *errbuf, size_t lerrbuf)

{

LPTSTR lpszMsgBuf = NULL;

char b[80];

 

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |

 FORMAT_MESSAGE_FROM_SYSTEM |

 FORMAT_MESSAGE_IGNORE_INSERTS,

 NULL, (DWORD)err,

 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

                  (LPTSTR)&lpszMsgBuf, 0, NULL);

 

    if ( lpszMsgBuf == NULL ) {

sprintf(b, "Error: (%d)", err);

if ( lerrbuf > 0 ) strncpy(errbuf, b, lerrbuf-1);

return(-1);

 

    }

 

    strncpy(errbuf, lpszMsgBuf, MIN(lerrbuf-1, strlen(lpszMsgBuf)-1));

    LocalFree(lpszMsgBuf);

    return(0);

}

*/

#endif

 

 

 

kingate一个代理服务器,能代理多种协议,包括:http,https,socks,ftp,pop3,smtp,telnet,dns。能运行在linux,freebsd,...及其它类unix,还有windows(最新版本要求nt 4.0以上)操作系统上 kingate一个多线程的程序(采用线程池的方式),所以短时间内大量的连接请求,kingate还能保持很高的性能。 kingate还支持tcp端口映射,可以实现别人访问你的内网主机。 kingate有强大的规则控制,包括源IP地址,目的IP地址,目的端口,及采用的代理协议。 kingate也支持时间控制。即可以设置什么时间内,所设置的规则才有效。 kingate内置了用户认证,以及记录用户的使用情况,包括用户的使用时间,接收和发送的数据量,可以很容易实现对用户的记费。 kingate采用http来进行管理,使得管理kingate及查看运行信息变得一件很轻松的事情,也可以远程管理。 kingate还支持内存及硬盘共二级缓存(仅限http代理). 最新版本的kingate还可以用来加速web服务器(采用缓存原理)。提升你的web服务器的性能。 kingate还是一款国人开发的代理服务器。 特别提示: 此版本经过WINDOWS & VS2005编译通过。 如果你需要自己定制一个代理服务器类软件。这份源码一定会满足你的需要。 1. 开源社区上这个源码WINDOWS下编译不过。这个是经过修改的。 2. 原版本需要配置, 本版本去掉配置文件. 直接程序内配置. 3. 设置输出路径更合理.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值