disseminator_gns.go (3042B)
1 // This file is part of tdir, the Taler Directory implementation. 2 // Copyright (C) 2025 Martin Schanzenbach 3 // 4 // Taldir is free software: you can redistribute it and/or modify it 5 // under the terms of the GNU Affero General Public License as published 6 // by the Free Software Foundation, either version 3 of the License, 7 // or (at your option) any later version. 8 // 9 // Taldir is distributed in the hope that it will be useful, but 10 // WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 // Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 // 17 // SPDX-License-Identifier: AGPL3.0-or-later 18 19 package taldir 20 21 import ( 22 "fmt" 23 "os/exec" 24 "strings" 25 ) 26 27 type GnsDisseminator struct { 28 // Config 29 config *TaldirConfig 30 } 31 32 func (d *GnsDisseminator) gns_check_is_disseminated(e *Entry) bool { 33 path, err := exec.LookPath("gnunet-gns") 34 if err != nil { 35 return false 36 } 37 zone := d.config.Ini.Section("taldir-disseminator-gns").Key("zone").MustString("taldir") 38 out, err := exec.Command(path, "--lookup", e.HsAlias+"."+zone, "--type", "TXT").Output() 39 if err != nil { 40 return false 41 } 42 return strings.Contains(string(out), "TXT: `"+e.TargetURI+"'") 43 } 44 45 func (d *GnsDisseminator) DisseminateStop(e *Entry) error { 46 if !d.gns_check_is_disseminated(e) { 47 return nil 48 } 49 path, err := exec.LookPath("gnunet-namestore") 50 if err != nil { 51 return fmt.Errorf("path of command not found: %w", err) 52 } 53 zone := d.config.Ini.Section("taldir-disseminator-gns").Key("zone").MustString("taldir") 54 out, err := exec.Command(path, "--delete", "--public", "--zone", zone, "--type", "TXT", "--name", e.HsAlias, "--value", e.TargetURI).Output() 55 if err != nil { 56 return fmt.Errorf("failed to execute disseminator command: `%s', %w", out, err) 57 } 58 return nil 59 } 60 61 func (d *GnsDisseminator) DisseminateStart(e *Entry) error { 62 if d.gns_check_is_disseminated(e) { 63 fmt.Printf("`%s' is already being disseminated\n", e.HsAlias) 64 return nil 65 } 66 path, err := exec.LookPath("gnunet-namestore") 67 if err != nil { 68 return fmt.Errorf("path of command not found: %w", err) 69 } 70 expiration := d.config.Ini.Section("taldir-disseminator-gns").Key("expiration").MustString("1d") 71 zone := d.config.Ini.Section("taldir-disseminator-gns").Key("zone").MustString("taldir") 72 out, err := exec.Command(path, "--add", "--public", "--expiration", expiration, "--zone", zone, "--type", "TXT", "--name", e.HsAlias, "--value", e.TargetURI).Output() 73 if err != nil { 74 return fmt.Errorf("failed to execute disseminator command: `%s', %w", out, err) 75 } 76 return nil 77 } 78 79 func (d *GnsDisseminator) Name() string { 80 return "gns" 81 } 82 83 func (d *GnsDisseminator) IsEnabled() bool { 84 return d.config.Ini.Section("taldir-disseminator-gns").Key("enabled").MustBool(false) 85 } 86 87 func makeGnsDisseminator(cfg *TaldirConfig) GnsDisseminator { 88 d := GnsDisseminator{ 89 config: cfg, 90 } 91 return d 92 }