mirror of
				https://github.com/nadoo/glider.git
				synced 2025-11-04 07:42:38 +08:00 
			
		
		
		
	conn: add the ability to get outbound ip
This commit is contained in:
		
							parent
							
								
									31a6c94d75
								
							
						
					
					
						commit
						192cffc95c
					
				
							
								
								
									
										14
									
								
								conn.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								conn.go
									
									
									
									
									
								
							@ -3,6 +3,7 @@ package main
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
@ -79,3 +80,16 @@ func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// OutboundIP returns preferred outbound ip of this machine
 | 
			
		||||
func OutboundIP() net.IP {
 | 
			
		||||
	conn, err := net.Dial("udp", "8.8.8.8:80")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer conn.Close()
 | 
			
		||||
 | 
			
		||||
	localAddr := conn.LocalAddr().(*net.UDPAddr)
 | 
			
		||||
 | 
			
		||||
	return localAddr.IP
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user