Clash.Meta/adapters/outbound/shadowsocks.go

105 lines
2.4 KiB
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package adapters
import (
"bytes"
2018-11-21 13:47:46 +08:00
"encoding/json"
2018-06-10 22:50:03 +08:00
"fmt"
"net"
"strconv"
2018-12-22 23:56:42 +08:00
obfs "github.com/Dreamacro/clash/component/simple-obfs"
2018-06-10 22:50:03 +08:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/go-shadowsocks2/core"
"github.com/Dreamacro/go-shadowsocks2/socks"
2018-06-10 22:50:03 +08:00
)
type ShadowSocks struct {
2018-12-22 23:56:42 +08:00
*Base
server string
obfs string
obfsHost string
cipher core.Cipher
2018-06-10 22:50:03 +08:00
}
type ShadowSocksOption 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,omitempty"`
ObfsHost string `proxy:"obfs-host,omitempty"`
}
2018-12-22 23:56:42 +08:00
func (ss *ShadowSocks) Generator(metadata *C.Metadata) (net.Conn, error) {
2018-10-22 21:14:22 +08:00
c, err := net.DialTimeout("tcp", ss.server, tcpTimeout)
2018-06-10 22:50:03 +08:00
if err != nil {
return nil, fmt.Errorf("%s connect error", ss.server)
}
tcpKeepAlive(c)
switch ss.obfs {
case "tls":
c = obfs.NewTLSObfs(c, ss.obfsHost)
case "http":
_, port, _ := net.SplitHostPort(ss.server)
c = obfs.NewHTTPObfs(c, ss.obfsHost, port)
}
c = ss.cipher.StreamConn(c)
2018-09-30 12:25:52 +08:00
_, err = c.Write(serializesSocksAddr(metadata))
2018-12-22 23:56:42 +08:00
return c, err
2018-06-10 22:50:03 +08:00
}
2018-11-21 13:47:46 +08:00
func (ss *ShadowSocks) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"type": ss.Type().String(),
})
}
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
2018-11-04 21:12:16 +08:00
server := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
cipher := option.Cipher
password := option.Password
2018-09-21 11:33:29 +08:00
ciph, err := core.PickCipher(cipher, nil, password)
if err != nil {
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
2018-06-10 22:50:03 +08:00
}
obfs := option.Obfs
obfsHost := "bing.com"
if option.ObfsHost != "" {
obfsHost = option.ObfsHost
}
return &ShadowSocks{
2018-12-22 23:56:42 +08:00
Base: &Base{
name: option.Name,
tp: C.Shadowsocks,
},
server: server,
cipher: ciph,
obfs: obfs,
obfsHost: obfsHost,
}, nil
2018-06-10 22:50:03 +08:00
}
2018-09-30 12:25:52 +08:00
func serializesSocksAddr(metadata *C.Metadata) []byte {
2018-06-10 22:50:03 +08:00
var buf [][]byte
2018-09-30 12:25:52 +08:00
aType := uint8(metadata.AddrType)
p, _ := strconv.Atoi(metadata.Port)
2018-06-10 22:50:03 +08:00
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
2018-09-30 12:25:52 +08:00
switch metadata.AddrType {
2018-06-10 22:50:03 +08:00
case socks.AtypDomainName:
2018-09-30 12:25:52 +08:00
len := uint8(len(metadata.Host))
host := []byte(metadata.Host)
2018-06-22 23:56:41 +08:00
buf = [][]byte{{aType, len}, host, port}
2018-06-10 22:50:03 +08:00
case socks.AtypIPv4:
2018-09-30 12:25:52 +08:00
host := metadata.IP.To4()
2018-06-22 23:56:41 +08:00
buf = [][]byte{{aType}, host, port}
2018-06-10 22:50:03 +08:00
case socks.AtypIPv6:
2018-09-30 12:25:52 +08:00
host := metadata.IP.To16()
2018-06-22 23:56:41 +08:00
buf = [][]byte{{aType}, host, port}
2018-06-10 22:50:03 +08:00
}
2018-09-21 11:33:29 +08:00
return bytes.Join(buf, nil)
2018-06-10 22:50:03 +08:00
}