mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 09:48:07 +02:00
code improvements; http listener (#32)
This commit is contained in:
parent
04267936cf
commit
20aface112
@ -13,7 +13,14 @@ var proxyCmd = &cobra.Command{
|
|||||||
Use: "proxy <configPath>",
|
Use: "proxy <configPath>",
|
||||||
Short: "Start a zrok proxy",
|
Short: "Start a zrok proxy",
|
||||||
Run: func(_ *cobra.Command, args []string) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
if err := listen.Run(&listen.Config{IdentityPath: args[0], Address: "0.0.0.0:10111"}); err != nil {
|
httpListener, err := listen.NewHTTP(&listen.Config{
|
||||||
|
IdentityPath: args[0],
|
||||||
|
Address: "0.0.0.0:10111",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := httpListener.Run(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -20,6 +20,7 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type httpBind struct {
|
type httpBind struct {
|
||||||
|
cfg *Config
|
||||||
Requests func() int32
|
Requests func() int32
|
||||||
listener edge.Listener
|
listener edge.Listener
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
@ -46,14 +47,15 @@ func NewHTTP(cfg *Config) (*httpBind, error) {
|
|||||||
|
|
||||||
handler := util.NewProxyHandler(proxy)
|
handler := util.NewProxyHandler(proxy)
|
||||||
return &httpBind{
|
return &httpBind{
|
||||||
|
cfg: cfg,
|
||||||
Requests: handler.Requests,
|
Requests: handler.Requests,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
handler: handler,
|
handler: handler,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *httpBind) Run() error {
|
func (self *httpBind) Run() error {
|
||||||
if err := http.Serve(p.listener, p.handler); err != nil {
|
if err := http.Serve(self.listener, self.handler); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -22,10 +22,16 @@ type Config struct {
|
|||||||
Address string
|
Address string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(cfg *Config) error {
|
type httpListen struct {
|
||||||
|
cfg *Config
|
||||||
|
zCtx ziti.Context
|
||||||
|
handler http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTP(cfg *Config) (*httpListen, error) {
|
||||||
zCfg, err := config.NewFromFile(cfg.IdentityPath)
|
zCfg, err := config.NewFromFile(cfg.IdentityPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error loading config")
|
return nil, errors.Wrap(err, "error loading config")
|
||||||
}
|
}
|
||||||
zCfg.ConfigTypes = []string{model.ZrokProxyConfig}
|
zCfg.ConfigTypes = []string{model.ZrokProxyConfig}
|
||||||
zCtx := ziti.NewContextWithConfig(zCfg)
|
zCtx := ziti.NewContextWithConfig(zCfg)
|
||||||
@ -35,21 +41,20 @@ func Run(cfg *Config) error {
|
|||||||
|
|
||||||
proxy, err := NewServiceProxy(zCtx, &resolver{})
|
proxy, err := NewServiceProxy(zCtx, &resolver{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
proxy.Transport = zTransport
|
proxy.Transport = zTransport
|
||||||
return http.ListenAndServe(cfg.Address, basicAuth(util.NewProxyHandler(proxy), "zrok", &resolver{}, zCtx))
|
|
||||||
|
handler := basicAuth(util.NewProxyHandler(proxy), "zrok", &resolver{}, zCtx)
|
||||||
|
return &httpListen{
|
||||||
|
cfg: cfg,
|
||||||
|
zCtx: zCtx,
|
||||||
|
handler: handler,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type resolver struct{}
|
func (self *httpListen) Run() error {
|
||||||
|
return http.ListenAndServe(self.cfg.Address, self.handler)
|
||||||
func (r *resolver) Service(host string) string {
|
|
||||||
logrus.Debugf("host = '%v'", host)
|
|
||||||
tokens := strings.Split(host, ".")
|
|
||||||
if len(tokens) > 0 {
|
|
||||||
return tokens[0]
|
|
||||||
}
|
|
||||||
return "zrok"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ZitiDialContext struct {
|
type ZitiDialContext struct {
|
||||||
@ -236,3 +241,14 @@ func writeUnauthorizedResponse(w http.ResponseWriter, realm string) {
|
|||||||
w.WriteHeader(401)
|
w.WriteHeader(401)
|
||||||
w.Write([]byte("No Authorization\n"))
|
w.Write([]byte("No Authorization\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type resolver struct{}
|
||||||
|
|
||||||
|
func (r *resolver) Service(host string) string {
|
||||||
|
logrus.Debugf("host = '%v'", host)
|
||||||
|
tokens := strings.Split(host, ".")
|
||||||
|
if len(tokens) > 0 {
|
||||||
|
return tokens[0]
|
||||||
|
}
|
||||||
|
return "zrok"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user