mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
取消Client的LocalAddr,改为 LocalTCPAddr 和 LocalUDPAddr 删除direct中的对应条目。这样可更清晰地配置双本地地址 将设置sendthrough设置双地址的代码移动到 proxy.newClient函数 这样不仅direct可指定不同的tcp和udp的本地地址,任何client协议都可以了 为ClientCreator 接口 添加 UseUDPAsMsgConn 方法,direct和ss返回true 在ss的client的EstablishUDPChannel进行自行拨号 在ss的server建立后,自动循环监听udp,绕过vs的基本监听机制。因为vs架构的限制,一个代理只能有一个唯一的传输层协议。 ServerCreator 接口 添加 AfterCommonConfServer 方法
112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package simplesocks
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/url"
|
|
|
|
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
|
"github.com/e1732a364fed/v2ray_simple/proxy"
|
|
"github.com/e1732a364fed/v2ray_simple/utils"
|
|
)
|
|
|
|
func init() {
|
|
proxy.RegisterClient(Name, ClientCreator{})
|
|
}
|
|
|
|
type ClientCreator struct{ proxy.CreatorCommonStruct }
|
|
|
|
func (ClientCreator) UseUDPAsMsgConn() bool {
|
|
return false
|
|
}
|
|
|
|
func (ClientCreator) URLToDialConf(u *url.URL, dc *proxy.DialConf, format int) (*proxy.DialConf, error) {
|
|
if dc == nil {
|
|
dc = &proxy.DialConf{}
|
|
}
|
|
return dc, nil
|
|
}
|
|
|
|
func (ClientCreator) NewClient(dc *proxy.DialConf) (proxy.Client, error) {
|
|
|
|
return &Client{}, nil
|
|
}
|
|
|
|
type Client struct {
|
|
proxy.Base
|
|
}
|
|
|
|
func (*Client) GetCreator() proxy.ClientCreator {
|
|
return ClientCreator{}
|
|
}
|
|
func (c *Client) Name() string {
|
|
return Name
|
|
}
|
|
|
|
func WriteAddrToBuf(target netLayer.Addr, buf *bytes.Buffer) {
|
|
if len(target.IP) > 0 {
|
|
if ip4 := target.IP.To4(); ip4 == nil {
|
|
buf.WriteByte(ATypIP6)
|
|
buf.Write(target.IP)
|
|
} else {
|
|
buf.WriteByte(ATypIP4)
|
|
buf.Write(ip4)
|
|
}
|
|
} else if l := len(target.Name); l > 0 {
|
|
buf.WriteByte(ATypDomain)
|
|
buf.WriteByte(byte(l))
|
|
buf.WriteString(target.Name)
|
|
}
|
|
|
|
buf.WriteByte(byte(target.Port >> 8))
|
|
buf.WriteByte(byte(target.Port << 8 >> 8))
|
|
}
|
|
|
|
func (c *Client) Handshake(underlay net.Conn, firstPayload []byte, target netLayer.Addr) (io.ReadWriteCloser, error) {
|
|
if target.Port <= 0 {
|
|
return nil, errors.New("simplesocks Client Handshake failed, target port invalid")
|
|
|
|
}
|
|
buf := utils.GetBuf()
|
|
buf.WriteByte(CmdTCP)
|
|
WriteAddrToBuf(target, buf)
|
|
if len(firstPayload) > 0 {
|
|
buf.Write(firstPayload)
|
|
utils.PutBytes(firstPayload)
|
|
}
|
|
|
|
_, err := underlay.Write(buf.Bytes())
|
|
utils.PutBuf(buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &TCPConn{
|
|
Conn: underlay,
|
|
underlayIsBasic: netLayer.IsBasicConn(underlay),
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) EstablishUDPChannel(underlay net.Conn, firstPayload []byte, target netLayer.Addr) (netLayer.MsgConn, error) {
|
|
if target.Port <= 0 {
|
|
return nil, errors.New("simplesocks Client EstablishUDPChannel failed, target port invalid")
|
|
|
|
}
|
|
buf := utils.GetBuf()
|
|
buf.WriteByte(CmdUDP)
|
|
WriteAddrToBuf(target, buf)
|
|
|
|
uc := NewUDPConn(underlay, nil)
|
|
uc.handshakeBuf = buf
|
|
uc.fullcone = c.IsFullcone
|
|
|
|
if len(firstPayload) == 0 {
|
|
return uc, nil
|
|
|
|
} else {
|
|
return uc, uc.WriteMsgTo(firstPayload, target)
|
|
}
|
|
}
|