• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    穿透代理服務器編程

    發布: 2007-7-14 21:11 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 92次 | 進入軟件測試論壇討論

    領測軟件測試網 田進恩 allfresh@263.net


        關鍵詞:代理服務器、Socks4、Socks5、Http代理  

        在網絡程序設計過程中,我們經常要與各種類型的代理服務器打交道,比如在企業內部網通過代理去訪問Internet網上的服務器等等,一般代理服務器支持幾種常見的代理協議標準,如Socks4,Socks5,Http代理,其中Socks5需要用戶驗證,代理相對復雜。我在查閱RFC文檔和相關資料后,特總結一些TCP協議穿透代理服務器的程序片斷,希望對大家有所幫助。

    //使用到的結構
    struct sock4req1
    {
    char VN;
    char CD;
    unsigned short Port;
    unsigned long IPAddr;
    char other[1];
    };

    struct sock4ans1
    {
    char VN;
    char CD;
    };

    struct sock5req1
    {
    char Ver;
    char nMethods;
    char Methods[255];
    };

    struct sock5ans1
    {
    char Ver;
    char Method;
    };

    struct sock5req2
    {
    char Ver;
    char Cmd;
    char Rsv;
    char Atyp;
    char other[1];
    };

    struct sock5ans2
    {
    char Ver;
    char Rep;
    char Rsv;
    char Atyp;
    char other[1];
    };

    struct authreq
    {
    char Ver;
    char Ulen;
    char Name[255];
    char PLen;
    char Pass[255];
    };

    struct authans
    {
    char Ver;
    char Status;
    };

    //通過Socks4方式代理
    if( !ClientSock.Connect( g_ProxyInfo.m_strProxyIP,g_ProxyInfo.m_nProxyPort) )
    {
    m_sError = _T("不能連接到代理服務器!");
    ClientSock.Close();
    return FALSE;
    }
    char buff[100];
    memset(buff,0,100);
    struct sock4req1 *m_proxyreq;
    m_proxyreq = (struct sock4req1 *)buff;
    m_proxyreq->VN = 4;
    m_proxyreq->CD = 1;
    m_proxyreq->Port = ntohs(GetPort());
    m_proxyreq->IPAddr = inet_addr(GetServerHostName());
    ClientSock.Send(buff,9);
    struct sock4ans1 *m_proxyans;
    m_proxyans = (struct sock4ans1 *)buff;
    memset(buff,0,100);
    ClientSock.Receive(buff,100);
    if(m_proxyans->VN != 0 || m_proxyans->CD != 90)
    {
    m_sError = _T("通過代理連接主站不成功!");
    ClientSock.Close();
    return FALSE;
    }


      

    //通過Socks5方式代理
    if( !ClientSock.Connect( g_ProxyInfo.m_strProxyIP,g_ProxyInfo.m_nProxyPort) )
    {
    m_sError = _T("不能連接到代理服務器!");
    ClientSock.Close();
    return FALSE;
    }
    char buff[600];
    struct sock5req1 *m_proxyreq1;
    m_proxyreq1 = (struct sock5req1 *)buff;
    m_proxyreq1->Ver = 5;
    m_proxyreq1->nMethods = 2;
    m_proxyreq1->Methods[0] = 0;
    m_proxyreq1->Methods[1] = 2;
    ClientSock.Send(buff,4);
    struct sock5ans1 *m_proxyans1;
    m_proxyans1 = (struct sock5ans1 *)buff;
    memset(buff,0,600);
    ClientSock.Receive(buff,600);
    if(m_proxyans1->Ver != 5 || (m_proxyans1->Method!=0 && m_proxyans1->Method!=2))
    {
    m_sError = _T("通過代理連接主站不成功!");
    ClientSock.Close();
    return FALSE;
    }
    if(m_proxyans1->Method == 2)
    {
    int nUserLen = strlen(g_ProxyInfo.m_strProxyUser);
    int nPassLen = strlen(g_ProxyInfo.m_strProxyPass);
    struct authreq *m_authreq;
    m_authreq = (struct authreq *)buff;
    m_authreq->Ver = 1;
    m_authreq->Ulen = nUserLen;
    strcpy(m_authreq->Name,g_ProxyInfo.m_strProxyUser);
    m_authreq->PLen = nPassLen;
    strcpy(m_authreq->Pass,g_ProxyInfo.m_strProxyPass);
    ClientSock.Send(buff,513);
    struct authans *m_authans;
    m_authans = (struct authans *)buff;
    memset(buff,0,600);
    ClientSock.Receive(buff,600);
    if(m_authans->Ver != 1 || m_authans->Status != 0)
    {
    m_sError = _T("代理服務器用戶驗證不成功!");
    ClientSock.Close();
    return FALSE;
    }
    }
    struct sock5req2 *m_proxyreq2;
    m_proxyreq2 = (struct sock5req2 *)buff;
    m_proxyreq2->Ver = 5;
    m_proxyreq2->Cmd = 1;
    m_proxyreq2->Rsv = 0;
    m_proxyreq2->Atyp = 1;
    unsigned long tmpLong = inet_addr(GetServerHostName());
    unsigned short port = ntohs(GetPort());
    memcpy(m_proxyreq2->other,&tmpLong,4);
    memcpy(m_proxyreq2->other+4,&port,2);
    ClientSock.Send(buff,sizeof(struct sock5req2)+5);
    struct sock5ans2 *m_proxyans2;
    memset(buff,0,600);
    m_proxyans2 = (struct sock5ans2 *)buff;
    ClientSock.Receive(buff,600);
    if(m_proxyans2->Ver != 5 || m_proxyans2->Rep != 0)
    {
    m_sError = _T("通過代理連接主站不成功!");
    ClientSock.Close();
    return FALSE;
    }


      

    //通過HTTP方式代理
    if( !ClientSock.Connect( g_ProxyInfo.m_strProxyIP,g_ProxyInfo.m_nProxyPort) )
    {
    m_sError = _T("不能連接到代理服務器!");
    ClientSock.Close();
    return FALSE;
    }
    char buff[600];
    sprintf( buff, "%s%s:%d%s","CONNECT ",GetServerHostName(),GetPort()," HTTP/1.1\r\nUser-Agent: MyApp/0.1\r\n\r\n");
    ClientSock.Send(buff,strlen(buff)); //發送請求
    memset(buff,0,600);
    ClientSock.Receive(buff,600);
    if(strstr(buff, "HTTP/1.0 200 Connection established") == NULL) //連接不成功
    {
    m_sError = _T("通過代理連接主站不成功!");
    ClientSock.Close();
    return FALSE;
    }


        我們一般先與代理服務器連通,然后向代理服務器發送代理驗證的用戶名和密碼(如果需要,如Socks5代理),驗證成功后,再向代理服務器發送需要連接的目的地址和端口。以上代碼僅用于TCP連接,如果在內部網偵聽或通過UDP協議發送信息,可查閱RFC1829等文檔資料。

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>