-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
missing a bunch of tests rn but whatever i'll fix it later lol
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ linters-settings: | |
max-complexity: 17 | ||
funlen: | ||
lines: 100 | ||
statements: 50 | ||
gci: | ||
sections: | ||
- standard | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package hostsfile | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
scp "github.com/bramvdbogaerde/go-scp" | ||
"go.uber.org/zap" | ||
|
||
"github.com/sapslaj/zonepop/config/configtypes" | ||
"github.com/sapslaj/zonepop/endpoint" | ||
"github.com/sapslaj/zonepop/pkg/log" | ||
"github.com/sapslaj/zonepop/pkg/sshconnection" | ||
"github.com/sapslaj/zonepop/pkg/utils" | ||
"github.com/sapslaj/zonepop/provider" | ||
) | ||
|
||
type HostsFileProviderConfigSSH struct { | ||
Host string | ||
Username string | ||
Password string | ||
} | ||
|
||
type HostsFileProviderConfig struct { | ||
File string | ||
Permissions string | ||
SSH HostsFileProviderConfigSSH | ||
} | ||
|
||
type hostsFileProvider struct { | ||
config HostsFileProviderConfig | ||
logger *zap.Logger | ||
forwardLookupFilter configtypes.EndpointFilterFunc | ||
} | ||
|
||
func NewHostsFileProvider( | ||
providerConfig HostsFileProviderConfig, | ||
forwardLookupFilter configtypes.EndpointFilterFunc, | ||
) (provider.Provider, error) { | ||
if providerConfig.Permissions == "" { | ||
providerConfig.Permissions = "0644" | ||
} | ||
p := &hostsFileProvider{ | ||
config: providerConfig, | ||
logger: log.MustNewLogger().Named("hosts_file_provider"), | ||
forwardLookupFilter: forwardLookupFilter, | ||
} | ||
return p, nil | ||
} | ||
|
||
func (p *hostsFileProvider) UpdateEndpoints(ctx context.Context, endpoints []*endpoint.Endpoint) error { | ||
// TODO: reverse lookup? | ||
forwardEndpoints := utils.Filter(p.forwardLookupFilter, endpoints) | ||
result := p.endpointsToFile(forwardEndpoints) | ||
var err error | ||
if p.config.SSH.Host != "" { | ||
err = p.saveToSSH(ctx, result) | ||
if err != nil { | ||
p.logger.Sugar().Errorw("failed to save to local file", "err", err) | ||
} | ||
} else { | ||
err = p.saveToLocalFile(ctx, result) | ||
if err != nil { | ||
p.logger.Sugar().Errorw("failed to save to remote SSH file", "err", err) | ||
} | ||
} | ||
return err | ||
} | ||
|
||
func (p *hostsFileProvider) endpointsToFile(endpoints []*endpoint.Endpoint) string { | ||
p.logger.Sugar().Infof("generating %d host file entries", len(endpoints)) | ||
var s strings.Builder | ||
s.WriteString("# Generated by ZonePop\n") | ||
for _, endpoint := range endpoints { | ||
for _, ipv4 := range endpoint.IPv4s { | ||
s.WriteString(fmt.Sprintf("%s\t%s\n", ipv4, endpoint.Hostname)) | ||
} | ||
for _, ipv6 := range endpoint.IPv6s { | ||
s.WriteString(fmt.Sprintf("%s\t%s\n", ipv6, endpoint.Hostname)) | ||
} | ||
} | ||
return s.String() | ||
} | ||
|
||
func (p *hostsFileProvider) saveToLocalFile(ctx context.Context, result string) error { | ||
p.logger.Sugar().Infof("saving hosts file to (local) %s with permissions %s", p.config.File, p.config.Permissions) | ||
perm, err := strconv.ParseInt(p.config.Permissions, 8, 0) | ||
if err != nil { | ||
return err | ||
} | ||
p.logger.Sugar().Infof("perm: %s %d", p.config.Permissions, fs.FileMode(perm)) | ||
return os.WriteFile(p.config.File, []byte(result), fs.FileMode(perm)) | ||
} | ||
|
||
func (p *hostsFileProvider) saveToSSH(ctx context.Context, result string) error { | ||
p.logger.Sugar().Infof("saving hosts file to (SSH) %s:%s with permissions %s", p.config.SSH.Host, p.config.File, p.config.Permissions) | ||
conn, err := sshconnection.Connect(p.config.SSH.Host, p.config.SSH.Username, p.config.SSH.Password) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Disconnect() | ||
client, err := scp.NewClientBySSH(conn.Client) | ||
if err != nil { | ||
return err | ||
} | ||
reader := strings.NewReader(result) | ||
return client.CopyFile(ctx, reader, p.config.File, p.config.Permissions) | ||
} |