"Fossies" - the Fresh Open Source Software Archive

Member "geoipupdate-4.3.0/pkg/geoipupdate/geoip_updater.go" (16 Apr 2020, 1622 Bytes) of package /linux/misc/geoipupdate-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package geoipupdate
    2 
    3 import (
    4     "bytes"
    5     "fmt"
    6     "io"
    7     "io/ioutil"
    8     "log"
    9     "net/http"
   10     "net/url"
   11 
   12     "github.com/pkg/errors"
   13 )
   14 
   15 // NewClient creates an *http.Client for use in updating.
   16 func NewClient(
   17     config *Config,
   18 ) *http.Client {
   19     var client *http.Client
   20     transport := http.DefaultTransport
   21     if config.Proxy != nil {
   22         proxy := http.ProxyURL(config.Proxy)
   23         transport.(*http.Transport).Proxy = proxy
   24     }
   25     client = &http.Client{Transport: transport}
   26     return client
   27 }
   28 
   29 // GetFilename looks up the filename for the given edition ID.
   30 func GetFilename(
   31     config *Config,
   32     editionID string,
   33     client *http.Client,
   34 ) (string, error) {
   35     maxMindURL := fmt.Sprintf(
   36         "%s/app/update_getfilename?product_id=%s",
   37         config.URL,
   38         url.QueryEscape(editionID),
   39     )
   40 
   41     if config.Verbose {
   42         log.Printf("Performing get filename request to %s", maxMindURL)
   43     }
   44     res, err := client.Get(maxMindURL)
   45     if err != nil {
   46         return "", errors.Wrap(err, "error performing HTTP request")
   47     }
   48     defer func() {
   49         if err := res.Body.Close(); err != nil {
   50             log.Fatalf("error closing response body: %+v", errors.Wrap(err, "closing body"))
   51         }
   52     }()
   53 
   54     buf, err := ioutil.ReadAll(io.LimitReader(res.Body, 256))
   55     if err != nil {
   56         return "", errors.Wrap(err, "error reading response body")
   57     }
   58 
   59     if res.StatusCode != http.StatusOK {
   60         return "", errors.Errorf("unexpected HTTP status code: %s: %s", res.Status, buf)
   61     }
   62 
   63     if len(buf) == 0 {
   64         return "", errors.New("response body is empty")
   65     }
   66 
   67     if bytes.Count(buf, []byte("\n")) > 0 ||
   68         bytes.Count(buf, []byte("\x00")) > 0 {
   69         return "", errors.New("invalid characters in filename")
   70     }
   71 
   72     return string(buf), nil
   73 }