zrok/proxy/proxy.go

28 lines
719 B
Go
Raw Normal View History

2022-07-19 22:15:54 +02:00
package proxy
import (
2022-07-20 20:36:14 +02:00
"github.com/openziti-test-kitchen/zrok/util"
2022-07-20 20:16:01 +02:00
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/pkg/errors"
2022-07-19 22:15:54 +02:00
"net/http"
)
func Run(cfg *Config) error {
2022-07-20 20:16:01 +02:00
zCfg, err := config.NewFromFile(cfg.IdentityPath)
if err != nil {
return errors.Wrap(err, "error loading config")
}
zCtx := ziti.NewContextWithConfig(zCfg)
2022-07-21 21:46:14 +02:00
zDialCtx := util.ZitiDialContext{Context: zCtx}
2022-07-21 21:26:44 +02:00
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialCtx.Dial
2022-07-21 21:46:14 +02:00
2022-07-21 22:01:39 +02:00
proxy, err := util.NewProxy("http://zrok")
2022-07-20 20:36:14 +02:00
if err != nil {
2022-07-21 22:01:39 +02:00
return err
2022-07-20 20:36:14 +02:00
}
2022-07-21 21:46:14 +02:00
proxy.Transport = zTransport
2022-07-21 22:01:39 +02:00
return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
2022-07-19 22:15:54 +02:00
}