2020-07-22 15:02:15 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
|
|
"github.com/Dreamacro/clash/component/ssr/obfs"
|
|
|
|
"github.com/Dreamacro/clash/component/ssr/protocol"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/go-shadowsocks2/core"
|
2021-02-15 06:32:03 +00:00
|
|
|
"github.com/Dreamacro/go-shadowsocks2/shadowaead"
|
2020-07-22 15:02:15 +00:00
|
|
|
"github.com/Dreamacro/go-shadowsocks2/shadowstream"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ShadowSocksR struct {
|
|
|
|
*Base
|
2021-02-15 06:32:03 +00:00
|
|
|
cipher core.Cipher
|
2020-07-22 15:02:15 +00:00
|
|
|
obfs obfs.Obfs
|
|
|
|
protocol protocol.Protocol
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShadowSocksROption struct {
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
Server string `proxy:"server"`
|
|
|
|
Port int `proxy:"port"`
|
|
|
|
Password string `proxy:"password"`
|
|
|
|
Cipher string `proxy:"cipher"`
|
|
|
|
Obfs string `proxy:"obfs"`
|
|
|
|
ObfsParam string `proxy:"obfs-param,omitempty"`
|
|
|
|
Protocol string `proxy:"protocol"`
|
|
|
|
ProtocolParam string `proxy:"protocol-param,omitempty"`
|
|
|
|
UDP bool `proxy:"udp,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssr *ShadowSocksR) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
2021-02-15 06:32:03 +00:00
|
|
|
c = ssr.obfs.StreamConn(c)
|
2020-07-22 15:02:15 +00:00
|
|
|
c = ssr.cipher.StreamConn(c)
|
2021-02-15 06:32:03 +00:00
|
|
|
var (
|
|
|
|
iv []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch conn := c.(type) {
|
|
|
|
case *shadowstream.Conn:
|
|
|
|
iv, err = conn.ObtainWriteIV()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *shadowaead.Conn:
|
2020-07-22 15:02:15 +00:00
|
|
|
return nil, fmt.Errorf("invalid connection type")
|
|
|
|
}
|
2021-02-15 06:32:03 +00:00
|
|
|
c = ssr.protocol.StreamConn(c, iv)
|
2020-07-22 15:02:15 +00:00
|
|
|
_, err = c.Write(serializesSocksAddr(metadata))
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
|
|
c, err := dialer.DialContext(ctx, "tcp", ssr.addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s connect error: %w", ssr.addr, err)
|
|
|
|
}
|
|
|
|
tcpKeepAlive(c)
|
|
|
|
|
|
|
|
c, err = ssr.StreamConn(c, metadata)
|
|
|
|
return NewConn(c, ssr), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssr *ShadowSocksR) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
|
|
|
pc, err := dialer.ListenPacket("udp", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := resolveUDPAddr("udp", ssr.addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pc = ssr.cipher.PacketConn(pc)
|
2021-02-15 06:32:03 +00:00
|
|
|
pc = ssr.protocol.PacketConn(pc)
|
2020-07-22 15:02:15 +00:00
|
|
|
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ssr), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ssr *ShadowSocksR) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]string{
|
|
|
|
"type": ssr.Type().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
|
|
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
|
|
cipher := option.Cipher
|
|
|
|
password := option.Password
|
|
|
|
coreCiph, err := core.PickCipher(cipher, nil, password)
|
|
|
|
if err != nil {
|
2021-02-15 06:32:03 +00:00
|
|
|
return nil, fmt.Errorf("ssr %s initialize error: %w", addr, err)
|
2020-07-22 15:02:15 +00:00
|
|
|
}
|
2021-02-15 06:32:03 +00:00
|
|
|
var (
|
|
|
|
ivSize int
|
|
|
|
key []byte
|
|
|
|
)
|
|
|
|
if option.Cipher == "dummy" {
|
|
|
|
ivSize = 0
|
|
|
|
key = core.Kdf(option.Password, 16)
|
|
|
|
} else {
|
|
|
|
ciph, ok := coreCiph.(*core.StreamCipher)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s is not dummy or a supported stream cipher in ssr", cipher)
|
|
|
|
}
|
|
|
|
ivSize = ciph.IVSize()
|
|
|
|
key = ciph.Key
|
2020-07-22 15:02:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 06:32:03 +00:00
|
|
|
obfs, obfsOverhead, err := obfs.PickObfs(option.Obfs, &obfs.Base{
|
|
|
|
Host: option.Server,
|
|
|
|
Port: option.Port,
|
|
|
|
Key: key,
|
|
|
|
IVSize: ivSize,
|
|
|
|
Param: option.ObfsParam,
|
2020-07-22 15:02:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ssr %s initialize obfs error: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol, err := protocol.PickProtocol(option.Protocol, &protocol.Base{
|
2021-02-15 06:32:03 +00:00
|
|
|
Key: key,
|
|
|
|
Overhead: obfsOverhead,
|
|
|
|
Param: option.ProtocolParam,
|
2020-07-22 15:02:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("ssr %s initialize protocol error: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ShadowSocksR{
|
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
addr: addr,
|
|
|
|
tp: C.ShadowsocksR,
|
|
|
|
udp: option.UDP,
|
|
|
|
},
|
2021-02-15 06:32:03 +00:00
|
|
|
cipher: coreCiph,
|
2020-07-22 15:02:15 +00:00
|
|
|
obfs: obfs,
|
|
|
|
protocol: protocol,
|
|
|
|
}, nil
|
|
|
|
}
|