Example #1
0
func syncHandler(w http.ResponseWriter, r *http.Request) {
	workerUuid := r.URL.Query().Get("uuid")
	if worker, ok := workerMap[workerUuid]; ok {
		if r.Method == "POST" {
			r.ParseForm()
			if b64_parts, ok := r.Form["data"]; ok {
				b64 := b64_parts[0]
				decodeLen := base64.StdEncoding.DecodedLen(len(b64))
				data := make([]byte, decodeLen)
				n, err := base64.StdEncoding.Decode(data, []byte(b64))
				if err != nil {
					crowbar.WriteHTTPError(w, "Could not decode B64.")
				} else {
					worker.commandChannel <- workerCommand{command: command_data, extra: data[:n]}
					crowbar.WriteHTTPOK(w, "Sent.")
				}
			} else {
				crowbar.WriteHTTPError(w, "Data is required.")
			}
		} else {
			response := <-worker.responseChannel
			switch response.response {
			case response_data:
				crowbar.WriteHTTPData(w, response.extra_byte)
			case response_quit:
				crowbar.WriteHTTPQuit(w, response.extra_string)
			}
		}
	} else {
		crowbar.WriteHTTPError(w, "No such UUID")
	}
}
Example #2
0
func authHandler(w http.ResponseWriter, r *http.Request) {
	nonce := make([]byte, 16)
	_, err := rand.Read(nonce)
	if err != nil {
		crowbar.WriteHTTPError(w, "Internal error.")
		return
	}

	username := r.URL.Query().Get("username")
	_, ok := UserGet(username)

	if !ok {
		crowbar.WriteHTTPError(w, "No such user.")
		return
	}

	nonceMap[username] = nonce
	crowbar.WriteHTTPData(w, nonce)
}