2017-11-25 13:36:03 +01:00
package collector
import (
"bytes"
"encoding/base64"
"encoding/json"
"net"
"net/http"
"strconv"
"time"
2019-08-03 03:58:23 +02:00
"github.com/containous/traefik/v2/pkg/anonymize"
"github.com/containous/traefik/v2/pkg/config/static"
"github.com/containous/traefik/v2/pkg/log"
"github.com/containous/traefik/v2/pkg/version"
2017-11-25 13:36:03 +01:00
"github.com/mitchellh/hashstructure"
)
// collectorURL URL where the stats are send
2019-03-14 19:32:03 +01:00
const collectorURL = "https://collect.traefik.io/9vxmmkcdmalbdi635d4jgc5p5rx0h7h8"
2017-11-25 13:36:03 +01:00
// Collected data
type data struct {
Version string
Codename string
BuildDate string
Configuration string
Hash string
}
// Collect anonymous data.
2018-11-27 17:42:04 +01:00
func Collect ( staticConfiguration * static . Configuration ) error {
anonConfig , err := anonymize . Do ( staticConfiguration , false )
2017-11-25 13:36:03 +01:00
if err != nil {
return err
}
log . Infof ( "Anonymous stats sent to %s: %s" , collectorURL , anonConfig )
2018-11-27 17:42:04 +01:00
hashConf , err := hashstructure . Hash ( staticConfiguration , nil )
2017-11-25 13:36:03 +01:00
if err != nil {
return err
}
data := & data {
Version : version . Version ,
Codename : version . Codename ,
BuildDate : version . BuildDate ,
Hash : strconv . FormatUint ( hashConf , 10 ) ,
Configuration : base64 . StdEncoding . EncodeToString ( [ ] byte ( anonConfig ) ) ,
}
buf := new ( bytes . Buffer )
err = json . NewEncoder ( buf ) . Encode ( data )
if err != nil {
return err
}
2019-06-17 11:48:05 +02:00
resp , err := makeHTTPClient ( ) . Post ( collectorURL , "application/json; charset=utf-8" , buf )
if resp != nil {
resp . Body . Close ( )
}
2017-11-25 13:36:03 +01:00
return err
}
func makeHTTPClient ( ) * http . Client {
dialer := & net . Dialer {
2019-03-18 11:30:07 +01:00
Timeout : 30 * time . Second ,
2017-11-25 13:36:03 +01:00
KeepAlive : 30 * time . Second ,
DualStack : true ,
}
transport := & http . Transport {
Proxy : http . ProxyFromEnvironment ,
DialContext : dialer . DialContext ,
IdleConnTimeout : 90 * time . Second ,
TLSHandshakeTimeout : 10 * time . Second ,
ExpectContinueTimeout : 1 * time . Second ,
}
return & http . Client { Transport : transport }
}