Compare commits
No commits in common. "adf22b588f83caf74ce0c1f546ea6bb8a1f8c63b" and "cd009e8ee68d73cbf5507668cafbd25f5e4cf36d" have entirely different histories.
adf22b588f
...
cd009e8ee6
|
@ -1,27 +0,0 @@
|
||||||
name: Go audit
|
|
||||||
run-name: ${{ gitea.actor }} pushed to main! 🚀
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [main]
|
|
||||||
pull_request:
|
|
||||||
branches: [main]
|
|
||||||
jobs:
|
|
||||||
audit:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: '>=1.22.0'
|
|
||||||
|
|
||||||
- name: Verify dependencies
|
|
||||||
run: go mod verify
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: go build -v ./...
|
|
||||||
|
|
||||||
- name: Run go vet
|
|
||||||
run: go vet ./...
|
|
|
@ -8,7 +8,6 @@
|
||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
dist/
|
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
# This is an example .goreleaser.yml file with some sensible defaults.
|
|
||||||
# Make sure to check the documentation at https://goreleaser.com
|
|
||||||
|
|
||||||
# The lines below are called `modelines`. See `:help modeline`
|
|
||||||
# Feel free to remove those if you don't want/need to use them.
|
|
||||||
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
|
||||||
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
|
||||||
|
|
||||||
version: 1
|
|
||||||
|
|
||||||
before:
|
|
||||||
hooks:
|
|
||||||
# You may remove this if you don't use go modules.
|
|
||||||
- go mod tidy
|
|
||||||
# you may remove this if you don't need go generate
|
|
||||||
- go generate ./...
|
|
||||||
|
|
||||||
gitea_urls:
|
|
||||||
api: https://git.bulgariu.xyz/api/v1
|
|
||||||
download: https://git.bulgariu.xyz
|
|
||||||
|
|
||||||
builds:
|
|
||||||
- env:
|
|
||||||
- CGO_ENABLED=0
|
|
||||||
goos:
|
|
||||||
- linux
|
|
||||||
- windows
|
|
||||||
goarch:
|
|
||||||
- amd64
|
|
||||||
|
|
||||||
archives:
|
|
||||||
- format: tar.gz
|
|
||||||
# this name template makes the OS and Arch compatible with the results of `uname`.
|
|
||||||
name_template: >-
|
|
||||||
{{ .ProjectName }}_
|
|
||||||
{{- title .Os }}_
|
|
||||||
{{- if eq .Arch "amd64" }}x86_64
|
|
||||||
{{- else if eq .Arch "386" }}i386
|
|
||||||
{{- else }}{{ .Arch }}{{ end }}
|
|
||||||
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
|
||||||
# use zip for windows archives
|
|
||||||
format_overrides:
|
|
||||||
- goos: windows
|
|
||||||
format: zip
|
|
||||||
|
|
||||||
changelog:
|
|
||||||
sort: asc
|
|
||||||
filters:
|
|
||||||
exclude:
|
|
||||||
- "^docs:"
|
|
||||||
- "^test:"
|
|
10
README.md
10
README.md
|
@ -1,11 +1,3 @@
|
||||||
# mini-chat
|
# mini-chat
|
||||||
|
|
||||||
Tiny IRC-like chat server built for compatibility with netcat and written in Go
|
Tiny IRC-like chat server I'm building in Go
|
||||||
|
|
||||||
# # Usage
|
|
||||||
|
|
||||||
# # # Starting the server
|
|
||||||
./mini-chat server --port 1337
|
|
||||||
|
|
||||||
# # # Connecting to the server
|
|
||||||
nc $SERVER_IP $SERVER_PORT
|
|
18
cmd/root.go
18
cmd/root.go
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2024 Raul
|
Copyright © 2024 Raul
|
||||||
*/
|
|
||||||
|
|
||||||
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -10,16 +10,18 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "mini-chat",
|
Use: "mini-chat",
|
||||||
Short: "Minimalistic chat server built using Go",
|
Short: "A brief description of your application",
|
||||||
Long: `mini-chat was originally designed to be used as a standalone server
|
Long: `A longer description that spans multiple lines and likely contains
|
||||||
users could connect to using netcat, but it can be also used with the binary
|
examples and usage of using your application. For example:
|
||||||
client
|
|
||||||
|
|
||||||
Examples:
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
./mini-chat server --port 8080`,
|
This application is a tool to generate the needed files
|
||||||
|
to quickly create a Cobra application.`,
|
||||||
// Uncomment the following line if your bare application
|
// Uncomment the following line if your bare application
|
||||||
// has an action associated with it:
|
// has an action associated with it:
|
||||||
// Run: func(cmd *cobra.Command, args []string) { },
|
// Run: func(cmd *cobra.Command, args []string) { },
|
||||||
|
@ -45,3 +47,5 @@ func init() {
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,31 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2024 Raul
|
Copyright © 2024 Raul
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
//"log"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
//"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// serverCmd represents the server command
|
// serverCmd represents the server command
|
||||||
var serverCmd = &cobra.Command{
|
var serverCmd = &cobra.Command{
|
||||||
Use: "server",
|
Use: "server",
|
||||||
Short: "Main chat server",
|
Short: "A brief description of your command",
|
||||||
Long: `You can connect to this server by running the following command from
|
Long: `A longer description that spans multiple lines and likely contains examples
|
||||||
a client:
|
and usage of using your command. For example:
|
||||||
nc $SERVER_IP $PORT
|
|
||||||
|
|
||||||
Assuming your IP is 192.168.0.30 and the server port is left on default the
|
Cobra is a CLI library for Go that empowers applications.
|
||||||
command would be as follows:
|
This application is a tool to generate the needed files
|
||||||
nc 192.168.0.30 1302`,
|
to quickly create a Cobra application.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
server(cmd)
|
server(cmd)
|
||||||
},
|
},
|
||||||
|
@ -44,13 +46,6 @@ func init() {
|
||||||
serverCmd.Flags().String("port", "1302", "Port to listen on")
|
serverCmd.Flags().String("port", "1302", "Port to listen on")
|
||||||
}
|
}
|
||||||
|
|
||||||
var receiverIsStarted bool = false
|
|
||||||
|
|
||||||
type chatter struct {
|
|
||||||
Username string
|
|
||||||
IP string
|
|
||||||
}
|
|
||||||
|
|
||||||
func server(cmd *cobra.Command) {
|
func server(cmd *cobra.Command) {
|
||||||
var lport string
|
var lport string
|
||||||
port, _ := cmd.Flags().GetString("port")
|
port, _ := cmd.Flags().GetString("port")
|
||||||
|
@ -63,66 +58,30 @@ func server(cmd *cobra.Command) {
|
||||||
ln, err := net.Listen("tcp", ":"+lport)
|
ln, err := net.Listen("tcp", ":"+lport)
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
fmt.Printf("Listening on port %v...\n", lport)
|
fmt.Printf("Listening on port %v...\n", lport)
|
||||||
masterChannel := make(chan string, 20)
|
|
||||||
|
|
||||||
slaveChannel := make(chan string, 20)
|
|
||||||
go masterReceiver(slaveChannel, masterChannel)
|
|
||||||
// TODO: get channels properly working
|
|
||||||
// receivingChannel := make(chan string)
|
|
||||||
// sendingChannel := make(chan string)
|
|
||||||
for {
|
for {
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
numOfClients++
|
go handleConn(conn)
|
||||||
go handleConn(conn, slaveChannel, masterChannel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var numOfClients int = 0
|
type chatter struct {
|
||||||
|
Username string
|
||||||
func masterReceiver(slaveChannel chan<- string, masterChannel <-chan string) {
|
IP string
|
||||||
for {
|
|
||||||
message := <-masterChannel
|
|
||||||
for i := 0; i < numOfClients; i++ {
|
|
||||||
slaveChannel <- message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// func receiver(conn net.Conn, ch chan string) {
|
func handleConn(conn net.Conn) {
|
||||||
// fmt.Println("THE RECEIVER HAS BEEN STARTED, YOU CAN ONLY SEE THIS MESSAGE ONCE")
|
|
||||||
// receiverIsStarted = false
|
|
||||||
// for {
|
|
||||||
// select {
|
|
||||||
// case otherMessage := <-ch:
|
|
||||||
// conn.Write([]byte(otherMessage))
|
|
||||||
// default:
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func handleConn(conn net.Conn, slaveChannel <-chan string, masterChannel chan<- string) {
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
//var otherMessage string
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
//select {
|
|
||||||
message := <-slaveChannel
|
|
||||||
conn.Write([]byte(message))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
IP := getIP(conn)
|
IP := getIP(conn)
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
date := fmt.Sprintf("%d-%02d-%02d | %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
date := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
||||||
final_message_connect := fmt.Sprintf("[%v] Received connection from %v!\n", date, IP)
|
final_message := fmt.Sprintf("[%v] Received connection from %v!\n", date, IP)
|
||||||
fmt.Print(final_message_connect)
|
fmt.Print(final_message)
|
||||||
conn.Write([]byte("What's your name?\nName: "))
|
conn.Write([]byte("What's your name?\nName: "))
|
||||||
name, err := bufio.NewReader(conn).ReadString('\n')
|
name, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
numOfClients--
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var NewUser = new(chatter)
|
var NewUser = new(chatter)
|
||||||
|
@ -132,13 +91,10 @@ func handleConn(conn net.Conn, slaveChannel <-chan string, masterChannel chan<-
|
||||||
message, err := bufio.NewReader(conn).ReadString('\n')
|
message, err := bufio.NewReader(conn).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf(NewUser.Username + " disconnected!\n")
|
fmt.Printf(NewUser.Username + " disconnected!\n")
|
||||||
numOfClients--
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
finalMessage := fmt.Sprint("{" + NewUser.IP + "} " + NewUser.Username + ": " + message)
|
fmt.Print("{" + NewUser.IP + "} " + NewUser.Username + ": " + message)
|
||||||
fmt.Print(finalMessage)
|
conn.Write([]byte(message))
|
||||||
masterChannel <- finalMessage
|
|
||||||
//conn.Write([]byte(message))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue