From 2c206cee2d4c0b8866506e97505a8054dec07f37 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 23 Feb 2015 19:30:59 -0500 Subject: [PATCH 1/3] Fix formatting --- store/random.go | 2 +- store/store.go | 8 ++++---- write-telnet.go | 50 +++++++++++++++++++++++++------------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/store/random.go b/store/random.go index 01a9cd7..cb2fd37 100644 --- a/store/random.go +++ b/store/random.go @@ -16,7 +16,7 @@ func GenerateRandomString(dictionary string, l int) string { var bytes = make([]byte, l) rand.Read(bytes) for k, v := range bytes { - bytes[k] = dictionary[v%byte(len(dictionary))] + bytes[k] = dictionary[v%byte(len(dictionary))] } return string(bytes) } diff --git a/store/store.go b/store/store.go index ca95a13..2adfcc9 100644 --- a/store/store.go +++ b/store/store.go @@ -1,9 +1,9 @@ package store import ( - "os" - "io" "bytes" + "io" + "os" ) const ( @@ -16,9 +16,9 @@ func SavePost(outDir string, post []byte) (string, error) { if err != nil { return "", err } - + defer f.Close() - + out := post[:0] for _, b := range post { if b < 32 && b != 10 && b != 13 { diff --git a/write-telnet.go b/write-telnet.go index f64fe25..bf8f653 100644 --- a/write-telnet.go +++ b/write-telnet.go @@ -1,32 +1,32 @@ package main import ( - "fmt" - "net" "bytes" + "flag" + "fmt" "io/ioutil" + "net" "os/exec" - "flag" "github.com/writeas/writeas-telnet/store" ) var ( - banner []byte - outDir string + banner []byte + outDir string staticDir string debugging bool rsyncHost string ) const ( - colBlue = "\033[0;34m" - colGreen = "\033[0;32m" + colBlue = "\033[0;34m" + colGreen = "\033[0;32m" colBGreen = "\033[1;32m" - colCyan = "\033[0;36m" - colBRed = "\033[1;31m" - colBold = "\033[1;37m" - noCol = "\033[0m" + colCyan = "\033[0;36m" + colBRed = "\033[1;31m" + colBold = "\033[1;37m" + noCol = "\033[0m" hr = "————————————————————————————————————————————————————————————————————————————————" ) @@ -50,7 +50,7 @@ func main() { 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...") var err error banner, err = ioutil.ReadFile(staticDir + "/banner.txt") @@ -58,7 +58,7 @@ func main() { fmt.Println(err) } fmt.Println("DONE") - + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", *portPtr)) if err != nil { panic(err) @@ -71,7 +71,7 @@ func main() { fmt.Println(err) continue } - + go handleConnection(conn) } } @@ -98,17 +98,17 @@ func handleConnection(c net.Conn) { outputBytes(c, banner) output(c, fmt.Sprintf("\n%sWelcome to write.as!%s\n", colBGreen, noCol)) output(c, fmt.Sprintf("If this is freaking you out, you can get notified of the %sbrowser-based%s launch\ninstead at https://write.as.\n\n", colBold, noCol)) - + waitForEnter(c) - + c.Close() - + fmt.Printf("Connection from %v closed.\n", c.RemoteAddr()) } func waitForEnter(c net.Conn) { b := make([]byte, 4) - + output(c, fmt.Sprintf("%sPress Enter to continue...%s\n", colBRed, noCol)) for { n, err := c.Read(b) @@ -126,7 +126,7 @@ func waitForEnter(c net.Conn) { break } } - + output(c, fmt.Sprintf("Enter anything you like.\nPress %sCtrl-D%s to publish and quit.\n%s\n", colBold, noCol, hr)) readInput(c) } @@ -137,15 +137,15 @@ func checkExit(b []byte, n int) bool { func readInput(c net.Conn) { defer c.Close() - + b := make([]byte, 4096) - + var post bytes.Buffer - + for { 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]) @@ -162,14 +162,14 @@ func readInput(c net.Conn) { if rsyncHost != "" { output(c, "\nPosting to secure site...") - exec.Command("rsync", "-ptgou", outDir + "/" + file, rsyncHost + ":").Run() + 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 } - + if err != nil || n == 0 { break } From 91b6298952d9e6c6648cc618591877aeef8957ea Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 28 Feb 2015 02:00:11 -0500 Subject: [PATCH 2/3] write-telnet -> nerds --- README.md | 6 +- nerds.go | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ write-telnet.go | 177 -------------------------------------------------------- 3 files changed, 180 insertions(+), 180 deletions(-) create mode 100644 nerds.go delete mode 100644 write-telnet.go diff --git a/README.md b/README.md index d44e9b5..a8c04ef 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Write.as ======== -[![Build Status](https://travis-ci.org/writeas/writeas-telnet.svg)](https://travis-ci.org/writeas/writeas-telnet) +[![Build Status](https://travis-ci.org/writeas/nerds.svg)](https://travis-ci.org/writeas/nerds) This is a simple telnet-based interface for publishing text. Users connect and paste / type what they want to publish. Upon indicating that they're finished, a link is generated to access their new post on the web. @@ -12,7 +12,7 @@ telnet nerds.write.as ## Run it yourself ``` Usage: - write-telnet [options] + nerds [options] Options: --debug @@ -30,7 +30,7 @@ Options: The default configuration (without any flags) is essentially: ``` -write-telnet -o /var/write -s . -p 2323 +nerds -o /var/write -s . -p 2323 ``` ## How it works diff --git a/nerds.go b/nerds.go new file mode 100644 index 0000000..4bc1512 --- /dev/null +++ b/nerds.go @@ -0,0 +1,177 @@ +package main + +import ( + "bytes" + "flag" + "fmt" + "io/ioutil" + "net" + "os/exec" + + "github.com/writeas/nerds/store" +) + +var ( + banner []byte + outDir string + staticDir string + debugging bool + rsyncHost string +) + +const ( + colBlue = "\033[0;34m" + colGreen = "\033[0;32m" + colBGreen = "\033[1;32m" + colCyan = "\033[0;36m" + colBRed = "\033[1;31m" + colBold = "\033[1;37m" + noCol = "\033[0m" + + hr = "————————————————————————————————————————————————————————————————————————————————" +) + +func main() { + // Get any arguments + outDirPtr := flag.String("o", "/var/write", "Directory where text files will be stored.") + staticDirPtr := flag.String("s", "./static", "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...") + 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 { + fmt.Println(err) + continue + } + + go handleConnection(conn) + } +} + +func output(c net.Conn, m string) bool { + _, err := c.Write([]byte(m)) + if err != nil { + c.Close() + return false + } + return true +} + +func outputBytes(c net.Conn, m []byte) bool { + _, err := c.Write(m) + if err != nil { + c.Close() + return false + } + return true +} + +func handleConnection(c net.Conn) { + outputBytes(c, banner) + output(c, fmt.Sprintf("\n%sWelcome to write.as!%s\n", colBGreen, noCol)) + output(c, fmt.Sprintf("If this is freaking you out, you can get notified of the %sbrowser-based%s launch\ninstead at https://write.as.\n\n", colBold, noCol)) + + waitForEnter(c) + + c.Close() + + fmt.Printf("Connection from %v closed.\n", c.RemoteAddr()) +} + +func waitForEnter(c net.Conn) { + b := make([]byte, 4) + + output(c, fmt.Sprintf("%sPress Enter to continue...%s\n", colBRed, noCol)) + for { + n, err := c.Read(b) + + if debugging { + fmt.Print(b[0:n]) + fmt.Printf("\n%d: %s\n", n, b[0:n]) + } + + if bytes.IndexRune(b[0:n], '\n') > -1 { + break + } + if err != nil || n == 0 { + c.Close() + break + } + } + + output(c, fmt.Sprintf("Enter anything you like.\nPress %sCtrl-D%s to publish and quit.\n%s\n", colBold, noCol, hr)) + readInput(c) +} + +func checkExit(b []byte, n int) bool { + return n > 0 && bytes.IndexRune(b[0:n], '\n') == -1 +} + +func readInput(c net.Conn) { + defer c.Close() + + b := make([]byte, 4096) + + var post bytes.Buffer + + for { + 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 := store.SavePost(outDir, post.Bytes()) + if err != nil { + fmt.Printf("There was an error saving: %s\n", err) + output(c, "Something went terribly wrong, sorry. Try again later?\n\n") + break + } + output(c, fmt.Sprintf("\n%s\nPosted to %shttp://nerds.write.as/%s%s", hr, colBlue, file, noCol)) + + if rsyncHost != "" { + output(c, "\nPosting to secure site...") + 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 + } + + if err != nil || n == 0 { + break + } + } +} diff --git a/write-telnet.go b/write-telnet.go deleted file mode 100644 index bf8f653..0000000 --- a/write-telnet.go +++ /dev/null @@ -1,177 +0,0 @@ -package main - -import ( - "bytes" - "flag" - "fmt" - "io/ioutil" - "net" - "os/exec" - - "github.com/writeas/writeas-telnet/store" -) - -var ( - banner []byte - outDir string - staticDir string - debugging bool - rsyncHost string -) - -const ( - colBlue = "\033[0;34m" - colGreen = "\033[0;32m" - colBGreen = "\033[1;32m" - colCyan = "\033[0;36m" - colBRed = "\033[1;31m" - colBold = "\033[1;37m" - noCol = "\033[0m" - - hr = "————————————————————————————————————————————————————————————————————————————————" -) - -func main() { - // Get any arguments - outDirPtr := flag.String("o", "/var/write", "Directory where text files will be stored.") - staticDirPtr := flag.String("s", "./static", "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...") - 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 { - fmt.Println(err) - continue - } - - go handleConnection(conn) - } -} - -func output(c net.Conn, m string) bool { - _, err := c.Write([]byte(m)) - if err != nil { - c.Close() - return false - } - return true -} - -func outputBytes(c net.Conn, m []byte) bool { - _, err := c.Write(m) - if err != nil { - c.Close() - return false - } - return true -} - -func handleConnection(c net.Conn) { - outputBytes(c, banner) - output(c, fmt.Sprintf("\n%sWelcome to write.as!%s\n", colBGreen, noCol)) - output(c, fmt.Sprintf("If this is freaking you out, you can get notified of the %sbrowser-based%s launch\ninstead at https://write.as.\n\n", colBold, noCol)) - - waitForEnter(c) - - c.Close() - - fmt.Printf("Connection from %v closed.\n", c.RemoteAddr()) -} - -func waitForEnter(c net.Conn) { - b := make([]byte, 4) - - output(c, fmt.Sprintf("%sPress Enter to continue...%s\n", colBRed, noCol)) - for { - n, err := c.Read(b) - - if debugging { - fmt.Print(b[0:n]) - fmt.Printf("\n%d: %s\n", n, b[0:n]) - } - - if bytes.IndexRune(b[0:n], '\n') > -1 { - break - } - if err != nil || n == 0 { - c.Close() - break - } - } - - output(c, fmt.Sprintf("Enter anything you like.\nPress %sCtrl-D%s to publish and quit.\n%s\n", colBold, noCol, hr)) - readInput(c) -} - -func checkExit(b []byte, n int) bool { - return n > 0 && bytes.IndexRune(b[0:n], '\n') == -1 -} - -func readInput(c net.Conn) { - defer c.Close() - - b := make([]byte, 4096) - - var post bytes.Buffer - - for { - 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 := store.SavePost(outDir, post.Bytes()) - if err != nil { - fmt.Printf("There was an error saving: %s\n", err) - output(c, "Something went terribly wrong, sorry. Try again later?\n\n") - break - } - output(c, fmt.Sprintf("\n%s\nPosted to %shttp://nerds.write.as/%s%s", hr, colBlue, file, noCol)) - - if rsyncHost != "" { - output(c, "\nPosting to secure site...") - 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 - } - - if err != nil || n == 0 { - break - } - } -} From 49f00434702ac3e28d0da46f9b512dad92cdb11c Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 14 Mar 2015 13:46:29 -0400 Subject: [PATCH 3/3] Store posts with 13-character IDs The new IDs will be be served as text/html on write.as, instead of text/plain. --- store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/store.go b/store/store.go index 2adfcc9..cd62838 100644 --- a/store/store.go +++ b/store/store.go @@ -7,7 +7,7 @@ import ( ) const ( - FriendlyIdLen = 12 + FriendlyIdLen = 13 ) func SavePost(outDir string, post []byte) (string, error) {