aboutsummaryrefslogtreecommitdiff
path: root/gigi.go
blob: 4bfb16cdf1cc2b9a93e053f4917e24e71d63faaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main

import (
	"log"
	"net"
	"os"
	"os/exec"
	"strconv"
	"sync"
)

const (
	maximumCommandLength = 256
	modeStatic           = 0
	modeDynamic          = 1
)

var bufferPool = sync.Pool{
	New: func() interface{} {
		b := make([]byte, maximumCommandLength)

		return &b
	},
}

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", ":"+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) {
	connectionReadBuffer := bufferPool.Get().(*[]byte)

	defer bufferPool.Put(connectionReadBuffer)
	defer connection.Close()

	n, readError := connection.Read(*connectionReadBuffer)

	if readError != nil {
		log.Println("warn: could not read from connection")

		return
	}

	filename := cleanBuffer((*connectionReadBuffer)[:n])

	if len(filename) == 0 {
		filename = "default"
	}

	var fileContent []byte
	var fileReadError error

	switch mode {
	case modeDynamic:
		fileContent, fileReadError = runFile(filename)
	default:
		fileContent, fileReadError = readFile(filename)
	}

	if fileReadError != nil {
		log.Printf("warn: could not read from file: %s", filename)

		return
	}

	if _, connectionWriteError := connection.Write(fileContent); connectionWriteError != nil {
		log.Printf("warn: could not write to connection: %s", connectionWriteError.Error())
	} else {
		log.Printf("info: success: %s", filename)
	}
}

func cleanBuffer(buf []byte) string {
	result := make([]byte, 0, len(buf))

	for _, b := range buf {
		if b != 0 && b != '\n' && b != '\r' {
			result = append(result, b)
		}
	}

	return string(result)
}

func readFile(filename string) ([]byte, error) {
	fileContent, fileReadError := os.ReadFile("./.gigi/" + 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 nil, fileReadError
		}
	}

	return fileContent, nil
}

func runFile(arguments string) ([]byte, 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 nil, commandError
	}

	return commandOutput, nil
}