|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + |
| 7 | + "github.com/threefoldtech/zos/pkg/gridtypes" |
| 8 | +) |
| 9 | + |
| 10 | +func (s *Service) NetworkPublicIps(arg any, reply *Ips) error { |
| 11 | + ips, err := s.provisionStub.ListPublicIPs(s.ctx) |
| 12 | + if err != nil { |
| 13 | + return err |
| 14 | + } |
| 15 | + |
| 16 | + reply.Ips = append(reply.Ips, ips...) |
| 17 | + return nil |
| 18 | +} |
| 19 | + |
| 20 | +func (s *Service) NetworkHasIpv6(arg any, reply *bool) error { |
| 21 | + ipData, err := s.networkerStub.GetPublicIPv6Subnet(s.ctx) |
| 22 | + hasIP := ipData.IP != nil && err == nil |
| 23 | + *reply = hasIP |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +func (s *Service) NetworkInterfaces(arg any, reply *Interfaces) error { |
| 28 | + type q struct { |
| 29 | + inf string |
| 30 | + ns string |
| 31 | + rename string |
| 32 | + } |
| 33 | + |
| 34 | + for _, i := range []q{ |
| 35 | + {"zos", "", "zos"}, |
| 36 | + {"nygg6", "ndmz", "ygg"}, |
| 37 | + } { |
| 38 | + ips, _, err := s.networkerStub.Addrs(s.ctx, i.inf, i.ns) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("failed to get ips for '%s' interface: %w", i, err) |
| 41 | + } |
| 42 | + |
| 43 | + reply.Interfaces = append(reply.Interfaces, Interface{ |
| 44 | + Name: i.rename, |
| 45 | + Ips: func() []string { |
| 46 | + var list []string |
| 47 | + for _, item := range ips { |
| 48 | + ip := net.IP(item) |
| 49 | + list = append(list, ip.String()) |
| 50 | + } |
| 51 | + |
| 52 | + return list |
| 53 | + }(), |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (s *Service) NetworkPublicConfig(arg any, reply *PublicConfig) error { |
| 61 | + config, err := s.networkerStub.GetPublicConfig(s.ctx) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + reply.Domain = config.Domain |
| 67 | + reply.IPv4 = config.IPv4.String() |
| 68 | + reply.IPv6 = config.IPv6.String() |
| 69 | + reply.GW4 = config.GW4.String() |
| 70 | + reply.GW6 = config.GW6.String() |
| 71 | + reply.Type = string(config.Type) |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (s *Service) NetworkWGPorts(arg any, reply *WGPorts) error { |
| 76 | + ports, err := s.networkerStub.WireguardPorts(s.ctx) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + for _, port := range ports { |
| 82 | + reply.Ports = append(reply.Ports, uint64(port)) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (s *Service) NetworkPrivateIps(arg string, reply *Ips) error { |
| 89 | + ips, err := s.provisionStub.ListPrivateIPs(s.ctx, GetTwinID(s.ctx), gridtypes.Name(arg)) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + reply.Ips = append(reply.Ips, ips...) |
| 95 | + return nil |
| 96 | +} |
0 commit comments