diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | README.md | 51 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 20 | ||||
| -rw-r--r-- | libvsic/libvsic.go | 130 |
5 files changed, 207 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d0d6b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vsicd
\ No newline at end of file @@ -1,2 +1,53 @@ # vsic *very* simple internet chat +## features +- simple text-based protocol +- tiny and fast +- light and customizable +## 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" +motd = "~/.config/vsicd/motd.txt" + +[moderation] +banlist = "~/.config/vsicd/bans.txt" +modcmd = "python3 ~/.config/vsicd/moderation.py" + +max_conns_per_ip = 4 +max_msgs_per_sec = 1 +max_msg_size = 4096 +max_keepalive_timeout = 120 + +[server.tcp] +enabled = false # both are disabled by default + +[server.tls] +enabled = true +port = 4570 +tls_cert = "/etc/certs/example.com.crt" +tcp_key = "/etc/certs/example.com.key" +``` +## vs2c +a simple and light cli-based vsic client, also written in go. simple file-based configuration +```toml +# ~/.config/vs2c/config.toml + +[servers.coolserver] +name = "cool server" +port = 4570 +tls = true +nick = "initframs" + +[servers.offtopic] +name = "offtopic lounge" +port = 4572 +tls = false +nick = "ilovevsic2642" +```
\ No newline at end of file @@ -0,0 +1,5 @@ +module vsic + +go 1.25 + +require github.com/pelletier/go-toml/v2 v2.2.2 @@ -0,0 +1,20 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/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= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/libvsic/libvsic.go b/libvsic/libvsic.go new file mode 100644 index 0000000..343132a --- /dev/null +++ b/libvsic/libvsic.go @@ -0,0 +1,130 @@ +package vsic + +import ( + "bufio" + "crypto/rand" + "encoding/binary" + "errors" + "net" + "strings" + "time" +) + +type Config struct { + MaxMsgSize int + TimeoutSec int +} + +type Conn struct { + NetConn net.Conn + R *bufio.Reader + W *bufio.Writer + Nick string + cfg Config +} + +func Wrap(c net.Conn, cfg Config) *Conn { + if cfg.MaxMsgSize <= 0 { + cfg.MaxMsgSize = 4096 + } + if cfg.TimeoutSec <= 0 { + cfg.TimeoutSec = 120 + } + + return &Conn{ + NetConn: c, + R: bufio.NewReaderSize(c, cfg.MaxMsgSize), + W: bufio.NewWriter(c), + cfg: cfg, + } +} + +func (c *Conn) Close() error { + return c.NetConn.Close() +} + +func (c *Conn) ReadLine() (string, error) { + _ = c.NetConn.SetReadDeadline(time.Now().Add(time.Duration(c.cfg.TimeoutSec) * time.Second)) + + line, err := c.R.ReadString('\n') + if err != nil { + return "", err + } + + if len(line) > c.cfg.MaxMsgSize { + return "", errors.New("message too big") + } + + 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") + } + + return line, nil +} + +func (c *Conn) WriteLine(s string) error { + _ = c.NetConn.SetWriteDeadline(time.Now().Add(10 * time.Second)) + + if len(s) > c.cfg.MaxMsgSize { + return errors.New("message too big") + } + + if strings.ContainsAny(s, "\n\r") { + return errors.New("invalid control chars") + } + + if _, err := c.W.WriteString(s + "\n"); err != nil { + return err + } + + return c.W.Flush() +} + +func ParseCommand(line string) (cmd string, arg string) { + if i := strings.IndexByte(line, ' '); i != -1 { + return line[:i], strings.TrimSpace(line[i+1:]) + } + return line, "" +} + +func ValidNick(n string) bool { + if len(n) < 3 || len(n) > 20 { + return false + } + for _, r := range n { + if !(r >= 'a' && r <= 'z' || + r >= 'A' && r <= 'Z' || + r >= '0' && r <= '9' || + r == '_') { + return false + } + } + return true +} + +func RandomSuffix() string { + var b [2]byte + _, _ = rand.Read(b[:]) + n := binary.BigEndian.Uint16(b[:]) % 10000 + return "_" + pad4(int(n)) +} + +func pad4(n int) string { + if n < 10 { + return "000" + itoa(n) + } + if n < 100 { + return "00" + itoa(n) + } + if n < 1000 { + return "0" + itoa(n) + } + return itoa(n) +} + +func itoa(n int) string { + return strings.TrimPrefix(strings.TrimPrefix(time.Unix(int64(n), 0).UTC().Format("0000"), "1970"), "") +} |
