Skip to content

Commit 2850890

Browse files
committed
dep: remove pkg/errors as dependency
1 parent 92e5f12 commit 2850890

26 files changed

+154
-181
lines changed

acme/acme.go

Lines changed: 34 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()
@@ -324,7 +325,7 @@ func (c *CertManager) RequestCertificate() error {
324325
c.certPKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
325326
if err != nil {
326327
c.certPKeyMu.Unlock()
327-
return errors.Wrap(err, "generating certificate private key")
328+
return fmt.Errorf("generating certificate private key: %w", err)
328329
}
329330
c.hasCertPKey = true
330331
}
@@ -340,29 +341,29 @@ func (c *CertManager) RequestCertificate() error {
340341
c.certPKeyMu.Unlock()
341342

342343
if err != nil {
343-
return errors.Wrap(err, "generating csr")
344+
return fmt.Errorf("generating csr: %w", err)
344345
}
345346
csr, err := x509.ParseCertificateRequest(csrDer)
346347
if err != nil {
347-
return errors.Wrap(err, "parsing csr")
348+
return fmt.Errorf("parsing csr: %w", err)
348349
}
349350

350351
var pKey []byte
351352
pKey, err = c.ExportPrivateKey()
352353
if err != nil {
353-
return errors.Wrap(err, "encoding private key to pem")
354+
return fmt.Errorf("encoding private key to pem: %w", err)
354355
}
355356

356357
// now we can create a order
357358
o, err := c.client.NewOrderDomains(c.account, names...)
358359
if err != nil {
359-
return errors.Wrap(err, "creating order")
360+
return fmt.Errorf("creating order: %w", err)
360361
}
361362

362363
for _, authURL := range o.Authorizations {
363364
auth, err := c.client.FetchAuthorization(c.account, authURL)
364365
if err != nil {
365-
return errors.Wrap(err, "fetching authorization")
366+
return fmt.Errorf("fetching authoriztion: %w", err)
366367
}
367368
chal, ok := auth.ChallengeMap[acme.ChallengeTypeDNS01]
368369
if !ok {
@@ -377,15 +378,15 @@ func (c *CertManager) RequestCertificate() error {
377378

378379
ok, err = c.config.DNSProvider.Update(ctx, host, "TXT", txt)
379380
if err != nil {
380-
return errors.Wrap(err, "updating dns record")
381+
return fmt.Errorf("updating dns record: %w", err)
381382
}
382383
if !ok {
383384
return errors.New("dns update failed")
384385
}
385386

386387
chal, err = c.client.UpdateChallenge(c.account, chal)
387388
if err != nil {
388-
return errors.Wrap(err, "updating challenge")
389+
return fmt.Errorf("updating challenge: %w", err)
389390
}
390391
return nil
391392
}()
@@ -399,12 +400,12 @@ func (c *CertManager) RequestCertificate() error {
399400

400401
o, err = c.client.FinalizeOrder(c.account, o, csr)
401402
if err != nil {
402-
return errors.Wrap(err, "finalizing order")
403+
return fmt.Errorf("finalizing order: %w", err)
403404
}
404405

405406
certs, err := c.client.FetchCertificates(c.account, o.Certificate)
406407
if err != nil {
407-
return errors.Wrap(err, "fetching certificates")
408+
return fmt.Errorf("fetching certificates: %w", err)
408409
}
409410

410411
var pemData []string
@@ -421,7 +422,7 @@ func (c *CertManager) RequestCertificate() error {
421422
}
422423

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

427428
return nil
@@ -439,7 +440,7 @@ func (c *CertManager) GetCertificatesFunc(chi *tls.ClientHelloInfo) (*tls.Certif
439440
func keyToPEM(pKey *ecdsa.PrivateKey) ([]byte, error) {
440441
enc, err := x509.MarshalECPrivateKey(pKey)
441442
if err != nil {
442-
return nil, errors.Wrap(err, "marshaling private key to pem")
443+
return nil, fmt.Errorf("marshalling private key to pem: %w", err)
443444
}
444445
return pem.EncodeToMemory(&pem.Block{
445446
Type: "EC PRIVATE KEY",
@@ -451,7 +452,7 @@ func pemToKey(b []byte) (*ecdsa.PrivateKey, error) {
451452
blk, _ := pem.Decode(b)
452453
pKey, err := x509.ParseECPrivateKey(blk.Bytes)
453454
if err != nil {
454-
return nil, errors.Wrap(err, "parsing private key from pem")
455+
return nil, fmt.Errorf("parsing private key from pem: %w", err)
455456
}
456457
return pKey, nil
457458
}

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

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

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ require (
2525
github.com/miekg/dns v1.1.45
2626
github.com/mitchellh/mapstructure v1.4.3 // indirect
2727
github.com/onsi/ginkgo v1.16.5 // indirect
28-
github.com/pkg/errors v0.9.1
2928
github.com/prometheus/client_golang v1.12.1
3029
github.com/sethvargo/go-diceware v0.2.1
3130
github.com/urfave/cli/v2 v2.3.0

0 commit comments

Comments
 (0)