forked from madcitygg/csgostats-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (75 loc) · 2.36 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
package main
import (
"fmt"
"log"
"net"
//"net/http"
"os"
"os/signal"
//"strings"
"syscall"
"github.com/jessevdk/go-flags"
"github.com/mitchellh/colorstring"
)
const (
version = "0.0.2"
)
type Options struct {
Bind string `short:"b" long:"bind" value-name:"IP" description:"Address to bind to" default:"0.0.0.0"`
Port int `short:"p" long:"port" value-name:"PORT" description:"Port to listen on" default:"auto"`
Target string `short:"t" long:"target" value-name:"ADDRESS" description:"API endpoints to forward logs to" default:"http://logs.madcity.gg/api/logs/"`
Verbose bool `short:"v" long:"verbose" description:"Verbose output"`
Version bool `long:"version" description:"Show version and exit"`
// LogDirectory string `short:"l" long:"logdir" value-name:"DIRECTORY" description:"Directory to write logs to"`
}
func main() {
// Parse options
var options Options
var parser = flags.NewParser(&options, flags.Default)
if _, err := parser.Parse(); err != nil {
os.Exit(1)
}
// Show version and exit
if options.Version {
colorstring.Println(version)
os.Exit(0)
}
// Test target
// client := &http.Client{}
// resp, err := client.Post(strings.TrimLeft(options.Target, "/")+"/ping", "application/json", nil)
// if err != nil {
// colorstring.Printf("[red]ERROR: Could not ping %s, got: %s\n", options.Target, err.Error())
// os.Exit(1)
// }
// resp.Body.Close()
// Open socket
address := fmt.Sprintf("%s:%d", options.Bind, options.Port)
resolvedAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
colorstring.Printf("[red]ERROR: Could not resolve %s\n", address)
os.Exit(1)
}
socket, err := net.ListenUDP("udp", resolvedAddr)
if err != nil {
colorstring.Printf("[red]ERROR: Could not bind to %s\n", address)
colorstring.Printf("[red]%s\n", err.Error())
os.Exit(1)
}
defer socket.Close()
socket.SetReadBuffer(1048576)
// Tell user where to send stuff
socketAddr := socket.LocalAddr().(*net.UDPAddr)
log.Printf("Listening on %s:%d\n", options.Bind, socketAddr.Port)
// Start listener
listener := NewListener(socket, &options)
go listener.Listen()
// Handle SIGINT and SIGTERM.
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
s := <-c
log.Println("Got signal:", s)
log.Println("Attempting graceful shutdown")
// Stop the service gracefully.
listener.Stop()
log.Println("Done")
}