aboutsummaryrefslogtreecommitdiff
path: root/gigi.go
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-07-18 03:20:21 +0000
committerFuwn <[email protected]>2024-07-18 03:20:21 +0000
commit8739ca873a651ffdc9e909ed58e56ca4b0e2fe64 (patch)
treed1d943aaac46b2e7f2c94f8cc75547cee221fc96 /gigi.go
parentdocs(readme): fix configuration path (diff)
downloadgigi-8739ca873a651ffdc9e909ed58e56ca4b0e2fe64.tar.xz
gigi-8739ca873a651ffdc9e909ed58e56ca4b0e2fe64.zip
feat: rewrite in go
Diffstat (limited to 'gigi.go')
-rw-r--r--gigi.go120
1 files changed, 120 insertions, 0 deletions
diff --git a/gigi.go b/gigi.go
new file mode 100644
index 0000000..e127b7d
--- /dev/null
+++ b/gigi.go
@@ -0,0 +1,120 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net"
+ "os"
+ "os/exec"
+ "strconv"
+ "strings"
+)
+
+const (
+ maximumCommandLength = 256
+ modeStatic = 0
+ modeDynamic = 1
+)
+
+func main() {
+ fingerPortOption := os.Getenv("GIGI_PORT")
+ modeOption := os.Getenv("GIGI_DYNAMIC")
+ mode := modeStatic
+
+ if fingerPortOption == "" {
+ fingerPortOption = "79"
+ }
+
+ if modeOption != "" {
+ if mode, _ = strconv.Atoi(modeOption); mode > 0 {
+ mode = modeDynamic
+ }
+ }
+
+ listener, listenerError := net.Listen("tcp", fmt.Sprintf(":%s", fingerPortOption))
+
+ if listenerError != nil {
+ log.Fatalf("error: %s\n", listenerError.Error())
+ }
+
+ for {
+ connection, connectionError := listener.Accept()
+
+ if connectionError != nil {
+ log.Println("warn: listener could not accept connection")
+ }
+
+ go handleConnection(connection, mode)
+ }
+}
+
+func handleConnection(connection net.Conn, mode int) {
+ defer connection.Close()
+
+ connectionReadBuffer := make([]byte, maximumCommandLength)
+ _, readError := connection.Read(connectionReadBuffer)
+
+ if readError != nil {
+ log.Println("warn: could not read from connection")
+
+ return
+ }
+
+ bufferContent := strings.Replace(
+ strings.Replace(
+ strings.Replace(string(connectionReadBuffer), "\x00", "", -1),
+ "\n", "", -1), "\r", "", -1)
+
+ if len(bufferContent) == 0 {
+ bufferContent = "default"
+ }
+
+ var fileContent string
+ var fileReadError error
+
+ switch mode {
+ case modeDynamic:
+ fileContent, fileReadError = runFile(bufferContent)
+
+ default:
+ fileContent, fileReadError = readFile(bufferContent)
+ }
+
+ if fileReadError != nil {
+ log.Printf("warn: could not read from file: %s", bufferContent)
+
+ return
+ }
+
+ connection.Write([]byte(fileContent))
+ log.Printf("info: success: %s", bufferContent)
+}
+
+func readFile(filename string) (string, error) {
+ fileContent, fileReadError := os.ReadFile(fmt.Sprintf("./.gigi/%s", filename))
+
+ if fileReadError != nil {
+ fileContent, fileReadError = os.ReadFile("./.gigi/default")
+
+ if fileReadError != nil {
+ log.Printf("error: could not read from file: %s\n", filename)
+
+ return "", fileReadError
+ }
+ }
+
+ return string(fileContent), nil
+}
+
+func runFile(arguments string) (string, error) {
+ command := exec.Command("./.gigi/do", arguments)
+ commandOutput, commandError := command.Output()
+
+ if commandError != nil {
+ log.Printf("error: could not run command: %s\n", commandError)
+
+ return "", commandError
+ }
+
+ return string(commandOutput), nil
+}