[6] | 1 | // LocalProxy.cpp : ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´ëÇÑ ÁøÀÔÁ¡À» Á¤ÀÇÇÕ´Ï´Ù.
|
---|
| 2 | //
|
---|
| 3 |
|
---|
| 4 | #ifdef _WIN32
|
---|
| 5 | # define _CRT_SECURE_NO_WARNINGS
|
---|
| 6 | # include <process.h>
|
---|
| 7 | #else
|
---|
| 8 | # include <errno.h>
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | #include "CRXSocket.h"
|
---|
| 12 | #include "CRXHttpRequest.h"
|
---|
| 13 | #include "CRXHttpResponse.h"
|
---|
| 14 |
|
---|
| 15 | #include <iostream>
|
---|
| 16 | #include <iomanip>
|
---|
| 17 | #include <sstream>
|
---|
| 18 |
|
---|
| 19 | #include <stdio.h>
|
---|
| 20 | #include <string.h>
|
---|
| 21 |
|
---|
| 22 | using namespace std;
|
---|
| 23 |
|
---|
| 24 | /*-----------------------------------------------------------------*/
|
---|
| 25 | int CRXCreateThread (int (* aThreadFunc) (void *), void * aThreadArg);
|
---|
| 26 | int CRXProxyThread (void * aThreadArg);
|
---|
| 27 |
|
---|
| 28 | /* error */
|
---|
| 29 | #ifdef _WIN32
|
---|
| 30 | # define CRX_ERROR() GetLastError ()
|
---|
| 31 | # ifndef __func__
|
---|
| 32 | # define __func__ __FUNCTION__
|
---|
| 33 | # endif
|
---|
| 34 | #else
|
---|
| 35 | # define CRX_ERROR() errno
|
---|
| 36 | #endif
|
---|
| 37 |
|
---|
| 38 | #define CRX_PRINT_ERROR(__code, __message) \
|
---|
| 39 | do { \
|
---|
[17] | 40 | char __error[4096] = {0x00, }; \
|
---|
[6] | 41 | ostringstream __stream; \
|
---|
| 42 | __stream << __message; \
|
---|
| 43 | string __string = __stream.str (); \
|
---|
[14] | 44 | sprintf (__error, "[%s][%d] (%d) %s\n", __func__, __LINE__, __code, __string.c_str ()); \
|
---|
[6] | 45 | cout << __error; \
|
---|
| 46 | } while (0)
|
---|
| 47 |
|
---|
| 48 | typedef struct __crx_proxy_thread_argument__
|
---|
| 49 | {
|
---|
| 50 | int mSocket;
|
---|
| 51 | } CRXProxyThreadArgs;
|
---|
| 52 | /*----------------------------------------------------------------*/
|
---|
| 53 |
|
---|
| 54 | int main (int argc, char* argv[])
|
---|
| 55 | {
|
---|
[14] | 56 | int aResult = 0;
|
---|
| 57 | const unsigned short aPort = 8080;
|
---|
[6] | 58 |
|
---|
| 59 | CRXSocket aSocket;
|
---|
| 60 | CRXProxyThreadArgs * aThreadArg;
|
---|
| 61 |
|
---|
| 62 | /*----------------------------------------------------------------*/
|
---|
| 63 | /*----------------------------------------------------------------
|
---|
| 64 | * Initialize
|
---|
| 65 | *----------------------------------------------------------------*/
|
---|
| 66 | aResult = aSocket.CreateServer (aPort);
|
---|
| 67 | if (aResult < 0)
|
---|
| 68 | {
|
---|
| 69 | CRX_PRINT_ERROR (aResult, "failed to create server");
|
---|
| 70 | return aResult;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | for (;;)
|
---|
| 74 | {
|
---|
| 75 | aResult = aSocket.Accept ();
|
---|
| 76 | if (aResult < 0)
|
---|
| 77 | {
|
---|
| 78 | CRX_PRINT_ERROR (aResult, "failed to accept");
|
---|
| 79 | break;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if ((aThreadArg = new(std::nothrow) CRXProxyThreadArgs) == NULL)
|
---|
| 83 | {
|
---|
| 84 | aResult = -1;
|
---|
| 85 | break;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | memset (aThreadArg, 0x0, sizeof (CRXProxyThreadArgs));
|
---|
| 89 | aThreadArg->mSocket = aResult;
|
---|
| 90 |
|
---|
| 91 | aResult = CRXCreateThread (CRXProxyThread, aThreadArg);
|
---|
| 92 | if (aResult < 0)
|
---|
| 93 | {
|
---|
| 94 | aResult = CRX_ERROR ();
|
---|
| 95 | CRX_PRINT_ERROR (aResult, "failed to create thread");
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /*----------------------------------------------------------------*/
|
---|
| 101 | aSocket.Close ();
|
---|
| 102 |
|
---|
| 103 | return aResult;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | int
|
---|
| 107 | CRXCreateThread (int (* aThreadFunc) (void *), void * aThreadArg)
|
---|
| 108 | {
|
---|
| 109 | #ifdef _WIN32
|
---|
| 110 | return _beginthread ((void (*) (void *))aThreadFunc, 0, aThreadArg);
|
---|
| 111 | #else
|
---|
| 112 | pthread_t aThreadID = 0;
|
---|
| 113 |
|
---|
| 114 | return pthread_create (&aThreadID, NULL, (void * (*) (void *))aThreadFunc, aThreadArg);
|
---|
| 115 | #endif
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | int
|
---|
| 119 | CRXProxyThread (void * aThreadArg)
|
---|
| 120 | {
|
---|
[14] | 121 | int aResult = 0;
|
---|
| 122 | int aReceivedSize = 0;
|
---|
[6] | 123 |
|
---|
[17] | 124 | const unsigned int aBufferSize = 1024 * 16;
|
---|
[14] | 125 | char aBuffer[aBufferSize] = {0x00, };
|
---|
[6] | 126 |
|
---|
[14] | 127 | CRXHttpRequest aHttpRequest;
|
---|
| 128 | CRXHttpResponse aHttpResponse;
|
---|
[6] | 129 |
|
---|
[14] | 130 | CRXSocket aWebServer;
|
---|
| 131 | CRXSocket aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket;
|
---|
[6] | 132 |
|
---|
[14] | 133 | bool aIsMoreRequest = false;
|
---|
[17] | 134 |
|
---|
[6] | 135 | /*----------------------------------------------------------------*/
|
---|
| 136 | delete (CRXProxyThreadArgs *)aThreadArg;
|
---|
| 137 |
|
---|
| 138 | do
|
---|
| 139 | {
|
---|
| 140 | /* BROWSER --- request ---> [PROXY] SERVER */
|
---|
| 141 | aReceivedSize = aWebBrowser.Receive (aBuffer, aBufferSize);
|
---|
| 142 | if (aReceivedSize < 0)
|
---|
| 143 | {
|
---|
[14] | 144 | CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client (" << CRX_ERROR () << ")");
|
---|
[16] | 145 | return aReceivedSize;
|
---|
[6] | 146 | }
|
---|
| 147 |
|
---|
| 148 | /* BROWSER ?--- waiting --- [PROXY] --- connect ---> SERVER */
|
---|
| 149 | if (!aWebServer)
|
---|
| 150 | {
|
---|
| 151 | /* parse http request */
|
---|
| 152 | aHttpRequest = aBuffer;
|
---|
| 153 |
|
---|
| 154 | /* connect */
|
---|
[14] | 155 | aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1);
|
---|
[6] | 156 | if (aResult < 0)
|
---|
| 157 | {
|
---|
[16] | 158 | CRX_PRINT_ERROR (aResult, "failed to connect to server ("
|
---|
| 159 | << CRX_ERROR ()
|
---|
| 160 | << ") <"
|
---|
| 161 | << aHttpRequest.GetHost ()
|
---|
| 162 | << ":"
|
---|
| 163 | << aHttpRequest.GetPort ()
|
---|
| 164 | << "><"
|
---|
| 165 | << aHttpRequest.GetURL ()
|
---|
| 166 | << ">");
|
---|
[6] | 167 | return aResult;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /* BROWSER ?--- waiting --- [PROXY] --- request ---> SERVER */
|
---|
[15] | 172 | aResult = aWebServer.Send (aBuffer, aReceivedSize);
|
---|
| 173 | if (aResult != aReceivedSize)
|
---|
[6] | 174 | {
|
---|
[16] | 175 | CRX_PRINT_ERROR (aResult, "failed to send to server <"
|
---|
| 176 | << aHttpRequest.GetHost ()
|
---|
| 177 | << ":"
|
---|
| 178 | << aHttpRequest.GetPort ()
|
---|
| 179 | << CRX_ERROR ()
|
---|
| 180 | << ")");
|
---|
[6] | 181 | return aResult;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /* BROWSER <--- response--- [PROXY] <--- response--- SERVER */
|
---|
| 185 | for (;;)
|
---|
| 186 | {
|
---|
| 187 | memset (aBuffer, 0x00, aBufferSize);
|
---|
| 188 |
|
---|
[15] | 189 | aResult = aWebServer.Receive (aBuffer, aBufferSize);
|
---|
| 190 | if (aResult < 0)
|
---|
[6] | 191 | {
|
---|
[16] | 192 | CRX_PRINT_ERROR (aResult, "failed to receive ("
|
---|
| 193 | << CRX_ERROR ()
|
---|
| 194 | << ") <"
|
---|
| 195 | << aHttpRequest.GetURL ()
|
---|
| 196 | << ">");
|
---|
[6] | 197 | break;
|
---|
| 198 | }
|
---|
[15] | 199 | else if (aResult == 0)
|
---|
[6] | 200 | {
|
---|
| 201 | break;
|
---|
| 202 | }
|
---|
[15] | 203 | aReceivedSize = aResult;
|
---|
[6] | 204 |
|
---|
[15] | 205 | aResult = aWebBrowser.Send (aBuffer, aReceivedSize);
|
---|
| 206 | if (aResult != aReceivedSize)
|
---|
[6] | 207 | {
|
---|
[16] | 208 | CRX_PRINT_ERROR (aResult, "failed to send ("
|
---|
| 209 | << CRX_ERROR ()
|
---|
| 210 | << ") <"
|
---|
| 211 | << aHttpRequest.GetURL ()
|
---|
| 212 | << ">");
|
---|
[6] | 213 | break;
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | } while (aIsMoreRequest);
|
---|
| 217 |
|
---|
| 218 | /*----------------------------------------------------------------*/
|
---|
| 219 | aWebBrowser.Close ();
|
---|
| 220 | aWebServer.Close ();
|
---|
| 221 |
|
---|
| 222 | return aResult;
|
---|
| 223 | }
|
---|