Add socks proxy.

This commit is contained in:
Tom Wilkie
2015-08-12 10:21:46 +00:00
parent 756fc41986
commit 6aefe40b6d
5 changed files with 148 additions and 0 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
cover/cover
socks/proxy
socks/image.tar

7
socks/Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM gliderlabs/alpine
MAINTAINER Weaveworks Inc <help@weave.works>
WORKDIR /
COPY proxy /
EXPOSE 8000
EXPOSE 8080
ENTRYPOINT ["/proxy"]

29
socks/Makefile Normal file
View File

@@ -0,0 +1,29 @@
.PHONY: all clean
IMAGE_TAR=image.tar
IMAGE_NAME=weaveworks/socksproxy
PROXY_EXE=proxy
NETGO_CHECK=@strings $@ | grep cgo_stub\\\.go >/dev/null || { \
rm $@; \
echo "\nYour go standard library was built without the 'netgo' build tag."; \
echo "To fix that, run"; \
echo " sudo go clean -i net"; \
echo " sudo go install -tags netgo std"; \
false; \
}
all: $(IMAGE_TAR)
$(IMAGE_TAR): Dockerfile $(PROXY_EXE)
docker build -t $(IMAGE_NAME) .
docker save $(IMAGE_NAME):latest > $@
$(PROXY_EXE): *.go
go get -tags netgo ./$(@D)
go build -ldflags "-extldflags \"-static\" -linkmode=external" -tags netgo -o $@ ./$(@D)
$(NETGO_CHECK)
clean:
-docker rmi $(IMAGE_NAME)
rm -rf $(PROXY_EXE) $(IMAGE_TAR)
go clean ./...

23
socks/connect.sh Executable file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
set -eu
if [ $# -ne 1 ]; then
echo "Usage: $0 <host>"
exit 1
fi
HOST=$1
echo "Starting proxy container..."
PROXY_CONTAINER=$(ssh $HOST weave run -d weaveworks/socksproxy)
function finish {
echo "Removing proxy container.."
ssh $HOST docker rm -f $PROXY_CONTAINER
}
trap finish EXIT
PROXY_IP=$(ssh $HOST -- "docker inspect --format='{{.NetworkSettings.IPAddress}}' $PROXY_CONTAINER")
echo 'Please configure your browser for proxy http://localhost:8080/proxy.pac'
ssh -L8000:$PROXY_IP:8000 -L8080:$PROXY_IP:8080 $HOST docker attach $PROXY_CONTAINER

87
socks/main.go Normal file
View File

@@ -0,0 +1,87 @@
package main
import (
"fmt"
"net"
"net/http"
"strings"
"os"
"text/template"
socks5 "github.com/armon/go-socks5"
"github.com/weaveworks/weave/common"
"github.com/docker/docker/pkg/mflag"
)
const (
pacfile = `
function FindProxyForURL(url, host) {
if(shExpMatch(host, "*.weave.local")) {
return "SOCKS5 localhost:8000";
}
{{range $key, $value := .}}
if (host == "{{$key}}") {
return "SOCKS5 localhost:8000";
}
{{end}}
return "DIRECT";
}
`
)
func main() {
var as []string
common.ListVar(&as, []string{"a", "-alias"}, []string{}, "Specify hostname aliases in the form alias:hostname. Can be repeated.")
mflag.Parse()
var aliases = map[string]string{}
for _, a := range as {
parts := strings.SplitN(a, ":", 2)
if len(parts) != 2 {
fmt.Printf("'%s' is not a valid alias.\n", a)
mflag.Usage()
os.Exit(1)
}
aliases[parts[0]] = parts[1]
}
go socksProxy(aliases)
t := template.Must(template.New("pacfile").Parse(pacfile))
http.HandleFunc("/proxy.pac", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
t.Execute(w, aliases)
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
type aliasingResolver struct {
aliases map[string]string
socks5.NameResolver
}
func (r aliasingResolver) Resolve(name string) (net.IP, error) {
if alias, ok := r.aliases[name]; ok {
return r.NameResolver.Resolve(alias)
}
return r.NameResolver.Resolve(name)
}
func socksProxy(aliases map[string]string) {
conf := &socks5.Config{
Resolver: aliasingResolver{
aliases: aliases,
NameResolver: socks5.DNSResolver{},
},
}
server, err := socks5.New(conf)
if err != nil {
panic(err)
}
if err := server.ListenAndServe("tcp", ":8000"); err != nil {
panic(err)
}
}