Beispiel #1
0
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	req1, req2 := DuplicateRequest(req)
	go func() {
		defer func() {
			if r := recover(); r != nil && *debug {
				fmt.Println("Recovered in f", r)
			}
		}()
		client_tcp_conn, _ := net.DialTimeout("tcp", h.Alternative, time.Duration(1*time.Second)) // Open new TCP connection to the server
		client_http_conn := httputil.NewClientConn(client_tcp_conn, nil)                          // Start a new HTTP connection on it
		client_http_conn.Write(req1)                                                              // Pass on the request
		client_http_conn.Read(req1)                                                               // Read back the reply
		client_http_conn.Close()                                                                  // Close the connection to the server
	}()
	defer func() {
		if r := recover(); r != nil && *debug {
			fmt.Println("Recovered in f", r)
		}
	}()

	client_tcp_conn, _ := net.DialTimeout("tcp", h.Target, time.Duration(3*time.Second)) // Open new TCP connection to the server
	client_http_conn := httputil.NewClientConn(client_tcp_conn, nil)                     // Start a new HTTP connection on it
	client_http_conn.Write(req2)                                                         // Pass on the request
	resp, _ := client_http_conn.Read(req2)                                               // Read back the reply
	resp.Write(w)                                                                        // Write the reply to the original connection
	client_http_conn.Close()                                                             // Close the connection to the server
}
Beispiel #2
0
// ServeHTTP duplicates the incoming request (req) and does the request to the Target and the Alternate target discading the Alternate response
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	req1, req2 := DuplicateRequest(req)
	go func() {
		defer func() {
			if r := recover(); r != nil && *debug {
				fmt.Println("Recovered in f", r)
			}
		}()
		// Open new TCP connection to the server
		clientTcpConn, err := net.DialTimeout("tcp", h.Alternative, time.Duration(time.Duration(*alternateTimeout)*time.Second))
		if err != nil {
			if *debug {
				fmt.Printf("Failed to connect to %s\n", h.Alternative)
			}
			return
		}
		clientHttpConn := httputil.NewClientConn(clientTcpConn, nil) // Start a new HTTP connection on it
		defer clientHttpConn.Close()                                 // Close the connection to the server
		err = clientHttpConn.Write(req1)                             // Pass on the request
		if err != nil {
			if *debug {
				fmt.Printf("Failed to send to %s: %v\n", h.Alternative, err)
			}
			return
		}
		_, err = clientHttpConn.Read(req1) // Read back the reply
		if err != nil {
			if *debug {
				fmt.Printf("Failed to receive from %s: %v\n", h.Alternative, err)
			}
			return
		}
	}()
	defer func() {
		if r := recover(); r != nil && *debug {
			fmt.Println("Recovered in f", r)
		}
	}()

	// Open new TCP connection to the server
	clientTcpConn, err := net.DialTimeout("tcp", h.Target, time.Duration(time.Duration(*productionTimeout)*time.Second))
	if err != nil {
		fmt.Printf("Failed to connect to %s\n", h.Target)
		return
	}
	clientHttpConn := httputil.NewClientConn(clientTcpConn, nil) // Start a new HTTP connection on it
	defer clientHttpConn.Close()                                 // Close the connection to the server
	err = clientHttpConn.Write(req2)                             // Pass on the request
	if err != nil {
		fmt.Printf("Failed to send to %s: %v\n", h.Target, err)
		return
	}
	resp, err := clientHttpConn.Read(req2) // Read back the reply
	if err != nil {
		fmt.Printf("Failed to receive from %s: %v\n", h.Target, err)
		return
	}
	resp.Write(w) // Write the reply to the original connection
}
Beispiel #3
0
func (ctx *runContext) connectToRunServer() (*http.Response, net.Conn, error) {
	if ctx.attachURL == "" {
		return nil, nil, errgo.New("No attach URL to connect to")
	}

	req, err := http.NewRequest("CONNECT", ctx.attachURL, nil)
	if err != nil {
		return nil, nil, errgo.Mask(err, errgo.Any)
	}
	req.SetBasicAuth("", config.AuthenticatedUser.AuthenticationToken)

	url, err := url.Parse(ctx.attachURL)
	if err != nil {
		return nil, nil, errgo.Mask(err, errgo.Any)
	}

	dial, err := net.Dial("tcp", url.Host)
	if err != nil {
		return nil, nil, errgo.Mask(err, errgo.Any)
	}

	var conn *httputil.ClientConn
	if url.Scheme == "https" {
		host := strings.Split(url.Host, ":")[0]
		config := *config.TlsConfig
		config.ServerName = host
		tls_conn := tls.Client(dial, &config)
		conn = httputil.NewClientConn(tls_conn, nil)
	} else if url.Scheme == "http" {
		conn = httputil.NewClientConn(dial, nil)
	} else {
		return nil, nil, errgo.Newf("Invalid scheme format %s", url.Scheme)
	}

	res, err := conn.Do(req)
	if err != httputil.ErrPersistEOF && err != nil {
		if err, ok := err.(*net.OpError); ok {
			if err.Err.Error() == "record overflow" {
				return nil, nil, errgo.Newf(
					"Fail to create a secure connection to Scalingo server\n"+
						"The encountered error is: %v (ID: CLI-1001)\n"+
						"Your firewall or proxy may block the connection to %s",
					err, url.Host,
				)
			}
		}
		return nil, nil, errgo.Mask(err, errgo.Any)
	}

	connection, _ := conn.Hijack()
	return res, connection, nil
}
Beispiel #4
0
func dial(host string) (conn *httputil.ClientConn) {
	var tcp net.Conn
	var err error
	fmt.Fprintf(os.Stderr, "http-gonsole: establishing a TCP connection ...\n")
	proxy := os.Getenv("HTTP_PROXY")
	if strings.Split(host, ":")[0] != "localhost" && len(proxy) > 0 {
		proxy_url, _ := url.Parse(proxy)
		tcp, err = net.Dial("tcp", proxy_url.Host)
	} else {
		tcp, err = net.Dial("tcp", host)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "http-gonsole:", err)
		os.Exit(1)
	}
	if *useSSL {
		if len(proxy) > 0 {
			connReq := &http.Request{
				Method: "CONNECT",
				URL:    &url.URL{Path: host},
				Host:   host,
				Header: make(http.Header),
			}
			connReq.Write(tcp)
			resp, err := http.ReadResponse(bufio.NewReader(tcp), connReq)
			if resp.StatusCode != 200 {
				fmt.Fprintln(os.Stderr, "http-gonsole:", resp.Status)
				os.Exit(1)
			}
			if err != nil {
				fmt.Fprintln(os.Stderr, "http-gonsole:", err)
				os.Exit(1)
			}
			tcp = tls.Client(tcp, nil)
			conn = httputil.NewClientConn(tcp, nil)
		} else {
			tcp = tls.Client(tcp, nil)
			conn = httputil.NewClientConn(tcp, nil)
		}
		if err = tcp.(*tls.Conn).Handshake(); err != nil {
			fmt.Fprintln(os.Stderr, "http-gonsole:", err)
			os.Exit(1)
		}
		if err = tcp.(*tls.Conn).VerifyHostname(strings.Split(host, ":")[0]); err != nil {
			fmt.Fprintln(os.Stderr, "http-gonsole:", err)
			os.Exit(1)
		}
	} else {
		conn = httputil.NewClientConn(tcp, nil)
	}
	return
}
Beispiel #5
0
func Connect(nurl *url.URL) {
	req := http.Request{
		Method: "GET",
		Header: http.Header{},
		URL:    nurl,
	}
	dial, err := net.Dial("tcp", nurl.Host)
	if err != nil {
		fmt.Println(err)
		return
	}
	clientconn := httputil.NewClientConn(dial, nil)
	clientconn.Do(&req)
	defer clientconn.Close()

	rwc, _ := clientconn.Hijack()
	defer rwc.Close()

	/*
		go func(rwc io.ReadWriteCloser) {
			bufRead(rwc)
		}(rwc)

		bufWrite(rwc)
	*/
	count := 0
	for {
		count++
		fmt.Println(count)
		time.Sleep(10 * time.Second)
	}
	time.Sleep(5 * time.Second)
}
Beispiel #6
0
func test_conn(conn *tls.Conn, options *ScanOptions, record *ScanRecord) bool {
	//check SSL certificate
	success := false
	for _, cert := range conn.ConnectionState().PeerCertificates {
		for _, verifyHost := range options.Config.ScanGoogleIP.SSLCertVerifyHosts {
			if cert.VerifyHostname(verifyHost) != nil {
				return false
			} else {
				success = true
			}
		}
		if success {
			break
		}
	}
	for _, verifyHost := range options.Config.ScanGoogleIP.HTTPVerifyHosts {
		conn.SetReadDeadline(time.Now().Add(record.httpVerifyTimeout))
		req, _ := http.NewRequest("HEAD", "https://"+verifyHost, nil)
		res, err := httputil.NewClientConn(conn, nil).Do(req)
		if nil != err || res.StatusCode >= 400 {
			return false
		}
	}
	return true
}
Beispiel #7
0
func sockRequest(method, endpoint string) ([]byte, error) {
	// FIX: the path to sock should not be hardcoded
	sock := filepath.Join("/", "var", "run", "docker.sock")
	c, err := net.DialTimeout("unix", sock, time.Duration(10*time.Second))
	if err != nil {
		return nil, fmt.Errorf("could not dial docker sock at %s: %v", sock, err)
	}

	client := httputil.NewClientConn(c, nil)
	defer client.Close()

	req, err := http.NewRequest(method, endpoint, nil)
	req.Header.Set("Content-Type", "application/json")
	if err != nil {
		return nil, fmt.Errorf("could not create new request: %v", err)
	}

	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("could not perform request: %v", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(resp.Body)
		return body, fmt.Errorf("received status != 200 OK: %s", resp.Status)
	}

	return ioutil.ReadAll(resp.Body)
}
// Regression test for https://github.com/docker/docker/issues/6231
func (s *DockerSuite) TestContainerApiChunkedEncoding(c *check.C) {
	// TODO Windows CI: This can be ported
	testRequires(c, DaemonIsLinux)

	conn, err := sockConn(time.Duration(10*time.Second), "")
	c.Assert(err, checker.IsNil)
	client := httputil.NewClientConn(conn, nil)
	defer client.Close()

	config := map[string]interface{}{
		"Image":     "busybox",
		"Cmd":       append([]string{"/bin/sh", "-c"}, defaultSleepCommand...),
		"OpenStdin": true,
	}
	b, err := json.Marshal(config)
	c.Assert(err, checker.IsNil)

	req, err := http.NewRequest("POST", "/containers/create", bytes.NewBuffer(b))
	c.Assert(err, checker.IsNil)
	req.Header.Set("Content-Type", "application/json")
	// This is a cheat to make the http request do chunked encoding
	// Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
	// https://golang.org/src/pkg/net/http/request.go?s=11980:12172
	req.ContentLength = -1

	resp, err := client.Do(req)
	c.Assert(err, checker.IsNil, check.Commentf("error creating container with chunked encoding"))
	resp.Body.Close()
	c.Assert(resp.StatusCode, checker.Equals, http.StatusCreated)
}
Beispiel #9
0
// Creates a new Docker client using the Docker unix socket.
func NewDockerClient(dockerSocketPath string) (*httputil.ClientConn, error) {
	conn, err := net.Dial("unix", dockerSocketPath)
	if err != nil {
		return nil, err
	}
	return httputil.NewClientConn(conn, nil), nil
}
Beispiel #10
0
func (b *ContainerBackup) request(method, path string, body io.Reader) (*http.Response, error) {
	req, err := http.NewRequest(method, path, body)
	if err != nil {
		return nil, err
	}

	conn, err := net.Dial(b.proto, b.addr)
	if err != nil {
		return nil, err
	}

	clientconn := httputil.NewClientConn(conn, nil)
	resp, err := clientconn.Do(req)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode < 200 || resp.StatusCode >= 400 {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return nil, err
		}
		if len(body) == 0 {
			return nil, fmt.Errorf("Error: %s", http.StatusText(resp.StatusCode))
		}

		return nil, fmt.Errorf("HTTP %s: %s", http.StatusText(resp.StatusCode), body)
	}
	return resp, nil
}
Beispiel #11
0
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) ([]byte, error) {
	c, err := sockConn(time.Duration(10 * time.Second))
	if err != nil {
		return nil, fmt.Errorf("could not dial docker daemon: %v", err)
	}

	client := httputil.NewClientConn(c, nil)
	defer client.Close()

	req, err := http.NewRequest(method, endpoint, data)
	if err != nil {
		return nil, fmt.Errorf("could not create new request: %v", err)
	}

	if ct == "" {
		ct = "application/json"
	}
	req.Header.Set("Content-Type", ct)

	resp, err := client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("could not perform request: %v", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		body, _ := ioutil.ReadAll(resp.Body)
		return body, fmt.Errorf("received status != 200 OK: %s", resp.Status)
	}

	return ioutil.ReadAll(resp.Body)
}
Beispiel #12
0
func listenToDockerDaemonMessages(ch chan<- DockerDaemonMessage) {
	conn, err := net.Dial("tcp", "0.0.0.0:2375")
	if err != nil {
		log.Fatal(err)
	}

	clientConn := httputil.NewClientConn(conn, nil)

	req, err := http.NewRequest("GET", "/events", nil)
	if err != nil {
		log.Fatal(err)
	}

	resp, err := clientConn.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	dec := json.NewDecoder(resp.Body)

	for {
		var m DockerDaemonMessage
		if err := dec.Decode(&m); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s: %s\n", m.Status, m.Id)
		ch <- m
	}
}
Beispiel #13
0
func ExampleSocks5Client() {
	user := ""
	password := ""
	client, err := NewSocks5Client("tcp", "127.0.0.1:1080", user, password, Direct)
	if err != nil {
		return
	}
	conn, err := client.Dial("tcp", "www.google.com:80")
	if err != nil {
		return
	}
	httpClient := httputil.NewClientConn(conn, nil)
	defer httpClient.Close()

	request, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		return
	}
	resp, err := httpClient.Do(request)
	if err != nil {
		return
	}
	dump, err := httputil.DumpResponse(resp, true)
	if err != nil {
		return
	}
	println(string(dump))
	return
}
Beispiel #14
0
func ExampleSocks4Client() {
	user := ""
	client, err := NewSocks4Client("tcp", "127.0.0.1:1080", user, Direct)
	if err != nil {
		return
	}
	addrs, err := net.LookupHost("www.google.com")
	if err != nil {
		return
	}
	if len(addrs) == 0 {
		return
	}
	conn, err := client.Dial("tcp", addrs[0]+":80")
	if err != nil {
		return
	}
	httpClient := httputil.NewClientConn(conn, nil)
	defer httpClient.Close()

	request, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		return
	}
	resp, err := httpClient.Do(request)
	if err != nil {
		return
	}
	dump, err := httputil.DumpResponse(resp, true)
	if err != nil {
		return
	}
	println(string(dump))
	return
}
Beispiel #15
0
func commToDaemon(method, path string, jsonData []byte) (resp *http.Response, err error) {
	//fmt.Println(method, "/api"+path, string(jsonData))

	req, err := http.NewRequest(strings.ToUpper(method), "/api"+path, bytes.NewBuffer(jsonData))

	if len(User.userName) > 0 {
		req.SetBasicAuth(User.userName, User.password)
	}

	/* else {
		req.Header.Set("Authorization", "Basic "+os.Getenv("DAEMON_USER_AUTH_INFO"))
	}
	*/
	conn, err := net.Dial("tcp", CmdHttpServer)
	if err != nil {
		fmt.Println(err.Error())
		fmt.Println("Datahub daemon not running? Use 'datahub --daemon' to start daemon.")
		os.Exit(2)
	}
	//client := &http.Client{}
	client := httputil.NewClientConn(conn, nil)
	return client.Do(req)
	/*
		defer resp.Body.Close()
		response = *resp
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println(string(body))
	*/
}
Beispiel #16
0
func (c *Client) hijack(method, path string, success chan struct{}, in io.Reader, out io.Writer) error {
	req, err := http.NewRequest(method, c.getURL(path), nil)
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "plain/text")
	protocol := c.endpointURL.Scheme
	address := c.endpointURL.Path
	if protocol != "unix" {
		protocol = "tcp"
		address = c.endpointURL.Host
	}
	dial, err := net.Dial(protocol, address)
	if err != nil {
		return err
	}
	defer dial.Close()
	clientconn := httputil.NewClientConn(dial, nil)
	clientconn.Do(req)
	if success != nil {
		success <- struct{}{}
		<-success
	}
	rwc, br := clientconn.Hijack()
	go func() {
		if in != nil {
			_, err = io.Copy(rwc, in)
		}
		rwc.(interface {
			CloseWrite() error
		}).CloseWrite()
	}()
	_, err = io.Copy(out, br)
	return err
}
Beispiel #17
0
func (c *Client) do(method, path string, data interface{}) ([]byte, int, error) {
	var params io.Reader
	if data != nil {
		buf, err := json.Marshal(data)
		if err != nil {
			return nil, -1, err
		}
		params = bytes.NewBuffer(buf)
	}

	if path != "/version" && !c.SkipServerVersionCheck && c.expectedApiVersion == nil {
		err := c.checkApiVersion()
		if err != nil {
			return nil, -1, err
		}
	}

	req, err := http.NewRequest(method, c.getURL(path), params)
	if err != nil {
		return nil, -1, err
	}
	req.Header.Set("User-Agent", userAgent)
	if data != nil {
		req.Header.Set("Content-Type", "application/json")
	} else if method == "POST" {
		req.Header.Set("Content-Type", "plain/text")
	}
	var resp *http.Response
	protocol := c.endpointURL.Scheme
	address := c.endpointURL.Path
	if protocol == "unix" {
		dial, err := net.Dial(protocol, address)
		if err != nil {
			return nil, -1, err
		}
		clientconn := httputil.NewClientConn(dial, nil)
		resp, err = clientconn.Do(req)
		if err != nil {
			return nil, -1, err
		}
		defer clientconn.Close()
	} else {
		resp, err = c.client.Do(req)
	}
	if err != nil {
		if strings.Contains(err.Error(), "connection refused") {
			return nil, -1, ErrConnectionRefused
		}
		return nil, -1, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, -1, err
	}
	if resp.StatusCode < 200 || resp.StatusCode >= 400 {
		return nil, resp.StatusCode, newError(resp.StatusCode, body)
	}
	return body, resp.StatusCode, nil
}
Beispiel #18
0
func (h *UnixHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	conn, err := net.Dial("unix", h.path)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	c := httputil.NewClientConn(conn, nil)
	defer c.Close()

	res, err := c.Do(r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	defer res.Body.Close()

	for i, r := range res.Header {
		for _, s := range r {
			w.Header().Add(i, s)
		}
	}

	if _, err := io.Copy(w, res.Body); err != nil {
		log.Println(err)
	}
}
Beispiel #19
0
func (c *Client) eventHijack(startTime int64, eventChan chan *APIEvents, errChan chan error) error {
	uri := "/events"
	if startTime != 0 {
		uri += fmt.Sprintf("?since=%d", startTime)
	}
	protocol := c.endpointURL.Scheme
	address := c.endpointURL.Path
	if protocol != "unix" {
		protocol = "tcp"
		address = c.endpointURL.Host
	}
	var dial net.Conn
	var err error
	if c.TLSConfig == nil {
		dial, err = c.Dialer.Dial(protocol, address)
	} else {
		dial, err = tlsDialWithDialer(c.Dialer, protocol, address, c.TLSConfig)
	}
	if err != nil {
		return err
	}
	conn := httputil.NewClientConn(dial, nil)
	req, err := http.NewRequest("GET", uri, nil)
	if err != nil {
		return err
	}
	res, err := conn.Do(req)
	if err != nil {
		return err
	}
	go func(res *http.Response, conn *httputil.ClientConn) {
		defer conn.Close()
		defer res.Body.Close()
		decoder := json.NewDecoder(res.Body)
		for {
			var event APIEvents
			if err = decoder.Decode(&event); err != nil {
				if err == io.EOF || err == io.ErrUnexpectedEOF {
					c.eventMonitor.RLock()
					if c.eventMonitor.enabled && c.eventMonitor.C == eventChan {
						// Signal that we're exiting.
						eventChan <- EOFEvent
					}
					c.eventMonitor.RUnlock()
					break
				}
				errChan <- err
			}
			if event.Time == 0 {
				continue
			}
			if !c.eventMonitor.isEnabled() || c.eventMonitor.C != eventChan {
				return
			}
			transformEvent(&event)
			eventChan <- &event
		}
	}(res, conn)
	return nil
}
Beispiel #20
0
func (p *Peer) doConnect() bool {
	netcon, err := transport.UnixDialer("unix", p.ConnectionString)
	if err != nil {
		log.Printf("################ - peer.connect: Connection failure - %s / %s - %s", p.Name, p.ConnectionString, err.Error())
		return delay()
	}
	log.Printf("################ - peer.connect: Connection established %s / %s", p.Name, p.ConnectionString)

	client := httputil.NewClientConn(netcon, nil)
	req, err := http.NewRequest("GET", "/forward", nil)
	if err != nil {
		log.Printf("################ - peer.connect: Creating new GET request failed %s / %s - %v", p.Name, p.ConnectionString, err)
		return delay()
	}

	req.Header.Set("Content-Type", "plain/text")
	req.Header.Set("Connection", "Keep-Alive")

	defer client.Close()
	defer netcon.Close()

	log.Printf("################ - peer.connect: Performing GET request %s / %s - %v", p.Name, p.ConnectionString, req)
	resp, err := client.Do(req)
	if err != nil {
		log.Printf("################ - peer.connect: GET /forward failed %s / %s - %s", p.Name, p.ConnectionString, err.Error())
		return delay()
	}
	log.Printf("################ - peer.connect: Get Result %s / %s", p.Name, p.ConnectionString)

	defer resp.Body.Close()

	p.enterLoop(client)
	return false
}
func newConn(url *url.URL) (*httputil.ClientConn, error) {
	addr := url.Host
	if !hasPort(addr) {
		addr += ":" + url.Scheme
	}
	var conn net.Conn
	var err error
	if url.Scheme == "http" {
		conn, err = net.Dial("tcp", addr)
		if err != nil {
			return nil, err
		}
	} else { // https
		conn, err = tls.Dial("tcp", addr, nil)
		if err != nil {
			return nil, err
		}
		h := url.Host
		if hasPort(h) {
			h = h[0:strings.LastIndex(h, ":")]
		}
		if err := conn.(*tls.Conn).VerifyHostname(h); err != nil {
			return nil, err
		}
	}

	return httputil.NewClientConn(conn, nil), nil
}
// Regression test for https://github.com/docker/docker/issues/6231
func (s *DockerSuite) TestContainersApiChunkedEncoding(c *check.C) {
	testRequires(c, DaemonIsLinux)
	out, _ := dockerCmd(c, "create", "-v", "/foo", "busybox", "true")
	id := strings.TrimSpace(out)

	conn, err := sockConn(time.Duration(10 * time.Second))
	c.Assert(err, checker.IsNil)
	client := httputil.NewClientConn(conn, nil)
	defer client.Close()

	bindCfg := strings.NewReader(`{"Binds": ["/tmp:/foo"]}`)
	req, err := http.NewRequest("POST", "/containers/"+id+"/start", bindCfg)
	c.Assert(err, checker.IsNil)
	req.Header.Set("Content-Type", "application/json")
	// This is a cheat to make the http request do chunked encoding
	// Otherwise (just setting the Content-Encoding to chunked) net/http will overwrite
	// https://golang.org/src/pkg/net/http/request.go?s=11980:12172
	req.ContentLength = -1

	resp, err := client.Do(req)
	c.Assert(err, checker.IsNil, check.Commentf("error starting container with chunked encoding"))
	resp.Body.Close()
	c.Assert(resp.StatusCode, checker.Equals, 204)

	out, err = inspectFieldJSON(id, "HostConfig.Binds")
	c.Assert(err, checker.IsNil)

	var binds []string
	c.Assert(json.NewDecoder(strings.NewReader(out)).Decode(&binds), checker.IsNil)
	c.Assert(binds, checker.HasLen, 1, check.Commentf("Got unexpected binds: %v", binds))

	expected := "/tmp:/foo"
	c.Assert(binds[0], checker.Equals, expected, check.Commentf("got incorrect bind spec"))
}
Beispiel #23
0
func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
	c, err := sockConn(time.Duration(10 * time.Second))
	if err != nil {
		return nil, nil, fmt.Errorf("could not dial docker daemon: %v", err)
	}

	client := httputil.NewClientConn(c, nil)

	req, err := http.NewRequest(method, endpoint, data)
	if err != nil {
		client.Close()
		return nil, nil, fmt.Errorf("could not create new request: %v", err)
	}

	if ct != "" {
		req.Header.Set("Content-Type", ct)
	}

	resp, err := client.Do(req)
	if err != nil {
		client.Close()
		return nil, nil, fmt.Errorf("could not perform request: %v", err)
	}
	body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
		defer resp.Body.Close()
		return client.Close()
	})

	return resp, body, nil
}
Beispiel #24
0
func (self *Stream) Connect() (*http.Response, error) {
	url := new(url.URL)
	url.Scheme = "https"
	url.Host = "streaming.campfirenow.com"
	url.Path = "/room/" + strconv.Itoa(self.room.Id) + "/live.json"

	conn, err := net.Dial("tcp", url.Host+":443")

	if err != nil {
		return nil, err
	}

	ssl := tls.Client(conn, nil)
	client := httputil.NewClientConn(ssl, nil)

	req, err := http.NewRequest("GET", url.String(), nil)

	req.SetBasicAuth(self.token, "X")

	resp, err := client.Do(req)

	if err != nil {
		log.Print("Couldn't initiate stream.")
		return nil, err
	}

	return resp, nil
}
Beispiel #25
0
func TestHostHandlers(t *testing.T) {
	for _, h := range handlers {
		http.Handle(h.pattern, stringHandler(h.msg))
	}
	ts := httptest.NewServer(nil)
	defer ts.Close()

	conn, err := net.Dial("tcp", ts.Listener.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	cc := httputil.NewClientConn(conn, nil)
	for _, vt := range vtests {
		var r *http.Response
		var req http.Request
		if req.URL, err = url.Parse(vt.url); err != nil {
			t.Errorf("cannot parse url: %v", err)
			continue
		}
		if err := cc.Write(&req); err != nil {
			t.Errorf("writing request: %v", err)
			continue
		}
		r, err := cc.Read(&req)
		if err != nil {
			t.Errorf("reading response: %v", err)
			continue
		}
		s := r.Header.Get("Result")
		if s != vt.expected {
			t.Errorf("Get(%q) = %q, want %q", vt.url, s, vt.expected)
		}
	}
}
Beispiel #26
0
// Proxies a plaintext request, returning the response to the client.
func (p *InterceptingProxyHandler) proxyPlaintextRequest(w http.ResponseWriter, r *http.Request) {
	requestHost := r.URL.Host

	if !strings.ContainsAny(requestHost, ":") {
		requestHost = requestHost + ":80"
	}

	conn, err := net.Dial("tcp", requestHost)

	if err != nil {
		log.Println("bad request (dial):", err)
		http.Error(w, "Bad proxy request", http.StatusInternalServerError)
		return
	}

	clientConn := httputil.NewClientConn(conn, nil)
	response, err := clientConn.Do(r)

	if err != nil && err != httputil.ErrPersistEOF {
		log.Println("bad request:", err)
		http.Error(w, "Bad proxy request", http.StatusInternalServerError)
		return
	}

	for k, v := range response.Header {
		w.Header()[k] = v
	}

	io.Copy(w, response.Body)
}
Beispiel #27
0
func find_match_hosts(conn *tls.Conn, options *ScanOptions, record *ScanRecord) bool {
	if nil == record.MatchHosts || len(record.MatchHosts) == 0 {
		record.MatchHosts = options.SSLMatchHosts(conn)
	}

	newhosts := make([]string, 0)
	for _, host := range record.MatchHosts {
		if options.HaveHostInRecords(host) {
			continue
		}
		newhosts = append(newhosts, host)
		valid := true
		for _, pattern := range options.Config.ScanGoogleHosts.HTTPVerifyHosts {
			if matchHostnames(pattern, host) {
				conn.SetReadDeadline(time.Now().Add(record.httpVerifyTimeout))
				req, _ := http.NewRequest("HEAD", "https://"+host, nil)
				res, err := httputil.NewClientConn(conn, nil).Do(req)
				if nil != err || res.StatusCode >= 400 {
					valid = false
				}
				break
			}
		}
		if valid {
			newhosts = append(newhosts, host)
		}
	}
	record.MatchHosts = newhosts
	return len(record.MatchHosts) > 0
}
Beispiel #28
0
func request(path string) (*http.Response, error) {
	req, err := http.NewRequest("GET", path, nil)
	if err != nil {
		return nil, err
	}

	conn, err := net.Dial(*proto, *addr)
	if err != nil {
		return nil, err
	}

	clientconn := httputil.NewClientConn(conn, nil)
	resp, err := clientconn.Do(req)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode < 200 || resp.StatusCode >= 400 {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return nil, err
		}
		if len(body) == 0 {
			return nil, fmt.Errorf("Error: %s", http.StatusText(resp.StatusCode))
		}

		return nil, fmt.Errorf("HTTP %s: %s", http.StatusText(resp.StatusCode), body)
	}
	return resp, nil
}
Beispiel #29
0
func (self *Agent) HijackHTTP(keepalive bool) (net.Conn, error) {
	url := self.peerUrl.String()
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "text/plain")
	req.Header.Set("Connection", "Upgrade")
	req.Header.Set("Upgrade", "tcp")
	req.Host = self.peerUrl.Host

	dial, err := self.dial(self.peerUrl)
	// When we set up a TCP connection for hijack, there could be long periods
	// of inactivity (a long running command with no output) that in certain
	// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
	// state. Setting TCP KeepAlive on the socket connection will prohibit
	// ECONNTIMEOUT unless the socket connection truly is broken
	if tcpConn, ok := dial.(*net.TCPConn); keepalive && ok {
		tcpConn.SetKeepAlive(true)
		tcpConn.SetKeepAlivePeriod(30 * time.Second)
	}
	if err != nil {
		log.Error("pangolin-agent: dial failed ", err)
		return nil, err
	}
	clientconn := httputil.NewClientConn(dial, nil)
	// Server hijacks the connection, error 'connection closed' expected
	clientconn.Do(req)

	conn, _ := clientconn.Hijack()
	return conn, nil
}
Beispiel #30
0
// Performs the actual lookup and returns the HTTP response.
func performLookup(urlString string) (*http.Response, error) {
	url_, err := url.Parse(urlString)
	if err != nil {
		return nil, err
	}

	// Open a TCP connection.
	conn, err := net.Dial("tcp", url_.Host+":"+url_.Scheme)
	if err != nil {
		return nil, err
	}

	// Perform the HTTP request.
	client := httputil.NewClientConn(conn, nil)
	request, err := http.NewRequest("GET", urlString, nil)
	if err != nil {
		return nil, err
	}
	request.Header.Set("User-Agent", "Armadillo File Manager")
	err = client.Write(request)
	if err != nil {
		return nil, err
	}
	return client.Read(request)
}