File
Blob: tun/server/resolve.go
| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "go.miragespace.co/specter/spec/protocol" |
| 8 | "go.miragespace.co/specter/spec/tun" |
| 9 | |
| 10 | "go.uber.org/zap" |
| 11 | ) |
| 12 | |
| 13 | func (s *Server) publishDestinations(ctx context.Context) error { |
| 14 | destinations := &protocol.TunnelDestination{ |
| 15 | Chord: s.ChordTransport.Identity(), |
| 16 | Tunnel: s.TunnelTransport.Identity(), |
| 17 | } |
| 18 | |
| 19 | buf, err := destinations.MarshalVT() |
| 20 | if err != nil { |
| 21 | return err |
| 22 | } |
| 23 | |
| 24 | keys := []string{ |
| 25 | tun.DestinationByChordKey(s.ChordTransport.Identity()), |
| 26 | tun.DestinationByTunnelKey(s.TunnelTransport.Identity()), |
| 27 | } |
| 28 | |
| 29 | for _, key := range keys { |
| 30 | err := s.Chord.Put(ctx, []byte(key), buf) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | s.Logger.Info("Destinations published on chord", |
| 37 | zap.String("chord", keys[0]), |
| 38 | zap.String("tunnel", keys[1]), |
| 39 | ) |
| 40 | |
| 41 | return nil |
| 42 | } |
| 43 | |
| 44 | func (s *Server) unpublishDestinations(ctx context.Context) { |
| 45 | keys := []string{ |
| 46 | tun.DestinationByChordKey(s.ChordTransport.Identity()), |
| 47 | tun.DestinationByTunnelKey(s.TunnelTransport.Identity()), |
| 48 | } |
| 49 | |
| 50 | for _, key := range keys { |
| 51 | err := s.Chord.Delete(ctx, []byte(key)) |
| 52 | if err != nil { |
| 53 | s.Logger.Error("Failed to unpublish destinations on chord", zap.Error(err)) |
| 54 | return |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | s.Logger.Info("Destination unpublished on chord", |
| 59 | zap.String("chord", keys[0]), |
| 60 | zap.String("tunnel", keys[1])) |
| 61 | } |
| 62 | |
| 63 | func (s *Server) lookupDestination(ctx context.Context, key string) (*protocol.TunnelDestination, error) { |
| 64 | buf, err := s.Chord.Get(ctx, []byte(key)) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | if len(buf) == 0 { |
| 69 | return nil, fmt.Errorf("no destination found with key: %s", key) |
| 70 | } |
| 71 | dst := &protocol.TunnelDestination{} |
| 72 | if err := dst.UnmarshalVT(buf); err != nil { |
| 73 | return nil, fmt.Errorf("tunnel destination decode failure: %w", err) |
| 74 | } |
| 75 | return dst, nil |
| 76 | } |