-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
268 lines (229 loc) · 7.64 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/Akilan1999/remotegameplay/config"
"github.com/Akilan1999/remotegameplay/core"
gameserver "github.com/Akilan1999/remotegameplay/server"
"log"
"math/rand"
"net/http"
"os/exec"
"time"
)
func main() {
addr := flag.String("addr", "0.0.0.0", "Listen address")
//port := flag.String("port", "8888", "port for running the server")
barrier := flag.Bool("barrier", false, "set external port barrier")
tls := flag.Bool("tls", false, "Use TLS")
setconfig := flag.Bool("setconfig", false, "Generates a config file")
certFile := flag.String("certFile", "files/server.crt", "TLS cert file")
keyFile := flag.String("keyFile", "files/server.key", "TLS key file")
headless := flag.Bool("headless", false, "Creating screenshare using headless mode")
roomInfo := flag.Bool("roomInfo", false, "Getting room id of headless server")
killServer := flag.Bool("killServer", false, "Kills the laplace")
killChromium := flag.Bool("killChromium", false, "Kills all chromuim")
BinaryToExecute := flag.String("BinaryToExecute", "", "Providing path (i.e Absolute path) of binary to execute")
GameServer := flag.Bool("GameServer", false, "Starts the game server by default")
Migrate := flag.Bool("Migrate", false, "Sets up the tables for the Sqlite database")
BothServers := flag.Bool("BothServers", false, "Starts the Gameserver and screenshare. Also ensures the screenshare can broadcast the availability to the game server")
flag.Parse()
rand.Seed(time.Now().UnixNano())
server := core.GetHttp()
if *setconfig {
err := config.SetDefaults()
if err != nil {
return
}
return
}
Config, err := config.ConfigInit()
if err != nil {
log.Fatalln(err)
}
// if barrier flag is provided
if *barrier {
natBarrier, err := core.EscapeNATBarrier()
if err != nil {
fmt.Println(err)
return
}
Config.NATEscapeBarrierPort = natBarrier
err = Config.WriteConfig()
if err != nil {
return
}
fmt.Println(Config.IPAddress + ":" + Config.NATEscapeBarrierPort)
// Starts infinite loop to FRP server running for escaping NAT
for {
}
}
// Print out room information
if *roomInfo {
room, err := core.ReadRoomsFile()
if err != nil {
log.Fatalln(err)
}
PrettyPrint(room)
return
}
// kills laplace server
if *killServer {
cmd := exec.Command("pkill", "remotegameplay")
if err := cmd.Run(); err != nil {
fmt.Println(err)
}
return
}
// kills chromium server
if *killChromium {
cmd := exec.Command("pkill", "chromium")
if err := cmd.Run(); err != nil {
fmt.Println(err)
}
return
}
if *Migrate {
// Connect to Sqlite
connect, err := gameserver.Connect()
if err != nil {
fmt.Println(err)
}
// Migrate table
session, err := gameserver.CreateTables(connect)
if err != nil {
fmt.Println(err)
}
session.Scan(gameserver.GameSession{})
return
}
// running implementation to escape NAT
GameServerPort, ScreenSharePort, err := core.EscapeNAT(Config.InternalScreenSharePort, Config.InternalGameServerPort)
if err != nil {
log.Fatalln(err)
}
Config.IPAddress = "64.227.168.102"
Config.NATEscapeGameServerPort = GameServerPort
Config.NATEscapeScreenSharePort = ScreenSharePort
Config.NATEscapeBarrierPort = ""
err = Config.WriteConfig()
if err != nil {
log.Fatalln(err)
}
// If both server and screenshare have be triggered (first set the according flags to true)
if *BothServers {
*headless = true
*tls = true
*GameServer = true
}
if *GameServer {
go gameserver.Server(Config.InternalGameServerPort)
time.Sleep(2 * time.Second)
}
if !*GameServer || *BothServers {
if *tls {
log.Println("Listening on TLS:", *addr+":"+Config.InternalScreenSharePort)
go http.ListenAndServeTLS(*addr+":"+Config.InternalScreenSharePort, *certFile, *keyFile, server)
time.Sleep(2 * time.Second)
} else {
log.Println("Listening:", *addr+":"+Config.InternalScreenSharePort)
go http.ListenAndServe(*addr+":"+Config.InternalScreenSharePort, server)
time.Sleep(2 * time.Second)
}
}
// If both server selected set the remote Gameserver to local now.
if *BothServers {
Config.BackendURL = "http://" + Config.IPAddress + ":" + Config.NATEscapeGameServerPort + "/"
err = Config.WriteConfig()
if err != nil {
fmt.Println(err)
return
}
}
// Running in headless mode
if *headless {
// Running starting a browser as a background process
go func() {
Config, err = config.ConfigInit()
if err != nil {
log.Fatalln(err)
}
//// Returns the URl address type
//Addr := Ip4or6(Config.IPAddress)
//
//// If address is provided
//if *addr != "" {
// Addr = *addr
// // Add brackets if the ip address is ipv6
// Addr = Ip4or6(Addr)
//}
var TaskExecute string
if *BinaryToExecute != "" {
TaskExecute = *BinaryToExecute
} else {
// Read binary from config file
TaskExecute = Config.ScriptToExecute
}
// Starting screen share headless
cmd := exec.Command(Config.BrowserCommand, "--no-sandbox", "--auto-select-desktop-capture-source="+Config.ScreenName, "--url", "https://"+*addr+":"+Config.InternalScreenSharePort+"/?mode=headless", "--ignore-certificate-errors")
if err := cmd.Start(); err != nil {
log.Fatalln(err)
}
// Makes program sleep for 2 seconds to allow chromium browser to open
time.Sleep(3 * time.Second)
err = core.BroadcastServerToBackend()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("success broadcasting to game server")
}
// Task to be executed
err = RunTask(TaskExecute)
if err != nil {
fmt.Println(err)
return
}
}()
}
for {
}
// Start P2PRC server
//_, err = abstractions.Start()
//if err != nil {
// return
//}
}
// PrettyPrint print the contents of the obj (
// Reference: https://stackoverflow.com/questions/24512112/how-to-print-struct-variables-in-console
func PrettyPrint(data interface{}) {
var p []byte
// var err := error
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}
// Ip4or6 Helper function to check if the IP address is IPV4 or
// IPV6 (https://socketloop.com/tutorials/golang-check-if-ip-address-is-version-4-or-6)
func Ip4or6(s string) string {
for i := 0; i < len(s); i++ {
switch s[i] {
case '.':
return s
case ':':
return "[" + s + "]"
}
}
return "[" + s + "]"
}
func RunTask(task string) error {
// Halts the process
cmd := exec.Command("sh", task)
if err := cmd.Start(); err != nil {
return err
}
return nil
}