-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosqueal.go
57 lines (55 loc) · 1.41 KB
/
gosqueal.go
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
package main
import (
"database/sql"
"flag"
"github.com/rs/zerolog/log"
_ "modernc.org/sqlite"
"net"
"os"
)
func main() {
srvHost := flag.String("host", "0.0.0.0", "server ip")
srvPort := flag.String("port", "1118", "server port")
flag.Parse()
hostname, err := os.Hostname()
if err != nil {
panic(err)
os.Exit(1)
}
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
panic(err)
os.Exit(1)
}
db.Exec(`
create table translog(time timestamp primary key, client text, query text);
`)
server, err := net.Listen("tcp", *srvHost+":"+*srvPort)
if err != nil {
log.Error().Msg(hostname + ":: error listening:" + err.Error())
os.Exit(1)
}
defer server.Close()
log.Info().Msg(hostname + ":: listening on " + *srvHost + ":" + *srvPort)
for {
connection, err := server.Accept()
if err != nil {
log.Info().Msg("Error accepting: " + err.Error())
os.Exit(1)
}
ip := connection.RemoteAddr().String()
log.Info().Msg(hostname + ":: client connected from " + ip)
buffer := make([]byte, 1024)
mLen, err := connection.Read(buffer)
if err != nil {
log.Info().Msg("Error reading:" + err.Error())
}
log.Info().Msg("From " + ip + " Received: " + string(buffer[:mLen]))
_, err = db.Exec("INSERT into translog VALUES(TIME('now'),?,?);", ip, string(buffer[:mLen]))
if err != nil {
log.Info().Msg("error: " + err.Error())
}
_, err = db.Exec(string(buffer[:mLen]))
connection.Close()
}
}