mirror of
				https://github.com/nadoo/glider.git
				synced 2025-11-04 07:42:38 +08:00 
			
		
		
		
	udptun: support direct mode
This commit is contained in:
		
							parent
							
								
									76a12aa8d1
								
							
						
					
					
						commit
						b094d296f3
					
				@ -12,9 +12,6 @@ var Direct = &direct{}
 | 
			
		||||
func (d *direct) Addr() string { return "DIRECT" }
 | 
			
		||||
 | 
			
		||||
func (d *direct) Dial(network, addr string) (net.Conn, error) {
 | 
			
		||||
	if network == "uot" {
 | 
			
		||||
		network = "udp"
 | 
			
		||||
	}
 | 
			
		||||
	c, err := net.Dial(network, addr)
 | 
			
		||||
	if c, ok := c.(*net.TCPConn); ok {
 | 
			
		||||
		c.SetKeepAlive(true)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								ss.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								ss.go
									
									
									
									
									
								
							@ -217,7 +217,7 @@ func (s *SS) Dial(network, addr string) (net.Conn, error) {
 | 
			
		||||
	switch network {
 | 
			
		||||
	case "tcp":
 | 
			
		||||
		return s.DialTCP(target)
 | 
			
		||||
	case "uot":
 | 
			
		||||
	case "udp":
 | 
			
		||||
		target[0] = target[0] | 0x8
 | 
			
		||||
		return s.DialTCP(target)
 | 
			
		||||
	// case "udp":
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										39
									
								
								udptun.go
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								udptun.go
									
									
									
									
									
								
							@ -35,6 +35,45 @@ func (s *UDPTun) ListenAndServe() {
 | 
			
		||||
 | 
			
		||||
	logf("proxy-udptun listening UDP on %s", s.addr)
 | 
			
		||||
 | 
			
		||||
	if s.sDialer.Addr() == "DIRECT" {
 | 
			
		||||
		s.ServeDirect(c)
 | 
			
		||||
	} else {
 | 
			
		||||
		s.ServeSS(c)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ServeDirect .
 | 
			
		||||
func (s *UDPTun) ServeDirect(c net.PacketConn) {
 | 
			
		||||
	buf := make([]byte, udpBufSize)
 | 
			
		||||
 | 
			
		||||
	for {
 | 
			
		||||
		n, clientAddr, err := c.ReadFrom(buf)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logf("proxy-udptun read error: %v", err)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		rc, err := s.sDialer.Dial("udp", s.raddr)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logf("proxy-udptun failed to connect to server %v: %v", s.raddr, err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if urc, ok := rc.(*net.UDPConn); ok {
 | 
			
		||||
			urc.Write(buf[:n])
 | 
			
		||||
			go func() {
 | 
			
		||||
				timedCopy(c, clientAddr, urc, 5*time.Minute, false)
 | 
			
		||||
				urc.Close()
 | 
			
		||||
			}()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		logf("proxy-udptun %s <-> %s", clientAddr, s.raddr)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ServeSS .
 | 
			
		||||
func (s *UDPTun) ServeSS(c net.PacketConn) {
 | 
			
		||||
	// var nm sync.Map
 | 
			
		||||
	buf := make([]byte, udpBufSize)
 | 
			
		||||
	tgt := ParseAddr(s.raddr)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user