tcp_proxy_server.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * tcp proxy server
  3. *
  4. * @test: make examples
  5. * @http_server: bin/httpd -d
  6. * @proxy_server: bin/tcp_proxy_server 1234 127.0.0.1:8080
  7. * bin/tcp_proxy_server 1234 www.baidu.com
  8. * @client: bin/curl -v 127.0.0.1:1234
  9. * bin/nc 127.0.0.1 1234
  10. * > GET / HTTP/1.1
  11. * > Connection: close
  12. * > [Enter]
  13. * > GET / HTTP/1.1
  14. * > Connection: keep-alive
  15. * > [Enter]
  16. */
  17. #include "hloop.h"
  18. #include "hsocket.h"
  19. // hloop_create_tcp_server => on_accept(connio) => proxyio = hloop_create_tcp_client
  20. // on_proxy_connect => hio_read(connio) hio_read(proxyio)
  21. // on_recv(connio) => hio_write(proxyio)
  22. // on_proxy_recv(proxyio) => hio_write(connio)
  23. // on_close(connio) => hio_close(proxyio)
  24. // on_proxy(proxyio) => hio_close(connio)
  25. static char proxy_host[64] = "127.0.0.1";
  26. static int proxy_port = 80;
  27. static void on_proxy_close(hio_t* proxyio) {
  28. hio_t* connio = (hio_t*)hevent_userdata(proxyio);
  29. if (connio) {
  30. hevent_set_userdata(proxyio, NULL);
  31. hio_close(connio);
  32. }
  33. }
  34. static void on_proxy_recv(hio_t* proxyio, void* buf, int readbytes) {
  35. hio_t* connio = (hio_t*)hevent_userdata(proxyio);
  36. assert(connio != NULL);
  37. hio_write(connio, buf, readbytes);
  38. }
  39. static void on_proxy_connect(hio_t* proxyio) {
  40. hio_t* connio = (hio_t*)hevent_userdata(proxyio);
  41. assert(connio != NULL);
  42. hio_read(connio);
  43. hio_setcb_close(proxyio, on_proxy_close);
  44. hio_setcb_read(proxyio, on_proxy_recv);
  45. hio_read(proxyio);
  46. }
  47. static void on_close(hio_t* io) {
  48. printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  49. hio_t* proxyio = (hio_t*)hevent_userdata(io);
  50. if (proxyio) {
  51. hio_close(proxyio);
  52. }
  53. }
  54. static void on_recv(hio_t* io, void* buf, int readbytes) {
  55. printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  56. char localaddrstr[SOCKADDR_STRLEN] = {0};
  57. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  58. printf("[%s] <=> [%s]\n",
  59. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  60. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  61. printf("< %.*s", readbytes, (char*)buf);
  62. hio_t* proxyio = (hio_t*)hevent_userdata(io);
  63. assert(proxyio != NULL);
  64. hio_write(proxyio, buf, readbytes);
  65. }
  66. static void on_accept(hio_t* io) {
  67. printf("on_accept connfd=%d\n", hio_fd(io));
  68. char localaddrstr[SOCKADDR_STRLEN] = {0};
  69. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  70. printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
  71. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  72. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  73. hio_setcb_read(io, on_recv);
  74. hio_setcb_close(io, on_close);
  75. hio_t* proxyio = hloop_create_tcp_client(hevent_loop(io), proxy_host, proxy_port, on_proxy_connect);
  76. if (proxyio == NULL) {
  77. hio_close(io);
  78. }
  79. hevent_set_userdata(proxyio, io);
  80. hevent_set_userdata(io, proxyio);
  81. }
  82. int main(int argc, char** argv) {
  83. if (argc < 3) {
  84. printf("Usage: tcp port proxy_host:proxy_port\n");
  85. return -10;
  86. }
  87. int port = atoi(argv[1]);
  88. char* pos = strchr(argv[2], ':');
  89. if (pos) {
  90. int len = pos - argv[2];
  91. if (len > 0) {
  92. memcpy(proxy_host, argv[2], len);
  93. proxy_host[len] = '\0';
  94. }
  95. proxy_port = atoi(pos + 1);
  96. } else {
  97. strncpy(proxy_host, argv[2], sizeof(proxy_host));
  98. }
  99. if (proxy_port == 0) proxy_port = 80;
  100. printf("proxy: [%s:%d]\n", proxy_host, proxy_port);
  101. hloop_t* loop = hloop_new(0);
  102. hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
  103. if (listenio == NULL) {
  104. return -20;
  105. }
  106. printf("listenfd=%d\n", hio_fd(listenio));
  107. hloop_run(loop);
  108. hloop_free(&loop);
  109. return 0;
  110. }