Beispiel #1
0
func (d *Daemon) getClientConfig() (*clientConfig, error) {
	var (
		transport *http.Transport
		scheme    string
		addr      string
		proto     string
	)
	if d.UseDefaultTLSHost {
		option := &tlsconfig.Options{
			CAFile:   "fixtures/https/ca.pem",
			CertFile: "fixtures/https/client-cert.pem",
			KeyFile:  "fixtures/https/client-key.pem",
		}
		tlsConfig, err := tlsconfig.Client(*option)
		if err != nil {
			return nil, err
		}
		transport = &http.Transport{
			TLSClientConfig: tlsConfig,
		}
		addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
		scheme = "https"
		proto = "tcp"
	} else if d.UseDefaultHost {
		addr = opts.DefaultUnixSocket
		proto = "unix"
		scheme = "http"
		transport = &http.Transport{}
	} else {
		addr = d.sockPath()
		proto = "unix"
		scheme = "http"
		transport = &http.Transport{}
	}

	if err := sockets.ConfigureTransport(transport, proto, addr); err != nil {
		return nil, err
	}
	transport.DisableKeepAlives = true

	return &clientConfig{
		transport: transport,
		scheme:    scheme,
		addr:      addr,
	}, nil
}
Beispiel #2
0
func newHttpTransporter() (trans *httpTransporter) {
	tr := new(http.Transport)
	tr.DisableCompression = true
	tr.DisableKeepAlives = false
	tr.MaxIdleConnsPerHost = 4
	jar := cookieJar
	if DisableGlobalCookie {
		jar, _ = cookiejar.New(nil)
	}
	client := new(http.Client)
	client.Jar = jar
	client.Transport = tr
	trans = new(httpTransporter)
	trans.Client = client
	trans.Header = new(http.Header)
	return
}
Beispiel #3
0
// NewClient creates an http client for the specific host
func NewClient(host string) (*http.Client, error) {
	// FIXME(vdemeester) 10*time.Second timeout of SockRequest… ?
	proto, addr, _, err := dclient.ParseHost(host)
	if err != nil {
		return nil, err
	}
	transport := new(http.Transport)
	if proto == "tcp" && os.Getenv("DOCKER_TLS_VERIFY") != "" {
		// Setup the socket TLS configuration.
		tlsConfig, err := getTLSConfig()
		if err != nil {
			return nil, err
		}
		transport = &http.Transport{TLSClientConfig: tlsConfig}
	}
	transport.DisableKeepAlives = true
	err = sockets.ConfigureTransport(transport, proto, addr)
	return &http.Client{
		Transport: transport,
	}, err
}
// Goroutine to run a robot
func robot() {
	var payload interface{}

	transport := new(http.Transport)
	transport.DisableKeepAlives = true
	client := &http.Client{Transport: transport}

	id := uuid.New()

	// login
	resp, err := client.PostForm(msgurl,
		url.Values{"command": {"login"}, "id": {id}})

	if err != nil {
		log.Printf("http login error: %v", err)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	if err != nil {
		log.Printf("http login error: %v", err)
		return
	}

	// extract payload
	err = json.Unmarshal(body, &payload)

	if err != nil {
		log.Printf("json unmarshal error: %v", err)
		log.Printf("json data: %s", string(body))
		return
	}

	outerObj := payload.(map[string]interface{})
	respType := outerObj["type"].(string)

	// check for login errors
	if respType == "status" {
		status := outerObj["status"].(string)
		if status == "error" {
			message := outerObj["message"].(string)
			log.Printf("chat server login error: %s", message)
			return
		}
	}

	// get additional user data
	//publicId := outerObj["publicid"].(string)
	//userName := outerObj["username"].(string)

	// run the poller
	go robotPoller(id)

	msgCount := 0

	// main loop
	for {
		// send chat
		resp, err = client.PostForm(msgurl,
			url.Values{
				"command": {"broadcast"},
				"id":      {id},
				"message": {fmt.Sprintf("message %d", msgCount)},
			})

		msgCount++

		if err != nil {
			log.Printf("http broadcast error: %v", err)
			return
		}

		body, err = ioutil.ReadAll(resp.Body)
		resp.Body.Close()

		// check for broadcast errors
		if respType == "status" {
			status := outerObj["status"].(string)
			if status == "error" {
				message := outerObj["message"].(string)
				log.Printf("chat server broadcast error: %s", message)
				return
			}
		}

		// sleep a bit
		duration := rand.Int31n(int32(maxDelay-minDelay)) + int32(minDelay)
		time.Sleep(time.Duration(duration) * time.Millisecond)

		transport.CloseIdleConnections()
	}
}
// Goroutine to run a long poll thread for a robot
func robotPoller(id string) {
	var payload interface{}

	transport := new(http.Transport)
	transport.DisableKeepAlives = true
	client := &http.Client{Transport: transport}

	for {
		// send in a poll request and wait for response
		resp, err := client.PostForm(pollurl,
			url.Values{"id": {id}})

		if err != nil {
			log.Printf("poller post error: %v", err)
			return
		}

		//log.Printf("************** %v %v", resp, err)
		body, err := ioutil.ReadAll(resp.Body)
		resp.Body.Close()

		if err != nil {
			log.Printf("http poll request error: %v", err)
			return
		}

		// extract payload
		err = json.Unmarshal(body, &payload)

		if err != nil {
			log.Printf("json poll unmarshal error: %v", err)
			log.Printf("json poll data: %s", string(body))
			return
		}

		outerObj := payload.([]interface{})

		for _, v := range outerObj {
			message := v.(map[string]interface{})

			respType := message["type"].(string)

			// check for login errors
			if respType == "status" {
				status := message["status"].(string)
				if status == "error" {
					message := message["message"].(string)
					log.Printf("chat server pollrequest error: %s", message)
					return
				}
			}

			// parse message
			switch respType {
			case "message":
				//log.Printf("%s: message: %s: %s", id, message["username"].(string), message["message"].(string))

			case "newuser":
				//log.Printf("%s: new user: %s", id, message["username"].(string))

			default:
				log.Printf("%s: unknown type: %s", id, respType)
			}
		}

		transport.CloseIdleConnections()
	}
}