11#![ feature( plugin, extern_prelude) ]
2- #! [ plugin ( rocket_codegen ) ]
3- extern crate colored ;
2+ extern crate futures ;
3+ extern crate hyper ;
44#[ macro_use]
55extern crate lazy_static;
6- extern crate rocket;
6+ extern crate colored;
7+
78#[ macro_use]
89extern crate serde_derive;
910
@@ -12,8 +13,6 @@ use colored::*;
1213use file_finder:: CTFile ;
1314use man:: CTMan ;
1415use ports:: CTPorts ;
15- use rocket:: http:: ContentType ;
16- use rocket:: response:: Content ;
1716use std:: env;
1817use std:: error:: Error ;
1918use std:: fs:: File ;
@@ -31,6 +30,14 @@ pub mod banner;
3130#[ macro_use]
3231pub mod ports_html;
3332
33+
34+ use futures:: future;
35+ use hyper:: rt:: Future ;
36+ use hyper:: service:: service_fn;
37+ use hyper:: { Body , Method , Request , Response , Server , StatusCode , HeaderMap } ;
38+ use hyper:: header:: CONTENT_TYPE ;
39+
40+
3441pub fn show_banner ( ) {
3542 let show_banner = env:: var ( "CTNOBANNER" ) . unwrap_or ( "false" . to_string ( ) ) ;
3643 if show_banner == "false" {
@@ -66,9 +73,10 @@ pub fn init_project(){
6673
6774pub fn start_port_listening ( ) {
6875 println ! ( "👂 Started ports web server at http://localhost:1500, CTRL+C to exit..." ) ;
69- start_rocket ( ) ;
76+ start_hyper ( ) ;
7077}
7178
79+
7280pub fn show_man ( man_entry : Option < & str > , ct_file : Option < CTFile > ) {
7381 if let Some ( ct_file) = ct_file {
7482 if let Some ( ct_man) = CTMan :: all ( & ct_file) {
@@ -83,22 +91,46 @@ pub fn show_man(man_entry: Option<&str>, ct_file: Option<CTFile>) {
8391 }
8492}
8593
86- #[ get( "/scan" ) ]
8794fn scan ( ) -> String {
8895 serde_json:: to_string ( & CTPorts :: all ( ) . unwrap ( ) ) . unwrap ( )
8996}
9097
91- # [ get ( "/" , format = "text/html" ) ]
92- fn home_page ( ) -> Content < String > {
93- Content ( ContentType :: HTML , INDEX_HTML ! ( ) . to_string ( ) )
98+
99+ fn home_page ( ) -> & ' static str {
100+ INDEX_HTML ! ( )
94101}
95102
96- pub fn start_rocket ( ) {
97- let config = rocket:: config:: Config :: build ( rocket:: config:: Environment :: Production )
98- . port ( 1500 )
99- . finalize ( ) . expect ( "Could not create config" ) ;
103+ type BoxFut = Box < Future < Item = Response < Body > , Error = hyper:: Error > + Send > ;
104+
105+ fn echo ( req : Request < Body > ) -> BoxFut {
106+ let mut response = Response :: new ( Body :: empty ( ) ) ;
107+
108+ match ( req. method ( ) , req. uri ( ) . path ( ) ) {
109+ ( & Method :: GET , "/" ) => {
110+ let mut map = HeaderMap :: new ( ) ;
111+
112+ map. insert ( CONTENT_TYPE , "text/html;charset=utf-8" . parse ( ) . unwrap ( ) ) ;
100113
101- rocket:: custom ( config, false )
102- . mount ( "/" , routes ! [ scan, home_page] )
103- . launch ( ) ;
104- }
114+ * response. headers_mut ( ) = map;
115+ * response. body_mut ( ) = Body :: from ( home_page ( ) ) ;
116+ }
117+ ( & Method :: GET , "/scan" ) => {
118+ * response. body_mut ( ) = Body :: from ( scan ( ) ) ;
119+ }
120+ _ => {
121+ * response. status_mut ( ) = StatusCode :: NOT_FOUND ;
122+ }
123+ }
124+ Box :: new ( future:: ok ( response) )
125+ }
126+
127+ pub fn start_hyper ( ) {
128+ let addr = ( [ 0 , 0 , 0 , 0 ] , 1500 ) . into ( ) ;
129+
130+ let server = Server :: bind ( & addr)
131+ . serve ( || service_fn ( echo) )
132+ . map_err ( |e| eprintln ! ( "server error: {}" , e) ) ;
133+
134+
135+ hyper:: rt:: run ( server) ;
136+ }
0 commit comments