diff options
| -rw-r--r-- | README.md | 46 | ||||
| -rw-r--r-- | cmd/vsicd/main.go | 3 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | libvsic/libvsic.go | 132 |
5 files changed, 50 insertions, 138 deletions
@@ -1,5 +1,9 @@ -# vsic -*very* simple internet chat +# vsicd + +*very* simple internet chat (vsic) daemon + +if you want to host a vsic server this part of the vsic project, `vsicd`, is probably what you're looking for. if you're looking to develop custom software that interacts with vsic, check out [libvsic](https://github.com/initframs/vsic). + ## features - simple text-based protocol - tiny (5.2M) and fast (starts in 0.004s, stops in 1.005s*) @@ -44,4 +48,40 @@ 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. ```bash CGO_ENABLED=0 go build -ldflags="-s -w" -o vsicd -```
\ No newline at end of file +``` + +## 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 | +| ------------------ | --------------------------- | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| `n/a` | when connecting to a server | inform the client they've connected | `CONNECTED` | +| `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..977ef82 100644 --- a/cmd/vsicd/main.go +++ b/cmd/vsicd/main.go @@ -16,9 +16,10 @@ import ( "sync" "syscall" "time" - vsic "vsic/libvsic" "github.com/pelletier/go-toml/v2" + + vsic "github.com/initframs/vsic" ) type Config struct { @@ -1,5 +1,6 @@ -module vsic +module vsicd -go 1.25 +go 1.26 require github.com/pelletier/go-toml/v2 v2.2.2 +require github.com/initframs/vsic v0.1.0 @@ -1,6 +1,8 @@ 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/initframs/vsic v0.1.0 h1:t9bL2rUoVKb7JicTZ22B3ttQhYfWC3YBNwFzc4+d7KI= +github.com/initframs/vsic v0.1.0/go.mod h1:7m79fa1Dyh/vT7Yzx2QmsOO0DK6bpnBZXHUis1vM+vY= 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= diff --git a/libvsic/libvsic.go b/libvsic/libvsic.go deleted file mode 100644 index 2f9800e..0000000 --- a/libvsic/libvsic.go +++ /dev/null @@ -1,132 +0,0 @@ -package vsic - -import ( - "bufio" - "crypto/rand" - "encoding/binary" - "errors" - "net" - "strings" - "time" -) - -const Version = "0.1.0" - -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"), "") -} |
