Exemplo n.º 1
0
func contactIQHandler(w http.ResponseWriter, r *http.Request, user userView) {
	id := r.URL.Query().Get("id")
	if id == "" {
		http.Error(w, "'id' param missing", http.StatusBadRequest)
		return
	}
	http.Redirect(w, r, scheduler.GetStreamURL(id), http.StatusFound)
}
Exemplo n.º 2
0
func getLatestIQDataHandler(
	sdb *db.StationDB, cdb *db.ContactDB,
	w http.ResponseWriter, r *http.Request) {

	log.Printf("Request: %s", r.URL.String())

	req, err := parseGetLatestIQDataRequest(r.URL.Query())
	if err != nil {
		log.Printf("getLatestIQDataHandler: "+
			"parse request error: %s", err.Error())
		http.Error(w, "Error parsing request.",
			http.StatusBadRequest)
		return
	}

	sat := db.GlobalSatelliteDB().Map[req.SatelliteId]
	if sat == nil {
		http.Error(w, "Unknown satellite_id.", http.StatusBadRequest)
		return
	}

	station, err := sdb.Lookup(req.StationId)
	if err != nil {
		log.Printf("Error looking up station: %s", err.Error())
		http.Error(w, "", http.StatusUnauthorized)
		return
	}
	if station == nil {
		log.Printf("Error looking up station.")
		http.Error(w, "", http.StatusUnauthorized)
		return
	}
	// Authenticate the station.
	if station.Secret == nil || *station.Secret != req.StationSecret {
		log.Printf("Authentication failed.")
		http.Error(w, "", http.StatusUnauthorized)
		return
	}

	// Make sure that the user is authorized for the satellite.
	found_good_id := false
	for _, station_id := range sat.AuthorizedStationId {
		if station_id == *station.Id {
			found_good_id = true
		}
	}
	if !found_good_id {
		log.Printf("Authentication failed.")
		http.Error(w, "", http.StatusUnauthorized)
		return
	}

	contacts, err := cdb.SearchBySatelliteId(req.SatelliteId, req.Limit)
	if err != nil {
		log.Printf("getLatestIQDataHandler: "+
			"SearchBySatelliteId error: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	packets := make(GetLatestIQDataResponse, 0)

	for _, c := range contacts {
		if c.StartTimestamp == nil {
			continue
		}
		timestamp := *c.StartTimestamp
		for _, b := range c.Blob {
			if len(packets) >= req.Limit {
				continue
			}

			if b.Format == nil ||
				*b.Format != pb.Contact_Blob_IQ {
				continue
			}

			var p IQLink
			p.Timestamp = timestamp
			p.URL = scheduler.GetStreamURL(*c.Id)

			if b.IqParams != nil {
				if b.IqParams.Type != nil {
					p.Type = b.IqParams.Type.String()
				}
				if b.IqParams.SampleRate != nil {
					p.SampleRate = int(
						*b.IqParams.SampleRate)
				}
			}

			packets = append(packets, p)
		}
	}

	json_body, err := json.Marshal(packets)
	if err != nil {
		log.Printf("json Marshal error: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
	}

	w.Header().Add("Content-Length", fmt.Sprintf("%d", len(json_body)))
	w.Header().Add("Content-Type", "application/json")
	_, err = w.Write(json_body)
	if err != nil {
		log.Printf("Error writing response: %s", err.Error())
	}
}
Exemplo n.º 3
0
func consoleJSONHandler(sdb *db.StationDB, contactdb *db.ContactDB,
	m *rpc.Client,
	w http.ResponseWriter, r *http.Request, user userView) {
	query := r.URL.Query()

	id := query.Get("id")
	if id == "" {
		http.Error(w, "'id' param missing", http.StatusBadRequest)
		return
	}
	action := query.Get("action")
	if action == "" {
		http.Error(w, "'action' param missing", http.StatusBadRequest)
		return
	}

	auth, _ := canOperateStation(sdb, id, user.Id)
	if !auth {
		http.Error(w, "Not authorized", http.StatusUnauthorized)
		return
	}

	// FIXME: authentication and scheduling/locking

	satellite_id := query.Get("satellite_id")
	if satellite_id != "" &&
		db.GlobalSatelliteDB().Map[satellite_id] == nil {
		http.Error(
			w, "invalid satellite_id",
			http.StatusBadRequest)
		return
	}

	switch action {
	case "ReceiverGetState":
		callStation(m, w, id, action, nil)
	case "ReceiverStart":
		log.Printf("ReceiverStart: satellite_id=%s", satellite_id)
		contact_id, err := contacts.StartNewConsoleContact(
			sdb, contactdb, id, user.Id, satellite_id)
		if err != nil {
			log.Printf("StartNewIQContact error: %s", err.Error())
			http.Error(w, "", http.StatusInternalServerError)
			return
		}
		v := url.Values{}
		v.Add("stream_url", scheduler.GetStreamURL(contact_id))
		callStation(m, w, id, action, v)
	case "ReceiverStop":
		callStation(m, w, id, action, nil)
	case "ReceiverWaterfallPNG":
		callStation(m, w, id, action, nil)
	case "ReceiverSetFrequency":
		hz := query.Get("hz")
		if hz == "" {
			http.Error(
				w, "'hz' param missing", http.StatusBadRequest)
			return
		}
		v := url.Values{}
		v.Add("hz", hz)
		callStation(m, w, id, action, v)
	case "TNCStart":
		log.Printf("TNCStart: satellite_id=%s", satellite_id)

		host, port := scheduler.GetAPIServer()
		if satellite_id == "" {
			host = ""
			port = "0"
		}

		v := url.Values{}
		v.Add("api_host", host)
		v.Add("api_port", port)
		v.Add("satellite_id", satellite_id)
		callStation(m, w, id, action, v)
	case "TNCStop":
		callStation(m, w, id, action, nil)
	case "TNCGetLatestFrames":
		callStation(m, w, id, action, nil)
	case "MotorGetState":
		callStation(m, w, id, action, nil)
	case "MotorStart":
		program := query.Get("program")
		if program == "" {
			http.Error(w, "'program' param missing",
				http.StatusBadRequest)
			return
		}
		v := url.Values{}
		v.Add("program", program)
		callStation(m, w, id, action, v)
	case "MotorStop":
		callStation(m, w, id, action, nil)
	default:
		http.NotFound(w, r)
	}
}