Compare commits
6 Commits
seq
...
ae02c9dfc0
| Author | SHA1 | Date | |
|---|---|---|---|
| ae02c9dfc0 | |||
| fae2cceae2 | |||
| 552f9ac9c2 | |||
| eb71ae1c51 | |||
| 0bd27c5a4c | |||
| f5bf217243 |
@@ -1,12 +0,0 @@
|
||||
all: client server
|
||||
|
||||
re: clean client server
|
||||
|
||||
clean:
|
||||
rm client.o server.o
|
||||
|
||||
client:
|
||||
go build -o client.o client.go
|
||||
|
||||
server:
|
||||
go build -o server.o server.go
|
||||
@@ -1,7 +1,7 @@
|
||||
// =============================================================================
|
||||
// Auth: alex
|
||||
// File: client.go
|
||||
// Revn: 06-14-2024 3.0
|
||||
// Revn: 01-09-2025 4.0
|
||||
// Func: ask server for a message of the day quote
|
||||
//
|
||||
// TODO: remove ioutil import
|
||||
@@ -33,6 +33,7 @@
|
||||
// STARTED BYTE-BASED PWFT REWRITE
|
||||
//*06-14-2024: finished byte-based PWFT rewrite
|
||||
// commented
|
||||
//*01-09-2025: basically just copy/pasted from /la/ into /r1/, /r2/
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
@@ -226,28 +227,74 @@ func handle( conn net.Conn ) {
|
||||
// to remove. There was probably a way to handle this a little
|
||||
// more simply, but this, I feel, has some nuance to it that I
|
||||
// actually like
|
||||
// redo /la/, list all quotes
|
||||
// redo /r1/, list all quotes
|
||||
// split recv'd message over newline to create quote list
|
||||
quotes := strings.Split( msg[1], "\n" )
|
||||
for p, v := range quotes { // iterate over quotes
|
||||
// print num of quote, followed by that quote
|
||||
fmt.Println( p + 1, "\t", v )
|
||||
if len( msg ) > 2 { // multistage send condition
|
||||
if msg[1] == "X" { // final message
|
||||
// TODO update
|
||||
// read entire temp storage file as byte array
|
||||
tfile, err := ioutil.ReadFile( "temp.q" )
|
||||
check( err ) // check read for errors
|
||||
// cast file to string, concat with recv'd string
|
||||
tstr := string( tfile ) + msg[2]
|
||||
// split concat over newlines, separating quotes
|
||||
tlist := strings.Split( tstr, "\n" )
|
||||
// iterate over array, printing each quote
|
||||
for p, v := range tlist {
|
||||
fmt.Println( p + 1, "\t", v )
|
||||
}
|
||||
// delete temp storage file
|
||||
err = os.Remove( "temp.q" )
|
||||
check( err ) // check rm for errors
|
||||
// ask user which quote to remove
|
||||
resp = "r2@" + in( "\n-> " )
|
||||
} else { // first or intermediate message
|
||||
// open temp quote storage file, creating if
|
||||
// necessary, with write-only permissions, in
|
||||
// append mode, wr u perm, r g/w perm
|
||||
f, err := os.OpenFile( "temp.q", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644 )
|
||||
check( err ) // check file open for errors
|
||||
// write received message to file, keeping track
|
||||
// of bytes written
|
||||
n, err = f.WriteString( msg[2] )
|
||||
check( err ) // check file write for errors
|
||||
// cast bytes already written field to int
|
||||
b, err := strconv.Atoi( msg[1] )
|
||||
check( err ) // check cast for errors
|
||||
// add bytes just written to bytes already
|
||||
// written, cast back to string, and append to
|
||||
// response variable
|
||||
resp = "r1@" + strconv.Itoa( b + n )
|
||||
}
|
||||
} else {
|
||||
// split recv'd message over \n, create quote list
|
||||
quotes := strings.Split( msg[1], "\n" )
|
||||
for p, v := range quotes { // iterate over quotes
|
||||
// print num of quote, followed by that quote
|
||||
fmt.Println( p + 1, "\t", v )
|
||||
}
|
||||
// ask user which quote to remove
|
||||
resp = "r2@" + in( "\n-> " )
|
||||
}
|
||||
// ask user which quote to remove
|
||||
resp = "r2@" + in( "\n-> " )
|
||||
case "r2": // /r2/, remove ( second transaction )
|
||||
// if server says remove succeeded
|
||||
if msg[1] == "success" {
|
||||
fmt.Println( "Remove successful\n" ) // success
|
||||
// list all quotes to validate remove succeeded
|
||||
// TODO should /aa/ also make /lr/ before /te/?
|
||||
resp = "lr@dumvar" // set transaction end message
|
||||
resp = "lr@init" // set transaction end message
|
||||
// if server says remove failed
|
||||
} else if msg[1] == "error" {
|
||||
fmt.Println( "Remove failed" ) // print failure
|
||||
resp = "te@error" // set transaction end message
|
||||
// if unknown status message
|
||||
} else {
|
||||
// TODO there's a known issue where if the buffer
|
||||
// size ( -s ) is less than 10, on the server side,
|
||||
// 'success' will get cut off ( r2@success )
|
||||
// and /r2/ will end up down here despite having
|
||||
// actually worked correctly, and with not display the
|
||||
// list, as it should on success
|
||||
// print unknown status
|
||||
fmt.Println( "Unknown error code: " + msg[1] )
|
||||
// TODO should this be unknown instead of error?
|
||||
@@ -292,7 +339,7 @@ func main() {
|
||||
flag.BoolVar( &add, "a", false, "add quote to list" )
|
||||
flag.BoolVar( &remove, "r", false, "remove quote from list" )
|
||||
flag.BoolVar( &list, "l", false, "print quote list" )
|
||||
flag.StringVar( &dest, "ip", ":1300", "ip:port of server" )
|
||||
flag.StringVar( &dest, "ip", "earth-1.net:1300", "ip:port of server" )
|
||||
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
|
||||
flag.Parse() // process flags
|
||||
|
||||
@@ -338,7 +385,7 @@ func main() {
|
||||
_, rerr := strconv.Atoi( flag.Args()[0] )
|
||||
// if there was an error, just pretend there's no args
|
||||
if rerr != nil {
|
||||
command = "r1@dumvar" // remove request 1
|
||||
command = "r1@init" // remove request 1
|
||||
// TODO come up w/ useful argument, replace dumvar
|
||||
} else {
|
||||
// if user knows the number, take care of it
|
||||
@@ -348,7 +395,7 @@ func main() {
|
||||
}
|
||||
} else { // if no argument was specified for remove
|
||||
// jump right into remove request 1
|
||||
command = "r1@dumvar"
|
||||
command = "r1@init"
|
||||
}
|
||||
default: // if no flag was specified
|
||||
// begin transaction for quote request
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=Ekho MOTD Server
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
User=
|
||||
ExecStart=/usr/local/bin/server
|
||||
WorkingDirectory=/home/
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,58 @@
|
||||
# delete server bin
|
||||
[private]
|
||||
server_clean:
|
||||
rm server
|
||||
|
||||
# delete client bin
|
||||
[private]
|
||||
client_clean:
|
||||
rm client
|
||||
|
||||
# delete binaries
|
||||
clean: server_clean client_clean
|
||||
|
||||
# build server bin
|
||||
server:
|
||||
go build server.go
|
||||
|
||||
# build client bin
|
||||
client:
|
||||
go build client.go
|
||||
|
||||
# build both bins
|
||||
build: client server
|
||||
|
||||
# delete binaries and make them again
|
||||
rebuild: clean build
|
||||
|
||||
# install server
|
||||
ins_server:
|
||||
#!/usr/bin/bash
|
||||
echo "enter port of server or leave blank to use default (1300)"
|
||||
read PORT
|
||||
if [[ $PORT -ne "" ]]; then
|
||||
sed -i -e "s/1300/$PORT/" server.go
|
||||
fi
|
||||
echo "go build server.go"
|
||||
go build server.go
|
||||
USER=($(users))
|
||||
sed -i -e "s/^User=/User=${USER[0]}/" ekho.service
|
||||
sed -i -e "s/^WorkingDirectory=.*/WorkingDirectory=\/home\/${USER[0]}\//" ekho.service
|
||||
echo "sudo mv server /usr/local/bin/server"
|
||||
sudo mv server /usr/local/bin/server
|
||||
echo "sudo mv ekho.service /etc/systemd/system/"
|
||||
sudo mv ekho.service /etc/systemd/system/
|
||||
|
||||
# install client
|
||||
ins_client:
|
||||
#!/usr/bin/bash
|
||||
echo "enter ip and port of server, 'aaa.bbb.ccc.ddd:port"
|
||||
read IP_PORT
|
||||
sed -i -e "s/:1300/$IP_PORT/" client.go
|
||||
echo "go build -o ekho client.go"
|
||||
go build -o ekho client.go
|
||||
sed -i -e "s/$IP_PORT/:1300/" client.go
|
||||
echo "sudo mv ekho /usr/local/bin/"
|
||||
sudo mv ekho /usr/local/bin/
|
||||
echo "add 'ekho' to shell config (config.fish, .bashrc, etc)"
|
||||
|
||||
@@ -1 +1,9 @@
|
||||
@user:
|
||||
shmuley
|
||||
floglin
|
||||
@all:
|
||||
blingo
|
||||
no but like actually blingo shmingo
|
||||
i would prefer not to
|
||||
@default:
|
||||
thank you for using ekho
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
// =============================================================================
|
||||
// Auth: alex
|
||||
// File: server.go
|
||||
// Revn: 06-14-2024 4.0
|
||||
// Revn: 05-12-2026 2.1.2
|
||||
// Func: host MOTD connection
|
||||
//
|
||||
// TODO: catch keyboard int. signal
|
||||
// remove ioutil import
|
||||
// add more /codes/ to handle
|
||||
// add flags, like specify port
|
||||
// TODO:
|
||||
// add user-specific prints
|
||||
// write usage and errors to logfile
|
||||
// remove ioutil import
|
||||
// send logfile to client on request
|
||||
// add color printing to qfile
|
||||
// add flags?
|
||||
// add more /codes/ to handle
|
||||
// =============================================================================
|
||||
// CHANGE LOG
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -32,7 +34,7 @@
|
||||
// fixed bug in /r2/ where removing quote at final
|
||||
// position would break
|
||||
// removed deadcode from /r2/, unused "quit" bool?
|
||||
//*10-17-2023: fixed bug where quotes are read from and saved to two
|
||||
//#10-17-2023: fixed bug where quotes are read from and saved to two
|
||||
// different files
|
||||
// removed implicit newline at end of qfile to stop ghost
|
||||
// line bug in /la/
|
||||
@@ -43,6 +45,12 @@
|
||||
// 06-13-2024: removed debug print statements
|
||||
// commented
|
||||
//*06-14-2024: byte count-based PWFT rewrite
|
||||
//*06-17-2024: catch keyboard interrupts with os/signal and syscall
|
||||
//#01-09-2025: copied lr/la PWFT updates into r1/r2
|
||||
//*04-28-2026: removed implicit newline at end of qfile to stop ghost
|
||||
// line bug in /la/ AGAIN
|
||||
// 05-11-2026: updated fread() to handle users in qfile
|
||||
// 05-12-2026: added v and vv input flags
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
@@ -55,9 +63,11 @@ import (
|
||||
"io/ioutil" // ReadFile
|
||||
"net" // ResovleTCPAddr, ListenTCP, listener.Accept
|
||||
// conn.Read,Write
|
||||
"os" // Exit
|
||||
"strconv" // Itoa
|
||||
"os" // Exit, Create, os.Signal
|
||||
"os/signal" // Notify
|
||||
"strconv" // Itoa, Atoi
|
||||
"strings" // Split
|
||||
"syscall" // syscall.SIGINT,SIGTERM
|
||||
)
|
||||
|
||||
|
||||
@@ -111,10 +121,15 @@ func fread() string {
|
||||
// bugs where a blank item was introduced into qlist and calling
|
||||
// /la/ would produce a blank line on the client side that didn't
|
||||
// really exist
|
||||
//qfile = qfile[:len( qfile ) - 1]
|
||||
qfile = qfile[:len( qfile ) - 1]
|
||||
// cast bytes to string, split string over newline into array
|
||||
qlist = strings.Split( string( qfile ), "\n" )
|
||||
// deal with the trailing
|
||||
//qlist = strings.Split( string( qfile ), "\n" )
|
||||
profiles := strings.Split( string( qfile ), "@" )
|
||||
for _, p := range profiles {
|
||||
quotes := strings.Split( p, ":" )
|
||||
qlist[quotes[0]] = strings.Split( quotes[1], "\n" )
|
||||
}
|
||||
|
||||
return string( qfile ) // return file as string
|
||||
}
|
||||
|
||||
@@ -140,6 +155,17 @@ func handle( conn net.Conn ) {
|
||||
n, err := conn.Read( buffer[:] )
|
||||
checkN( err, conn ) // make sure read worked
|
||||
|
||||
// message anatomy
|
||||
// command@number[@user]
|
||||
// @ @ delimiter
|
||||
// command command, like list request or quote request
|
||||
// number sequence or quote number
|
||||
// user quote username if applicable, default if not give
|
||||
// add request message anatomy
|
||||
// command@quote[@user]
|
||||
// quote quote to be added
|
||||
// user quote username, default if not given
|
||||
|
||||
// cast message to string, take first n bytes, split over @
|
||||
command := strings.Split( string( buffer[:n] ), "@" )
|
||||
|
||||
@@ -153,9 +179,20 @@ func handle( conn net.Conn ) {
|
||||
// concat and send
|
||||
resp = "sa@" + num // size answer
|
||||
case "qr": // /qr/, quote request
|
||||
// see if user was sent
|
||||
subcommands := strings.Split( command[1], ":" )
|
||||
|
||||
// keep track of name, or default if need be
|
||||
var name string
|
||||
if len( subcommands ) == 1 {
|
||||
name = "default"
|
||||
} else {
|
||||
name = subcommands[1]
|
||||
}
|
||||
|
||||
// convert message argument to string to make sure it's a
|
||||
// valid number
|
||||
num, nerr := strconv.Atoi( command[1] )
|
||||
num, nerr := strconv.Atoi( subcommands[0] )
|
||||
// nerr will not be nil if comm[1] is not a number
|
||||
if nerr != nil {
|
||||
// formulate an error message
|
||||
@@ -163,7 +200,7 @@ func handle( conn net.Conn ) {
|
||||
resp = "ba@qr@!" // bad answer
|
||||
} else { // comm[1] was a number, but...
|
||||
switch { // how do we know it's a valid number
|
||||
case num > len( qlist ):
|
||||
case num > len( qlist[name] ):
|
||||
// bad answer in quote request
|
||||
resp = "ba@qr@>"
|
||||
// why bad? num is >
|
||||
@@ -173,7 +210,7 @@ func handle( conn net.Conn ) {
|
||||
// why bad? num is -
|
||||
default:
|
||||
// good num, get quote, send off
|
||||
resp = "qa@" + qlist[num] // quote answer
|
||||
resp = "qa@" + qlist[name][num] // quote answer
|
||||
}
|
||||
}
|
||||
case "lr": // /lr/, list request
|
||||
@@ -206,7 +243,7 @@ func handle( conn net.Conn ) {
|
||||
} else {
|
||||
// get slice from start index to end, concat
|
||||
// with response
|
||||
resp = resp + qfile[num:end]
|
||||
resp = resp + qfile[bytes:end]
|
||||
}
|
||||
}
|
||||
} else { // file is smaller than the tx size
|
||||
@@ -215,14 +252,48 @@ func handle( conn net.Conn ) {
|
||||
}
|
||||
case "ar": // /ar/, add request
|
||||
// append new quote to list
|
||||
qlist = append( qlist, command[1] )
|
||||
qlist["f"] = append( qlist["f"], command[1] )
|
||||
// also append new quote to file variable
|
||||
qfile = qfile + "\n" + command[1]
|
||||
writeFile( qfile )
|
||||
resp = "aa@success" // add answer
|
||||
case "r1": // /r1/, remove request 1
|
||||
// same as list, just send whole file
|
||||
resp = "r1@" + qfile // remove request 1
|
||||
resp = "r1@" // begin crafting response
|
||||
// file too big for single transmission
|
||||
// initial header size is 5, r1@1@, so counting that into
|
||||
// the transmission size, the file must be longer than 251
|
||||
// ( default ) bytes to reach this block of code
|
||||
if len( qfile ) > ( tsize - 5 ) {
|
||||
if command[1] == "init" { // first request
|
||||
// add message number, delim, and the first
|
||||
// transmittable bytes
|
||||
resp = resp + "0@" + qfile[:( tsize - 5 )]
|
||||
} else { // subsequent requests
|
||||
// convert byte count to int
|
||||
bytes, err := strconv.Atoi( command[1] )
|
||||
check( err ) // make sure convert worked
|
||||
// replace tx'd bytes and delim to new header
|
||||
resp = resp + command[1] + "@"
|
||||
rlen := len( resp ) // capture header length
|
||||
// slices are always of size "txsize - header
|
||||
// length", so find end byte
|
||||
end := bytes + ( tsize - rlen )
|
||||
// if end goes beyond the bounds of the list
|
||||
if end > len( qfile ) {
|
||||
// replace byte count with X to signal
|
||||
// final message, concat the rest of the file,
|
||||
// beginning with the start index
|
||||
resp = "r1@X@" + qfile[bytes:]
|
||||
} else {
|
||||
// get slice from start index to end, concat
|
||||
// with response
|
||||
resp = resp + qfile[bytes:end]
|
||||
}
|
||||
}
|
||||
} else { // file is smaller than the tx size
|
||||
// just concat entire list to send
|
||||
resp = resp + qfile // list answer
|
||||
}
|
||||
case "r2": // /r2/, remove request 2
|
||||
// convert message argument to string to make sure it's a
|
||||
// valid number
|
||||
@@ -248,14 +319,14 @@ func handle( conn net.Conn ) {
|
||||
default:
|
||||
// replace the quote at that position with
|
||||
// final quote
|
||||
qlist[num] = qlist[len( qlist ) - 1]
|
||||
qlist["f"][num] = qlist["f"][len( qlist ) - 1]
|
||||
// remove second copy of quote
|
||||
qlist = qlist[:len( qlist ) - 1]
|
||||
qlist["f"] = qlist["f"][:len( qlist ) - 1]
|
||||
// O(1) remove from list at any position
|
||||
// must entirely recreate quote string
|
||||
var f string = "" // init empty string
|
||||
// iterate over list, disregard index
|
||||
for _, v := range qlist {
|
||||
for _, v := range qlist["f"] {
|
||||
// add next quote to file and separate
|
||||
// quotes with newline
|
||||
f = f + v + "\n"
|
||||
@@ -268,6 +339,11 @@ func handle( conn net.Conn ) {
|
||||
writeFile( qfile ) // write back to file
|
||||
// let client know remove was successful
|
||||
resp = "r2@success"
|
||||
// TODO for buffer sizes ( -s ) below 10, this
|
||||
// will cause a soft error on the other end
|
||||
// if it's less than 10, 'success' gets cut
|
||||
// off and client doesn't register it as an
|
||||
// actual success
|
||||
}
|
||||
}
|
||||
case "te": // /te/, transaction end
|
||||
@@ -290,20 +366,46 @@ func handle( conn net.Conn ) {
|
||||
|
||||
|
||||
// globals
|
||||
var qlist []string // list of quotes
|
||||
//var qlist []string // list of quotes
|
||||
var qlist = make( map[string][]string )
|
||||
var qfile string // file object
|
||||
var tsize int // size of transmission buffer
|
||||
|
||||
var VERS string // version
|
||||
|
||||
// main, create port and wait for connection
|
||||
func main() {
|
||||
|
||||
// create buffered channel for catching keyboard interrupt signal
|
||||
sigs := make( chan os.Signal, 1 )
|
||||
// set interrupt and terminate signals to notify channel
|
||||
signal.Notify( sigs, syscall.SIGINT, syscall.SIGTERM )
|
||||
|
||||
// flag vars
|
||||
var v bool // simple version
|
||||
var vv bool // verbose version
|
||||
|
||||
// flag for default transmission size
|
||||
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
|
||||
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
|
||||
flag.BoolVar( &v, "v", false, "display version and exit" )
|
||||
flag.BoolVar( &vv, "vv", false, "display version and about info and exit" )
|
||||
flag.Parse()
|
||||
|
||||
qfile = fread() // read quote file, init ( global ) list of quotes
|
||||
// FIXME get better versioning method
|
||||
VERS = "2.1.2"
|
||||
|
||||
if vv { // display info and exit
|
||||
fmt.Println( "ekho server v" + VERS )
|
||||
fmt.Println( "ekho is named after Echo the Oread" )
|
||||
fmt.Println( "in Greek mythology, Echo was cursed by Hera to only be able to say the last words spoken to her" )
|
||||
fmt.Println( "i found this to be quite fitting for a MOTD server" )
|
||||
return
|
||||
} else if v { // display info and exit
|
||||
fmt.Println( VERS )
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
qfile = fread() // read quote file, init ( global ) list of quotes
|
||||
service := ":1300" // create service on ip and port
|
||||
|
||||
// resolve ip address and port
|
||||
@@ -313,15 +415,19 @@ func main() {
|
||||
// create listener object from ip:port
|
||||
listener, err := net.ListenTCP( "tcp", addr )
|
||||
|
||||
go func() { // keyboard interrupt thread
|
||||
// print kill message
|
||||
fmt.Println( "press ctrl+C to stop..." )
|
||||
<- sigs // blocking read of signal from channel
|
||||
fmt.Println( "\nexiting" ) // exit message
|
||||
os.Exit( 0 ) // exeunt
|
||||
}()
|
||||
|
||||
for {
|
||||
// wait, create connection when found
|
||||
conn, err := listener.Accept()
|
||||
check( err ) // make sure connection works
|
||||
|
||||
go handle( conn ) // handle connection
|
||||
}
|
||||
|
||||
os.Exit( 0 ) // exeunt
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user