forked from p4gefau1t/trojan-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
49 lines (41 loc) · 1.13 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
package shadowsocks
import (
"context"
"github.com/shadowsocks/go-shadowsocks2/core"
"github.com/faireal/trojan-go/common"
"github.com/faireal/trojan-go/config"
"github.com/faireal/trojan-go/log"
"github.com/faireal/trojan-go/tunnel"
)
type Client struct {
underlay tunnel.Client
core.Cipher
}
func (c *Client) DialConn(address *tunnel.Address, tunnel tunnel.Tunnel) (tunnel.Conn, error) {
conn, err := c.underlay.DialConn(address, &Tunnel{})
if err != nil {
return nil, err
}
return &Conn{
aeadConn: c.Cipher.StreamConn(conn),
Conn: conn,
}, nil
}
func (c *Client) DialPacket(tunnel tunnel.Tunnel) (tunnel.PacketConn, error) {
panic("not supported")
}
func (c *Client) Close() error {
return c.underlay.Close()
}
func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) {
cfg := config.FromContext(ctx, Name).(*Config)
cipher, err := core.PickCipher(cfg.Shadowsocks.Method, nil, cfg.Shadowsocks.Password)
if err != nil {
return nil, common.NewError("invalid shadowsocks cipher").Base(err)
}
log.Debug("shadowsocks client created")
return &Client{
underlay: underlay,
Cipher: cipher,
}, nil
}