Ejemplo n.º 1
0
func randCryptoCtx() event.CryptoContext {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	tmp := uint64(r.Int63())
	var ctx event.CryptoContext
	ctx.EncryptIV = tmp
	ctx.DecryptIV = tmp
	ctx.Method = event.GetDefaultCryptoMethod()
	return ctx
}
Ejemplo n.º 2
0
func httpInvoke(w http.ResponseWriter, r *http.Request) {
	b := make([]byte, r.ContentLength)
	r.Body.Read(b)
	buf := bytes.NewBuffer(b)
	ctx := appengine.NewContext(r)

	var cryptoContext event.CryptoContext
	err, ev := event.DecryptEvent(buf, &cryptoContext)
	if nil != err {
		ctx.Errorf("Decode auth event failed:%v", err)
		return
	}
	//var iv uint64
	if auth, ok := ev.(*event.AuthEvent); ok {
		if !remote.ServerConf.VerifyUser(auth.User) {
			return
		}
		cryptoContext.DecryptIV = auth.IV
		cryptoContext.EncryptIV = auth.IV
		cryptoContext.Method = auth.EncryptMethod
	} else {
		ctx.Errorf("Expected auth event, but got %T", ev)
		return
	}

	err, ev = event.DecryptEvent(buf, &cryptoContext)
	if nil != err {
		ctx.Errorf("Decode http request event failed:%v", err)
		return
	}
	if req, ok := ev.(*event.HTTPRequestEvent); ok {
		res := fetch(ctx, req)
		var resbuf bytes.Buffer
		event.EncryptEvent(&resbuf, res, &cryptoContext)
		headers := w.Header()
		headers.Add("Content-Type", "application/octet-stream")
		headers.Add("Content-Length", strconv.Itoa(resbuf.Len()))
		w.WriteHeader(http.StatusOK)
		w.Write(resbuf.Bytes())
	} else {
		ctx.Errorf("Expected http request event, but got %T", ev)
		return
	}
}