如何查找服务器真实IP

如何查找运行程序的计算机或服务器的公共 IP 地址?

就像程序执行时一样,它检测服务器的公共并打印例如running at 123.45.67.89


aluckdog
浏览 173回答 2
2回答

慕妹3242003

简短的回答是,没有办法保证返回您的“公共”IP地址。第一个问题是,您的公共IP地址是什么?计算机的地址(由要连接到的系统显示)可能会有所不同,具体取决于本地 Internet 服务的配置方式以及要连接到的服务:正如我在评论中提到的,在典型的家庭环境中,您的计算机没有公共IP地址。公共地址由路由器托管。如果您通过代理或VPN访问服务,则计算机的地址可能与直接连接到服务时完全不同。在具有多个接口的系统上,选择的源地址可能取决于您要连接到的地址:不同的地址可能具有不同的路由。您可以尝试使用 http://icanhazip.com/ 等服务来尝试确定您的公共 IP。这在许多情况下是正确的,但不是所有情况下。

叮当猫咪

公共IP地址是一个模糊的概念,在实践中,它可能是也可能不是静态地址。你对此了解多少?它只是一个在一定时间内有效的终结点,这取决于许多因素,例如使用哪个接口发出查询。我们可以使用主线位托伦特 dht&nbsp;给我们一些指示。Go 语言提供了由阿纳克罗利克斯编写的很酷的&nbsp;dht 包。当使用谓词查询节点时,我们会收到一个数据包,其中包含对等方与我们的查询关联的远程IP地址。这在&nbsp;bep10&nbsp;中进行了描述。find_peers如果UDP连接不是一个好的选择,您可以选择查询比特跟踪器,如bep24中所述考虑到对等方可能是恶意的,因此结果越多越好。下面的程序输出与从查询的节点队列的 POV 启动查询的计算机关联的外部网络地址列表。地址按响应数评分。另读&nbsp;https://www.bittorrent.org/beps/bep_0005.htmlfound 9 bootstrap peersfound 6 peers4&nbsp; &nbsp; [2001:861:51c5:xxx:40d1:8061:1fe0:xxx]:90902&nbsp; &nbsp; 81.96.42.191:90904同行告诉我们我们正在使用,我们可以推断这是ipv6。[2001:861:51c5:xxx:40d1:8061:1fe0:xxx]:90902他们告诉我们正在使用,ipv4接口。81.96.42.191:9090package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "errors"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net"&nbsp; &nbsp; "os"&nbsp; &nbsp; "sort"&nbsp; &nbsp; "sync"&nbsp; &nbsp; "time"&nbsp; &nbsp; "github.com/anacrolix/dht"&nbsp; &nbsp; "github.com/anacrolix/dht/krpc"&nbsp; &nbsp; "github.com/anacrolix/torrent/bencode")var maxTimeout = time.Second * 5func main() {&nbsp; &nbsp; b, _ := ioutil.ReadFile("db.json")&nbsp; &nbsp; var rawAddrs []string&nbsp; &nbsp; json.Unmarshal(b, &rawAddrs)&nbsp; &nbsp; defer func() {&nbsp; &nbsp; &nbsp; &nbsp; if len(rawAddrs) < 1 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if len(rawAddrs) > 30 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawAddrs = rawAddrs[:30]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; buf, err := json.Marshal(rawAddrs)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; err = ioutil.WriteFile("db.json", buf, os.ModePerm)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(os.Stderr, "%v peers recorded\n", len(rawAddrs))&nbsp; &nbsp; }()&nbsp; &nbsp; bootstrap, err := parseAddrs(rawAddrs)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; bootstrap, err = globalBootstrapAddrs()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; findPeers := []byte(`d1:ad2:id20:abcdefghij01234567899:info_hash20:mnopqrstuvwxyz123456e1:q9:get_peers1:t2:aa1:y1:qe`)&nbsp; &nbsp; local, err := net.ResolveUDPAddr("udp", "0.0.0.0:9090")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; ln, err := net.ListenUDP("udp", local)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; addrscores := map[string]int{}&nbsp; &nbsp; var drain drain&nbsp; &nbsp; defer drain.Wait()&nbsp; &nbsp; fmt.Fprintf(os.Stderr, "found %v bootstrap peers\n", len(bootstrap))&nbsp; &nbsp; res, errs := readResponses(ln, len(bootstrap), sendQuery(ln, bootstrap, findPeers))&nbsp; &nbsp; drain.Errors(errs)&nbsp; &nbsp; peers := []net.Addr{}&nbsp; &nbsp; for d := range res {&nbsp; &nbsp; &nbsp; &nbsp; if isValidAddr(d.IP.UDP()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addrscores[d.IP.String()]++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.R.ForAllNodes(func(arg1 krpc.NodeInfo) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peers = append(peers, arg1.Addr.UDP())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if len(peers) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(os.Stderr, "found %v peers\n", len(peers))&nbsp; &nbsp; &nbsp; &nbsp; res, errs = readResponses(ln, len(peers), sendQuery(ln, peers, findPeers))&nbsp; &nbsp; &nbsp; &nbsp; drain.Errors(errs)&nbsp; &nbsp; &nbsp; &nbsp; for d := range res {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isValidAddr(d.IP.UDP()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addrscores[d.IP.String()]++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for _, peer := range peers {&nbsp; &nbsp; &nbsp; &nbsp; if isValidAddr(peer) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawAddrs = append(rawAddrs, peer.String())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; addrs := make([]string, 0, len(addrscores))&nbsp; &nbsp; for addr := range addrscores {&nbsp; &nbsp; &nbsp; &nbsp; addrs = append(addrs, addr)&nbsp; &nbsp; }&nbsp; &nbsp; sort.Slice(addrs, func(i int, j int) bool {&nbsp; &nbsp; &nbsp; &nbsp; return addrscores[addrs[i]] > addrscores[addrs[j]]&nbsp; &nbsp; })&nbsp; &nbsp; for _, addr := range addrs {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%-4v %v\n", addrscores[addr], addr)&nbsp; &nbsp; }}type drain struct{ sync.WaitGroup }func (d *drain) Errors(errs <-chan error) {&nbsp; &nbsp; d.Add(1)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; defer d.Done()&nbsp; &nbsp; &nbsp; &nbsp; for err := range errs {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()}func parseAddrs(rawAddrs []string) (addrs []net.Addr, err error) {&nbsp; &nbsp; for _, s := range rawAddrs {&nbsp; &nbsp; &nbsp; &nbsp; host, port, err := net.SplitHostPort(s)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ua, err := net.ResolveUDPAddr("udp", net.JoinHostPort(host, port))&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf("error resolving %q: %v", host, err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; addrs = append(addrs, ua)&nbsp; &nbsp; }&nbsp; &nbsp; if len(addrs) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; err = errors.New("nothing resolved")&nbsp; &nbsp; }&nbsp; &nbsp; return}func globalBootstrapAddrs() (addrs []net.Addr, err error) {&nbsp; &nbsp; bootstrap, err := dht.GlobalBootstrapAddrs("udp")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; for _, b := range bootstrap {&nbsp; &nbsp; &nbsp; &nbsp; addrs = append(addrs, b.Raw())&nbsp; &nbsp; }&nbsp; &nbsp; return}func isValidAddr(addr net.Addr) bool { // so weird guys.&nbsp; &nbsp; return addr.String() != "<nil>" && addr.String() != ":0"}func sendQuery(ln *net.UDPConn, peers []net.Addr, query []byte) chan error {&nbsp; &nbsp; errs := make(chan error)&nbsp; &nbsp; for _, addr := range peers {&nbsp; &nbsp; &nbsp; &nbsp; go func(addr net.Addr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _, err := ln.WriteTo(query, addr)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errs <- addressedError{Op: "send", error: err, Addr: addr}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }(addr)&nbsp; &nbsp; }&nbsp; &nbsp; return errs}func readResponses(ln *net.UDPConn, count int, errs chan error) (<-chan krpc.Msg, <-chan error) {&nbsp; &nbsp; data := make(chan krpc.Msg)&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; for i := 0; i < count; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf := make([]byte, 1000)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ln.SetReadDeadline(time.Now().Add(maxTimeout))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n, remoteAddr, err := ln.ReadFromUDP(buf)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errs <- addressedError{Op: "rcv", error: err, Addr: remoteAddr}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var m krpc.Msg&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err = bencode.Unmarshal(buf[:n], &m)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errs <- addressedError{Op: "rcv", error: err, Addr: remoteAddr}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data <- m&nbsp; &nbsp; &nbsp; &nbsp; }()&nbsp; &nbsp; }&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; &nbsp; &nbsp; close(errs)&nbsp; &nbsp; &nbsp; &nbsp; close(data)&nbsp; &nbsp; }()&nbsp; &nbsp; return data, errs}type addressedError struct {&nbsp; &nbsp; error&nbsp; &nbsp; Op&nbsp; &nbsp;string&nbsp; &nbsp; Addr net.Addr}func (a addressedError) Error() string {&nbsp; &nbsp; if !isValidAddr(a.Addr) {&nbsp; &nbsp; &nbsp; &nbsp; return fmt.Sprintf("%-5v %v", a.Op, a.error.Error())&nbsp; &nbsp; }&nbsp; &nbsp; return fmt.Sprintf("%-5v %v: %v", a.Op, a.Addr.String(), a.error.Error())}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go