mirror of
https://github.com/go-i2p/go-jump-addr.git
synced 2025-06-08 01:09:14 -04:00
add syncing
This commit is contained in:
23
serve.go
23
serve.go
@ -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 {
|
||||
|
@ -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
21
sync.go
@ -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)
|
||||
|
Reference in New Issue
Block a user