/
tls.go
42 lines (34 loc) · 1.01 KB
/
tls.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
// Package tls is intended to provide a minimal set of compatible interfaces with the
// Go standard library's tls package.
package tls
import (
"strconv"
"strings"
"tinygo.org/x/drivers/net"
)
// Dial makes a TLS network connection. It tries to provide a mostly compatible interface
// to tls.Dial().
// Dial connects to the given network address.
func Dial(network, address string, config *Config) (*net.TCPSerialConn, error) {
raddr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
hostname := strings.Split(address, ":")[0]
sendport := strconv.Itoa(raddr.Port)
if sendport == "0" {
sendport = "443"
}
// disconnect any old socket
net.ActiveDevice.DisconnectSocket()
// connect new socket
err = net.ActiveDevice.ConnectSSLSocket(hostname, sendport)
if err != nil {
return nil, err
}
return net.NewTCPSerialConn(net.SerialConn{Adaptor: net.ActiveDevice}, nil, raddr), nil
}
// Config is a placeholder for future compatibility with
// tls.Config.
type Config struct {
}