comparison go/server.go @ 16:b651aa41b9d4 default tip

hhsinfo method (server)
author pyon@macmini
date Mon, 15 Jul 2019 07:03:05 +0900
parents c262e17de9b1
children
comparison
equal deleted inserted replaced
15:c262e17de9b1 16:b651aa41b9d4
1 /* 1 /*
2 server.go : server-program. 2 server.go : server-program.
3 Version : 1.5 3 Version : 1.6
4 Last Change: 2019-05-29 水 08:58:19. 4 Last Change: 2019-07-15 Mon 06:34:42.
5 5
6 install to: server_root/ 6 install to: server_root/
7 7
8 server_root/server 8 server_root/server
9 + db/ 9 + db/
12 */ 12 */
13 package main 13 package main
14 14
15 import( 15 import(
16 "bufio" 16 "bufio"
17 "crypto/aes"
18 "crypto/cipher"
19 "encoding/hex"
17 "flag" 20 "flag"
18 "fmt" 21 "fmt"
19 "io" 22 "io"
23 "io/ioutil"
20 "log" 24 "log"
21 "net" 25 "net"
22 "net/http" 26 "net/http"
23 "os" 27 "os"
24 "path/filepath" 28 "path/filepath"
25 "strings" 29 "strings"
26 "time" 30 "time"
27 ) 31 )
28 32
33 type hhs struct {
34 no string
35 name string
36 kana string
37 addr string
38 birth string
39 sex string
40 }
41
42 func (h *hhs) String() string {
43 s := []string{h.no, h.name, h.kana, h.addr, h.birth, h.sex}
44 return strings.Join(s, ",")
45 }
46
47 func (h *hhs) SString() string {
48 s := []string{h.no, h.name, h.kana}
49 return strings.Join(s, ",")
50 }
51
29 var ( 52 var (
30 version string 53 version string
31 server string 54 server string
32 port string 55 port string
33 server_root string 56 server_root string
34 logfile string 57 logfile string
35 not_ac bool 58 not_ac bool
36 wlfile string 59 wlfile string
60
61 hdbfile string
62 key string
63 hhash map[string]hhs
37 ) 64 )
38 65
39 func init() { 66 func init() {
40 version = "1.5" // piece-image version 67 version = "1.6" // 1.6: hhs info version
41 port = ":3910" 68 port = ":3910"
42 server_root = filepath.Dir(os.Args[0]) 69 server_root = filepath.Dir(os.Args[0])
43 logfile = filepath.Join(server_root, "rsearcher.log") 70 logfile = filepath.Join(server_root, "rsearcher.log")
44 wlfile = "rsearcher.whitelist" 71 wlfile = filepath.Join(server_root, "rsearcher.whitelist")
72
73 hdbfile = filepath.Join(server_root, "db", "hhs.db")
74 key = "1234567890abcdef1234567890abcdef" // len = 32
75 read_hhsdb()
45 } 76 }
46 77
47 func main() { 78 func main() {
48 79
49 flag.BoolVar(&not_ac, "d", false, "no access control ( for debug )") 80 flag.BoolVar(&not_ac, "d", false, "no access control ( for debug )")
65 } 96 }
66 } 97 }
67 98
68 // start Web-server 99 // start Web-server
69 fmt.Println("server start [", server, "] ( program version", version, ")") 100 fmt.Println("server start [", server, "] ( program version", version, ")")
70 http.HandleFunc("/", handler ) 101 http.HandleFunc("/", handler)
71 http.HandleFunc("/upload/", upload_handler) 102 http.HandleFunc("/upload/", upload_handler)
72 http.HandleFunc("/mngdb/", mngdb_handler ) 103 http.HandleFunc("/mngdb/", mngdb_handler)
104 http.HandleFunc("/hinfo/", hinfo_handler)
105 http.HandleFunc("/hlist/", hlist_handler)
73 log.Fatal(http.ListenAndServe(server, nil)) 106 log.Fatal(http.ListenAndServe(server, nil))
74 } 107 }
75 108
109 /* 各種ハンドラ */
110 // 静的ファイル
76 func handler(w http.ResponseWriter, r *http.Request) { 111 func handler(w http.ResponseWriter, r *http.Request) {
77 if !not_ac && !is_valid_host(r.RemoteAddr) { 112 if !not_ac && !is_valid_host(r.RemoteAddr) {
78 http.NotFound(w, r) 113 http.NotFound(w, r)
79 return 114 return
80 } 115 }
98 w.Header().Set("Content-Type", "rsearcher/octet-stream") 133 w.Header().Set("Content-Type", "rsearcher/octet-stream")
99 w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size())) 134 w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size()))
100 io.Copy(w, f) 135 io.Copy(w, f)
101 } 136 }
102 137
138 // アップローダ
103 func upload_handler(w http.ResponseWriter, r *http.Request) { 139 func upload_handler(w http.ResponseWriter, r *http.Request) {
104 if !not_ac && !is_valid_host(r.RemoteAddr) { 140 if !not_ac && !is_valid_host(r.RemoteAddr) {
105 http.NotFound(w, r) 141 http.NotFound(w, r)
106 return 142 return
107 } 143 }
121 } 157 }
122 f.Close() 158 f.Close()
123 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n))) 159 w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
124 } 160 }
125 161
162 // データベース保存
126 func mngdb_handler(w http.ResponseWriter, r *http.Request) { 163 func mngdb_handler(w http.ResponseWriter, r *http.Request) {
127 if !not_ac && !is_valid_host(r.RemoteAddr) { 164 if !not_ac && !is_valid_host(r.RemoteAddr) {
128 http.NotFound(w, r) 165 http.NotFound(w, r)
129 return 166 return
130 } 167 }
131 168
132 fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/mngdb") 169 fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/mngdb")
133 write_log("[access] " + r.RemoteAddr + "manage-db") 170 write_log("[access] " + r.RemoteAddr + "manage-db")
171
134 db := r.URL.Path[len("/mngdb/"):] 172 db := r.URL.Path[len("/mngdb/"):]
135 173
136 file := filepath.Join(server_root, "db", db) 174 file := filepath.Join(server_root, "db", db)
137 175
138 f, err := os.Create(file) 176 f, err := os.Create(file)
146 return 184 return
147 } 185 }
148 f.Close() 186 f.Close()
149 } 187 }
150 188
189 // 被保険者情報取得
190 func hinfo_handler(w http.ResponseWriter, r *http.Request) {
191 if !not_ac && !is_valid_host(r.RemoteAddr) {
192 http.NotFound(w, r)
193 return
194 }
195 fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/hinfo")
196 write_log("[access] " + r.RemoteAddr + "hinfo")
197
198 h := r.URL.Path[len("/hinfo/"):]
199 hhs := hhash[h]
200
201 w.Write([]byte(hhs.String()))
202 }
203
204 func hlist_handler(w http.ResponseWriter, r *http.Request) {
205 if !not_ac && !is_valid_host(r.RemoteAddr) {
206 http.NotFound(w, r)
207 return
208 }
209 fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/hlist")
210 write_log("[access] " + r.RemoteAddr + "list")
211
212 hlist := r.URL.Path[len("/hlist/"):]
213 var s string
214 for _, h := range strings.Split(hlist, ":") {
215 hhs := hhash[h]
216 s += hhs.SString() + "\n"
217 }
218
219 w.Write([]byte(s))
220 }
221
222 // ホワイトリスト判定
151 func is_valid_host(host string) bool { 223 func is_valid_host(host string) bool {
152 f, _ := os.Open(wlfile) 224 f, _ := os.Open(wlfile)
153 defer f.Close() 225 defer f.Close()
154 input := bufio.NewScanner(f) 226 input := bufio.NewScanner(f)
155 for input.Scan() { 227 for input.Scan() {
158 } 230 }
159 } 231 }
160 return false 232 return false
161 } 233 }
162 234
235 // 被保険者DB読み込み
236 func read_hhsdb() {
237 hhash = make(map[string]hhs)
238 pt := decrypto(key, hdbfile)
239 for _, line := range strings.Split(pt, "\n") {
240 c := strings.Split(line, ",")
241 if len(c) == 6 {
242 hhash[c[0]] = hhs{no: c[0], name: c[1], kana: c[2], addr: c[3], birth: c[4], sex: c[5]}
243 }
244 }
245 }
246
247 // 復号化
248 func decrypto( key, file string ) string {
249 k, _ := hex.DecodeString(key)
250 block, err := aes.NewCipher(k)
251 if err != nil {
252 panic( err )
253 }
254 ciphertext, err := ioutil.ReadFile(file)
255 if err != nil {
256 log.Fatal(err)
257 }
258
259 iv := ciphertext[ :aes.BlockSize ]
260 plaintext := make([]byte, len(ciphertext[ aes.BlockSize: ]))
261 stream := cipher.NewCTR(block, iv)
262 stream.XORKeyStream(plaintext, ciphertext[ aes.BlockSize: ])
263
264 return string(plaintext)
265 }
266
267 // ログ
163 func write_log(msg string) { 268 func write_log(msg string) {
164 f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 269 f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
165 if err != nil { 270 if err != nil {
166 log.Fatal(err) 271 log.Fatal(err)
167 } 272 }