mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package adapters
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
|
|
obfs "github.com/Dreamacro/clash/component/simple-obfs"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/go-shadowsocks2/core"
|
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
|
)
|
|
|
|
type ShadowSocks struct {
|
|
*Base
|
|
server string
|
|
obfs string
|
|
obfsHost string
|
|
cipher core.Cipher
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
func (ss *ShadowSocks) Generator(metadata *C.Metadata) (net.Conn, error) {
|
|
c, err := net.DialTimeout("tcp", ss.server, tcpTimeout)
|
|
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)
|
|
_, err = c.Write(serializesSocksAddr(metadata))
|
|
return c, err
|
|
}
|
|
|
|
func (ss *ShadowSocks) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(map[string]string{
|
|
"type": ss.Type().String(),
|
|
})
|
|
}
|
|
|
|
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|
server := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
cipher := option.Cipher
|
|
password := option.Password
|
|
ciph, err := core.PickCipher(cipher, nil, password)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
|
|
}
|
|
|
|
obfs := option.Obfs
|
|
obfsHost := "bing.com"
|
|
if option.ObfsHost != "" {
|
|
obfsHost = option.ObfsHost
|
|
}
|
|
|
|
return &ShadowSocks{
|
|
Base: &Base{
|
|
name: option.Name,
|
|
tp: C.Shadowsocks,
|
|
},
|
|
server: server,
|
|
cipher: ciph,
|
|
obfs: obfs,
|
|
obfsHost: obfsHost,
|
|
}, nil
|
|
}
|
|
|
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
|
var buf [][]byte
|
|
aType := uint8(metadata.AddrType)
|
|
p, _ := strconv.Atoi(metadata.Port)
|
|
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
|
switch metadata.AddrType {
|
|
case socks.AtypDomainName:
|
|
len := uint8(len(metadata.Host))
|
|
host := []byte(metadata.Host)
|
|
buf = [][]byte{{aType, len}, host, port}
|
|
case socks.AtypIPv4:
|
|
host := metadata.IP.To4()
|
|
buf = [][]byte{{aType}, host, port}
|
|
case socks.AtypIPv6:
|
|
host := metadata.IP.To16()
|
|
buf = [][]byte{{aType}, host, port}
|
|
}
|
|
return bytes.Join(buf, nil)
|
|
}
|