mirror of
https://github.com/mpolden/echoip.git
synced 2024-11-14 01:12:47 +01:00
Close connection when testing port
This commit is contained in:
parent
55d72fd053
commit
4664693b0a
@ -98,9 +98,12 @@ func ipFromRequest(r *http.Request) (net.IP, error) {
|
|||||||
|
|
||||||
func testPort(ip net.IP, port uint64) error {
|
func testPort(ip net.IP, port uint64) error {
|
||||||
address := fmt.Sprintf("%s:%d", ip, port)
|
address := fmt.Sprintf("%s:%d", ip, port)
|
||||||
if _, err := net.DialTimeout("tcp", address, 2*time.Second); err != nil {
|
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
defer conn.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,7 +176,7 @@ func (a *API) JSONHandler(w http.ResponseWriter, r *http.Request) *appError {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) TestPortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
func (a *API) PortHandler(w http.ResponseWriter, r *http.Request) *appError {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
port, err := strconv.ParseUint(vars["port"], 10, 16)
|
port, err := strconv.ParseUint(vars["port"], 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -286,7 +289,7 @@ func (a *API) Handlers() http.Handler {
|
|||||||
r.Handle("/", appHandler(a.DefaultHandler)).Methods("GET")
|
r.Handle("/", appHandler(a.DefaultHandler)).Methods("GET")
|
||||||
|
|
||||||
// Port testing
|
// Port testing
|
||||||
r.Handle("/port/{port:[0-9]+}", appHandler(a.TestPortHandler)).Methods("GET")
|
r.Handle("/port/{port:[0-9]+}", appHandler(a.PortHandler)).Methods("GET")
|
||||||
|
|
||||||
// Not found handler which returns JSON when appropriate
|
// Not found handler which returns JSON when appropriate
|
||||||
r.NotFoundHandler = appHandler(a.NotFoundHandler)
|
r.NotFoundHandler = appHandler(a.NotFoundHandler)
|
||||||
|
Loading…
Reference in New Issue
Block a user