-
Notifications
You must be signed in to change notification settings - Fork 41
/
conn.go
70 lines (56 loc) · 1.35 KB
/
conn.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
package tun
import (
"context"
"errors"
"io"
"net"
"time"
mdata "github.com/go-gost/core/metadata"
)
type conn struct {
ifce io.ReadWriteCloser
laddr net.Addr
raddr net.Addr
cancel context.CancelFunc
}
func (c *conn) Read(b []byte) (n int, err error) {
return c.ifce.Read(b)
}
func (c *conn) Write(b []byte) (n int, err error) {
return c.ifce.Write(b)
}
func (c *conn) LocalAddr() net.Addr {
return c.laddr
}
func (c *conn) RemoteAddr() net.Addr {
return c.raddr
}
func (c *conn) SetDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (c *conn) SetReadDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (c *conn) SetWriteDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (c *conn) Close() (err error) {
if c.cancel != nil {
c.cancel()
}
return c.ifce.Close()
}
type metadataConn struct {
net.Conn
md mdata.Metadata
}
// Metadata implements metadata.Metadatable interface.
func (c *metadataConn) Metadata() mdata.Metadata {
return c.md
}
func withMetadata(md mdata.Metadata, c net.Conn) net.Conn {
return &metadataConn{
Conn: c,
md: md,
}
}