-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecker.java
More file actions
190 lines (160 loc) · 5.67 KB
/
Checker.java
File metadata and controls
190 lines (160 loc) · 5.67 KB
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
package id.co.knt.smartbee.controller;
import id.co.knt.smartbee.dao.WorkbenchDao;
import id.co.knt.smartbee.entity.WorkBench;
import id.web.pos.integra.gawl.Gawl;
import id.web.pos.integra.gawl.Gawl.UnknownCharacterException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.inject.Inject;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping({"/l"})
public class Checker
{
public static String CHECK_SUCCESS = "0";
public static String CHECK_FAILED_REGISTRATION = "1";
public static String CHECK_FAILED_ACTIVATION = "2";
public static String REGISTER_SUCCESS = "0";
public static String REGISTER_DUPLICATE_MODULE = "1";
public static String REGISTER_INVALID_SERIAL = "2";
public static String ACTIVATION_SUCCESS = "0";
public static String ACTIVATION_FAILED = "1";
public static char SEPARATOR = ':';
private Gawl gawl;
private WorkbenchDao workbenchDao;
public Checker() {}
@Inject
public void setGawl(Gawl gawl) { this.gawl = gawl; }
@Inject
public void setWorkbenchDao(WorkbenchDao workbenchDao)
{
this.workbenchDao = workbenchDao;
}
@RequestMapping({"/hello"})
@ResponseBody
public String hello() {
return "hello";
}
@RequestMapping(value={"/c/{id}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
@ResponseBody
public String check(@PathVariable int id)
{
String result = CHECK_SUCCESS;
try {
WorkBench w = workbenchDao.findByModule(Integer.valueOf(id));
if (gawl.validate(w.getSerial())) {
Map<String, Byte> info = gawl.extract(w.getSerial());
Byte module = (Byte)info.get("module");
if ((!module.equals(Integer.valueOf(63))) && (Byte.toUnsignedInt(module.byteValue()) == id))
{
String passKey = gawl.pass(((Byte)info.get("seed1")).byteValue(), ((Byte)info.get("seed2")).byteValue());
if (w.getActivationKey() == null) {
result = CHECK_FAILED_ACTIVATION + SEPARATOR + passKey;
}
else if (!gawl.challenge(passKey, w.getActivationKey()))
{
result = CHECK_FAILED_ACTIVATION + SEPARATOR + passKey;
}
}
else
{
result = CHECK_FAILED_REGISTRATION;
System.out.println("unmatched serial number to module");
}
}
else {
result = CHECK_FAILED_REGISTRATION;
System.out.println("invalid serial number");
}
} catch (Throwable e) {
result = CHECK_FAILED_REGISTRATION;
}
return result;
}
@RequestMapping(value={"/r/{id}/{serial}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
@ResponseBody
public String register(@PathVariable int id, @PathVariable String serial)
{
String result = REGISTER_SUCCESS;
if (gawl.validate(serial)) {
try
{
Map<String, Byte> info = gawl.extract(serial);
Byte module = (Byte)info.get("module");
if ((!module.equals(Integer.valueOf(63))) && (Byte.toUnsignedInt(module.byteValue()) == id))
{
String passKey = gawl.pass(((Byte)info.get("seed1")).byteValue(), ((Byte)info.get("seed2")).byteValue());
result = REGISTER_SUCCESS + SEPARATOR + passKey;
WorkBench bench = new WorkBench();
bench.setModule(Integer.valueOf(id));
bench.setSerial(serial);
bench.setRegisteredDate(new Date());
workbenchDao.save(bench);
}
else {
result = CHECK_FAILED_REGISTRATION;
}
}
catch (Gawl.UnknownCharacterException e) {
result = CHECK_FAILED_REGISTRATION;
System.out.println("register failed due to invalid serial number");
}
} else {
result = CHECK_FAILED_REGISTRATION;
}
return result;
}
@RequestMapping(value={"/a/{id}/{activationKey}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
@ResponseBody
public String activate(@PathVariable int id, @PathVariable String activationKey)
{
String result = ACTIVATION_SUCCESS;
try {
System.out.println("id:" + id);
WorkBench b = workbenchDao.findByModule(Integer.valueOf(id));
Map<String, Byte> info = gawl.extract(b.getSerial());
String passKey = gawl.pass(((Byte)info.get("seed1")).byteValue(), ((Byte)info.get("seed2")).byteValue());
if (!gawl.challenge(passKey, activationKey)) {
result = ACTIVATION_FAILED;
System.out.println("invalid activationKey");
} else {
b.setActivationKey(activationKey);
workbenchDao.save(b);
}
} catch (Throwable t) {
result = ACTIVATION_FAILED;
t.printStackTrace();
}
return result;
}
@RequestMapping({"/list"})
@ResponseBody
public String list() {
StringBuilder result = new StringBuilder();
List<WorkBench> list = workbenchDao.list();
for (WorkBench b : list) {
result.append(b.getModule()).append(':').append(b.getSerial()).append(':').append(b.getActivationKey()).append("\r\n");
}
return result.toString();
}
@RequestMapping({"/bye/{name}"})
@ResponseBody
public String bye(@PathVariable String name) {
StringBuilder result = new StringBuilder();
if (name.equals("4321")) {
List<WorkBench> list = workbenchDao.list();
for (WorkBench b : list) {
workbenchDao.delete(b);
result.append("bye ").append(b.getId()).append("\r\n");
}
}
else {
result.append("who 're you ").append(name).append('?');
}
return result.toString();
}
}