-
Notifications
You must be signed in to change notification settings - Fork 2
/
hl7proxy.go
318 lines (264 loc) · 7.71 KB
/
hl7proxy.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"regexp"
"strings"
"time"
)
const (
START_BLOCK_CHAR = '\x0b'
END_BLOCK_CHAR = '\x1c'
CR_CHAR = '\x0d'
)
var httpClient = &http.Client{
Timeout: time.Second * 10,
}
func HL7TS(t time.Time) string {
return fmt.Sprintf(
"%d%d%d%d%d%d",
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
t.Minute(),
t.Second())
}
func makeAckAndIdentity(msg string) (string, string, error) {
if !strings.HasPrefix(msg, "MSH") {
return "", "", fmt.Errorf("Ivalid message, message should start with 'MSH'")
}
mshrbound := strings.IndexByte(msg, '\n')
if mshrbound == -1 {
mshrbound = strings.IndexByte(msg, '\r')
}
if mshrbound == -1 {
return "", "", fmt.Errorf("Ivalid message, can't detect end of MSH segment, no '\n' or '\r' byte found")
}
msh := msg[0:mshrbound]
mshSegments := strings.Split(msh, "|")
if len(mshSegments) < 12 {
return "", "", fmt.Errorf("Ivalid message, 'MSH' segment doesn't has enough fields")
}
ackMsh := []string{
"MSH",
"^~\\&",
mshSegments[4],
mshSegments[5],
mshSegments[2],
mshSegments[3],
HL7TS(time.Now()),
"",
"ACK",
"",
mshSegments[10],
mshSegments[11],
}
ackMsa := []string{
"MSA",
"AA",
mshSegments[9],
"",
"",
"",
"",
}
identity := fmt.Sprintf("type: %s, ctlid: %s, from: %s, to: %s", mshSegments[8], mshSegments[9], mshSegments[3], mshSegments[5])
return strings.Join(ackMsh, "|") + "\r" + strings.Join(ackMsa, "|") + "\r", identity, nil
}
func ScanHL7Msgs(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, END_BLOCK_CHAR); i >= 0 && len(data) >= (i + 2) {
// We have a full END_BLOCK-terminated msg.
if data[0] != START_BLOCK_CHAR {
return 0, nil, fmt.Errorf("HL7 content should be preffixed with 'Start Block character (1 byte). ASCII <VT>, i.e., <0x0B>'.")
} else if data[i + 1] != CR_CHAR {
return 0, nil, fmt.Errorf("HL7 content should be suffixed with 'End Block character (1 byte). ASCII <FS>, i.e., <0x1C>' and 'Carriage Return (1 byte). ASCII <CR> character, i.e., <0x0D>'.")
}
return i + 2, data[1:i], nil
}
if atEOF {
return 0, nil, fmt.Errorf("Unexpected EOF, HL7 content should be enclosed by special characters to form a Block. The Block format is as follows: <SB>dddd<EB><CR>")
}
// Request more data.
return 0, nil, nil
}
func ConnectionHandler(conn net.Conn, opts options) {
defer conn.Close()
defer log.Printf("INFO: Client %s disconnected", conn.RemoteAddr())
var scanner = bufio.NewScanner(conn)
buf := make([]byte, opts.maxmsgsize * 1024 / 5)
scanner.Buffer(buf,opts.maxmsgsize * 1024)
scanner.Split(ScanHL7Msgs)
for scanner.Scan() {
hl7msg := scanner.Text()
log.Printf("INFO: New message received. Length: %d bytes", len(hl7msg))
var ackmsg, identity, err = makeAckAndIdentity(hl7msg)
if err != nil {
log.Printf("ERROR: Message processing fail: %s", err)
m := 50
if m >= len(hl7msg) {
m = len(hl7msg)
}
log.Printf("INFO: Message: %s...", hl7msg[0:m])
return
}
log.Printf("INFO: Message identity: %s", identity)
// Remove null characters from a string if present
hl7msgCln := strings.ReplaceAll(hl7msg, "\u0000", "")
if hl7msg != hl7msgCln {
log.Printf("WARN: Null character detected and replaced in message")
}
err = deliverMessageToAidbox(hl7msgCln, opts)
if err != nil {
log.Printf("Error: Unable to deliver message to aidbox: %s", err)
return
}
err = ack(conn, ackmsg)
if err != nil {
log.Printf("Error: Unable to send ACK: %s", err)
return
}
}
if err := scanner.Err(); err != nil {
if errors.Is(err, bufio.ErrTooLong) {
log.Printf("ERROR: The size of the received message exceeds the maximum limit of %d kB. Please ensure the message size is within the permissible limit to avoid this error", opts.maxmsgsize * 1024)
} else {
log.Printf("ERROR: Invalid data received: %s", err)
}
}
}
var headerSplitRegexp = regexp.MustCompile(`:\s*`)
type message struct {
Name string
Age int
Active bool
lastLoginAt string
}
type Hl7v2Message struct {
ResourceType string `json:"resourceType"`
Status string `json:"status"`
Src string `json:"src"`
Config Hl7v2Config `json:"config"`
}
type Hl7v2Config struct {
ResourceType string `json:"resourceType"`
ID string `json:"id"`
}
func deliverMessageToAidbox(hl7msg string, opts options) error {
msg := &Hl7v2Message{
ResourceType: "Hl7v2Message",
Status: "received",
Src: hl7msg,
Config: Hl7v2Config{
ResourceType: "Hl7v2Config",
ID: opts.configId,
},
}
body, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("unable to serialize request body: %s", err)
}
req, err := http.NewRequest("POST", opts.aidboxUrl+"/Hl7v2Message", bytes.NewReader(body))
if err != nil {
return fmt.Errorf("unable to create http request: %s\n", err)
}
req.Header.Add("Content-Type", "application/json")
for _, header := range opts.headers {
parts := headerSplitRegexp.Split(header, 2)
req.Header.Add(parts[0], parts[1])
}
response, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("error during HTTP request: %s\n", err)
}
if response.StatusCode < 200 || response.StatusCode > 299 {
var body []byte
var scoderr = fmt.Sprintf("response code: %d", response.StatusCode)
if response.Body != nil {
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("%s, cannot read response body: %s", scoderr, err)
}
}
return fmt.Errorf("%s, aidbox response: \"%s\"", scoderr, body)
} else {
duration := response.Header.Get("x-duration")
log.Printf("INFO: Message delivered to Aidbox: %s (%sms)", response.Status, duration)
}
return nil
}
func ack(conn net.Conn, ack string) error {
log.Printf("INFO: Sending ACK")
_, err := fmt.Fprint(conn, string(START_BLOCK_CHAR), ack, string(END_BLOCK_CHAR), string(CR_CHAR))
return err;
}
type flagsStringsArray []string
func (i *flagsStringsArray) String() string {
var s = "(todo: correct string representation) "
for _, f := range *i {
s = s + f
}
return s
}
func (i *flagsStringsArray) Set(value string) error {
*i = append(*i, value)
return nil
}
type options struct {
port int;
host string;
aidboxUrl string;
configId string;
headers flagsStringsArray;
maxmsgsize int;
}
func main() {
var opts options;
opts.headers = make(flagsStringsArray, 0);
flag.IntVar(&opts.port, "port", 5000, "HL7 port to listen")
flag.StringVar(&opts.aidboxUrl, "url", "", "Aidbox URL to send messages to (i.e. 'https://foo.aidbox.app/') (required)")
flag.StringVar(&opts.configId, "config", "", "An ID of existing Hl7v2Config resource (required)")
flag.StringVar(&opts.host, "host", "", "host to listen")
flag.IntVar(&opts.maxmsgsize, "max-message-size", 64, "Max message size in kB")
flag.Var(&opts.headers, "header", "Additional HTTP headers in format 'Header: value'")
flag.Parse()
listenStr := fmt.Sprintf("%s:%d", opts.host, opts.port)
if opts.configId == "" {
fmt.Printf("No required -config flag is provided. Usage:\n")
flag.PrintDefaults()
os.Exit(1)
}
if opts.aidboxUrl == "" {
fmt.Printf("No required -url flag is provided. Usage:\n")
flag.PrintDefaults()
os.Exit(1)
}
psock, err := net.Listen("tcp", listenStr)
log.Printf("INFO: Listening to %s", listenStr)
if err != nil {
log.Printf("ERROR: Unable to open port: %s", err)
return
}
for {
conn, err := psock.Accept()
log.Printf("INFO: New connection from %s", conn.RemoteAddr())
if err != nil {
log.Printf("Unable to accept connection: %s", err)
return
}
go ConnectionHandler(conn, opts)
}
}