Skip to content
Snippets Groups Projects
Verified Commit 2a0f7813 authored by Spotlight Deveaux's avatar Spotlight Deveaux :fox:
Browse files

owostatus: preemptively close TCP connections

Alongside https://golang.org/pkg/net/http/#Client.CloseIdleConnections being called on every request, this should close the bodies of previous validation files to keep used ports low.
parent 26ad8a3a
Branches 1-handle-when-the-host-is-out-of-ports master
No related tags found
No related merge requests found
package main
import "net/http"
// DomainStatus holds a written status corresponding to a domain.
// If you update these types, please additionally update the enum in PostgreSQL.
type DomainStatus string
......@@ -15,22 +13,4 @@ const (
ServiceError DomainStatus = "service_error"
)
var UserAgent = "OwoStatusClient (https://owo.codes/spotlightishere/status.owo.cloud, 1.0.0)"
func getAgentRequest(url string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", UserAgent)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
var UserAgent = "OwoStatusClient (https://owo.codes/spotlightishere/status.owo.cloud, 1.0.0)"
\ No newline at end of file
......@@ -2,7 +2,6 @@ package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
......@@ -75,21 +74,16 @@ func checkDomain(hostname string) (DomainStatus, error) {
log.Println("Accessing", url)
}
resp, err := getAgentRequest(url)
status, body, err := getAgentRequest(url)
if err != nil {
return Misconfigured, err
}
if resp.StatusCode != http.StatusOK {
return ServiceError, fmt.Errorf("service: server responded with status %d", resp.StatusCode)
if status != http.StatusOK {
return ServiceError, fmt.Errorf("service: server responded with status %d", status)
}
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ServiceError, fmt.Errorf("service: connection closed while reading file contents")
}
if string(response) != "1" {
if string(body) != "1" {
return ServiceError, fmt.Errorf("service: contents of file read are not what they should be")
}
......
package main
import (
"io/ioutil"
"fmt"
"log"
"net/http"
"strings"
)
......@@ -88,14 +89,13 @@ func updateDomainList() {
// populateRemoteDomainList parses the simple format used for domains remotely.
func populateRemoteDomainList() ([]string, error) {
resp, err := getAgentRequest(config.General.DomainListURL)
status, body, err := getAgentRequest(config.General.DomainListURL)
if err != nil {
return []string{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []string{}, err
if status != http.StatusOK {
return []string{}, fmt.Errorf("service: domain list request responded with status %d", status)
}
var list []string
......
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func getAgentRequest(url string) (int, []byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, nil, err
}
req.Header.Set("User-Agent", UserAgent)
resp, err := client.Do(req)
if err != nil {
return 0, nil, err
}
statusCode := resp.StatusCode
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, nil, fmt.Errorf("service: connection closed while reading file contents")
}
// Preemptively close idle connections to prevent ports from being clogged.
client.CloseIdleConnections()
return statusCode, body, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment