Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
198 lines (171 loc) · 5.22 KB
/
Copy pathexample_test.go
File metadata and controls
198 lines (171 loc) · 5.22 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2025 Sylvain Müller. All rights reserved.
// Mount of this source code is governed by a Apache-2.0 license that can be found
// at https://github.com/stdapi/edge/blob/main/LICENSE.txt.
package edge_test
import (
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/stdapi/edge"
)
// The handler signature selects the kind of traffic served, so no type argument is needed.
func ExampleProxy_Add() {
ca, err := edge.GenerateCA()
if err != nil {
log.Fatal(err)
}
p := edge.Must(edge.WithCA(ca))
// One request, on a hostname and a path.
p.MustAdd("api.stripe.com/v1/charges/{id}", func(c *edge.HTTPCtx) (*http.Response, error) {
if c.Param("id") == "ch_denied" {
return edge.Text(http.StatusPaymentRequired, "declined"), nil
}
return c.Forward()
}, edge.WithMethods[edge.HTTP](http.MethodGet, http.MethodPost))
// The catch-all is a route, not a hidden default: what passes through is written down.
p.MustAdd("api.stripe.com/*{any}", func(c *edge.HTTPCtx) (*http.Response, error) {
return c.Forward()
})
fmt.Println(p.Len())
// Output: 2
}
// A kind that routes whole connections is keyed on an authority alone, with no path.
func ExampleProxy_Add_stream() {
ca, _ := edge.GenerateCA()
p := edge.Must(edge.WithCA(ca))
p.MustAdd[edge.TCP]("smtp.test:465", func(c *edge.TCPCtx) error {
// TLS is already terminated, so the bytes are in the clear and the upstream
// handshake mirrors what the client asked for.
log.Printf("smtp over %q", c.ALPN())
up, err := c.Dial()
if err != nil {
return err
}
// Shaping is a wrapper on one side, so it applies to one direction alone.
_, _, err = edge.Relay(c.Client(), edge.Throttled(up, 256<<10))
return err
})
fmt.Println(p.Admits(edge.Target{Scheme: "https", Host: "smtp.test", Port: "465"}, edge.KindTCP))
// Output: true
}
func ExampleTCPCtx_StartTLS() {
ca, _ := edge.GenerateCA()
p := edge.Must(edge.WithCA(ca))
// Postgres starts in the clear: the client sends an 8-byte SSLRequest and the server
// answers 'S' before the handshake. The handler relays that exchange, then terminates.
p.MustAdd[edge.TCP]("db.test:5432", func(c *edge.TCPCtx) error {
up, err := c.Dial()
if err != nil {
return err
}
var req [8]byte
if _, err := io.ReadFull(c.Client(), req[:]); err != nil {
return err
}
if _, err := up.Write(req[:]); err != nil {
return err
}
var answer [1]byte
if _, err := io.ReadFull(up, answer[:]); err != nil {
return err
}
if _, err := c.Client().Write(answer[:]); err != nil {
return err
}
if answer[0] == 'S' {
if err := c.StartTLS(); err != nil {
return err
}
}
return c.Relay()
})
fmt.Println(p.Admits(edge.Target{Scheme: "http", Host: "db.test", Port: "5432"}, edge.KindTCP))
// Output: true
}
// Datagrams have units to lose, which is what makes drop and delay expressible.
func ExampleProxy_Add_datagram() {
p := edge.Must()
p.MustAdd("[0.0.0.0/0]:53", func(d *edge.DatagramCtx) error {
if d.Seq()%50 == 0 {
return d.Drop()
}
return d.Forward(edge.Delay(20 * time.Millisecond))
})
fmt.Println(p.Len())
// Output: 1
}
// A gRPC entry and an HTTP entry can name the same path. The request tells them apart.
func ExampleProxy_Add_grpc() {
ca, _ := edge.GenerateCA()
p := edge.Must(edge.WithCA(ca))
var calls int
p.MustAdd("api.test/billing.v1.Billing/Charge", func(c *edge.GRPCCtx) error {
calls++
if calls <= 2 {
return c.Fail(edge.CodeUnavailable, "upstream flaky")
}
return c.Forward()
})
fmt.Println(p.Admits(edge.Target{Scheme: "https", Host: "api.test", Port: "443"}, edge.KindHTTP))
// Output: true
}
// Past the 101 a connection is no longer HTTP, so its unit is the frame.
func ExampleProxy_Add_webSocket() {
ca, _ := edge.GenerateCA()
p := edge.Must(edge.WithCA(ca))
p.MustAdd("chat.test/ws", func(c *edge.WebSocketCtx) error {
src := edge.MergeFrames(c.Outbound(), c.Inbound())
for {
f, err := src.ReadFrame()
if err != nil {
return nil
}
// The ping is swallowed rather than relayed, to see how the client's
// keepalive reacts.
if !f.Inbound() && f.Opcode() == edge.OpPing {
continue
}
if err := f.Forward(); err != nil {
return err
}
}
})
fmt.Println(p.Len())
// Output: 1
}
// Entries change while traffic flows, and every unit is routed on the tree as it stands.
func ExampleProxy_Updates() {
ca, _ := edge.GenerateCA()
p := edge.Must(edge.WithCA(ca))
p.MustAdd("api.test/v1", func(c *edge.HTTPCtx) (*http.Response, error) { return c.Forward() })
err := p.Updates(func(txn *edge.Txn) error {
if _, err := txn.Delete[edge.HTTP]("api.test/v1"); err != nil {
return err
}
_, err := txn.Add("api.test/v1", func(c *edge.HTTPCtx) (*http.Response, error) {
return edge.Text(http.StatusTeapot, "closed"), nil
})
return err
})
if err != nil {
log.Fatal(err)
}
fmt.Println(p.Len())
// Output: 1
}
// Removing an entry governs the next request on connections that are already open. Closing is
// for ending a connection on its own terms, revoking an identity being the usual one.
func ExampleProxy_Sessions() {
ca, _ := edge.GenerateCA()
p := edge.Must(edge.WithCA(ca))
revoked := "alice"
for s := range p.Sessions() {
if s.Identity() == revoked {
_ = s.Close()
}
}
fmt.Println("swept")
// Output: swept
}