tcp_proxy_server.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. hevent_set_userdata(io, NULL);
  52. hio_close(proxyio);
  53. }
  54. }
  55. static void on_recv(hio_t* io, void* buf, int readbytes) {
  56. printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  57. char localaddrstr[SOCKADDR_STRLEN] = {0};
  58. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  59. printf("[%s] <=> [%s]\n",
  60. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  61. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  62. printf("< %.*s", readbytes, (char*)buf);
  63. hio_t* proxyio = (hio_t*)hevent_userdata(io);
  64. assert(proxyio != NULL);
  65. hio_write(proxyio, buf, readbytes);
  66. }
  67. static void on_accept(hio_t* io) {
  68. printf("on_accept connfd=%d\n", hio_fd(io));
  69. char localaddrstr[SOCKADDR_STRLEN] = {0};
  70. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  71. printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
  72. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  73. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  74. hio_t* proxyio = hloop_create_tcp_client(hevent_loop(io), proxy_host, proxy_port, on_proxy_connect);
  75. if (proxyio == NULL) {
  76. hio_close(io);
  77. return;
  78. }
  79. hio_setcb_read(io, on_recv);
  80. hio_setcb_close(io, on_close);
  81. hevent_set_userdata(io, proxyio);
  82. hevent_set_userdata(proxyio, io);
  83. }
  84. int main(int argc, char** argv) {
  85. if (argc < 3) {
  86. printf("Usage: tcp port proxy_host:proxy_port\n");
  87. return -10;
  88. }
  89. int port = atoi(argv[1]);
  90. char* pos = strchr(argv[2], ':');
  91. if (pos) {
  92. int len = pos - argv[2];
  93. if (len > 0) {
  94. memcpy(proxy_host, argv[2], len);
  95. proxy_host[len] = '\0';
  96. }
  97. proxy_port = atoi(pos + 1);
  98. } else {
  99. strncpy(proxy_host, argv[2], sizeof(proxy_host));
  100. }
  101. if (proxy_port == 0) proxy_port = 80;
  102. printf("proxy: [%s:%d]\n", proxy_host, proxy_port);
  103. hloop_t* loop = hloop_new(0);
  104. hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
  105. if (listenio == NULL) {
  106. return -20;
  107. }
  108. printf("listenfd=%d\n", hio_fd(listenio));
  109. hloop_run(loop);
  110. hloop_free(&loop);
  111. return 0;
  112. }