-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (74 loc) · 1.38 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
package main
import (
"crypto/sha1"
"crypto/tls"
"fmt"
"log"
"os"
"syscall"
"gobroker/actions"
"gobroker/db"
)
func setRLimit() {
var rLimit syscall.Rlimit
or := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err(or) {
os.Exit(3)
}
rLimit.Max = 20000
rLimit.Cur = 20000
or = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err(or) {
os.Exit(3)
}
or = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err(or) {
os.Exit(3)
}
log.Println("TCP limit set to:", rLimit.Max)
}
func err(e error) bool {
if e != nil {
log.Print(e)
return true
}
return false
}
func main() {
cert, or := tls.LoadX509KeyPair("cert/cert.pem", "cert/key.pem")
if err(or) {
createCertificate()
cert, or = tls.LoadX509KeyPair("cert/cert.pem", "cert/key.pem")
if err(or) {
os.Exit(1)
}
}
var fingerprint []byte
for _, s := range cert.Certificate {
fingerprint = append(fingerprint, s...)
}
sha := ""
for _, s := range sha1.Sum(fingerprint) {
if sha != "" {
sha += ":"
}
sha += fmt.Sprintf("%02X", s)
}
log.Println(sha)
cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
listen, or := tls.Listen("tcp4", db.Conf.Host, cfg)
if err(or) {
os.Exit(1)
}
log.Printf("Listening on %s", listen.Addr())
for {
conn, or := listen.Accept()
if or != nil {
if db.Conf.Debug {
log.Println(or)
}
continue
}
go actions.Handle(conn)
}
}