mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-10-06 17:27:05 +08:00

现在整个程序均通过了go test, main 也可以正常运行了。 Relay_UDP 函数添加流量计数; 发现之前 Relay函数的流量计数 在main.go里参数传反了,导致实际上计数的是上传而不是下载,已修复 对fullcone的情况做了特别考量。MsgConn的 Close函数在fullcone时不能随便被调用。 因此我添加了一个 CloseConnWithRaddr(raddr Addr) error 方法,以及 Fullcone() bool 方法 在utils包的init部分使用 rand 随机种子
56 lines
1.2 KiB
Go
56 lines
1.2 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
|
||
isfullcone bool
|
||
}
|
||
|
||
type ClientCreator struct{}
|
||
|
||
func (_ ClientCreator) NewClientFromURL(*url.URL) (proxy.Client, error) {
|
||
d := &Client{}
|
||
return d, nil
|
||
}
|
||
|
||
func (_ ClientCreator) NewClient(dc *proxy.DialConf) (proxy.Client, error) {
|
||
d := &Client{}
|
||
d.isfullcone = dc.Fullcone
|
||
return d, nil
|
||
}
|
||
|
||
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) {
|
||
|
||
return netLayer.NewUDPMsgConnClientWrapper(nil, d.isfullcone, false), nil
|
||
}
|