@@ -9,60 +9,103 @@ import (
99 "time"
1010)
1111
12- func AllPeers (ctx context.Context , appName string ) ([]net.IPAddr , error ) {
13- return Get6PN (ctx , fmt .Sprintf ("%s.internal" , appName ))
12+ // func AllPeers(ctx context.Context, appName string) ([]net.IPAddr, error) {
13+ // return Get6PN(ctx, fmt.Sprintf("%s.internal", appName))
14+ // }
15+
16+ // func Get6PN(ctx context.Context, hostname string) ([]net.IPAddr, error) {
17+ // nameserver := os.Getenv("FLY_NAMESERVER")
18+ // if nameserver == "" {
19+ // nameserver = "fdaa::3"
20+ // }
21+ // nameserver = net.JoinHostPort(nameserver, "53")
22+ // r := &net.Resolver{
23+ // PreferGo: true,
24+ // Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
25+ // d := net.Dialer{
26+ // Timeout: 1 * time.Second,
27+ // }
28+ // return d.DialContext(ctx, "udp6", nameserver)
29+ // },
30+ // }
31+ // ips, err := r.LookupIPAddr(ctx, hostname)
32+
33+ // if err != nil {
34+ // return ips, err
35+ // }
36+
37+ // // make sure we're including the local ip, just in case it's not in service discovery yet
38+ // local, err := r.LookupIPAddr(ctx, "fly-local-6pn")
39+
40+ // if err != nil || len(local) < 1 {
41+ // return ips, err
42+ // }
43+
44+ // localExists := false
45+ // for _, v := range ips {
46+ // if v.IP.String() == local[0].IP.String() {
47+ // localExists = true
48+ // }
49+ // }
50+
51+ // if !localExists {
52+ // ips = append(ips, local[0])
53+ // }
54+ // return ips, err
55+ // }
56+
57+ type Machine struct {
58+ ID string
59+ Region string
60+ }
61+
62+ func AllMachines (ctx context.Context , appName string ) ([]Machine , error ) {
63+ r := getResolver ()
64+ txts , err := r .LookupTXT (ctx , fmt .Sprintf ("vms.%s.internal" , appName ))
65+ if err != nil {
66+ return nil , err
67+ }
68+
69+ machines := make ([]Machine , 0 )
70+ for _ , txt := range txts {
71+ parts := strings .Split (txt , "," )
72+ for _ , part := range parts {
73+ parts := strings .Split (part , " " )
74+ if len (parts ) != 2 {
75+ return nil , fmt .Errorf ("invalid machine DNS TXT format: %s" , txt )
76+ }
77+ machines = append (machines , Machine {ID : parts [0 ], Region : parts [1 ]})
78+ }
79+ }
80+ return machines , nil
1481}
1582
16- func Get6PN ( ctx context. Context , hostname string ) ([] net.IPAddr , error ) {
83+ func getResolver () * net.Resolver {
1784 nameserver := os .Getenv ("FLY_NAMESERVER" )
1885 if nameserver == "" {
1986 nameserver = "fdaa::3"
2087 }
2188 nameserver = net .JoinHostPort (nameserver , "53" )
22- r := & net.Resolver {
89+ return & net.Resolver {
2390 PreferGo : true ,
24- Dial : func (ctx context.Context , network , address string ) (net.Conn , error ) {
91+ Dial : func (ctx context.Context , network , _ string ) (net.Conn , error ) {
2592 d := net.Dialer {
2693 Timeout : 1 * time .Second ,
2794 }
2895 return d .DialContext (ctx , "udp6" , nameserver )
2996 },
3097 }
31- ips , err := r .LookupIPAddr (ctx , hostname )
32-
33- if err != nil {
34- return ips , err
35- }
36-
37- // make sure we're including the local ip, just in case it's not in service discovery yet
38- local , err := r .LookupIPAddr (ctx , "fly-local-6pn" )
39-
40- if err != nil || len (local ) < 1 {
41- return ips , err
42- }
43-
44- localExists := false
45- for _ , v := range ips {
46- if v .IP .String () == local [0 ].IP .String () {
47- localExists = true
48- }
49- }
50-
51- if ! localExists {
52- ips = append (ips , local [0 ])
53- }
54- return ips , err
5598}
5699
57- func PrivateIPv6 () (net.IP , error ) {
58- ips , err := net .LookupIP ("fly-local-6pn" )
59- if err != nil && ! strings .HasSuffix (err .Error (), "no such host" ) && ! strings .HasSuffix (err .Error (), "server misbehaving" ) {
60- return nil , err
61- }
100+ // func PrivateIPv6() (net.IP, error) {
101+ // ips, err := net.LookupIP("fly-local-6pn")
102+ // if err != nil && !strings.HasSuffix(err.Error(), "no such host") && !strings.HasSuffix(err.Error(), "server misbehaving") {
103+ // return nil, err
104+ // }
62105
63- if len (ips ) > 0 {
64- return ips [0 ], nil
65- }
106+ // if len(ips) > 0 {
107+ // return ips[0], nil
108+ // }
66109
67- return net .ParseIP ("127.0.0.1" ), nil
68- }
110+ // return net.ParseIP("127.0.0.1"), nil
111+ // }
0 commit comments