diff 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
line wrap: on
line diff
--- a/go/server.go	Sat Jun 08 15:50:59 2019 +0900
+++ b/go/server.go	Mon Jul 15 07:03:05 2019 +0900
@@ -1,7 +1,7 @@
 /*
  server.go  : server-program.
- Version    : 1.5
- Last Change: 2019-05-29 水 08:58:19.
+ Version    : 1.6
+ Last Change: 2019-07-15 Mon 06:34:42.
 
  install to: server_root/
 
@@ -14,9 +14,13 @@
 
 import(
 	"bufio"
+	"crypto/aes"
+	"crypto/cipher"
+    "encoding/hex"
 	"flag"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"log"
 	"net"
     "net/http"
@@ -26,22 +30,49 @@
 	"time"
 )
 
+type hhs struct {
+    no string
+	name string
+    kana string
+	addr string
+	birth string
+	sex string
+}
+
+func (h *hhs) String() string {
+    s := []string{h.no, h.name, h.kana, h.addr, h.birth, h.sex}
+    return strings.Join(s, ",")
+}
+
+func (h *hhs) SString() string {
+    s := []string{h.no, h.name, h.kana}
+    return strings.Join(s, ",")
+}
+
 var (
 	version string
 	server string
-	port   string
+	port string
 	server_root string
 	logfile string
 	not_ac bool
 	wlfile string
+
+	hdbfile string
+	key string
+    hhash map[string]hhs
 )
 
 func init() {
-	version = "1.5"	// piece-image version
+	version = "1.6"	// 1.6: hhs info version
 	port = ":3910"
     server_root = filepath.Dir(os.Args[0])
 	logfile = filepath.Join(server_root, "rsearcher.log")
-	wlfile = "rsearcher.whitelist"
+	wlfile = filepath.Join(server_root, "rsearcher.whitelist")
+
+	hdbfile = filepath.Join(server_root, "db", "hhs.db")
+	key = "1234567890abcdef1234567890abcdef" // len = 32
+    read_hhsdb()
 }
 
 func main() {
@@ -67,12 +98,16 @@
 
 	// start Web-server
 	fmt.Println("server start [", server, "] ( program version", version, ")")
-	http.HandleFunc("/",        handler       )
+	http.HandleFunc("/",        handler)
 	http.HandleFunc("/upload/", upload_handler)
-	http.HandleFunc("/mngdb/",  mngdb_handler )
+	http.HandleFunc("/mngdb/",  mngdb_handler)
+	http.HandleFunc("/hinfo/",  hinfo_handler)
+	http.HandleFunc("/hlist/",  hlist_handler)
 	log.Fatal(http.ListenAndServe(server, nil))
 }
 
+/* 各種ハンドラ */
+// 静的ファイル
 func handler(w http.ResponseWriter, r *http.Request) {
 	if !not_ac && !is_valid_host(r.RemoteAddr) {
 		http.NotFound(w, r)
@@ -100,6 +135,7 @@
 	io.Copy(w, f)
 }
 
+// アップローダ
 func upload_handler(w http.ResponseWriter, r *http.Request) {
 	if !not_ac && !is_valid_host(r.RemoteAddr) {
 		http.NotFound(w, r)
@@ -123,6 +159,7 @@
 	w.Write([]byte(fmt.Sprintf("%d bytes are recieved.\n", n)))
 }
 
+// データベース保存
 func mngdb_handler(w http.ResponseWriter, r *http.Request) {
 	if !not_ac && !is_valid_host(r.RemoteAddr) {
 		http.NotFound(w, r)
@@ -131,6 +168,7 @@
 
 	fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/mngdb")
 	write_log("[access] " + r.RemoteAddr + "manage-db")
+
 	db := r.URL.Path[len("/mngdb/"):]
 
 	file := filepath.Join(server_root, "db", db)
@@ -148,6 +186,40 @@
 	f.Close()
 }
 
+// 被保険者情報取得
+func hinfo_handler(w http.ResponseWriter, r *http.Request) {
+	if !not_ac && !is_valid_host(r.RemoteAddr) {
+		http.NotFound(w, r)
+		return
+	}
+	fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/hinfo")
+	write_log("[access] " + r.RemoteAddr + "hinfo")
+
+	h := r.URL.Path[len("/hinfo/"):]
+    hhs := hhash[h]
+
+	w.Write([]byte(hhs.String()))
+}
+
+func hlist_handler(w http.ResponseWriter, r *http.Request) {
+	if !not_ac && !is_valid_host(r.RemoteAddr) {
+		http.NotFound(w, r)
+		return
+	}
+	fmt.Println("[access]", r.RemoteAddr, "|", time.Now().Format("2006-01-02 15:04" ), "|", "/hlist")
+	write_log("[access] " + r.RemoteAddr + "list")
+
+	hlist := r.URL.Path[len("/hlist/"):]
+    var s string
+	for _, h := range strings.Split(hlist, ":") {
+        hhs := hhash[h]
+        s += hhs.SString() + "\n"
+    }
+
+	w.Write([]byte(s))
+}
+
+// ホワイトリスト判定
 func is_valid_host(host string) bool {
 	f, _ := os.Open(wlfile)
 	defer f.Close()
@@ -160,6 +232,39 @@
 	return false
 }
 
+// 被保険者DB読み込み
+func read_hhsdb() {
+    hhash = make(map[string]hhs)
+	pt := decrypto(key, hdbfile)
+	for _, line := range strings.Split(pt, "\n") {
+        c := strings.Split(line, ",")
+        if len(c) == 6 {
+            hhash[c[0]] = hhs{no: c[0], name: c[1], kana: c[2], addr: c[3], birth: c[4], sex: c[5]}
+        }
+    }
+}
+
+// 復号化
+func decrypto( key, file string ) string {
+	k, _ := hex.DecodeString(key)
+	block, err := aes.NewCipher(k)
+	if err != nil {
+		panic( err )
+	}
+	ciphertext, err := ioutil.ReadFile(file)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	iv := ciphertext[ :aes.BlockSize ]
+	plaintext := make([]byte, len(ciphertext[ aes.BlockSize: ]))
+	stream := cipher.NewCTR(block, iv)
+	stream.XORKeyStream(plaintext, ciphertext[ aes.BlockSize: ])
+
+	return string(plaintext)
+}
+
+// ログ
 func write_log(msg string) {
 	f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
 	if err != nil {