├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── libraries
│ └── Maven__io_netty_netty_all_4_1_3_Final.xml
├── misc.xml
├── modules.xml
├── uiDesigner.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── pom.xml
└── src
└── main
└── java
└── com
└── renhuanhuan
├── fileServer
├── HttpFileServer.java
└── HttpFileServerHandler.java
└── proxy
├── BackendHandlerInitializer.java
├── FrontendHandlerInitializer.java
├── HttpBackendHandler.java
├── HttpFrontendHandler.java
└── HttpProxyServer.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | target/
3 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | netty-http-proxy
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/libraries/Maven__io_netty_netty_all_4_1_3_Final.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 | true
87 | DEFINITION_ORDER
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 | project
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 | 1473332447730
592 |
593 |
594 | 1473332447730
595 |
596 |
597 |
598 |
599 |
600 | 1473332705738
601 |
602 |
603 |
604 | 1473332705738
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 | file://$PROJECT_DIR$/src/main/java/com/renhuanhuan/fileServer/HttpFileServerHandler.java
664 | 116
665 |
666 |
667 |
668 |
669 | file://$PROJECT_DIR$/src/main/java/com/renhuanhuan/fileServer/HttpFileServerHandler.java
670 | 143
671 |
672 |
673 |
674 |
675 | file://$PROJECT_DIR$/src/main/java/com/renhuanhuan/fileServer/HttpFileServerHandler.java
676 | 29
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 |
893 |
894 |
895 |
896 | No facets are configured
897 |
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
909 |
910 |
911 |
912 |
913 |
914 |
915 |
916 |
917 |
918 |
919 | 1.8
920 |
921 |
922 |
923 |
924 |
925 |
926 |
927 |
928 |
929 |
930 |
931 | netty-http-proxy
932 |
933 |
934 |
935 |
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 | 1.8
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 |
953 |
954 |
955 |
956 | Maven: io.netty:netty-all:4.1.3.Final
957 |
958 |
959 |
960 |
961 |
962 |
963 |
964 |
965 |
966 |
967 |
968 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # netty-http-proxy-server
2 | Http proxy server based on netty
3 |
4 | 
5 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.renhuanhuan
8 | netty-http-proxy
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | io.netty
14 | netty-all
15 | 4.1.3.Final
16 |
17 |
18 |
19 |
20 |
21 |
22 | org.apache.maven.plugins
23 | maven-shade-plugin
24 | 2.4.3
25 |
26 |
27 | package
28 |
29 | shade
30 |
31 |
32 |
33 |
34 | com.renhuanhuan.proxy.HttpProxyServer
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-compiler-plugin
44 |
45 | 1.8
46 | 1.8
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/fileServer/HttpFileServer.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.fileServer;
2 |
3 | import io.netty.bootstrap.ServerBootstrap;
4 | import io.netty.channel.ChannelInitializer;
5 | import io.netty.channel.EventLoopGroup;
6 | import io.netty.channel.nio.NioEventLoopGroup;
7 | import io.netty.channel.socket.SocketChannel;
8 | import io.netty.channel.socket.nio.NioServerSocketChannel;
9 | import io.netty.handler.codec.http.HttpObjectAggregator;
10 | import io.netty.handler.codec.http.HttpServerCodec;
11 | import io.netty.handler.stream.ChunkedWriteHandler;
12 |
13 | /**
14 | * Created by rahul on 9/17/16.
15 | */
16 | public class HttpFileServer {
17 |
18 | public void start(final int port, final String url) throws InterruptedException {
19 |
20 | EventLoopGroup bossGroup = new NioEventLoopGroup();
21 | EventLoopGroup workerGroup = new NioEventLoopGroup();
22 |
23 | try {
24 | ServerBootstrap b = new ServerBootstrap();
25 | b.group(bossGroup, workerGroup)
26 | .channel(NioServerSocketChannel.class)
27 | .childHandler(new ChannelInitializer() {
28 | @Override
29 | protected void initChannel(SocketChannel ch) throws Exception {
30 | ch.pipeline().addLast(new HttpServerCodec())
31 | .addLast(new HttpObjectAggregator(1024 * 1024))
32 | .addLast(new ChunkedWriteHandler())
33 | .addLast(new HttpFileServerHandler(url));
34 | }
35 | }).bind(port).sync().channel().closeFuture().sync();
36 | } finally {
37 | bossGroup.shutdownGracefully();
38 | workerGroup.shutdownGracefully();
39 | }
40 | }
41 |
42 | public static void main(String[] args) throws InterruptedException {
43 | int port = 8080;
44 | String url = "/home/rahul";
45 | new HttpFileServer().start(port, url);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/fileServer/HttpFileServerHandler.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.fileServer;
2 |
3 | import io.netty.buffer.ByteBuf;
4 | import io.netty.buffer.Unpooled;
5 | import io.netty.channel.*;
6 | import io.netty.handler.codec.http.*;
7 | import io.netty.handler.stream.ChunkedFile;
8 | import io.netty.util.CharsetUtil;
9 |
10 | import java.io.File;
11 | import java.io.FileNotFoundException;
12 | import java.io.RandomAccessFile;
13 | import java.io.UnsupportedEncodingException;
14 | import java.net.URLDecoder;
15 | import java.util.regex.Pattern;
16 |
17 | /**
18 | * Created by rahul on 9/17/16.
19 | */
20 | public class HttpFileServerHandler extends SimpleChannelInboundHandler {
21 |
22 | private final String url;
23 |
24 | public HttpFileServerHandler(String url) {
25 | this.url = url;
26 | }
27 |
28 | @Override
29 | protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
30 | if (msg.method() != HttpMethod.GET) {
31 | sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED);
32 | return;
33 | }
34 |
35 | final String uri = msg.uri();
36 | final String path = sanitizeUri(uri);
37 |
38 | if (path == null) {
39 | sendError(ctx, HttpResponseStatus.FORBIDDEN);
40 | return;
41 | }
42 |
43 | File file = new File(path);
44 | if (file.isHidden() || !file.exists()) {
45 | sendError(ctx, HttpResponseStatus.NOT_FOUND);
46 | return;
47 | }
48 |
49 | if (file.isDirectory()) {
50 | if (uri.endsWith("/")) {
51 | sendListing(ctx, file);
52 | } else {
53 | sendRedirect(ctx, uri + "/");
54 | }
55 | return;
56 | }
57 |
58 | if (!file.isFile()) {
59 | sendError(ctx, HttpResponseStatus.FORBIDDEN);
60 | return;
61 | }
62 |
63 | RandomAccessFile randomAccessFile;
64 | try {
65 | randomAccessFile = new RandomAccessFile(file, "r");
66 | } catch (FileNotFoundException e) {
67 | sendError(ctx, HttpResponseStatus.NOT_FOUND);
68 | return;
69 | }
70 |
71 | long fileLength = randomAccessFile.length();
72 | HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
73 | response.headers().set(HttpHeaderNames.CONTENT_LENGTH, fileLength);
74 | response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.FILE);
75 | if (msg.headers().get(HttpHeaderNames.CONNECTION).equals(HttpHeaderValues.KEEP_ALIVE)) {
76 | response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
77 | }
78 | ctx.write(response);
79 |
80 | ChannelFuture sendFileFuture = ctx.write(new ChunkedFile(randomAccessFile, 0, fileLength, 8192),
81 | ctx.newProgressivePromise());
82 | sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
83 | @Override
84 | public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception {
85 | if (total < 0) {
86 | System.out.println("Transfer progress: " + progress);
87 | } else {
88 | System.out.println("Transfer progress: " + progress + "/" + total);
89 | }
90 | }
91 |
92 | @Override
93 | public void operationComplete(ChannelProgressiveFuture future) throws Exception {
94 | System.out.println("Transfer complete.");
95 | }
96 | });
97 |
98 | ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
99 | if (msg.headers().get(HttpHeaderNames.CONNECTION).equals(HttpHeaderValues.KEEP_ALIVE)) {
100 | lastContentFuture.addListener(ChannelFutureListener.CLOSE);
101 | }
102 | }
103 |
104 | @Override
105 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
106 | cause.printStackTrace();
107 | if (ctx.channel().isActive()) {
108 | sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR);
109 | }
110 | }
111 |
112 | private static final Pattern INSECURE_URI = Pattern.compile(".*[<>&\"].*");
113 | private static final Pattern ALLOWED_FILE_NAME = Pattern.compile("[A-Za-z0-9][-_A-Za-z-0-9\\.]*");
114 |
115 | private String sanitizeUri(String uri) {
116 | try {
117 | uri = URLDecoder.decode(uri, "UTF-8");
118 | } catch (UnsupportedEncodingException e) {
119 | try {
120 | uri = URLDecoder.decode(uri, "ISO-8859-1");
121 | } catch (UnsupportedEncodingException e1) {
122 | throw new Error();
123 | }
124 | }
125 |
126 | // if (!uri.startsWith(url)) {
127 | // return null;
128 | // }
129 |
130 | if (!uri.startsWith("/")) {
131 | return null;
132 | }
133 |
134 | uri = uri.replace('/', File.separatorChar);
135 | if (uri.contains(File.separator + '.') || uri.contains('.' + File.separator) ||
136 | uri.startsWith(".") || uri.endsWith(".") || INSECURE_URI.matcher(uri).matches()) {
137 | return null;
138 | }
139 |
140 | return uri;
141 | }
142 |
143 | private void sendListing(ChannelHandlerContext ctx, File dir) {
144 | FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
145 | response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8");
146 |
147 | StringBuilder buf = new StringBuilder();
148 | String dirPath = dir.getPath();
149 |
150 | buf.append("\r\n");
151 | buf.append("");
152 | buf.append(dirPath);
153 | buf.append("\r\n");
154 |
155 | buf.append("");
156 | buf.append("Contents: ");
157 | buf.append("
\r\n");
158 |
159 | buf.append("");
160 | buf.append("- ..
\r\n");
161 | for (File f : dir.listFiles()) {
162 | if (f.isHidden() || !f.canRead()) {
163 | continue;
164 | }
165 | String name = f.getName();
166 | // if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
167 | // continue;
168 | // }
169 | buf.append("- ");
172 | buf.append(name);
173 | buf.append("
\r\n");
174 | }
175 | buf.append("
\r\n");
176 |
177 | ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
178 | response.content().writeBytes(buffer);
179 | buffer.release();
180 | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
181 | }
182 |
183 | private void sendRedirect(ChannelHandlerContext ctx, String newUri) {
184 | FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FOUND);
185 | response.headers().set(HttpHeaderNames.LOCATION, newUri);
186 | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
187 | }
188 |
189 | private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
190 | FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status,
191 | Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
192 | response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8");
193 | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
194 | }
195 | }
196 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/proxy/BackendHandlerInitializer.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.proxy;
2 |
3 | import io.netty.channel.Channel;
4 | import io.netty.channel.ChannelInitializer;
5 | import io.netty.channel.socket.SocketChannel;
6 | import io.netty.handler.codec.http.HttpClientCodec;
7 | import io.netty.handler.codec.http.HttpObjectAggregator;
8 |
9 | /**
10 | * Created by rahul on 9/8/16.
11 | */
12 | public class BackendHandlerInitializer extends ChannelInitializer{
13 |
14 | final Channel inboundChannel;
15 |
16 | public BackendHandlerInitializer(Channel inboundChannel) {
17 | this.inboundChannel = inboundChannel;
18 | }
19 |
20 | @Override
21 | public void initChannel(SocketChannel ch) throws Exception {
22 | ch.pipeline().addLast(new HttpClientCodec())
23 | .addLast(new HttpObjectAggregator(1024 * 1024))
24 | .addLast(new HttpBackendHandler(inboundChannel));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/proxy/FrontendHandlerInitializer.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.proxy;
2 |
3 | import io.netty.channel.ChannelInitializer;
4 | import io.netty.channel.socket.SocketChannel;
5 | import io.netty.handler.codec.http.HttpObjectAggregator;
6 | import io.netty.handler.codec.http.HttpServerCodec;
7 |
8 | /**
9 | * Created by rahul on 9/8/16.
10 | */
11 | public class FrontendHandlerInitializer extends ChannelInitializer {
12 |
13 | @Override
14 | public void initChannel(SocketChannel ch) throws Exception {
15 | ch.pipeline().addLast(new HttpServerCodec())
16 | .addLast(new HttpObjectAggregator(1024 * 1024))
17 | .addLast(new HttpFrontendHandler());
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/proxy/HttpBackendHandler.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.proxy;
2 |
3 | import io.netty.channel.*;
4 | import io.netty.handler.codec.http.FullHttpResponse;
5 |
6 | /**
7 | * Created by rahul on 9/8/16.
8 | */
9 | public class HttpBackendHandler extends SimpleChannelInboundHandler {
10 |
11 | private final Channel inboundChannel;
12 |
13 | public HttpBackendHandler(Channel inboundChannel) {
14 | this.inboundChannel = inboundChannel;
15 | }
16 |
17 | @Override
18 | public void channelActive(ChannelHandlerContext ctx) throws Exception {
19 | System.out.println("Backend Handler is Active!");
20 | super.channelActive(ctx);
21 | }
22 |
23 | @Override
24 | public void channelRead0(final ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
25 | inboundChannel.writeAndFlush(msg.retain()).addListener(new ChannelFutureListener() {
26 | public void operationComplete(ChannelFuture future) throws Exception {
27 | if (!future.isSuccess()) {
28 | future.channel().close();
29 | }
30 | }
31 | });
32 | }
33 |
34 | @Override
35 | public void channelInactive(ChannelHandlerContext ctx) {
36 | System.out.println("Backend Handler destroyed!");
37 | HttpFrontendHandler.closeOnFlush(inboundChannel);
38 | }
39 |
40 | @Override
41 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
42 | cause.printStackTrace();
43 | HttpFrontendHandler.closeOnFlush(ctx.channel());
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/proxy/HttpFrontendHandler.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.proxy;
2 |
3 | import io.netty.bootstrap.Bootstrap;
4 | import io.netty.buffer.Unpooled;
5 | import io.netty.channel.*;
6 | import io.netty.handler.codec.http.FullHttpRequest;
7 |
8 | import java.util.regex.Matcher;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | * Created by rahul on 9/8/16.
13 | */
14 | @SuppressWarnings("Duplicates")
15 | public class HttpFrontendHandler extends SimpleChannelInboundHandler{
16 |
17 | private Channel outboundChannel;
18 |
19 | @Override
20 | public void channelActive(ChannelHandlerContext ctx) throws Exception {
21 | System.out.println("FrontEnd Handler is Active!");
22 | super.channelActive(ctx);
23 | }
24 |
25 | @Override
26 | public void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception {
27 | final Channel inboundChannel = ctx.channel();
28 |
29 | String host = msg.headers().get("Host");
30 | int port = 80;
31 |
32 | String pattern = "(http://|https://)?([^:]+)(:[\\d]+)?";
33 | Pattern r = Pattern.compile(pattern);
34 | Matcher m = r.matcher(host);
35 | if (m.find()) {
36 | host = m.group(2);
37 | port = (m.group(3) == null) ? 80 : Integer.parseInt(m.group(3).substring(1));
38 | }
39 |
40 | Bootstrap b = new Bootstrap();
41 | b.group(inboundChannel.eventLoop()) // use inboundChannel thread
42 | .channel(ctx.channel().getClass())
43 | .handler(new BackendHandlerInitializer(inboundChannel));
44 |
45 | ChannelFuture f = b.connect(host, port);
46 | outboundChannel = f.channel();
47 | msg.retain();
48 | ChannelFuture channelFuture = f.addListener(new ChannelFutureListener() {
49 | public void operationComplete(ChannelFuture future) throws Exception {
50 | if (future.isSuccess()) {
51 | outboundChannel.writeAndFlush(msg);
52 | } else {
53 | inboundChannel.close();
54 | }
55 | }
56 | });
57 | }
58 |
59 | @Override
60 | public void channelInactive(ChannelHandlerContext ctx) {
61 | System.out.println("Frontend Handler destroyed!");
62 | if (outboundChannel != null) {
63 | closeOnFlush(outboundChannel);
64 | }
65 | }
66 |
67 | @Override
68 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
69 | cause.printStackTrace();
70 | closeOnFlush(ctx.channel());
71 | }
72 |
73 | static void closeOnFlush(Channel ch) {
74 | if (ch.isActive()) {
75 | ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/main/java/com/renhuanhuan/proxy/HttpProxyServer.java:
--------------------------------------------------------------------------------
1 | package com.renhuanhuan.proxy;
2 |
3 | import io.netty.bootstrap.ServerBootstrap;
4 | import io.netty.channel.EventLoopGroup;
5 | import io.netty.channel.nio.NioEventLoopGroup;
6 | import io.netty.channel.socket.nio.NioServerSocketChannel;
7 |
8 | /**
9 | * Created by rahul on 9/8/16.
10 | */
11 | public class HttpProxyServer {
12 |
13 | public static void main(String[] args) throws Exception {
14 |
15 | int LOCAL_PORT = (args.length > 0) ? Integer.parseInt(args[0]) : 5688;
16 |
17 | System.out.println("Proxying on port " + LOCAL_PORT);
18 |
19 | EventLoopGroup bossGroup = new NioEventLoopGroup(1);
20 | EventLoopGroup workerGroup = new NioEventLoopGroup();
21 |
22 | try {
23 | ServerBootstrap b = new ServerBootstrap();
24 | b.group(bossGroup, workerGroup)
25 | .channel(NioServerSocketChannel.class)
26 | .childHandler(new FrontendHandlerInitializer())
27 | .bind(LOCAL_PORT).sync().channel().closeFuture().sync();
28 | } finally {
29 | bossGroup.shutdownGracefully();
30 | workerGroup.shutdownGracefully();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------