mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
tun的tcp功能在mac上测试通过;修订代码;修复若干问题:
修正ReplaceSymbol函数 SelfListen时也打印Listen日志 修正machine的DefaultClient的赋值 添加tun示例文件以及路由指导
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
_ "github.com/e1732a364fed/ui/winmanifest"
|
||||
"github.com/e1732a364fed/v2ray_simple/utils"
|
||||
"go.uber.org/zap"
|
||||
|
||||
_ "github.com/e1732a364fed/v2ray_simple/proxy/tun"
|
||||
)
|
||||
|
||||
var testFunc func()
|
||||
|
||||
37
examples/tun.client.toml
Normal file
37
examples/tun.client.toml
Normal file
@@ -0,0 +1,37 @@
|
||||
# tun; vs的tun功能仅在 vs_gui 系列中存在。
|
||||
|
||||
# 你要配置好路由表才能让tun正常使用。
|
||||
# 在mac/windows上,路由表不是那么好配置,尤其是没有linux那么灵活
|
||||
# 而如果是在linux上的话,直接推荐使用tproxy
|
||||
|
||||
# 对于小白来说,下面的指导太过于高级,难以看懂,因此对于小白来说推荐全自动化的方案。
|
||||
|
||||
# 在macos上, 配置路由表还需要在tun设备建立之后才能进行,因为tun设备的名称是无法自定义的
|
||||
|
||||
# 下面给出macos上的路由表配置指导
|
||||
# 首先删除默认路由,然后将路由指向 utun3 (用户自己运行的到的名称可能不同)
|
||||
# 最后将自己的服务器的ip (我们的例子是127.0.0.1,请你改成实际服务器ip) 的路由指向原来的 路由器地址
|
||||
# 这个方案只适用于
|
||||
|
||||
# sudo route delete -host default
|
||||
# sudo route add default -interface utun3
|
||||
# sudo route add -host 127.0.0.1 192.168.1.1
|
||||
|
||||
# 关闭vs后,要将原来的路由添回。不会的话,重启可以复原。
|
||||
|
||||
# sudo route delete -host default
|
||||
# sudo route add default 192.168.1.1
|
||||
|
||||
|
||||
[[listen]]
|
||||
protocol = "tun"
|
||||
|
||||
|
||||
[[dial]]
|
||||
protocol = "vlesss"
|
||||
uuid = "a684455c-b14f-11ea-bf0d-42010aaa0003"
|
||||
host = "127.0.0.1"
|
||||
port = 4433
|
||||
version = 0
|
||||
insecure = true
|
||||
utls = true
|
||||
@@ -36,13 +36,11 @@ func (m *M) LoadDialConf(conf []*proxy.DialConf) (ok bool) {
|
||||
}
|
||||
}
|
||||
|
||||
if m.DefaultOutClient == nil {
|
||||
if len(m.allClients) > 0 {
|
||||
m.DefaultOutClient = m.allClients[0]
|
||||
if len(m.allClients) > 0 {
|
||||
m.DefaultOutClient = m.allClients[0]
|
||||
|
||||
} else {
|
||||
m.DefaultOutClient = v2ray_simple.DirectClient
|
||||
}
|
||||
} else {
|
||||
m.DefaultOutClient = v2ray_simple.DirectClient
|
||||
}
|
||||
return
|
||||
|
||||
|
||||
13
main.go
13
main.go
@@ -75,11 +75,22 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.
|
||||
var extraCloser io.Closer
|
||||
|
||||
var is, tcp, udp bool
|
||||
//tproxy 和 shadowsocks 都用到了 SelfListen
|
||||
//tproxy,tun/tap 和 shadowsocks(udp) 都用到了 SelfListen
|
||||
if is, tcp, udp = inServer.SelfListen(); is {
|
||||
var chantcp chan netLayer.TCPRequestInfo
|
||||
var chanudp chan netLayer.UDPRequestInfo
|
||||
|
||||
if ce := utils.CanLogInfo("Listening"); ce != nil {
|
||||
|
||||
ce.Write(
|
||||
zap.String("tag", inServer.GetTag()),
|
||||
zap.String("protocol", proxy.GetFullName(inServer)),
|
||||
zap.String("listen_addr", inServer.AddrStr()),
|
||||
zap.String("defaultClient", proxy.GetFullName(defaultOutClient)),
|
||||
zap.String("dial_addr", defaultOutClient.AddrStr()),
|
||||
)
|
||||
}
|
||||
|
||||
if tcp {
|
||||
chantcp = make(chan netLayer.TCPRequestInfo, 2)
|
||||
go func() {
|
||||
|
||||
@@ -9,25 +9,27 @@ import (
|
||||
"github.com/e1732a364fed/v2ray_simple/utils"
|
||||
"github.com/eycorsican/go-tun2socks/core"
|
||||
"github.com/eycorsican/go-tun2socks/tun"
|
||||
"github.com/songgao/water"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type coreConnAdapter struct {
|
||||
type coreUDPConnAdapter struct {
|
||||
core.UDPConn
|
||||
netLayer.EasyDeadline
|
||||
}
|
||||
|
||||
func (h *coreConnAdapter) ReadMsgFrom() ([]byte, netLayer.Addr, error) {
|
||||
func (h *coreUDPConnAdapter) ReadMsgFrom() ([]byte, netLayer.Addr, error) {
|
||||
|
||||
return nil, netLayer.Addr{}, nil
|
||||
}
|
||||
func (h *coreConnAdapter) WriteMsgTo([]byte, netLayer.Addr) error {
|
||||
func (h *coreUDPConnAdapter) WriteMsgTo([]byte, netLayer.Addr) error {
|
||||
return nil
|
||||
}
|
||||
func (h *coreConnAdapter) CloseConnWithRaddr(raddr netLayer.Addr) error {
|
||||
func (h *coreUDPConnAdapter) CloseConnWithRaddr(raddr netLayer.Addr) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
func (h *coreConnAdapter) Fullcone() bool {
|
||||
func (h *coreUDPConnAdapter) Fullcone() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -45,13 +47,15 @@ func newHandler() *handler {
|
||||
|
||||
func (h *handler) Handle(conn net.Conn, target *net.TCPAddr) error {
|
||||
tad := netLayer.NewAddrFromTCPAddr(target)
|
||||
//实测 这里 target 就是 conn.RemoteAddr()
|
||||
|
||||
h.tcpChan <- netLayer.TCPRequestInfo{Target: tad, Conn: conn}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *handler) Connect(conn core.UDPConn, target *net.UDPAddr) error {
|
||||
uad := netLayer.NewAddrFromUDPAddr(target)
|
||||
adapter := &coreConnAdapter{UDPConn: conn}
|
||||
adapter := &coreUDPConnAdapter{UDPConn: conn}
|
||||
adapter.InitEasyDeadline()
|
||||
|
||||
h.udpChan <- netLayer.UDPRequestInfo{Target: uad, MsgConn: adapter}
|
||||
@@ -67,12 +71,23 @@ func ListenTun() (tunDev io.ReadWriteCloser, err error) {
|
||||
|
||||
//macos 上无法指定tun名称
|
||||
tunDev, err = tun.OpenTunDevice("", "10.1.0.10", "10.1.0.20", "255.255.255.0", nil, false)
|
||||
if err == nil {
|
||||
if ce := utils.CanLogInfo("created new tun device"); ce != nil {
|
||||
ce.Write(zap.String("name", tunDev.(*water.Interface).Name()))
|
||||
}
|
||||
}
|
||||
/*
|
||||
如果你是 tun listen, direct dial,
|
||||
要配置好路由表;如果默认情况的话,会造成无限本地回环,因为我们代理发出的流量又被导向代理本身
|
||||
|
||||
|
||||
*/
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func HandleTun(tunDev io.ReadWriteCloser) (tcpChan chan netLayer.TCPRequestInfo, udpChan chan netLayer.UDPRequestInfo) {
|
||||
lwipWriter := core.NewLWIPStack().(io.Writer)
|
||||
func HandleTun(tunDev io.ReadWriteCloser) (tcpChan <-chan netLayer.TCPRequestInfo, udpChan <-chan netLayer.UDPRequestInfo, closer io.Closer) {
|
||||
lwip := core.NewLWIPStack()
|
||||
core.RegisterOutputFn(func(data []byte) (int, error) {
|
||||
return tunDev.Write(data)
|
||||
})
|
||||
@@ -80,12 +95,13 @@ func HandleTun(tunDev io.ReadWriteCloser) (tcpChan chan netLayer.TCPRequestInfo,
|
||||
core.RegisterTCPConnHandler(nh)
|
||||
core.RegisterUDPConnHandler(nh)
|
||||
go func() {
|
||||
_, err := io.CopyBuffer(lwipWriter, tunDev, make([]byte, utils.MTU))
|
||||
_, err := io.CopyBuffer(lwip, tunDev, make([]byte, utils.MTU))
|
||||
if err != nil {
|
||||
log.Fatalf("tun copying from tunDev to lwip failed: %v", err)
|
||||
}
|
||||
}()
|
||||
tcpChan = nh.tcpChan
|
||||
udpChan = nh.udpChan
|
||||
closer = lwip
|
||||
return
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha
|
||||
Target: targetAddr,
|
||||
}
|
||||
|
||||
if ce := utils.CanLogInfo("TProxy loop read got new tcp"); ce != nil {
|
||||
if ce := utils.CanLogInfo("TProxy got new tcp"); ce != nil {
|
||||
ce.Write(zap.String("->", targetAddr.String()))
|
||||
}
|
||||
if tm.Closed() {
|
||||
@@ -175,7 +175,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha
|
||||
}
|
||||
break
|
||||
} else {
|
||||
if ce := utils.CanLogInfo("TProxy loop read got new udp"); ce != nil {
|
||||
if ce := utils.CanLogInfo("TProxy got new udp"); ce != nil {
|
||||
ce.Write(zap.String("->", raddr.String()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ import (
|
||||
|
||||
const name = "tun"
|
||||
|
||||
func init() {
|
||||
proxy.RegisterServer(name, &ServerCreator{})
|
||||
}
|
||||
|
||||
type ServerCreator struct{ proxy.CreatorCommonStruct }
|
||||
|
||||
func (ServerCreator) URLToListenConf(url *url.URL, lc *proxy.ListenConf, format int) (*proxy.ListenConf, error) {
|
||||
@@ -36,7 +40,8 @@ type Server struct {
|
||||
|
||||
stopped bool
|
||||
|
||||
infoChan chan<- netLayer.TCPRequestInfo
|
||||
infoChan chan<- netLayer.TCPRequestInfo
|
||||
lwipCloser io.Closer
|
||||
}
|
||||
|
||||
func (*Server) Name() string { return name }
|
||||
@@ -56,6 +61,7 @@ func (s *Server) Close() error {
|
||||
func (s *Server) Stop() {
|
||||
if !s.stopped {
|
||||
s.stopped = true
|
||||
s.lwipCloser.Close()
|
||||
close(s.infoChan)
|
||||
}
|
||||
|
||||
@@ -71,16 +77,20 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha
|
||||
}
|
||||
s.infoChan = infoChan
|
||||
|
||||
tchan, _ := tun.HandleTun(tunDev)
|
||||
tchan, _, lwipcloser := tun.HandleTun(tunDev)
|
||||
go func() {
|
||||
for tr := range tchan {
|
||||
if s.stopped {
|
||||
return
|
||||
}
|
||||
if ce := utils.CanLogInfo("tun got new tcp"); ce != nil {
|
||||
ce.Write(zap.String("->", tr.Target.String()))
|
||||
}
|
||||
infoChan <- tr
|
||||
|
||||
}
|
||||
}()
|
||||
s.lwipCloser = lwipcloser
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -264,18 +264,20 @@ func CommonSplit_regex(s, e1, e2 string) (ok bool, v1, v2 string) {
|
||||
|
||||
// the first part of synonyms is the one to be replaced, the last part of synonyms is the persistent one.
|
||||
func ReplaceBytesSynonyms(bs []byte, synonyms [][2][]byte) (result []byte) {
|
||||
result = bs
|
||||
for _, ss := range synonyms {
|
||||
|
||||
result = bytes.Replace(bs, ss[0], ss[1], -1)
|
||||
result = bytes.Replace(result, ss[0], ss[1], -1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// same as ReplaceBytesSynonyms
|
||||
func ReplaceStringsSynonyms(bs string, synonyms [][2]string) (result string) {
|
||||
result = bs
|
||||
for _, ss := range synonyms {
|
||||
|
||||
result = strings.Replace(bs, ss[0], ss[1], -1)
|
||||
result = strings.Replace(result, ss[0], ss[1], -1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user