mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-10-19 15:14:36 +08:00

不再使用 UDP_Putter 等机制去转发udp,而是用一个 netLayer.MsgConn 结构 proxy.Server 和 proxy.Client 接口改动, Client在握手udp时不再使用handshake方法, 而是用新的 EstablishUDPChannel 方法 Server 在 Handshake时会选择性返回两种接口,io.ReadWriteCloser 用于tcp, netLayer.MsgConn 用于 udp 此时vless、socks5、direct 的udp转发都已经成功经过了 go test 验证, 但是 main.go 还未修改。
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
// Package direct provies a struct that implements proxy.Client
|
||
package direct
|
||
|
||
import (
|
||
"io"
|
||
"net"
|
||
"net/url"
|
||
|
||
"github.com/hahahrfool/v2ray_simple/netLayer"
|
||
"github.com/hahahrfool/v2ray_simple/proxy"
|
||
)
|
||
|
||
const name = "direct"
|
||
|
||
func init() {
|
||
proxy.RegisterClient(name, &ClientCreator{})
|
||
}
|
||
|
||
//实现了 proxy.Client, netLayer.UDP_Putter_Generator
|
||
type Client struct {
|
||
proxy.ProxyCommonStruct
|
||
}
|
||
|
||
type ClientCreator struct{}
|
||
|
||
func NewClient() (proxy.Client, error) {
|
||
d := &Client{}
|
||
return d, nil
|
||
}
|
||
|
||
func (_ ClientCreator) NewClientFromURL(*url.URL) (proxy.Client, error) {
|
||
return NewClient()
|
||
}
|
||
|
||
func (_ ClientCreator) NewClient(*proxy.DialConf) (proxy.Client, error) {
|
||
return NewClient()
|
||
}
|
||
|
||
func (d *Client) Name() string { return name }
|
||
|
||
//若 underlay 为nil,则我们会自动对target进行拨号。
|
||
func (d *Client) Handshake(underlay net.Conn, target netLayer.Addr) (io.ReadWriteCloser, error) {
|
||
|
||
if underlay == nil {
|
||
return target.Dial()
|
||
}
|
||
|
||
return underlay, nil
|
||
|
||
}
|
||
|
||
//direct的Client的 EstablishUDPChannel 实际上就是直接拨号udp
|
||
func (d *Client) EstablishUDPChannel(_ net.Conn, target netLayer.Addr) (netLayer.MsgConn, error) {
|
||
conn, err := net.DialUDP("udp", nil, target.ToUDPAddr())
|
||
return &netLayer.UDPMsgConnWrapper{UDPConn: conn, IsClient: true, FirstAddr: target}, err
|
||
}
|