-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (85 loc) · 2.19 KB
/
main.go
File metadata and controls
110 lines (85 loc) · 2.19 KB
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
package main
import (
"flag"
"fmt"
"gamelinksafecli/config"
"gamelinksafecli/proxy"
"gamelinksafecli/webrtc"
"io"
"io/fs"
"log"
"os"
"os/signal"
"syscall"
)
const (
defaultPort uint = 8080
defaultProtocol string = "tcp"
)
func main() {
go handleInterrupt()
log.SetOutput(io.Discard)
rolPtr := flag.String("role", "host", "Role of the application (host/client)")
portPtr := flag.Uint("port", defaultPort, "Port to listen on")
protocolPtr := flag.String("protocol", defaultProtocol, "Protocol to use (tcp/udp)")
configFilePtr := flag.String("config", "servers.yml", "Path to the config file for STUN/TURN urls and credentials")
flag.BoolFunc("verbose", "Enables logging in stdout", func(string) error {
log.SetOutput(os.Stdout)
return nil
})
flag.Parse()
rol := *rolPtr
port := *portPtr
protocol := *protocolPtr
configFile := *configFilePtr
if port < 1 || port > 65535 {
fmt.Println("Error: Port must be between 1 and 65535")
return
}
if protocol != "tcp" && protocol != "udp" {
fmt.Println("Error: Protocol must be either 'tcp' or 'udp'")
return
}
if rol != "host" && rol != "client" {
fmt.Println("Error: Role must be either 'host' or 'client'")
return
}
if !fs.ValidPath(configFile) {
fmt.Println("Error: config file path must be a valid path")
return
}
iceServers := config.LoadICEServers(configFile)
fmt.Println("Starting proxy with the following configuration:")
fmt.Printf("Port: %d\n", port)
fmt.Printf("Protocol: %s\n", protocol)
var protocolEnum uint
switch protocol {
case "tcp":
protocolEnum = proxy.TCP
case "udp":
protocolEnum = proxy.UDP
}
if rol == "client" {
err := webrtc.ClientWebrtc(port, protocolEnum, iceServers)
if err != nil {
log.Printf("Error starting WebRTC client: %v\n", err)
return
}
return
}
if rol == "host" {
err := webrtc.HostWebrtc(port, protocolEnum, iceServers)
if err != nil {
log.Printf("Error starting WebRTC host: %v\n", err)
return
}
}
}
func handleInterrupt() {
stopChan := make(chan os.Signal, 2)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-stopChan
close(stopChan)
fmt.Println("Stop (Ctr+C) detected: closing connection")
os.Exit(0)
}