Skip to content

Commit cfc8ec7

Browse files
authored
Merge pull request #4 from zllovesuki/xor
2 parents eda4f58 + 2850890 commit cfc8ec7

29 files changed

+306
-357
lines changed

acme/acme.go

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"crypto/x509/pkix"
1111
"encoding/json"
1212
"encoding/pem"
13+
"errors"
14+
"fmt"
1315
"os"
1416
"path"
1517
"strings"
1618
"sync"
1719
"time"
1820

1921
"github.com/eggsampler/acme/v3"
20-
"github.com/pkg/errors"
2122
)
2223

2324
type CertManager struct {
@@ -64,7 +65,7 @@ func New(conf Config) (*CertManager, error) {
6465
}
6566
client, err := acme.NewClient(conf.Directory, acme.WithHTTPTimeout(time.Second*10))
6667
if err != nil {
67-
return nil, errors.Wrap(err, "initializing acme client")
68+
return nil, fmt.Errorf("initializing acme client: %w", err)
6869
}
6970
c := &CertManager{
7071
client: client,
@@ -84,17 +85,17 @@ func (c *CertManager) CreateAccount() error {
8485
var err error
8586
c.accKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
8687
if err != nil {
87-
return errors.Wrap(err, "generating new account private key")
88+
return fmt.Errorf("generating new account private key: %w", err)
8889
}
8990
c.account, err = c.client.NewAccountOptions(c.accKey, acme.NewAcctOptAgreeTOS(), acme.NewAcctOptWithContacts(c.config.Contact))
9091
if err != nil {
91-
return errors.Wrap(err, "creating account with CA")
92+
return fmt.Errorf("creating account with CA: %w", err)
9293
}
9394
c.hasAcc = true
9495

9596
pem, err := keyToPEM(c.accKey)
9697
if err != nil {
97-
return errors.Wrap(err, "converting pkey to pem")
98+
return fmt.Errorf("converting pkey to pem: %w", err)
9899
}
99100
af := AccountFile{
100101
PrivateKey: string(pem),
@@ -106,25 +107,25 @@ func (c *CertManager) CreateAccount() error {
106107
func (c *CertManager) persistAccount(af AccountFile) error {
107108
w, err := os.Create(path.Join(c.config.DataDir, "accounts.json"))
108109
if err != nil {
109-
return errors.Wrap(err, "opening accounts.json for writing")
110+
return fmt.Errorf("opening accounts.json for writing: %w", err)
110111
}
111112
defer w.Close()
112113
err = json.NewEncoder(w).Encode(&af)
113114
if err != nil {
114-
return errors.Wrap(err, "writing to accounts.json")
115+
return fmt.Errorf("writing to accounts.json: %w", err)
115116
}
116117
return nil
117118
}
118119

119120
func (c *CertManager) persisCerts(bundle Bundle) error {
120121
w, err := os.Create(path.Join(c.config.DataDir, "bundle.json"))
121122
if err != nil {
122-
return errors.Wrap(err, "opening bundle.json for writing")
123+
return fmt.Errorf("opening bundle.json for writing: %w", err)
123124
}
124125
defer w.Close()
125126
err = json.NewEncoder(w).Encode(&bundle)
126127
if err != nil {
127-
return errors.Wrap(err, "writing to bundle.json")
128+
return fmt.Errorf("writing to bundle.json: %w", err)
128129
}
129130
return nil
130131
}
@@ -138,7 +139,7 @@ func (c *CertManager) ExportAccount() (*AccountFile, error) {
138139
}
139140
pem, err := keyToPEM(c.accKey)
140141
if err != nil {
141-
return nil, errors.Wrap(err, "converting pkey to pem")
142+
return nil, fmt.Errorf("converting pkey to pem: %w", err)
142143
}
143144
af := AccountFile{
144145
PrivateKey: string(pem),
@@ -153,14 +154,14 @@ func (c *CertManager) LoadAccountFromFile() error {
153154
if errors.Is(err, os.ErrNotExist) {
154155
return ErrNoAccount
155156
}
156-
return errors.Wrap(err, "loading accounts.json")
157+
return fmt.Errorf("loading accounts.json: %w", err)
157158
}
158159
defer f.Close()
159160

160161
var af AccountFile
161162
err = json.NewDecoder(f).Decode(&af)
162163
if err != nil {
163-
return errors.Wrap(err, "decoding accounts.json")
164+
return fmt.Errorf("decoding accounts.json: %w", err)
164165
}
165166

166167
return c.ImportAccount(af, false)
@@ -172,14 +173,14 @@ func (c *CertManager) LoadBundleFromFile() error {
172173
if errors.Is(err, os.ErrNotExist) {
173174
return ErrNoCert
174175
}
175-
return errors.Wrap(err, "loading bundle.json")
176+
return fmt.Errorf("loading bundle.json: %w", err)
176177
}
177178
defer f.Close()
178179

179180
var bundle Bundle
180181
err = json.NewDecoder(f).Decode(&bundle)
181182
if err != nil {
182-
return errors.Wrap(err, "decoding bundle.json")
183+
return fmt.Errorf("decoding bundle.json: %w", err)
183184
}
184185

185186
return c.ImportBundle(bundle, false)
@@ -195,15 +196,15 @@ func (c *CertManager) ImportAccount(af AccountFile, persist bool) error {
195196

196197
pKey, err := pemToKey([]byte(af.PrivateKey))
197198
if err != nil {
198-
return errors.Wrap(err, "converting pem to pkey")
199+
return fmt.Errorf("converting pem to pkey: %w", err)
199200
}
200201
c.account, err = c.client.UpdateAccount(acme.Account{
201202
PrivateKey: pKey,
202203
URL: af.URL,
203204
}, c.config.Contact)
204205

205206
if err != nil {
206-
return errors.Wrap(err, "reloading accounts")
207+
return fmt.Errorf("reloading accounts: %w", err)
207208
}
208209

209210
c.hasAcc = true
@@ -226,7 +227,7 @@ func (c *CertManager) ImportPrivateKey(keyPem string) error {
226227
var err error
227228
c.certPKey, err = pemToKey([]byte(keyPem))
228229
if err != nil {
229-
return errors.Wrap(err, "decoding private key from pem")
230+
return fmt.Errorf("decoding private key from pem: %w", err)
230231
}
231232
return nil
232233
}
@@ -241,22 +242,22 @@ func (c *CertManager) ExportPrivateKey() ([]byte, error) {
241242

242243
pKey, err := keyToPEM(c.certPKey)
243244
if err != nil {
244-
return nil, errors.Wrap(err, "encoding private key to pem")
245+
return nil, fmt.Errorf("encoding private key to pem: %w", err)
245246
}
246247
return pKey, nil
247248
}
248249

249250
func (c *CertManager) ImportBundle(bundle Bundle, persist bool) error {
250251
pKey, err := pemToKey([]byte(bundle.PrivateKey))
251252
if err != nil {
252-
return errors.Wrap(err, "decoding private key")
253+
return fmt.Errorf("decoding private key: %w", err)
253254
}
254255
cert, err := tls.X509KeyPair(
255256
[]byte(strings.Join(bundle.Chain, "\n")),
256257
[]byte(bundle.PrivateKey),
257258
)
258259
if err != nil {
259-
return errors.Wrap(err, "generating x509 key pair")
260+
return fmt.Errorf("generating x509 key pair: %w", err)
260261
}
261262
c.certPKeyMu.Lock()
262263
c.certMu.Lock()
@@ -300,6 +301,7 @@ func (c *CertManager) RequestCertificate() error {
300301
common := c.config.RootZone
301302
apex := strings.TrimSuffix(c.config.Domain, c.config.RootZone)
302303
apex = strings.TrimSuffix(apex, ".")
304+
// TODO: the following routine is not robust
303305
switch {
304306
case apex == "":
305307
case apex[0] == 0x2a: // the "*" character
@@ -323,7 +325,7 @@ func (c *CertManager) RequestCertificate() error {
323325
c.certPKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
324326
if err != nil {
325327
c.certPKeyMu.Unlock()
326-
return errors.Wrap(err, "generating certificate private key")
328+
return fmt.Errorf("generating certificate private key: %w", err)
327329
}
328330
c.hasCertPKey = true
329331
}
@@ -339,29 +341,29 @@ func (c *CertManager) RequestCertificate() error {
339341
c.certPKeyMu.Unlock()
340342

341343
if err != nil {
342-
return errors.Wrap(err, "generating csr")
344+
return fmt.Errorf("generating csr: %w", err)
343345
}
344346
csr, err := x509.ParseCertificateRequest(csrDer)
345347
if err != nil {
346-
return errors.Wrap(err, "parsing csr")
348+
return fmt.Errorf("parsing csr: %w", err)
347349
}
348350

349351
var pKey []byte
350352
pKey, err = c.ExportPrivateKey()
351353
if err != nil {
352-
return errors.Wrap(err, "encoding private key to pem")
354+
return fmt.Errorf("encoding private key to pem: %w", err)
353355
}
354356

355357
// now we can create a order
356358
o, err := c.client.NewOrderDomains(c.account, names...)
357359
if err != nil {
358-
return errors.Wrap(err, "creating order")
360+
return fmt.Errorf("creating order: %w", err)
359361
}
360362

361363
for _, authURL := range o.Authorizations {
362364
auth, err := c.client.FetchAuthorization(c.account, authURL)
363365
if err != nil {
364-
return errors.Wrap(err, "fetching authorization")
366+
return fmt.Errorf("fetching authoriztion: %w", err)
365367
}
366368
chal, ok := auth.ChallengeMap[acme.ChallengeTypeDNS01]
367369
if !ok {
@@ -376,15 +378,15 @@ func (c *CertManager) RequestCertificate() error {
376378

377379
ok, err = c.config.DNSProvider.Update(ctx, host, "TXT", txt)
378380
if err != nil {
379-
return errors.Wrap(err, "updating dns record")
381+
return fmt.Errorf("updating dns record: %w", err)
380382
}
381383
if !ok {
382384
return errors.New("dns update failed")
383385
}
384386

385387
chal, err = c.client.UpdateChallenge(c.account, chal)
386388
if err != nil {
387-
return errors.Wrap(err, "updating challenge")
389+
return fmt.Errorf("updating challenge: %w", err)
388390
}
389391
return nil
390392
}()
@@ -398,12 +400,12 @@ func (c *CertManager) RequestCertificate() error {
398400

399401
o, err = c.client.FinalizeOrder(c.account, o, csr)
400402
if err != nil {
401-
return errors.Wrap(err, "finalizing order")
403+
return fmt.Errorf("finalizing order: %w", err)
402404
}
403405

404406
certs, err := c.client.FetchCertificates(c.account, o.Certificate)
405407
if err != nil {
406-
return errors.Wrap(err, "fetching certificates")
408+
return fmt.Errorf("fetching certificates: %w", err)
407409
}
408410

409411
var pemData []string
@@ -420,7 +422,7 @@ func (c *CertManager) RequestCertificate() error {
420422
}
421423

422424
if err := c.ImportBundle(bundle, true); err != nil {
423-
return errors.Wrap(err, "re-importing exported certificate")
425+
return fmt.Errorf("re-importing exported certificate: %w", err)
424426
}
425427

426428
return nil
@@ -438,7 +440,7 @@ func (c *CertManager) GetCertificatesFunc(chi *tls.ClientHelloInfo) (*tls.Certif
438440
func keyToPEM(pKey *ecdsa.PrivateKey) ([]byte, error) {
439441
enc, err := x509.MarshalECPrivateKey(pKey)
440442
if err != nil {
441-
return nil, errors.Wrap(err, "marshaling private key to pem")
443+
return nil, fmt.Errorf("marshalling private key to pem: %w", err)
442444
}
443445
return pem.EncodeToMemory(&pem.Block{
444446
Type: "EC PRIVATE KEY",
@@ -450,7 +452,7 @@ func pemToKey(b []byte) (*ecdsa.PrivateKey, error) {
450452
blk, _ := pem.Decode(b)
451453
pKey, err := x509.ParseECPrivateKey(blk.Bytes)
452454
if err != nil {
453-
return nil, errors.Wrap(err, "parsing private key from pem")
455+
return nil, fmt.Errorf("parsing private key from pem: %w", err)
454456
}
455457
return pKey, nil
456458
}

client/forward.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414

1515
type ForwardOpts struct {
1616
Logger *zap.Logger
17+
Sigs chan os.Signal
1718
URL string
1819
Addr string
1920
Debug bool
20-
Sigs chan os.Signal
21+
_ [7]byte
22+
_ [8]byte
2123
}
2224

2325
func Forward(ctx context.Context, opts ForwardOpts) {

cmd/server/authz.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package main
22

33
import (
44
"crypto/tls"
5+
"errors"
6+
"fmt"
57
"strings"
6-
7-
"github.com/pkg/errors"
88
)
99

1010
func checkClientSNI(domain string) func(tls.ConnectionState) error {
1111
return func(cs tls.ConnectionState) error {
1212
if !strings.HasSuffix(cs.ServerName, domain) {
13-
return errors.Errorf("unauthorized domain name: %s", cs.ServerName)
13+
return fmt.Errorf("unauthorized domain name: %s", cs.ServerName)
1414
}
1515
return nil
1616
}
@@ -26,7 +26,7 @@ func checkPeerSAN(required string) func(tls.ConnectionState) error {
2626
found = found || name == required
2727
}
2828
if !found {
29-
return errors.Errorf("%s must be present in SANs", required)
29+
return fmt.Errorf("%s must be present in SANs", required)
3030
}
3131
return nil
3232
}

cmd/server/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package main
22

33
import (
4+
"fmt"
5+
46
"github.com/zllovesuki/t/acme"
57
"github.com/zllovesuki/t/multiplexer/protocol"
68
"github.com/zllovesuki/t/provider"
79
"github.com/zllovesuki/t/server"
810

911
"github.com/gookit/config/v2"
1012
"github.com/gookit/config/v2/yaml"
11-
"github.com/pkg/errors"
1213
)
1314

1415
type WebConfig struct {
@@ -39,7 +40,7 @@ func getConfig(path string) (*ConfigBundle, error) {
3940

4041
err := cfg.LoadFiles(path)
4142
if err != nil {
42-
return nil, errors.Wrap(err, "loading config file")
43+
return nil, fmt.Errorf("loading config file: %w", err)
4344
}
4445

4546
var bundle ConfigBundle

example.config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ acme:
3232
zone: example.com
3333
tsigKey: sign.
3434
tsigSecret: yoursecrethere==
35-
tsigAlgo: hmac-sha256
35+
tsigAlgo: hmac-sha256.
3636
nameserver: dns.example.com:53
3737

3838
network:

gateway/gateway.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/zllovesuki/t/server"
1616
"github.com/zllovesuki/t/shared"
1717

18-
"github.com/pkg/errors"
1918
"go.uber.org/zap"
2019
)
2120

@@ -37,11 +36,11 @@ type Gateway struct {
3736
func New(conf GatewayConfig) (*Gateway, error) {
3837
md, err := template.New("content").Parse(tmpl)
3938
if err != nil {
40-
return nil, errors.Wrap(err, "reading markdown for apex template")
39+
return nil, fmt.Errorf("reading markdown for apex template: %w", err)
4140
}
4241
idx, err := template.New("index").Parse(index)
4342
if err != nil {
44-
return nil, errors.Wrap(err, "reading index for apex template")
43+
return nil, fmt.Errorf("reading index for apex template: %w", err)
4544
}
4645
d := conf.RootDomain
4746
if conf.GatewayPort != 443 {

gateway/tunnel_http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gateway
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net"
78
"net/http"
@@ -13,7 +14,6 @@ import (
1314
"github.com/zllovesuki/t/profiler"
1415
"github.com/zllovesuki/t/shared"
1516

16-
"github.com/pkg/errors"
1717
"go.uber.org/zap"
1818
)
1919

0 commit comments

Comments
 (0)