-
Notifications
You must be signed in to change notification settings - Fork 2
/
transport.go
140 lines (115 loc) · 3.09 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package ziti_caddy
import (
"context"
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
"github.com/openziti/sdk-golang/ziti"
"net"
"net/http"
"time"
)
var zitiContexts = ziti.NewSdkCollection()
func init() {
zitiContexts.ConfigTypes = append(zitiContexts.ConfigTypes, ziti.InterceptV1, ziti.ClientConfigV1)
caddy.RegisterModule(ZitiTransport{})
caddy.RegisterNetwork("ziti", newZitiListener)
}
// ZitiTransport implements reverse proxy that connects to a Ziti service
type ZitiTransport struct {
// Identity is a Ziti identity file
Identity string `json:"identity,omitempty"`
// Service is Ziti service proxy will connect to
Service string `json:"service,omitempty"`
// Terminator is a specific service terminator proxy will connect to
Terminator string `json:"terminator,omitempty"`
ztx ziti.Context
caddy caddy.Context
proxy *reverseproxy.HTTPTransport
}
func (z *ZitiTransport) Cleanup() error {
var err error
if z.ztx != nil {
zitiContexts.Remove(z.ztx)
z.ztx.Close()
z.ztx = nil
}
if z.proxy != nil {
err = z.proxy.Cleanup()
z.proxy = nil
}
return err
}
func (z *ZitiTransport) RoundTrip(request *http.Request) (*http.Response, error) {
return z.proxy.RoundTrip(request)
}
func (z *ZitiTransport) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if !d.Next() {
return fmt.Errorf("missing ziti transport configuration")
}
for d.NextBlock(0) {
switch d.Val() {
case "identity":
d.Args(&z.Identity)
case "service":
d.Args(&z.Service)
case "terminator":
d.Args(&z.Terminator)
default:
return fmt.Errorf("unsupported configuration option `%s`", d.Val())
}
}
return nil
}
// CaddyModule returns the Caddy module information.
func (ZitiTransport) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.reverse_proxy.transport.ziti",
New: func() caddy.Module {
return new(ZitiTransport)
},
}
}
func (z *ZitiTransport) Provision(ctx caddy.Context) error {
log := ctx.Logger(z)
log.Info("ZitiTransport is loading")
z.caddy = ctx
// default proxy roundTripper
ctr := new(reverseproxy.HTTPTransport)
err := ctr.Provision(z.caddy)
if err != nil {
return err
}
opts := &ziti.Options{
RefreshInterval: 30 * time.Second,
}
z.ztx, err = zitiContexts.NewContextFromFileWithOpts(z.Identity, opts)
if err != nil {
return err
}
err = z.ztx.Authenticate()
if err != nil {
return err
}
if z.Service != "" {
options := &ziti.DialOptions{
ConnectTimeout: time.Duration(ctr.DialTimeout),
Identity: z.Terminator,
}
ctr.Transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return z.ztx.DialWithOptions(z.Service, options)
}
} else {
ctr.Transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return z.ztx.DialAddr(network, addr)
}
}
z.proxy = ctr
return err
}
var (
_ caddyfile.Unmarshaler = (*ZitiTransport)(nil)
_ http.RoundTripper = (*ZitiTransport)(nil)
_ caddy.CleanerUpper = (*ZitiTransport)(nil)
)