add sync function

This commit is contained in:
eyedeekay
2025-03-17 19:35:40 -04:00
parent 917d05c0c0
commit 078bfb9aa3
3 changed files with 89 additions and 0 deletions

1
.gitignore vendored
View File

@ -23,3 +23,4 @@ go.work.sum
# env file
.env
i2pkeys

Binary file not shown.

88
sync.go Normal file
View File

@ -0,0 +1,88 @@
package jumpserver
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-i2p/i2pkeys"
)
func (j *JumpServer) SyncHostnames(u string) (host string, content string) {
uri, err := url.Parse(u)
if err != nil {
log.Printf("Failed to parse URL: %s\n", err)
return "", ""
}
if uri.Scheme == "" {
uri.Scheme = "http"
}
if uri.Host == "" {
log.Printf("Failed to parse URL: no host\n")
return "", ""
}
// Sync the hostnames of the jump server
// with the hostnames of the jump server
// at the given URL
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Printf("Failed to create request: %s\n", err)
return "", ""
}
req.Header.Set("User-Agent", "go-jump-addr")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Failed to get response: %s\n", err)
return "", ""
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Failed to get response: %s\n", resp.Status)
return "", ""
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to read response: %s\n", err)
return "", ""
}
// Extract the hostnames from the response
// and add them to the jump server
for _, host := range extractHostnames(body) {
vals := strings.Split(host, "=")
if len(vals) != 2 {
log.Printf("Failed to parse hostname: %s\n", host)
continue
}
hostname, dest := vals[0], vals[1]
addr, err := i2pkeys.NewI2PAddrFromString(dest)
if err != nil {
log.Printf("Failed to parse I2P address: %s\n", err)
continue
}
entry := &Hostname{
I2PAddr: &addr,
Time: time.Now(),
Registrant: Registrant{
Type: "service",
Name: uri.Host,
Description: "",
Tags: []string{uri.Host, uri.Scheme},
},
Hostname: hostname,
}
j.AddHostname(entry)
}
return uri.Host, string(body)
}
func extractHostnames(body []byte) []string {
// Extract hostnames from the response body
// and return them as a slice of strings
// This function is used by SyncHostnames
// to extract hostnames from the response
// of the jump server at the given URL
return strings.Split(string(body), "\n")
}