This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
forked from shadowsocks/shadowsocks-go
/
proxy.go
84 lines (70 loc) · 1.46 KB
/
proxy.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
81
82
83
84
package shadowsocks
import (
"errors"
"strings"
"fmt"
"net"
"time"
)
type Dialer struct {
cipher *Cipher
server string
support_udp bool
}
type ProxyConn struct {
*Conn
raddr *ProxyAddr
}
type ProxyAddr struct {
network string
address string
}
var ErrNilCipher = errors.New("cipher can't be nil.")
func NewDialer(server string, cipher *Cipher) (dialer *Dialer, err error) {
// Currently shadowsocks-go do not support UDP
if cipher == nil {
return nil, ErrNilCipher
}
return &Dialer {
cipher: cipher,
server: server,
support_udp: false,
}, nil
}
func (d *Dialer) Dial(network, addr string) (c net.Conn, err error) {
if strings.HasPrefix(network, "tcp") {
conn, err := Dial(addr, d.server, d.cipher.Copy())
if err != nil {
return nil, err
}
return &ProxyConn {
Conn: conn,
raddr: &ProxyAddr {
network: network,
address: addr,
},
}, nil
}
return nil, fmt.Errorf("unsupported connection type: %s", network)
}
func (c *ProxyConn) LocalAddr() net.Addr {
return c.Conn.LocalAddr()
}
func (c *ProxyConn) RemoteAddr() net.Addr {
return c.raddr
}
func (c *ProxyConn) SetDeadline(t time.Time) error {
return c.Conn.SetDeadline(t)
}
func (c *ProxyConn) SetReadDeadline(t time.Time) error {
return c.Conn.SetReadDeadline(t)
}
func (c *ProxyConn) SetWriteDeadline(t time.Time) error {
return c.Conn.SetWriteDeadline(t)
}
func (a *ProxyAddr) Network() string {
return a.network
}
func (a *ProxyAddr) String() string {
return a.address
}