Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proxy link vars to connect to endpoint #3

Merged
merged 8 commits into from
May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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