From 8cbc9443798da06e6d2fa6d05cf2aa9a6526bdfa Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 3 Feb 2015 21:43:12 -0500 Subject: [PATCH] Add server configuration with command args --- README.md | 24 ++++++++++++++++++++++++ telnet.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 52c8b77..3a3a45d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,30 @@ This is a simple telnet-based interface for publishing text. Users connect and p telnet nerds.write.as ``` +## Run it yourself +``` +Usage: + telnet [options] + +Options: + --debug + Enables garrulous debug logging. + -o + Directory where text files will be stored. + -s + Directory where required static files exist (like the banner). + -h + Hostname of the server to rsync saved files to. + -p + Port to listen on. +``` + +The default configuration (without any flags) is essentially: + +``` +telnet -o /var/write -s . -p 2323 +``` + ## How it works The user's input is simply written to a flat file in a given directory. To provide web access, a web server (sold separately) serves all files in this directory as `plain/text`. That's it! diff --git a/telnet.go b/telnet.go index 4ebf62b..d2fe859 100644 --- a/telnet.go +++ b/telnet.go @@ -10,10 +10,15 @@ import ( "os" "os/exec" "strings" + "flag" ) var ( banner []byte + outDir string + staticDir string + debugging bool + rsyncHost string ) const ( @@ -24,27 +29,46 @@ const ( colBRed = "\033[1;31m" colBold = "\033[1;37m" noCol = "\033[0m" - + nameLen = 12 - outDir = "/var/write/" - bannerDir = "./" + hr = "————————————————————————————————————————————————————————————————————————————————" ) func main() { - ln, err := net.Listen("tcp", ":9727") - if err != nil { - panic(err) - } - fmt.Println("Listening on localhost:9727") + // Get any arguments + outDirPtr := flag.String("o", "/var/write", "Directory where text files will be stored.") + staticDirPtr := flag.String("s", ".", "Directory where required static files exist.") + rsyncHostPtr := flag.String("h", "", "Hostname of the server to rsync saved files to.") + portPtr := flag.Int("p", 2323, "Port to listen on.") + debugPtr := flag.Bool("debug", false, "Enables garrulous debug logging.") + flag.Parse() + + outDir = *outDirPtr + staticDir = *staticDirPtr + rsyncHost = *rsyncHostPtr + debugging = *debugPtr + + fmt.Print("\nCONFIG:\n") + fmt.Printf("Output directory : %s\n", outDir) + fmt.Printf("Static directory : %s\n", staticDir) + fmt.Printf("rsync host : %s\n", rsyncHost) + fmt.Printf("Debugging enabled : %t\n\n", debugging) fmt.Print("Initializing...") - banner, err = ioutil.ReadFile(bannerDir + "/banner.txt") + var err error + banner, err = ioutil.ReadFile(staticDir + "/banner.txt") if err != nil { fmt.Println(err) } fmt.Println("DONE") + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", *portPtr)) + if err != nil { + panic(err) + } + fmt.Printf("Listening on localhost:%d\n", *portPtr) + for { conn, err := ln.Accept() if err != nil { @@ -120,6 +144,11 @@ func readInput(c net.Conn) { n, err := c.Read(b) post.Write(b[0:n]) + if debugging { + fmt.Print(b[0:n]) + fmt.Printf("\n%d: %s\n", n, b[0:n]) + } + if checkExit(b, n) { file, err := savePost(post.Bytes()) if err != nil { @@ -128,8 +157,13 @@ func readInput(c net.Conn) { break } output(c, fmt.Sprintf("\n%s\nPosted to %shttp://nerds.write.as/%s%s\nPosting to secure site...", hr, colBlue, file, noCol)) - exec.Command("rsync", "-ptgou", outDir + file, "www:").Run() - output(c, fmt.Sprintf("\nPosted! View at %shttps://write.as/%s%s\nSee you later.\n\n", colBlue, file, noCol)) + + if rsyncHost != "" { + exec.Command("rsync", "-ptgou", outDir + "/" + file, rsyncHost + ":").Run() + output(c, fmt.Sprintf("\nPosted! View at %shttps://write.as/%s%s", colBlue, file, noCol)) + } + + output(c, "\nSee you later.\n\n") break } @@ -141,7 +175,7 @@ func readInput(c net.Conn) { func savePost(post []byte) (string, error) { filename := generateFileName() - f, err := os.Create(outDir + filename) + f, err := os.Create(outDir + "/" + filename) defer f.Close()