Files
weave-scope/tools/socks/main.go
Marc CARRE c7a2117293 Update golang.org/x/net/context to latest.
```
$ gvt delete golang.org/x/net/context
$ gvt fetch golang.org/x/net/context
2017/10/08 XX:XX:XX Fetching: golang.org/x/net/context
$ git grep -l "golang.org/x/net/context" | grep -v vendor | xargs sed -i 's:golang.org/x/net/context:context:g'
$ git grep -l "context/ctxhttp" | grep -v vendor | xargs sed -i 's:context/ctxhttp:golang.org/x/net/context/ctxhttp:g'
$ gofmt -s -w app
$ gofmt -s -w common
$ gofmt -s -w probe
$ gofmt -s -w prog
$ gofmt -s -w tools
```

This fixes errors like:
```
app/multitenant/aws_collector.go:222: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/aws_collector.go:439: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/memcache_client.go:155: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogramStatus
app/multitenant/memcache_client.go:210: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogramStatus
app/multitenant/s3_client.go:74: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/s3_client.go:91: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/sqs_control_router.go:99: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/sqs_control_router.go:133: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/sqs_control_router.go:163: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/sqs_control_router.go:199: cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error in argument to instrument.TimeRequestHistogram
app/multitenant/sqs_control_router.go:199: too many errors
```
2017-10-08 18:37:28 +01:00

98 lines
2.1 KiB
Go

package main
import (
"fmt"
"net"
"net/http"
"os"
"strings"
"text/template"
"context"
socks5 "github.com/armon/go-socks5"
"github.com/weaveworks/common/mflag"
"github.com/weaveworks/common/mflagext"
)
type pacFileParameters struct {
HostMatch string
Aliases map[string]string
}
const (
pacfile = `
function FindProxyForURL(url, host) {
if(shExpMatch(host, "{{.HostMatch}}")) {
return "SOCKS5 localhost:8000";
}
{{range $key, $value := .Aliases}}
if (host == "{{$key}}") {
return "SOCKS5 localhost:8000";
}
{{end}}
return "DIRECT";
}
`
)
func main() {
var (
as []string
hostMatch string
)
mflagext.ListVar(&as, []string{"a", "-alias"}, []string{}, "Specify hostname aliases in the form alias:hostname. Can be repeated.")
mflag.StringVar(&hostMatch, []string{"h", "-host-match"}, "*.weave.local", "Specify main host shExpMatch expression in pacfile")
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, pacFileParameters{hostMatch, aliases})
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
type aliasingResolver struct {
aliases map[string]string
socks5.NameResolver
}
func (r aliasingResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
if alias, ok := r.aliases[name]; ok {
return r.NameResolver.Resolve(ctx, alias)
}
return r.NameResolver.Resolve(ctx, 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)
}
}