-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (99 loc) · 3.19 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
/*
* Copyright 2014-2015 LDLN
*
* This file is part of LDLN Serial Server.
*
* LDLN Serial Server is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* LDLN Serial Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LDLN Serial Server. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"log"
"github.com/tarm/serial"
"labix.org/v2/mgo"
"encoding/json"
"github.com/ldln/core/cryptoWrapper"
)
func main() {
// connect to mongodb
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
// connect to port
c := &serial.Config{Name: "/dev/ttyS0", Baud: 38400}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
// write message
n, err := s.Write([]byte("LDLN Serial is listening"))
if err != nil {
log.Fatal(err)
}
// read messages
for {
buf := make([]byte, 2048)
n, err = s.Read(buf)
if err != nil {
log.Fatal(err)
}
log.Printf("Incoming on serial: %q", buf[:n])
// split by double pipes /
// first part is user:pass
// second part is object type
// third part is key/value pairs, separated by pipes |
// each key/value pair is seperated by :
// i.e. user:pass/memo/timestamp:xxx|gps:xxx|humidity:xxx|temp:xxx|soilsensor:xxx|dfo10sensor:xxx|gyro:xxx
// i.e. user:pass/object_type/timestamp:xxx|gps:xxx|humidity:xxx|temp:xxx|soilsensor:xxx|dfo10sensor:xxx|gyro:xxx
// convert string to JSON to map[string]interface{}
v := make(map[string]interface{})
err := json.Unmarshal(buf[:n], &v)
if err != nil {
log.Printf("Not a JSON object")
} else {
// get auth
username := v["username"].(string)
password := v["password"].(string)
// create object
object_map := make(map[string]interface{})
object_map["uuid"] = v["uuid"].(string)
object_map["object_type"] = v["object_type"].(string)
object_map["time_modified_since_creation"] = v["time_modified_since_creation"].(float64)
// encrypted payload
// object_map["key_value_pairs"] = v["key_value_pairs"].(string)
// plaintext to be encrypted payload
byt, err := json.Marshal(v["key_value_pairs_plaintext"].(map[string]interface{}))
if err != nil {
panic(err)
}
log.Printf(string(byt[:]))
dek := cryptoWrapper.GetKeyFromUsernamePassword(username, password)
ciphertext := cryptoWrapper.Encrypt(dek, byt)
if ciphertext != nil {
object_map["key_value_pairs"] = ciphertext
// db insert
mc := session.DB("landline").C("SyncableObjects")
err = mc.Insert(object_map)
if err != nil {
panic(err)
}
log.Printf("Inserted object %q into database.", v["uuid"].(string))
} else {
s.Write([]byte("Encryption failed"))
log.Printf("Encryption failed")
}
}
}
}