コード例 #1
0
ファイル: middleware.go プロジェクト: go-ndn/health
// InfluxDB writes interest processing time to influxDB.
//
// The data collected can be viewed with: SELECT "value" FROM :name WHERE "name" = ':interest_name'.
func InfluxDB(client influxdb.Client, db, name string, tags map[string]string) mux.Middleware {
	return func(next mux.Handler) mux.Handler {
		return mux.HandlerFunc(func(w ndn.Sender, i *ndn.Interest) {
			before := time.Now()
			next.ServeNDN(w, i)
			t := make(map[string]string)
			for k, v := range tags {
				t[k] = v
			}
			t["name"] = i.Name.String()
			pt, _ := influxdb.NewPoint(name, t, map[string]interface{}{
				"value": float64(time.Since(before)) / float64(time.Millisecond),
			}, time.Now())
			bp, _ := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
				Database: db,
			})
			bp.AddPoint(pt)

			err := client.Write(bp)
			if err != nil {
				log.Println(err)
				return
			}
		})
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: go-ndn/ndn-cert-gen
func main() {
	var (
		identity string
		keyType  string
		file     string
	)
	flag.StringVar(&identity, "identity", "/ndn/guest/alice", "identity")
	flag.StringVar(&keyType, "type", "rsa", "[ rsa | ecdsa | hmac ]")
	flag.StringVar(&file, "file", "default", "file name for private key and certificate")
	flag.Parse()

	var (
		name = ndn.NewName(fmt.Sprintf("%s/%d/KEY/%%00%%00", identity, time.Now().UnixNano()/1000000))
		key  ndn.Key
	)
	switch keyType {
	case "rsa":
		pri, err := rsa.GenerateKey(rand.Reader, 2048)
		if err != nil {
			log.Println(err)
			return
		}
		key = &ndn.RSAKey{
			Name:       name,
			PrivateKey: pri,
		}
	case "ecdsa":
		pri, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
		if err != nil {
			log.Println(err)
			return
		}
		key = &ndn.ECDSAKey{
			Name:       name,
			PrivateKey: pri,
		}
	case "hmac":
		// 256 / 8 = 32
		pri := make([]byte, 32)
		rand.Read(pri)
		key = &ndn.HMACKey{
			Name:       name,
			PrivateKey: pri,
		}
	default:
		log.Println("unsupported key type")
		return
	}
	// private key
	pem, err := os.Create(file + ".pri")
	if err != nil {
		log.Println(err)
		return
	}
	defer pem.Close()
	err = ndn.EncodePrivateKey(key, pem)
	if err != nil {
		log.Println(err)
		return
	}
	// certificate
	cert, err := os.Create(file + ".ndncert")
	if err != nil {
		log.Println(err)
		return
	}
	defer cert.Close()
	err = ndn.EncodeCertificate(key, cert)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println(name, "exported")
}