-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (76 loc) · 2.23 KB
/
main.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
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
package main
import (
"bufio"
"fmt"
"net"
"shortsig/core/config"
"shortsig/core/log"
"shortsig/core/service"
"strings"
)
var conf config.Config
func main() {
// parse config
conf = config.ParseConfigs()
// print version
if conf.ShowVersion {
log.PrintConsole(log.INFO, "version %s", "1.0.1")
return
}
// spinning tcp server
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.Port))
log.PanicErr(err)
log.PrintConsole(log.INFO, "listening on port %d", conf.Port)
// listening to incoming tcp connections
for {
conn, err := listener.Accept()
if err != nil {
log.PrintConsole(log.WARN, "connection accept error %s", err)
continue
}
// whitelising
// connIP := strings.Split(conn.RemoteAddr().String(), ":")[0]
//
// is_whitelisted := false
// for _, ip := range conf.Whitelist {
// if ip == connIP { is_whitelisted = true }
// }
//
// if (!is_whitelisted) {
// log.Printf("IP %s not whitelisted", connIP)
// continue
// }
// accepting connections and handling them in a new thread
log.PrintConsoleAndTCP(conn, log.NET, "%s | connection accepted", conn.RemoteAddr())
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
s := bufio.NewScanner(conn)
for s.Scan() {
log.PrintConsole(log.DEBUG, "%s | Incoming Data", conn.RemoteAddr())
data := s.Text()
if !handleTCPCmd(data, conn) {
break
}
}
}
func handleTCPCmd(TCPData string, conn net.Conn) bool {
TCPDataArr := strings.Split(TCPData, " ")
TCPCmd := TCPDataArr[0]
TCPCmdArgs := TCPDataArr[1:]
switch TCPCmd {
case "":
log.PrintConsoleAndTCP(conn, log.NET, "%s | Empty TCP Command", conn.RemoteAddr())
case "exit":
log.PrintConsoleAndTCP(conn, log.NET, "%s | Disconnected", conn.RemoteAddr())
case "exec":
log.PrintConsoleAndTCP(conn, log.NET, "%s | Executing TCP Command %s", conn.RemoteAddr(), TCPCmdArgs)
service.ExecRoutine(conn, TCPCmdArgs, conf.Routines)
log.PrintConsoleAndTCP(conn, log.NET, "%s | Executed TCP Command %s", conn.RemoteAddr(), TCPCmdArgs)
default:
log.PrintConsoleAndTCP(conn, log.NET, "%s | Invalid TCP Command", conn.RemoteAddr().String())
}
return true
}