diff options
| -rw-r--r-- | README.md | 65 | ||||
| -rw-r--r-- | cmd/vsicd/main.go | 82 | ||||
| -rw-r--r-- | go.mod | 4 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | libvsic/libvsic.go | 1 |
5 files changed, 124 insertions, 30 deletions
@@ -1,17 +1,24 @@ # vsic + *very* simple internet chat + ## features + - simple text-based protocol - tiny (5.2M) and fast (starts in 0.004s, stops in 1.005s*) - light and customizable (toml config!) -> *note: start time measured as average over 128 starts/stops on Arch Linux (i7-4790k and 16GB DDR3) + > *note: start time measured as average over 128 starts/stops on Arch Linux (i7-4790k and 16GB DDR3) + ## vsicd + a vsic server written in go, designed to be completely async and have an ultra small footprint. only 1 config file and 3 commands. + ```bash vsicd start # start the vsic daemon vsicd stop # stop the vsic daemon vsicd info # see stats like ram/cpu usage, connected clients, and more ``` + ```toml # ~/.config/vsicd/config.toml name = "my cool server" @@ -21,8 +28,8 @@ irc still good though """ # vsicd automatically trims blank lines so your config can look better :D [moderation] -# banlist = "~/.config/vsicd/bans.txt" (in progress) -# modcmd = "python3 ~/.config/vsicd/moderation.py" (in progress) +# banlist = "~/.config/vsicd/bans.txt" (todo) +# modcmd = "python3 ~/.config/vsicd/moderation.py" (todo) max_conns_per_ip = 4 max_msgs_per_sec = 1 @@ -30,18 +37,60 @@ max_msg_size = 4096 max_keepalive_timeout = 120 [server.tcp] -enabled = false # both are disabled by default, you don't need to explicitly disable them +enabled = false # exactly one of server.tcp/server.tls must be enabled [server.tls] enabled = true port = 4570 -tls_cert = "/etc/certs/example.com.crt" -tcp_key = "/etc/certs/example.com.key" +ssl_cert = "/etc/certs/example.com.crt" +ssl_key = "/etc/certs/example.com.key" ``` + ### building vsicd + vsicd is pure go, so there's no need for C interop support :) -`-ldflags="-s -w"` removes the DWARF debugging info and the symbol table. this isn't recommended for dev, since DWARF lets you know what broke, but it reduces binary size. when reporting errors/bugs, don't use these options. +`-ldflags="-s -w"` removes the DWARF debugging info and the symbol table. this isn't recommended for development, but it reduces binary size. when reporting errors/bugs, don't use these options. + ```bash CGO_ENABLED=0 go build -ldflags="-s -w" -o vsicd -```
\ No newline at end of file +``` + +## libvsic + +libvsic is a collection of utils to help keep protocol behavior repeatable and to allow the protocol to be reused in other client/server implementations. + +## protocol reference + +the actual vsic TCP protocol. Commands are sent in the format `COMMAND [param]`. + +### commands (client) + +the responses in the table are assuming normal behavior, they don't include cases like hitting rate limits or sending invalid parameters. + + +| Command | Sent | Purpose | Responses | +| ------------------ | --------------------------- | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| `HELLO [username]` | when connecting to a server | initiate connection and register a username | `HELLO [username]` (note that if the name is already taken, a random numerical suffix is added), followed by an `MOTD` message | +| `MSG [msg]` | when sending a message | send a message for the server to broadcast | `MSG [username]: [msg]` is sent to all clients, including the original sender | +| `PING` | at specific timed intervals | tcp keepalive | `PONG` | +| `BYE` | when disconnecting | end connection | `CYA`, then server ends connection. | + + +### errors + +#### `ERROR 100` + +Sent after a malformed command or invalid parameter is received during the handshake. Terminates the connection. + +#### `ERROR 101` + +Sent after an invalid parameter is received (for example, empty `MSG`), but does not terminate the connection. Indicates the command was ignored by the server. + +#### `ERROR 102` + +Sent after an invalid command is received, but does not terminate the connection. Indicates the command was ignored by the server. + +#### `ERROR 200` + +Sent after the client attempts to send a message, but is being rate limited.
\ No newline at end of file diff --git a/cmd/vsicd/main.go b/cmd/vsicd/main.go index a160793..87b93a1 100644 --- a/cmd/vsicd/main.go +++ b/cmd/vsicd/main.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net" "os" "os/exec" @@ -22,9 +23,9 @@ import ( ) type Config struct { - Name string `toml:"name"` - Motd string `toml:"motd"` - AllowPriviledgedPort bool `toml:"allow_priviledged_port"` + Name string `toml:"name"` + Motd string `toml:"motd"` + AllowPrivilegedPort bool `toml:"allow_privileged_port"` Moderation struct { Banlist string `toml:"banlist"` @@ -78,6 +79,20 @@ func nicepanic(s string) { os.Exit(1) } +func ispidrunning(pid int) bool { + if pid <= 0 { + return false + } + + path := "/proc/" + strconv.Itoa(pid) + info, err := os.Stat(path) + if err != nil { + return false + } + + return info.IsDir() +} + func main() { if len(os.Args) < 2 { fmt.Println("usage: vsicd [start|stop|info]") @@ -126,6 +141,14 @@ func start() { } fmt.Println("vsicd started in background, pid:", cmd.Process.Pid) + time.Sleep(time.Second) + if !ispidrunning(cmd.Process.Pid) { + fmt.Println("vsicd crashed at start, the logs below may help:") + logFile.Seek(0, io.SeekStart) + io.Copy(os.Stdout, logFile) + fmt.Println() + return + } return } @@ -145,9 +168,9 @@ func start() { var err error if cfg.Server.TLS.Enabled { - cert, err := tls.LoadX509KeyPair(expand(cfg.Server.TLS.Cert), expand(cfg.Server.TLS.Key)) - if err != nil { - panic(err) + cert, errCert := tls.LoadX509KeyPair(expand(cfg.Server.TLS.Cert), expand(cfg.Server.TLS.Key)) + if errCert != nil { + panic(errCert) } ln, err = tls.Listen("tcp", fmt.Sprintf(":%d", cfg.Server.TLS.Port), &tls.Config{Certificates: []tls.Certificate{cert}}) @@ -164,7 +187,7 @@ func start() { go func() { <-sig fmt.Println("shutting down...") - ln.Close() // stop accepting new conns + ln.Close() s.mu.Lock() for _, c := range s.clients { @@ -251,7 +274,12 @@ func (s *Server) handle(nc net.Conn) { switch cmd { case "MSG": + if arg == "" { + client.Conn.WriteLine("ERROR 101") + continue + } if time.Since(client.LastMsg) < time.Second/time.Duration(max(1, s.cfg.MaxMsgsPerSec)) { + client.Conn.WriteLine("ERROR 200") continue } client.LastMsg = time.Now() @@ -264,6 +292,10 @@ func (s *Server) handle(nc net.Conn) { client.Conn.WriteLine("CYA") s.disconnect(client) return + + default: + client.Conn.WriteLine("ERROR 102") + continue } } @@ -346,7 +378,7 @@ func loadConfig() Config { // todo: more config validation if cfg.MaxMsgSize <= 0 { - cfg.MaxMsgSize = 4096 // so that someone cant just nuke the entire server if MaxMsgSize isnt specified + cfg.MaxMsgSize = 4096 } if cfg.MaxConnsPerIP <= 0 { @@ -357,22 +389,34 @@ func loadConfig() Config { cfg.MaxMsgsPerSec = 1 } - if !cfg.AllowPriviledgedPort { - cfg.AllowPriviledgedPort = false + if !cfg.AllowPrivilegedPort { + cfg.AllowPrivilegedPort = false } - if cfg.Server.TLS.Port == 0 && cfg.Server.TCP.Port == 0 { - nicepanic("no tcp or tls port defined, nothing to do") + tcpOn := cfg.Server.TCP.Enabled + tlsOn := cfg.Server.TLS.Enabled + if tcpOn == tlsOn { + if !tcpOn { + nicepanic("neither server.tcp nor server.tls is enabled; enable one") + } + nicepanic("server.tcp and server.tls cannot both be enabled; enable just one") } - if cfg.Server.TLS.Enabled && (cfg.Server.TLS.Cert == "" || cfg.Server.TLS.Key == "") { - nicepanic("tls enabled but cert or key not defined") + if tlsOn { + if cfg.Server.TLS.Port <= 0 { + nicepanic("tls enabled but server.tls.port is missing or invalid") + } + if cfg.Server.TLS.Cert == "" || cfg.Server.TLS.Key == "" { + nicepanic("tls enabled but cert or key not defined") + } + } else { + if cfg.Server.TCP.Port <= 0 { + nicepanic("tcp enabled but server.tcp.port is missing or invalid") + } } - if ((cfg.Server.TLS.Port <= 1000 && cfg.Server.TLS.Enabled) || (cfg.Server.TCP.Port <= 1000 && cfg.Server.TCP.Enabled)) && (cfg.AllowPriviledgedPort == false) { - fmt.Println(cfg.Server.TLS.Port) - fmt.Println(cfg.Server.TCP.Port) - nicepanic("it looks like you're trying to run vsicd on a priviledged port (<1000). this is disabled by default, but you can enable it by setting `allow_priviledged_port` at the root of your config") + if ((cfg.Server.TLS.Port <= 1000 && cfg.Server.TLS.Enabled) || (cfg.Server.TCP.Port <= 1000 && cfg.Server.TCP.Enabled)) && (cfg.AllowPrivilegedPort == false) { + nicepanic("it looks like you're trying to run vsicd on a privileged port (<1000). this is disabled by default, but you can enable it by setting `allow_privileged_port` at the root of your config") } return cfg @@ -386,7 +430,7 @@ func writePID() { func stop() { data, err := os.ReadFile(pidFile) if err != nil { - fmt.Println("vsicd not running") + fmt.Println("vsicd not running, if it should be, check the logs at ~/.config/vsicd/logs/vsicd.log") return } @@ -1,5 +1,5 @@ module vsic -go 1.25 +go 1.26 -require github.com/pelletier/go-toml/v2 v2.2.2 +require github.com/pelletier/go-toml/v2 v2.3.0
\ No newline at end of file @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pelletier/go-toml/v2 v2.3.0 h1:k59bC/lIZREW0/iVaQR8nDHxVq8OVlIzYCOJf421CaM= +github.com/pelletier/go-toml/v2 v2.3.0/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/libvsic/libvsic.go b/libvsic/libvsic.go index 2f9800e..f38a7cf 100644 --- a/libvsic/libvsic.go +++ b/libvsic/libvsic.go @@ -59,7 +59,6 @@ func (c *Conn) ReadLine() (string, error) { line = strings.TrimRight(line, "\r\n") - // stop multi line injection type stuff if strings.Contains(line, "\n") || strings.Contains(line, "\r") { return "", errors.New("invalid control chars") } |
