~evan-hoose/c2mon

4ca7c709e4a1a2231959b3f7e6c175504d96bd24 — Evan 1 year, 3 months ago
	new file:   README.md
	new file:   c2mon-core.rs
	new file:   c2mon-core.targets
	new file:   install.sh
	new file:   testserver
	new file:   uninstall.sh
6 files changed, 136 insertions(+), 0 deletions(-)

A README.md
A c2mon-core.rs
A c2mon-core.targets
A install.sh
A testserver
A uninstall.sh
A  => README.md +39 -0
@@ 1,39 @@
#C2mon-Core

C2mon-Core is a tool that take a list of IP addresses and ports as input, and
then outputs a file that shows whether or not those connections were 
successful. It is designed to run as a service, so that you can have another
program read in the file it outputs and display the information in a more 
friendly manner.

This repository also includes install and uninstall scripts.

#File Locations
Input file: /etc/c2mon/c2mon-core.targets
Output file: /etc/c2mon/c2mon-core.output

#Installing/Uninstalling

##Install Script
The install script needs to be run with root privleges.

The install script will move c2mon-core into /sbin, create the directory
/etc/c2mon , and move the targets and uninstall files into /etc/c2mon .

##Uninstall Script
Also needs to be run with root privleges.

Assuming the install script has been run, this will clean up all files
moved/created by the install script, in addition to the output file created
by c2mon-core.

##Manual Install/Uninstall
c2mon-core has three expectations.

1) c2mon-core.targets to be in /etc/c2mon
2) to be able to write to /etc/c2mon
3) to be able to make outbound TCP connections

Any way you can provide it these three things, it should work. As far as
uninstalling is concerned, only you know how to uninstall what you manually
set up.

A  => c2mon-core.rs +73 -0
@@ 1,73 @@
use std::io::Write;
use std::net::TcpStream;
use std::time::Duration;
use std::thread::sleep;
use std::fs::File;

fn main() -> std::io::Result<()> {
    //in_path and targets represent the file location and string which include
    //our list of target hosts
    let in_path = std::path::PathBuf::from("/etc/c2mon/c2mon-core.targets");
    let targets = std::fs::read_to_string(in_path)
        .expect("Could not read input file. Does /etc/c2mon/c2mon-core.targets exist?");
    //out_path is the path to which we will write our output file, out_string
    //will be what we write, and out_file is the file that we open at out_path
    let out_path = std::path::PathBuf::from("/etc/c2mon/c2mon-core.status");
    let mut out_string: String = "".to_string();
    //loop_time is the time in seconds we wait before running tests again.
    let loop_time = Duration::new(5, 0);
    
    //Now that we've finished setup, we want to run until the OS tells us to
    //quit.
    while 1 == 1 {
        for line in targets.lines(){
            //if the line contains a #, it's a comment line.
            if line.contains("#") == false{
                //host is more readable for the lines below, so we use that.
                let host = line;
                //test_tcp sounds like it should return true/false, but it
                //actually returns a string formated like:
                //"127.0.0.1:80         [✓]" 
                //or
                //"127.0.0.1:443        [X]"
                //This behavior may be changed at some point.
                let out_string_new_line = test_tcp(host);
                out_string = out_string + &out_string_new_line;
            }
        }
        output_status(out_string, &out_path)?;
        //We blank out_string so that it doesn't grow larger/repeat itself
        //in future iterations,
        out_string = "".to_string();
        //and then sleep for the time set earlier.
        sleep(loop_time);
    }
    Ok(())
}

//Output the value of 'text' into a file located at 'path'. Will truncate
//an existing file if something is located at 'path'.
fn output_status(text: String, path: &std::path::PathBuf) -> std::io::Result<()> {
    let mut out_file = File::create(path)?;
    write!(out_file, "{}", text)?;
    Ok(())
}

//If a connection can be mad at host, which is a string in the format IP:PORT,
//for example: 127.0.0.1:8080 . If connection is successful, output a string
//containing the IP:PORT and an indicator.
//
//Despite the name, I have kept this behavior because it makes main() more
//readable.
fn test_tcp(host: &str) -> String {
    let out_string: String;
    if let  Ok(_stream) = TcpStream::connect(host){
        //if the TCP connection is successful, add "host [✓] to the
        //next line in the out string.
        out_string = format!("{:20} [✓]\n", host);
    } else {
        //same as above, but for a failure case.
        out_string = format!("{:20} [X]\n", host);
    }
    out_string
}

A  => c2mon-core.targets +11 -0
@@ 1,11 @@
# This is the target file for c2mon, a tool for monitoring host uptime.
# Hashtags mark comments, everything else is regarded as an IP address
# in the forms:
#
# IPv4:
# 127.0.0.1:9876
#
# IPv6:
# [0:0:0:0:0:0:0:1]:80
127.0.0.1:1234
[::1]:1234

A  => install.sh +6 -0
@@ 1,6 @@
#!/bin/sh

mkdir /etc/c2mon/
mv ./c2mon-core.targets /etc/c2mon/
mv ./uninstall.sh /etc/c2mon
mv ./c2mon-core /sbin

A  => testserver +0 -0
A  => uninstall.sh +7 -0
@@ 1,7 @@
#!/bin/sh

rm /sbin/c2mon-core
rm /etc/c2mon/c2mon-core.targets
rm /etc/c2mon/c2mon-core.output
rm /etc/c2mon/uninstall.sh
rm -r /etc/c2mon