the_endian
the_endian

Reputation: 2527

Why can I not access this Rust simple server from the Internet?

I have the following server:

extern crate simple_server;
use crate::simple_server::*;

fn main() {
    let host = "127.0.0.1";
    let port = "7878";

    let server = Server::new(|request, mut response| {
        println!("Request received. {} {}", request.method(), request.uri());
        println!("=============BODY=================");
        let mut v: Vec<u8> = Vec::new();
        for b in request.body().iter() {
            v.push(*b);
        }
        let body_as_str = String::from_utf8(v);

        match body_as_str {
            Ok(strr) => println!("{}", strr),
            Err(e) => println!("{}", e),
        }

        //ROUTING
        match (request.method(), request.uri().path()) {
            (&Method::GET, "/") => {
                Ok(response.body("<h1>Hi!</h1><p>Hello hacker!</p>".as_bytes().to_vec())?)
            }
            (&Method::POST, "/") => {
                Ok(response.body("<h1>Hi!</h1><p>Hello hacker!</p>".as_bytes().to_vec())?)
            }
            (_, _) => {
                response.status(StatusCode::NOT_FOUND);
                Ok(response.body("<h1>404</h1><p>Not found!<p>".as_bytes().to_vec())?)
            }
        }
    });
    println!("Listening on: {} port {}", host, port);
    server.listen(host, port);
}

I'm not able to access the page going to http://my_server_ip:7878/ from the Internet.

I have my server's firewall setup to allow all on 7878 and also, other simple servers work just fine when configured to listen on that port, which makes me think this is a code problem with my specific Rust application. An example of a server which works fine is static-server on npm.

However, when I test this application on my build machine by going to "127.0.0.1:7878", it works fine.

Upvotes: 2

Views: 1096

Answers (1)

Stargateur
Stargateur

Reputation: 26697

By using "127.0.0.1" for the host variable you denied any connection that doesn't come from this address. Use "0.0.0.0", the unspecified address, for example.

Upvotes: 5

Related Questions