Skip to content

Commit

Permalink
Merge pull request #3 from convox/proxy-link
Browse files Browse the repository at this point in the history
Use proxy link vars to connect to endpoint
  • Loading branch information
ddollar committed May 21, 2016
2 parents e5dbd79 + d86e129 commit a001bfd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ RUN apk-install git

RUN apk-install go
ENV GOPATH /go
ENV PATH $GOPATH/bin:$PATH
ENV PATH /go/bin:$PATH

COPY bin/proxy-link /usr/bin/proxy-link

WORKDIR /go/src/github.com/convox/proxy
COPY . /go/src/github.com/convox/proxy
RUN go install ./...

ENTRYPOINT ["proxy"]
ENTRYPOINT ["proxy-link"]
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all: build

build:
docker build --no-cache -t convox/proxy .

test:
go test -cover -v ./...

release: build
docker push convox/proxy
11 changes: 11 additions & 0 deletions bin/proxy-link
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

from=${1}
shift
toport=${1}
shift
args=${@}

eval to="\$HOST_PORT_${toport}_TCP_ADDR:\$HOST_PORT_${toport}_TCP_PORT"

exec proxy $from $to $args
52 changes: 39 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,35 @@ import (
)

func die(err error) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
warn(err)
os.Exit(1)
}

func warn(err error) {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
}

func main() {
if len(os.Args) < 4 {
fmt.Fprintf(os.Stderr, "usage: proxy <from> <to> <protocol>\n")
fmt.Fprintf(os.Stderr, "usage: proxy <from> <to> <protocol> [options]\n")
os.Exit(1)
}

from := os.Args[1]
to := os.Args[2]
protocol := os.Args[3]
proxy := false

if len(os.Args) > 4 && os.Args[4] == "proxy" {
proxy = true
secure := false

if len(os.Args) > 4 {
for _, option := range os.Args[4:] {
switch option {
case "proxy":
proxy = true
case "secure":
secure = true
}
}
}

ln, err := net.Listen("tcp", fmt.Sprintf(":%s", from))
Expand Down Expand Up @@ -68,42 +80,56 @@ func main() {
}

if proxy {
go handleProxyConnection(conn, to)
go handleProxyConnection(conn, to, secure)
} else {
go handleTcpConnection(conn, to)
go handleTcpConnection(conn, to, secure)
}
}
}

func handleProxyConnection(in net.Conn, to string) {
func handleProxyConnection(in net.Conn, to string, secure bool) {
rp := strings.SplitN(in.RemoteAddr().String(), ":", 2)
top := strings.SplitN(to, ":", 2)

fmt.Printf("proxy %s:%s -> %s:%s\n", rp[0], rp[1], top[0], top[1])
fmt.Printf("proxy %s:%s -> %s:%s secure=%t\n", rp[0], rp[1], top[0], top[1], secure)

out, err := net.DialTimeout("tcp", to, 5*time.Second)

if err != nil {
die(err)
warn(err)
return
}

header := fmt.Sprintf("PROXY TCP4 %s 127.0.0.1 %s %s\r\n", rp[0], rp[1], top[1])

out.Write([]byte(header))

if secure {
out = tls.Client(out, &tls.Config{
InsecureSkipVerify: true,
})
}

pipe(in, out)
}

func handleTcpConnection(in net.Conn, to string) {
func handleTcpConnection(in net.Conn, to string, secure bool) {
rp := strings.SplitN(in.RemoteAddr().String(), ":", 2)
top := strings.SplitN(to, ":", 2)

fmt.Printf("tcp %s:%s -> %s:%s\n", rp[0], rp[1], top[0], top[1])
fmt.Printf("tcp %s:%s -> %s:%s secure=%t\n", rp[0], rp[1], top[0], top[1], secure)

out, err := net.DialTimeout("tcp", to, 5*time.Second)

if err != nil {
die(err)
warn(err)
return
}

if secure {
out = tls.Client(out, &tls.Config{
InsecureSkipVerify: true,
})
}

pipe(in, out)
Expand Down

0 comments on commit a001bfd

Please sign in to comment.