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-26 19:30:19 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-07-19 22:15:54 +02:00
|
|
|
"net/http"
|
2022-07-26 23:30:06 +02:00
|
|
|
"strings"
|
2022-07-19 22:15:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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-26 19:30:19 +02:00
|
|
|
proxy, err := util.NewServiceProxy(&resolver{})
|
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
|
|
|
}
|
2022-07-26 19:30:19 +02:00
|
|
|
|
|
|
|
type resolver struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resolver) Service(host string) string {
|
|
|
|
logrus.Infof("host = '%v'", host)
|
2022-07-26 23:30:06 +02:00
|
|
|
tokens := strings.Split(host, ".")
|
|
|
|
if len(tokens) > 0 {
|
|
|
|
return tokens[0]
|
|
|
|
}
|
2022-07-26 19:30:19 +02:00
|
|
|
return "zrok"
|
|
|
|
}
|