Skip to content

Commit e5c3826

Browse files
committed
fix: remove GenerateTLSConfig from code #231
1 parent 512cacb commit e5c3826

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,31 @@ go srv.ListenAndServe(ctx, "ws", "127.0.0.1:5080")
105105
### TLS transports
106106
```go
107107
// TLS
108-
conf := GenerateTLSConfig(certFile, keyFile, rootPems)
108+
conf := generateTLSConfig(certFile, keyFile, rootPems)
109109
srv.ListenAndServeTLS(ctx, "tcp", "127.0.0.1:5061", conf)
110110
srv.ListenAndServeTLS(ctx, "ws", "127.0.0.1:5081", conf)
111+
112+
func generateTLSConfig(certFile string, keyFile string, rootPems []byte) (*tls.Config, error) {
113+
roots := x509.NewCertPool()
114+
if rootPems != nil {
115+
ok := roots.AppendCertsFromPEM(rootPems)
116+
if !ok {
117+
return nil, fmt.Errorf("failed to parse root certificate")
118+
}
119+
}
120+
121+
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
122+
if err != nil {
123+
return nil, fmt.Errorf("fail to load cert. err=%w", err)
124+
}
125+
126+
conf := &tls.Config{
127+
Certificates: []tls.Certificate{cert},
128+
RootCAs: roots,
129+
}
130+
131+
return conf, nil
132+
}
111133
```
112134

113135
### UAC first

example/proxysip/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"context"
5+
"crypto/tls"
6+
"crypto/x509"
57
"encoding/json"
68
"errors"
79
"flag"
@@ -303,3 +305,25 @@ func newCancelRequest(inviteRequest *sip.Request) *sip.Request {
303305
cancelReq.SetDestination(inviteRequest.Destination())
304306
return cancelReq
305307
}
308+
309+
func generateTLSConfig(certFile string, keyFile string, rootPems []byte) (*tls.Config, error) {
310+
roots := x509.NewCertPool()
311+
if rootPems != nil {
312+
ok := roots.AppendCertsFromPEM(rootPems)
313+
if !ok {
314+
return nil, fmt.Errorf("failed to parse root certificate")
315+
}
316+
}
317+
318+
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
319+
if err != nil {
320+
return nil, fmt.Errorf("fail to load cert. err=%w", err)
321+
}
322+
323+
conf := &tls.Config{
324+
Certificates: []tls.Certificate{cert},
325+
RootCAs: roots,
326+
}
327+
328+
return conf, nil
329+
}

0 commit comments

Comments
 (0)