-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
80 lines (65 loc) · 1.36 KB
/
client.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
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
package epp
import (
"context"
"crypto/tls"
"net"
"github.com/domainr/epp2/internal/config"
"github.com/domainr/epp2/protocol"
"github.com/domainr/epp2/schema/epp"
)
type Client interface {
// Login(username, password, newPassword string) error
// Logout() error
Close() error
}
type client struct {
conn net.Conn
client protocol.Client
greeting epp.Body
}
func Dial(network, addr string, opts ...Options) (Client, error) {
var cfg config.Config
cfg.Join(opts...)
ctx := cfg.Context
if ctx == nil {
ctx = context.Background()
}
dialer := cfg.Dialer
if dialer == nil {
dialer = &net.Dialer{
KeepAlive: cfg.KeepAlive,
}
}
conn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
if cfg.TLSConfig != nil {
conn = tls.Client(conn, cfg.TLSConfig)
}
return connect(conn, cfg)
}
func Connect(conn net.Conn, opts ...Options) (Client, error) {
var cfg config.Config
cfg.Join(opts...)
return connect(conn, cfg)
}
func connect(conn net.Conn, cfg config.Config) (Client, error) {
ctx := cfg.Context
if ctx == nil {
ctx = context.Background()
}
c, greeting, err := protocol.Connect(ctx, conn)
if err != nil {
return nil, err
}
return &client{
conn: conn,
client: c,
greeting: greeting,
}, nil
}
func (c *client) Close() error {
// TODO: handle pending transactions
return c.conn.Close()
}