-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (148 loc) · 3.88 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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"
"github.com/gorilla/websocket"
)
type Config struct {
RFIDPort string // Port which RFID-unit is listening on
HTTPPort string // Listening Port of the HTTP and WebSocket server
SIPServer string // Address of the SIP-server
// Credentials for SIP user to use in rfid-hub
SIPUser string
SIPPass string
SIPDept string
SIPMaxConn int
RFIDTimeout time.Duration
WSProxy bool
LogSIPMessages bool
LogRFID bool
}
type rfidMsg struct {
barcode string
branch string
clientIP string
sipMsgType string
}
// global variables
var (
config = Config{
RFIDPort: "6005",
HTTPPort: "8899",
SIPServer: "sip_proxy:9999",
SIPUser: "autouser",
SIPPass: "autopass",
SIPMaxConn: 5,
LogSIPMessages: true,
RFIDTimeout: 15 * time.Minute,
WSProxy: true,
}
hub *Hub
logToRFID chan rfidMsg
)
func init() {
// TODO move these to command line flags
if os.Getenv("TCP_PORT") != "" {
config.RFIDPort = os.Getenv("TCP_PORT")
}
if os.Getenv("HTTP_PORT") != "" {
config.HTTPPort = os.Getenv("HTTP_PORT")
}
if os.Getenv("SIP_SERVER") != "" {
config.SIPServer = os.Getenv("SIP_SERVER")
}
if os.Getenv("SIP_USER") != "" {
config.SIPUser = os.Getenv("SIP_USER")
}
if os.Getenv("SIP_PASS") != "" {
config.SIPPass = os.Getenv("SIP_PASS")
}
// TODO move somewhere else
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
}
func main() {
flag.DurationVar(&config.RFIDTimeout, "rfid-timeout", 15*time.Minute, "RFID-timeout in Koha UI")
flag.IntVar(&config.SIPMaxConn, "sip-maxconn", 5, "Max size of SIP connection pool")
flag.BoolVar(&config.WSProxy, "ws-proxy", true, "WS goes through proxy, find client IP in request header")
rfidEndpoint := flag.String("rfid-endpoint", "http://rfidscanner.deichman.no/hub/in", "RDID scanner endpoint")
flag.Parse()
if *rfidEndpoint != "" {
config.LogRFID = true
logToRFID = make(chan rfidMsg, 100)
const msg = `{"sender":%q,"branch":%q,"client_IP":%q,"barcode":%q,"sip_message_type":%q}`
go func() {
client := http.Client{
Timeout: time.Duration(1 * time.Second),
}
for m := range logToRFID {
var b bytes.Buffer
fmt.Fprintf(&b, msg, "rfidhub", m.branch, m.clientIP, m.barcode, m.sipMsgType)
resp, err := client.Post(*rfidEndpoint, "application/json", &b)
if err == nil {
resp.Body.Close()
}
}
}()
}
log.SetFlags(log.Ltime | log.Lmicroseconds)
hub = newHub(config)
defer hub.Close()
log.Fatal(http.ListenAndServe(":"+config.HTTPPort, nil))
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var xForwardedRepl = strings.NewReplacer("10.173.251.150", "", ",", "", " ", "")
// serveWs handles websocket requests from the peer.
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
var ip string
if hub.config.WSProxy {
ip = xForwardedRepl.Replace(r.Header.Get("X-Forwarded-For"))
} else {
ip, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("ERR cannot get remote IP address: %v", err)
return
}
}
client := &Client{
IP: ip,
hub: hub,
conn: conn,
fromKoha: make(chan Message),
fromRFID: make(chan RFIDResp),
quit: make(chan bool, 5),
rfid: newRFIDManager(),
items: make(map[string]Message),
failedAlarmOn: make(map[string]string),
failedAlarmOff: make(map[string]string),
}
hub.Connect(client)
rfid, ok := client.initRFID(hub.config.RFIDPort)
if !ok {
hub.Disconnect(client)
return
}
go client.readFromRFID(rfid)
go client.Run(hub.config)
client.readFromKoha()
}