github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/build/proxy/proxy.go (about)

     1  package proxy
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  )
     7  
     8  // bash header
     9  const header = "#!/bin/bash\n"
    10  
    11  // this command string will check if the socat utility
    12  // exists, and if it does, will proxy connections to
    13  // the external IP address.
    14  const command = "[ -x /usr/bin/socat ] && socat TCP-LISTEN:%s,fork TCP:%s:%s &\n"
    15  
    16  // Proxy stores proxy configuration details mapping
    17  // a local port to an external IP address with the
    18  // same port number.
    19  type Proxy map[string]string
    20  
    21  func (p Proxy) Set(port, ip string) {
    22  	p[port] = ip
    23  }
    24  
    25  // String converts the proxy configuration details
    26  // to a bash script.
    27  func (p Proxy) String() string {
    28  	var buf bytes.Buffer
    29  	buf.WriteString(header)
    30  	for port, ip := range p {
    31  		buf.WriteString(fmt.Sprintf(command, port, ip, port))
    32  	}
    33  
    34  	return buf.String()
    35  }
    36  
    37  // Bytes converts the proxy configuration details
    38  // to a bash script in byte array format.
    39  func (p Proxy) Bytes() []byte {
    40  	return []byte(p.String())
    41  }