forked from ta3pks/betradarProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (85 loc) · 1.44 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
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"sync"
"time"
"./endpoints"
"./lib"
)
var cons = map[*net.Conn]*net.Conn{}
var lock sync.Mutex
func init() {
log.SetFlags(0)
go func() {
for {
time.Sleep(time.Second / 2)
fmt.Printf("\r\x1B[32mConnected Users (%d)\x1B[0m", len(cons))
}
}()
}
var con net.Conn
var err error
var listening bool
var s *bufio.Scanner
func main() {
listening = false
go endpoints.StartListening()
con, err = net.Dial("tcp", *lib.Addr)
if nil != err {
panic(err)
}
lib.SetConn(con)
Login(lib.GetConn())
go func() {
if listening {
return
}
listening = true
fmt.Println("listening on port", *lib.Port)
l, err := net.Listen("tcp", ":"+*lib.Port)
if nil != err {
panic(err)
}
for {
con, err := l.Accept()
if nil != err {
continue
}
lock.Lock()
cons[&con] = &con
lock.Unlock()
}
}()
s = bufio.NewScanner(lib.GetConn())
var scanChan = make(chan string)
go func() {
for s.Scan() {
scanChan <- s.Text()
}
time.Sleep(time.Second * 5)
log.Println("\nbetradar connection was interrrupted restarting")
}()
for {
var dt string
select {
case dt = <-scanChan:
case <-time.After(time.Minute):
fmt.Println("no data for a minute restarting")
os.Exit(0)
}
lock.Lock()
for _, sock := range cons {
_, err := fmt.Fprintln(*sock, dt)
if nil != err {
(*sock).Close()
delete(cons, sock)
continue
}
}
lock.Unlock()
}
}