add syncing

This commit is contained in:
eyedeekay
2025-03-17 19:42:17 -04:00
parent 078bfb9aa3
commit 1d63691280
3 changed files with 44 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package jumpserver
import (
"context"
"log"
"net/http"
"strings"
@ -14,6 +15,10 @@ func (j *JumpServer) Serve() error {
}
log.Printf("Listening on %s\n", l.Addr())
defer l.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
j.AutoUpdateMetadata(ctx)
j.StartSync(j.SyncURLs, ctx)
return http.Serve(l, j)
}
@ -21,6 +26,24 @@ func (j *JumpServer) Close() error {
return j.Garlic.Close()
}
func (j *JumpServer) AutoUpdateMetadata(ctx context.Context) {
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()
go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := j.updateMetadata(); err != nil {
log.Printf("Error updating metadata: %v", err)
}
}
}
}()
}
func (j *JumpServer) updateMetadata() error {
// Update the metadata of the jump server
for _, host := range j.Hostnames {

View File

@ -9,6 +9,7 @@ type JumpServer struct {
*gohtmlmetadata.Extractor
Index string `json:"index"` // The intro page/index page content of the jump server
Hostnames []*Hostname `json:"hostnames"` // The hostnames of the jump server
SyncURLs []string `json:"syncurls"` // The URLs to sync the hostnames of the jump server with
Garlic *onramp.Garlic
}

21
sync.go
View File

@ -1,6 +1,7 @@
package jumpserver
import (
"context"
"io/ioutil"
"log"
"net/http"
@ -11,7 +12,25 @@ import (
"github.com/go-i2p/i2pkeys"
)
func (j *JumpServer) SyncHostnames(u string) (host string, content string) {
func (j *JumpServer) StartSync(urls []string, ctx context.Context) {
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()
go func() {
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
for _, url := range urls {
j.syncHostnames(url)
}
}
}
}()
}
func (j *JumpServer) syncHostnames(u string) (host, content string) {
uri, err := url.Parse(u)
if err != nil {
log.Printf("Failed to parse URL: %s\n", err)