コード例 #1
0
ファイル: rputn-srv.go プロジェクト: rputbl/rputn-srv
func queryHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	hash64val := r.URL.String()[3:]
	hbv, err := hashbase.Un64(hash64val)
	if err != nil {
		cmc <- CM{i: CQueryBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed base64 encoding\"}")
		log.Printf("%s BADQUERY-BASE64-DECODE %s ", hostname, r.URL.String()[3:])
	} else {
		if len(hbv) != 28 {
			cmc <- CM{i: CQueryBad, n: 1}
			fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) hash\"}")
			log.Printf("%s BADQUERY-SHA224-SIZE %s ", hostname, r.URL.String()[3:])
		} else {

			assert, err := mstore.Query(hash64val)

			if err == nil {
				cmc <- CM{i: CQueryGood, n: 1}
				fmt.Fprintf(w, "%s%s%s", "{\"ok\" : \"success\",\"asserted\" : \"", assert, "\"}")
				log.Printf("%s QUERY %s ", hostname, r.URL.String()[3:])
			} else {
				cmc <- CM{i: CQueryGood, n: 1}
				fmt.Fprintf(w, "%s", "{\"ok\" : \"no such hash\"}")
				log.Printf("%s QUERY %s ", hostname, r.URL.String()[3:])
			}
		}
	}

}
コード例 #2
0
ファイル: rputn-srv.go プロジェクト: rputbl/rputn-srv
func assertHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	if len(r.URL.String()) < 4 {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too small)\"}")
		log.Printf("%s BADASSERT-QUERY-FORMAT %s ", hostname, r.URL.String())
		return
	}

	qline := r.URL.String()[3:]

	qFields := strings.FieldsFunc(qline, isAmpersand)

	if len(qFields) < 4 {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too few fields)\"}")
		log.Printf("%s BADASSERT-QUERY-FORMAT %s ", hostname, r.URL.String())
		return
	}

	fh64val := qFields[0]
	fhbv, err := hashbase.Un64(fh64val)
	if err != nil {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed file hash\"}")
		log.Printf("%s BADASSERT-BASE64-DECODE %s ", hostname, qline)
		return
	}
	if len(fhbv) != 28 {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) file hash\"}")
		log.Printf("%s BADASSERT-SHA224-SIZE %s ", hostname, qline)
		return
	}

	ph64val := qFields[1]
	phbv, err := hashbase.Un64(ph64val)
	if err != nil {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed asserter hash\"}")
		log.Printf("%s BADASSERT-BASE64-DECODE %s ", hostname, qline)
		return
	}
	if len(phbv) != 28 {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) asserter hash\"}")
		log.Printf("%s BADASSERT-SHA224-SIZE %s ", hostname, qline)
		return
	}

	assertstr := qFields[2]
	if len(assertstr) < 1 {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed assert content\"}")
		log.Printf("%s BADASSERT-ASSERT-FORMAT %s ", hostname, qline)
		return
	}

	sigstr := qFields[3]
	if len(sigstr) < 1 {
		cmc <- CM{i: CAssertBad, n: 1}
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed signature content\"}")
		log.Printf("%s BADASSERT-SIGNATURE-FORMAT %s ", hostname, qline)
		return
	}
	cmc <- CM{i: CAssertGood, n: 1}

	err = mstore.Assert(fh64val, ph64val, assertstr)
	if err != nil {
		fmt.Fprintf(w, "{\"error\" : \"%s\"}", err.Error())
		log.Printf("%s %s %s ", hostname, err.Error(), qline)
		return
	}

	cmc <- CM{i: CAssertion, n: 1}
	fmt.Fprintf(w, "%s", "{\"ok\" : \"query understood\"}")

	log.Printf("%s ASSERT %s by %s is %s", hostname, fh64val, ph64val, assertstr)

}
コード例 #3
0
ファイル: rputn-srv.go プロジェクト: rputbl/rputn-srv
func introHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	//    log.Printf("DEBUG-YEEK")
	//    fmt.Fprintf(w, "%s", "{\"debug\" : \"YEEK\"}")

	if len(r.URL.String()) < 4 {
		//      cmc <- CM{ i: CAssertBad, n:1 }
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too small)\"}")
		log.Printf("%s BADINTRO-QUERY-FORMAT %s ", hostname, r.URL.String())
		return
	}

	qline := r.URL.String()[3:]

	qFields := strings.FieldsFunc(qline, isAmpersand)

	if len(qFields) < 2 {
		//      cmc <- CM{ i: CAssertBad, n:1 }
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed query (too few fields)\"}")
		log.Printf("%s BADINTRO-QUERY-FORMAT %s ", hostname, r.URL.String())
		return
	}

	ph64val := qFields[0]
	phbv, err := hashbase.Un64(ph64val)
	if err != nil {
		//      cmc <- CM{ i: CAssertBad, n:1 }
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed file hash\"}")
		log.Printf("%s BADINTRO-BASE64-DECODE %s ", hostname, qline)
		return
	}
	if len(phbv) != 28 {
		//      cmc <- CM{ i: CAssertBad, n:1 }
		fmt.Fprintf(w, "%s", "{\"error\" : \"this is not a SHA224 (28 byte) public key hash\"}")
		log.Printf("%s BADINTRO-SHA224-SIZE %s ", hostname, qline)
		return
	}

	rsa_pub, err := url.QueryUnescape(qFields[1])
	if err != nil {
		//      cmc <- CM{ i: CAssertBad, n:1 }
		fmt.Fprintf(w, "%s", "{\"error\" : \"malformed escaped pubkey\"}")
		log.Printf("%s BADINTRO-PUBKEY-UNESCAPE %s ", hostname, qline)
		return
	}

	rsa_pub_stored, err := mstore.Query(ph64val)

	if err == nil {

		lspi := strings.LastIndex(rsa_pub_stored, " ")
		if lspi < 1 {
			//            cmc <- CM{ i: CAssertBad, n:1 }
			fmt.Fprintf(w, "%s", "{\"error\" : \"malformed stored user record\"}")
			log.Printf("%s BADINTRO-STOREDKEY-DECODE %s ", hostname, qline)
			return
		}

		rsa_pub_name := strings.Trim(rsa_pub_stored[lspi+1:], " \n")

		fmt.Fprintf(w, "%s%s%s", "{\"ok\" : \"success\",\"remembered\" : \"", rsa_pub_name, "\"}")
		log.Printf("%s INTRO-ALREADY %s ", hostname, rsa_pub_name)
	} else {
		lspi := strings.LastIndex(rsa_pub, " ")
		if lspi < 1 {
			//            cmc <- CM{ i: CAssertBad, n:1 }
			fmt.Fprintf(w, "%s", "{\"error\" : \"malformed stored user record\"}")
			log.Printf("%s BADINTRO-STOREDKEY-DECODE %s ", hostname, qline)
			return
		}

		rsa_pub_name := strings.Trim(rsa_pub[lspi+1:], " \n")

		cmc <- CM{i: CIntroduction, n: 1}

		err = mstore.Assert(ph64val, "INTRODUCING", rsa_pub)
		if err != nil {
			fmt.Fprintf(w, "{\"error\" : \"%s\"}", err.Error())
			log.Printf("%s %s %s ", hostname, err.Error(), qline)
			return
		}

		fmt.Fprintf(w, "%s%s%s", "{\"ok\" : \"success\",\"introduced\" : \"", rsa_pub_name, "\"}")
		log.Printf("%s INTRO-NEW %s ", hostname, rsa_pub_name)
	}

}