/* Create a proxy that encrypts outbound connection to TLS enabled endpoint */ func ExampleLoadCertificate() { tlscfg, err := proxy.LoadCertificate(proxy.CertOptions{ "s3://devops.org/cert/ca.pem", "file://cert.pem", "key.pem", }) if err != nil { log.Fatal(err) } // prepare proxy option with TLS enabled pxyOpts := &proxy.ConnOptions{ // Conneciton initiator does not encrypt data Net: "tcp4", From: ":6379", // destination endpoint must accept TLS encrypted data To: []string{"10.0.0.12:6379"}, // instructs go-proxy to establish TLS connection with config TLSConfig: proxy.TLSConfig{ Client: tlscfg, }, } context, cancel := ctx.WithCancel(ctx.Background()) defer cancel() err := proxy.To(context, pxyOpts) log.Warning(err) }
/* Create a proxy that listens on 0.0.0.0:6379 and foward to two remote host, balance connections */ func ExampleTo_static() { pxyOpts := &proxy.ConnOptions{ Net: "tcp4", From: ":6379", To: []string{"10.0.0.12:6379", "10.0.1.123:6379"}, Balance: true, } context, cancel := ctx.WithCancel(ctx.Background()) defer cancel() err := proxy.To(context, pxyOpts) log.Warning(err) }
func listen(wk ctx.Context, opts *proxy.ConnOptions) (halt <-chan struct{}) { var err error ending := make(chan struct{}, 1) go func() { defer close(ending) logger := log.WithFields(log.Fields{ "Net": opts.Net, "To": opts.To, "Balance": opts.Balance, }) if len(opts.FromRange) > 0 { logger = logger.WithFields(log.Fields{ "FromRange": opts.FromRange, }) } else { logger = logger.WithFields(log.Fields{ "From": opts.From, }) } if opts.Discovery != nil { logger = logger.WithFields(log.Fields{ "Endpoints": opts.Discovery.Endpoints, "Service": opts.Discovery.Service, }) } logger.Info("begin") if opts.Discovery != nil { if len(opts.FromRange) == 0 { err = proxy.Srv(wk, opts) } else { err = proxy.ClusterSrv(wk, opts) } } else if len(opts.To) != 0 { if len(opts.FromRange) == 0 { err = proxy.To(wk, opts) } else { err = proxy.ClusterTo(wk, opts) } } else { err = errors.New("Misconfigured connect options") } logger.WithFields(log.Fields{"err": err}).Warning("end") }() return ending }