This commit is contained in:
Martin Polden 2016-04-27 17:07:53 +02:00
parent c8db4935ca
commit 2c5f4e08b5
2 changed files with 27 additions and 17 deletions

View File

@ -35,7 +35,7 @@ type Response struct {
Hostname string `json:"hostname,omitempty"` Hostname string `json:"hostname,omitempty"`
} }
type TestPortResponse struct { type PortResponse struct {
IP net.IP `json:"ip"` IP net.IP `json:"ip"`
Port uint64 `json:"port"` Port uint64 `json:"port"`
Reachable bool `json:"reachable"` Reachable bool `json:"reachable"`
@ -86,6 +86,27 @@ func (a *API) newResponse(r *http.Request) (Response, error) {
}, nil }, nil
} }
func (a *API) newPortResponse(r *http.Request) (PortResponse, error) {
vars := mux.Vars(r)
port, err := strconv.ParseUint(vars["port"], 10, 16)
if err != nil {
return PortResponse{Port: port}, err
}
if port < 1 || port > 65355 {
return PortResponse{Port: port}, fmt.Errorf("invalid port: %d", port)
}
ip, err := ipFromRequest(a.IPHeader, r)
if err != nil {
return PortResponse{Port: port}, err
}
err = a.oracle.LookupPort(ip, port)
return PortResponse{
IP: ip,
Port: port,
Reachable: err == nil,
}, nil
}
func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError { func (a *API) CLIHandler(w http.ResponseWriter, r *http.Request) *appError {
ip, err := ipFromRequest(a.IPHeader, r) ip, err := ipFromRequest(a.IPHeader, r)
if err != nil { if err != nil {
@ -128,23 +149,9 @@ func (a *API) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
} }
func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError { func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
vars := mux.Vars(r) response, err := a.newPortResponse(r)
port, err := strconv.ParseUint(vars["port"], 10, 16)
if err != nil { if err != nil {
return badRequest(err).WithMessage("Invalid port: " + vars["port"]).AsJSON() return badRequest(err).WithMessage(fmt.Sprintf("Invalid port: %d", response.Port)).AsJSON()
}
if port < 1 || port > 65355 {
return badRequest(nil).WithMessage("Invalid port: " + vars["port"]).AsJSON()
}
ip, err := ipFromRequest(a.IPHeader, r)
if err != nil {
return internalServerError(err).AsJSON()
}
err = a.oracle.LookupPort(ip, port)
response := TestPortResponse{
IP: ip,
Port: port,
Reachable: err == nil,
} }
b, err := json.Marshal(response) b, err := json.Marshal(response)
if err != nil { if err != nil {

View File

@ -85,6 +85,9 @@ func TestJSONHandlers(t *testing.T) {
status int status int
}{ }{
{s.URL, `{"ip":"127.0.0.1","country":"Elbonia","city":"Bornyasherk","hostname":"localhost"}`, 200}, {s.URL, `{"ip":"127.0.0.1","country":"Elbonia","city":"Bornyasherk","hostname":"localhost"}`, 200},
{s.URL + "/port/foo", `{"error":"404 page not found"}`, 404},
{s.URL + "/port/0", `{"error":"Invalid port: 0"}`, 400},
{s.URL + "/port/65356", `{"error":"Invalid port: 65356"}`, 400},
{s.URL + "/port/31337", `{"ip":"127.0.0.1","port":31337,"reachable":true}`, 200}, {s.URL + "/port/31337", `{"ip":"127.0.0.1","port":31337,"reachable":true}`, 200},
{s.URL + "/foo", `{"error":"404 page not found"}`, 404}, {s.URL + "/foo", `{"error":"404 page not found"}`, 404},
} }